MPAndroidChart - How to change ValueFormatter during scaling?

I am using the MPAndroidChart library. I use CustomValueFormatter, which formats Float values ​​so that their accuracy is 1.

CustomValueFormatter Code:

public class CustomYAxisValueFormatter implements YAxisValueFormatter { private DecimalFormat mFormat; public CustomYAxisValueFormatter() { mFormat = new DecimalFormat("###,###,###,##0.0"); // sets precision to 1 } @Override public String getFormattedValue(float value, YAxis yAxis) { return mFormat.format(value); } } 

I set the formatter on the y axis.

Setting formatting:

  YAxis yAxis = lineChart.getAxisLeft(); //show left y-axis line yAxis.setValueFormatter(new CustomYAxisValueFormatter()); // set value formatter to format y-values. 

As a result, setValueFormatter(YAxisValueFormatter) by default creates the following formatter (CustomYAxisValueFormatter) .

The problem is that CustomYAxisValueFormatter cannot be recreated when scaling, resulting in duplicate y values.

Is it possible to create a CustomValueFormatter that changes the accuracy of the values ​​based on the zoom level?

+6
source share
1 answer

Have you setOnChartGestureListener

  lineChart.setOnChartGestureListener(new OnChartGestureListener() { @Override public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { } @Override public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { if(lastPerformedGesture == ChartTouchListener.ChartGesture.PINCH_ZOOM) { YAxis yAxis = lineChart.getAxisLeft(); //show left y-axis line yAxis.setValueFormatter(new CustomYAxisValueFormatter()); // setting your new value formatter } } @Override public void onChartLongPressed(MotionEvent me) { } @Override public void onChartDoubleTapped(MotionEvent me) { } @Override public void onChartSingleTapped(MotionEvent me) { } @Override public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) { } @Override public void onChartScale(MotionEvent me, float scaleX, float scaleY) { } @Override public void onChartTranslate(MotionEvent me, float dX, float dY) { } }); 
0
source

All Articles