6

I am using MPAndroidChart library. I am using a CustomValueFormatter which formats the Float values such that their precision 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 am setting the formatter to y-axis.

Setting the formatter:

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

As a result of setValueFormatter(YAxisValueFormatter), the above formatter (CustomYAxisValueFormatter) is created by default.

The problem is CustomYAxisValueFormatter can't be recreated upon zooming, thereby resulting in repetitive y-values.

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

  • 1
    http://stackoverflow.com/questions/32569618/mpandroidchart-how-can-i-avoid-the-repeated-values-in-y-axis if this can be of any help. – Dhinakaran Thennarasu Mar 16 '16 at 07:07
  • @Dhina Thank you for helping. But the answers in the link you've provided deals with forcing number of values in y-axis.It restricts y-values count based on zoom level. Even that solution causes repeated y-values on infinite zooming. All I need is to change the precision of values according to level of zooming. – Balalakshmi Sadhasivam Mar 16 '16 at 08:51

1 Answers1

0

Did you try 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) {

            }
        });
Dhinakaran Thennarasu
  • 3,336
  • 4
  • 26
  • 39
  • I've tried this. But I can't calculate precision values based on zoom level. Zoom level has to be passed to CustomYAxisValueFormatter's constructor which will change the precision according to zoom level parameter. Is it possible to relate zoom level and precision? – Balalakshmi Sadhasivam Mar 17 '16 at 05:53
  • _precision values based on zoom level._ can you elaborate what exactly does this mean? – Dhinakaran Thennarasu Mar 17 '16 at 06:08
  • If the chart is zoomed, I need to check visible y-values. If they are repeated, precision has to be increased by 1. Is it possible to look whether the chart has any repetitive y-values based on current zoomed level ( Is there any way to calculate how much the chart is zoomed based on scaleY values??? ) – Balalakshmi Sadhasivam Mar 17 '16 at 06:22
  • You could use this maybe from MotionEvent ? quote from [getYPrecision](http://developer.android.com/reference/android/view/MotionEvent.html#getYPrecision()) "Return the precision of the Y coordinates being reported. You can multiply this number with getY() to find the actual hardware value of the Y coordinate." – Dhinakaran Thennarasu Mar 17 '16 at 06:32
  • But I have to check whether the values shown in the axis are repeated – Balalakshmi Sadhasivam Jun 11 '16 at 03:58