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

标签 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?

最佳答案

你试过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) {

            }
        });

关于android - MPAndroidChart - 如何在缩放时更改 ValueFormatter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36028356/

相关文章:

android - 如何在 Android 上显示来自 URL 的图像

android - 错误: package R does not exist - generated fragment directions file after Gradle update

android - MPAndroidChart:在条形图内添加自定义图像

github - 有没有办法使用 git 自动将 Github 中的库更新到 Android Studio 项目中?

android - 渲染大量四边形的有效方法 (LibGDX/OpenGL ES)

android - 在平板电脑上强制手机布局

java - 将 FrameLayout 设置为在按钮单击时可见

android - 如何在 MPAndroid 图表中使用 X 值?

android - 从 MPAndroidChart 中删除 "No chart data available"并添加我自己的消息

android - 如何使用 Mikephil 饼图从项目中删除小数位