MPAndroidChart - 如何在缩放时更改ValueFormatter?

时间:2016-03-16 06:35:43

标签: android mpandroidchart

我正在使用MPAndroidChart库。我正在使用CustomValueFormatter格式化Float值,使其精度为1。

CustomValueFormatter代码:

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);
    }
}

我将格式化程序设置为y轴。

设置格式化程序:

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

由于setValueFormatter(YAxisValueFormatter),默认情况下会创建上述格式化程序(CustomYAxisValueFormatter)

问题是在缩放时无法重新创建CustomYAxisValueFormatter,从而导致重复的y值。

是否可以创建一个基于缩放级别更改值精度的CustomValueFormatter?

1 个答案:

答案 0 :(得分:0)

您是否尝试过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) {

            }
        });
相关问题