MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

How to hide some labels in x-axis of a bar chart, if the value is zero.

Open yigitserin opened this issue 10 years ago • 8 comments

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?

yigitserin avatar Sep 29 '16 13:09 yigitserin

Have a look at http://stackoverflow.com/questions/29699230/hide-0-values-in-staked-bar-chart. Hope this helps.

interblitz avatar Sep 29 '16 13:09 interblitz

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

yigitserin avatar Sep 29 '16 13:09 yigitserin

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 avatar Sep 29 '16 14:09 yigitserin

@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 avatar Jun 15 '17 22:06 CristianoYL

@CristianoYL Even i stuck in the same problem. Did you get any solution ? if yes Please help me

krishnamurthy12 avatar Dec 20 '18 14:12 krishnamurthy12

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.

CristianoYL avatar Dec 21 '18 05:12 CristianoYL

This solution worked for me.

barData.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {

                if (value > 0){
                    return super.getFormattedValue(value);
                }else{
                    return "";
                }
            }

 });

nisharamj avatar Jul 03 '20 09:07 nisharamj

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

}

ilshady avatar Feb 25 '24 05:02 ilshady