代码之家  ›  专栏  ›  技术社区  ›  ankit

仅在MPAndroidChart中显示一些值

  •  0
  • ankit  · 技术社区  · 7 年前

    我有一个y值数组,在一个月的日期内显示。为了简化,对于4月的第一周,我将得到x值{1,2,3,4,5,6,7}上的值{0200,0,0500,0100}。我可以使用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());
    

    但是,仍然显示零的值。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Akshay Nandwana    7 年前

    试着做你自己的 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 ());
    
        2
  •  0
  •   Android Geek    7 年前

    尝试以下操作:

    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