RunLoop RunLoop - 8 days ago 9
Android Question

Hide 0 values in staked bar chart

I am using MPAndroidChart to show a stacked bar chart containing 2 stacks. However 0 values are shown and overlap normal values as per screenshot. How can I hide 0 values?enter image description here

Answer

Use the IValueFormatter interface.

Example:

public class MyValueFormatter implements IValueFormatter {

    private DecimalFormat mFormat;

    public MyValueFormatter() {
        mFormat = new DecimalFormat("###,###,##0.00");
    }

    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {

        if(value > 0) {
            return mFormat.format(value);
        } else {
            return "";
        }
    }
}

Set it for the chart-data:

barData.setValueFormatter(new MyValueFormatter());

Also check the documentation.

Comments