MPAndroidChart
MPAndroidChart copied to clipboard
Realtime rolling window chart / Move to last X, but do not touch Y
I need realtime updated chart, which always moves to last X value (showing maximum 5 values). But keep Y unchanged. moveViewToX tries to set 0 to top of Y. I didn't find alternative, so I wrote my own. Hope you can review it and maybe add something similar to the library. Thanks.
import android.view.View;
import com.github.mikephil.charting.charts.BarLineChartBase;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.ViewPortHandler;
/**
* Runnable that is used for viewport modifications since they cannot be
* executed at any time. This can be used to delay the execution of viewport
* modifications until the onSizeChanged(...) method of the chartview is called.
*
* @author Philipp Jahoda
*/
public class MoveViewJobX implements Runnable {
protected ViewPortHandler mViewPortHandler;
protected float xIndex = 0f;
protected float yValue = 0f;
protected Transformer mTrans;
protected View view;
public MoveViewJobX(ViewPortHandler viewPortHandler, float xIndex, float yValue,
Transformer trans, View v) {
this.mViewPortHandler = viewPortHandler;
this.xIndex = xIndex;
this.yValue = yValue;
this.mTrans = trans;
this.view = v;
}
@Override
public void run() {
float[] pts = new float[] {
xIndex, yValue
};
mTrans.pointValuesToPixel(pts);
pts[1] = mViewPortHandler.getContentRect().top;
mViewPortHandler.centerViewPort(pts, view);
}
public static void moveViewToX(BarLineChartBase chart, float xIndex) {
ViewPortHandler mViewPortHandler = chart.getViewPortHandler();
Runnable job = new MoveViewJobX(mViewPortHandler, xIndex, 0f,
chart.getTransformer(YAxis.AxisDependency.LEFT), chart);
if (mViewPortHandler.hasChartDimens()) {
chart.post(job);
} else {
chart.addJob(job);
}
}
}
Then I use it like this
MoveViewJobX.moveViewToX(mChart, mChartData.getXValCount() - visibleXRangeMaximum - 1);
A good idea. it's better to let MoveViewJobX extends MoveViewJob