MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

How to lock the xIndex after add new data?

Open 531227501 opened this issue 9 years ago • 4 comments

for example,

list size = 20; element is info by year, 1998 - 2017; when user drag chart to the left( when the first element is 1998), request the before years from server: 1978 - 1997; then add the new date to the list, the list is 1978 - 2017 now; add the data to chart; the first element is not 1998 after setData(), but i want lock the xIndex when add new Data, i tried a lot, but it seems hard on your code, may you resolve it?

531227501 avatar Jul 17 '17 03:07 531227501

Any idea?

wuxudong avatar Aug 01 '18 05:08 wuxudong

I think you're looking for this https://github.com/PhilJay/MPAndroidChart/blob/c1f6fcebf0c3516e067b34312b283189f909bde5/MPChartExample/src/com/xxmassdeveloper/mpchartexample/DynamicalAddingActivity.java

m-makuch avatar Sep 24 '18 12:09 m-makuch

Finally , I get a smooth scroll effect with following code :

YAxis.AxisDependency axisDependency = root.getAxisLeft().isEnabled() ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT;

RectF contentRect = root.getViewPortHandler().getContentRect();
MPPointD center = root.getValuesByTouchPoint(contentRect.centerX(), contentRect.centerY(), axisDependency);

double originalVisibleXRange = root.getVisibleXRange();
double originVisibleYRange = getVisibleYRange(root, axisDependency);


root.setData((newData);

double newVisibleXRange = root.getVisibleXRange();
double newVisibleYRange = getVisibleYRange(root, axisDependency);

double newScaleX = (newVisibleXRange / originalVisibleXRange);
double newScaleY = (newVisibleYRange / originVisibleYRange);

root.zoom((float) newScaleX, (float) newScaleY, (float) center.x, (float) center.y, axisDependency);
root.postInvalidate();

wuxudong avatar Sep 27 '18 08:09 wuxudong

Closing old issue. If anyone still has this question please open a new one.

almic avatar Oct 28 '18 01:10 almic