0

I have an array of y values that I am displaying over the dates of a month. To simplify, for the first week of April, I would have the values {0,200,0,0,500,0,100} over the x values {1,2,3,4,5,6,7}. I am able to display them as a bar chart using MPAndroidChart. I am also able to hide and display the values over each bar using

barChart.getData().setDrawValues(true); //or false when I want to hide 

However, I want to display only the number that are non-zero, how would I be able to do so? Any pointers would be appreciated!

I tried creating my formatter the following way:

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) + " $";
        }
    }

and called it using this in my main function:

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

However the values of zero are still displayed.

ankit
  • 125
  • 4
  • 13
  • You can put some condition like `if(value > 0) dataSet.add(...)` or you can write your own ValueFormatter – Akshay Nandwana Apr 04 '18 at 06:15
  • I would prefer still adding the 0 values to my dataset. For example, using the example in the question, all 7 values {0, 200, 0, 0, 500, 0 ,100} would be sent to the dataSet. I would rather remove the display of the value 0 on the bar graph itself. – ankit Apr 04 '18 at 06:22
  • You can try using making your own ValueFormatter (here)[https://github.com/PhilJay/MPAndroidChart/wiki/The-ValueFormatter-interface] – Akshay Nandwana Apr 04 '18 at 06:31
  • Updated the question with my attempt – ankit Apr 04 '18 at 06:46
  • I am putting the code in the answer section, let me know if it works else I will check on my local machine. – Akshay Nandwana Apr 04 '18 at 06:53
  • i just realised i didnt put my condition on the formatter will add that and see if it works – ankit Apr 04 '18 at 06:56

2 Answers2

2

Try making your own IValueFormatter Interface

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

try setting value formatter to your barchart.

bar.setValueFormatter(new MyYAxisValueFormatter ());
Akshay Nandwana
  • 1,260
  • 11
  • 18
  • YAxis yAxis = barChart.getAxisLeft(); yAxis.setValueFormatter(new MyYAxisValueFormatter()); second line gives error: setValueFormatter in AxisBase cannot be applied to MyYAxisValueFormatter – ankit Apr 04 '18 at 07:12
  • `barChart.getAxisLeft();` did you try with `barChart.getYAxis();` – Akshay Nandwana Apr 04 '18 at 09:31
  • there is no getYAxis() function with barChart, only getXAxis(), at least not in the chart i'm using – ankit Apr 05 '18 at 20:04
  • I put a log in the formatter to see what it sent and it was the value 200, 400, 600, 800 and 1000, therefore making me realise we were formatiing the axis not the data. I used data.setValueFormatter(new MyYAxisValueFormatter ()) and it worked. WIll mark this as correct answer – ankit Apr 05 '18 at 20:48
0

try this:

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

OR

try this likn it helps you

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

Android Geek
  • 8,956
  • 2
  • 21
  • 35