如何更改robinhood / spark库样本中的Y轴长度?

时间:2019-06-26 14:59:38

标签: android github graph

GitHub Library

  1. 默认情况下,Y轴的范围是0-1之间,我想将其更改为0-2000吗?
  2. 在拖动时如何在布局中显示X轴的值?

主要活动

 sparkView = findViewById(R.id.sparkview);

        adapter = new RandomizedAdapter();
        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));
                }
            }
        });

SparkAdapter

  private final DataSetObservable observable = new DataSetObservable();

 
public abstract int getCount();
@NonNull
public abstract Object getItem(int index);

public float getX(int index) {
    return index;
}
public abstract float getY(int index);
@NonNull
public RectF getDataBounds() {
    final int count = getCount();
    final boolean hasBaseLine = hasBaseLine();

    float minY = hasBaseLine ? getBaseLine() : Float.MAX_VALUE;
    float maxY = hasBaseLine ? minY : -Float.MAX_VALUE;
    float minX = Float.MAX_VALUE;
    float maxX = -Float.MAX_VALUE;
    for (int i = 0; i < count; i++) {
        final float x = getX(i);
        minX = Math.min(minX, x);
        maxX = Math.max(maxX, x);

        final float y = getY(i);
        minY = Math.min(minY, y);
        maxY = Math.max(maxY, y);
    }
    // set values on the return object
    return createRectF(minX, minY, maxX, maxY);
}
@VisibleForTesting
RectF createRectF(float left, float top, float right, float bottom) {
    return new RectF(left, top, right, bottom);
}
public boolean hasBaseLine() {
    return false;
}
public float getBaseLine() {
    return 0;
}
public final void notifyDataSetChanged() {
    observable.notifyChanged();
}
public final void notifyDataSetInvalidated() {
    observable.notifyInvalidated();
}
public final void registerDataSetObserver(DataSetObserver observer) {
    observable.registerObserver(observer);
}
public final void unregisterDataSetObserver(DataSetObserver observer) {
    observable.unregisterObserver(observer);
}

我想要这样的输出Image,其中在“擦洗”中显示Y和X轴。

1 个答案:

答案 0 :(得分:0)

1)您可以通过提供自己的SparkAdapter子类并从getY()方法返回想要的任何值来更改提供给SparkView的Y值。 SparkView将自动缩放您的SparkAdapter提供的任何值以适合View边界。

MySparkAdapter.java

public class MySparkAdapter extends SparkAdapter {

  // pass your real data into the constructor
  public MySparkAdapter(...) {
    // store your data
  }

  @Override
  public float getY() {
    // return the y value from your data
  }

  ... // other override methods
}

MainActivity.java

sparkView = findViewById(R.id.sparkview);
sparkView.setAdapter(new MySparkAdapter(...));

2)每次更改清理值时,new SparkView.OnScrubListener()将收到对onScrubbed()的呼叫。您需要在其中进行的所有操作Object(与您的MySparkAdaptergetObject()返回的对象相同),并使用它来更新您的UI。

希望有帮助!