robinhood/spark

Scrubbing with X Value

itspatricyall opened this issue · 9 comments

First of all congratulations on this amazing resource that spark is.

I wish I could put the X Value along with the Y Value on the Scrubbing call but I don't know how to do it.

Thanks!

The SparkView.OnScrubListener that you set should receive a callback to onScrubbed() with the current Object from your SparkAdapter that is being scrubbed over. I'm not sure what your Object's type is in this case, but you should be able to pull an X and Y value from it.

Yeah that's okay here but when I put the Object value in an EditText it only show the Y value, I whish it could show both.

What type is your Object? Can you share your SparkAdapter code?

Of course, here it is:

The SparkAdapter:

 public class AdaptadorGrafico extends SparkAdapter {

        private Float[] yQuantidade;
        private Float[] xData;


        public AdaptadorGrafico(Float[] yQuantidade, Float[] xData) {
            this.yQuantidade = yQuantidade;
            this.xData = xData;
        }

        @Override
        public int getCount() {
            return yQuantidade.length;
        }

        @Override
        public Object getItem(int index) {
            return yQuantidade[index];
        }

        @Override
        public float getY(int index) {
            return yQuantidade[index];
        }

        @Override
        public float getX(int index) {
            return xData[index];
        }

        @Override
        public boolean hasBaseLine() {
            return true;
        }

        @Override
        public float getBaseLine() {
            return super.getBaseLine();
        }
    }

Setting the adapter:

SparkView sparkView = (SparkView) findViewById(R.id.graficoTotal);
        final TextView labelGrafico = (TextView) findViewById(R.id.labelGrafico);

        Float[] quantidades = bd.totalProducao();
        Float[] datas = bd.totalProducaoData();
        sparkView.setAdapter(new MainActivity.AdaptadorGrafico(quantidades, datas));

        sparkView.setScrubListener(new SparkView.OnScrubListener() {
            @Override
            public void onScrubbed(Object value) {
                if (value == null) {
                    labelGrafico.setText("Deslize sobre o gráfico para valores");
                } else {
                    labelGrafico.setText("Total : " + Float.parseFloat(value.toString()));
                }
            }
        });

Ideally you'd set a single type on your SparkAdapter instead of having separate arrays for your X and Y values:

public class Data {
    public final float x;
    public final float y;

    public Data(float x, float y) {
        this.x = x;
        this.y = y;
    }
}

public class AdaptadorGrafico extends SparkAdapter {

        private Data[] data;

        public AdaptadorGrafico(Data[] data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.length;
        }

        @Override
        public Object getItem(int index) {
            return data[index];
        }

        @Override
        public float getY(int index) {
            return data[index].y;
        }

        @Override
        public float getX(int index) {
            return data[index].x;
        }

        @Override
        public boolean hasBaseLine() {
            return true;
        }

        @Override
        public float getBaseLine() {
            return super.getBaseLine();
        }
    }

This way, in your OnScrubListener, Object will be of type Data so you can do:

        SparkView sparkView = (SparkView) findViewById(R.id.graficoTotal);
        final TextView labelGrafico = (TextView) findViewById(R.id.labelGrafico);

        Data[] data = bd.data();
        sparkView.setAdapter(new MainActivity.AdaptadorGrafico(data));

        sparkView.setScrubListener(new SparkView.OnScrubListener() {
            @Override
            public void onScrubbed(Object value) {
                Data data = (Data) value;
                if (value == null) {
                    labelGrafico.setText("Deslize sobre o gráfico para valores");
                } else {
                    float x = data.x;
                    float y = data.y;
                    // todo: update your TextView with x & y
                    labelGrafico.setText("X = ," + x + " Y = " + y);
                }
            }
        });

I'm not sure what your bd object is, but hopefully it's easy enough to convert to this kind of set up.

Hope this helps!

Thank you very much it worked fine!

You're welcome, glad it worked!

Hi, sorry to ride off of this but is there anyway to make the @Override public float getX(int index) in the adapter return v Java.util.Date instead of float? my Data object needs to be (float,Date).

SparkView needs a float to know how to plot the points on the Canvas. Your underlying data can still be a Date, but you just need to convert it to a float representation in getX, probably using date.getTime().