robinhood / spark

A simple Android sparkline chart view.
Apache License 2.0
1.28k stars 157 forks source link

Using SparkAdapter with X and Y value and displaying both on onScrubbed() #83

Closed ndrb closed 3 years ago

ndrb commented 3 years ago

Hello, Thank you for this great and easy to use library! I was wondering how I can use SparkAdapter to it's full extent. I have both X and Y value with Y being the price of the asset, and X the time. When the sparkline is clicked and onScrubbed is triggered I want to update the scrubInfoTextView with both values of X and Y. I am not too certain how to do that using the spark adapter.

 public class AssetAdapter extends SparkAdapter {
    private float[] yData;
    public float[] xData;

    public CryptoAdapter(float[] yData) {
        this.yData = yData;
    }

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

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

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

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

 public void setupSpark()
{
    sparkView = findViewById(R.id.sparkview);

    sparkView.setAdapter(adapter);

    sparkView.setScrubListener(new SparkView.OnScrubListener()
    {
        @Override
        public void onScrubbed(Object value) {
            if (value == null) {
                scrubInfoTextView.setText(R.string.scrub_empty);
            } else {
                scrubInfoTextView.setText(getString(R.string.scrub_format, value));
            }
        }
    });

    sparkView.setScrubLineWidth(0);
    sparkView.setScrubLineColor( getResources().getColor(R.color.colorPrimaryDark) );

    scrubInfoTextView = findViewById(R.id.scrub_info_textview);
}

In the SparkView class, there is a method: public void onScrubbed(float x, float y) but when I try to override it cannot find that method.

Stelios-T commented 3 years ago

I'm also interested if we can do that. I'm looking to display secondary data (in our case the time) apart from the data that's given for making the line.

ndrb commented 3 years ago

Actually, I just found another closed Issue that answered this problem. @danh32 answered this problem in #28 I tried using the implementation and it worked perfectly! Sorry about that!

Stelios-T commented 3 years ago

Great, thanks