Reputation: 125
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.
Upvotes: 0
Views: 3349
Reputation: 1292
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 ());
Upvotes: 2
Reputation: 9225
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
Upvotes: 0