在 MPAndroidChart 中仅显示一些值

Show only some values in MPAndroidChart

我有一个 y 值数组,显示了一个月的日期。为简化起见,对于四月的第一周,我将在 x 值 {1,2,3,4,5,6,7} 上设置值 {0,200,0,0,500,0,100}。我可以使用 MPAndroidChart 将它们显示为条形图。我还可以使用

在每个条上隐藏和显示值
barChart.getData().setDrawValues(true); //or false when I want to hide 

但是,我只想显示非零的数字,我该怎么做?任何指针将不胜感激!

我尝试通过以下方式创建格式化程序:

public class MyYAxisValueFormatter implements IAxisValueFormatter {

        private DecimalFormat mFormat;

        public MyYAxisValueFormatter() {
        // format values to 1 decimal digit
            mFormat = new DecimalFormat("###,###,##0.0");
        }

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            String val = "";

            if(value != 0)
                val = String.valueOf(value);

            return mFormat.format(value) + " $";
        }
    }

并在我的主要函数中使用它来调用它:

YAxis yAxis = barChart.getAxisLeft();
yAxis.setValueFormatter(new MyYAxisValueFormatter());

但是仍然显示零值。

试试这个:

private class MyValueFormatter implements ValueFormatter {

    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        // write your logic here
        if(value > 0)
            return value+"";
        else
            return "";
    }
}

试试这个链接,它对你有帮助

https://github.com/PhilJay/MPAndroidChart/issues/2402

尝试制作自己的IValueFormatter界面

public class MyYAxisValueFormatter implements IValueFormatter {

        private DecimalFormat mFormat;

        public MyYAxisValueFormatter() {
        // format values to 1 decimal digit
            mFormat = new DecimalFormat("###,###,##0.0");
        }

        @Override
        public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
            // "value" represents the position of the label on the axis (x or y)
             if(value > 0) {
               return mFormat.format(value);
             } else {
               return "";
             }
        }
    }

尝试为您的条形图设置值格式化程序。

bar.setValueFormatter(new MyYAxisValueFormatter ());