How to hide some labels in x-axis of a bar chart, if the value is zero.
I have a bar chart, but even if some values are 0 and bars are gone, it just shows a 0.0 for each bar. How to hide these?
Have a look at http://stackoverflow.com/questions/29699230/hide-0-values-in-staked-bar-chart. Hope this helps.
I did something like this but nothing changed:
barData.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
if (value>0){
return value+"";
}else{
return "0";
}
}
});
Constructing the value formatter elsewhere did the trick. Here's the code:
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 "";
}
}
@yigitserin I have the same question, too. I also override the ValueFormatter, but I can only hide the 0 values not the labels in the PieCHart.
I do want to display other none-zero-value labels, and I do want to show the labels for all values (including 0 ones) in the legend. So disabling drawing labels or remove zero values from dataset would not work.
How would you suggest me to do this? Please help.
@CristianoYL Even i stuck in the same problem. Did you get any solution ? if yes Please help me
Hi @krishnamurthy12, sorry I can't be of any help. It was a long time since I use this library, and I can't really remember what I did exactly. I don't even think I came up with a solution at that time.
This solution worked for me.
barData.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
if (value > 0){
return super.getFormattedValue(value);
}else{
return "";
}
}
});
hello there, if you want to hide first and last label here's the possible way to do it: final class DateValueFormatter: NSObject, AxisValueFormatter {
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
if value == axis?.axisMinimum || value == axis?.axisMaximum {
return ""
}
return "\(value)"
}
}