Search code examples
androidchartspie-chartmpandroidchartoverlapping

how to remove lines if values is 0 % in pie chart


I'm working on a pie chart, for that, i'm using MPAndroidChart library, the values may contain 0% for any data or more than one data and I'm displaying values outside of piechart using setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE). I don't want to display 0% values in a pie chart, I got a solution for that to use value formatted.

public class CustomPercentFormatter implements IValueFormatter {    

    private DecimalFormat mFormat;

    public CustomPercentFormatter() {
        mFormat = new DecimalFormat("###,###,##0.0");
    }

    public CustomPercentFormatter(DecimalFormat format) {
        this.mFormat = format;
    }

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

        if (value == 0.0f)
            return "";

        return mFormat.format(value) + " %";
    }
}

But if I'm using lines option then the line is displayed for 0 % values and overlapping for multiple 0% values, so can anyone help me to remove line option for 0% values?

image


Solution

  • I changed the method drawValues in PieChartRenderer class.

    Do not display line if value is 0.

    Just put one condition:

    if (entry.getValue() != 0.0) {
        if (dataSet.getValueLineColor() != ColorTemplate.COLOR_NONE) {
            c.drawLine(pt0x, pt0y, pt1x, pt1y, mValueLinePaint);
            c.drawLine(pt1x, pt1y, pt2x, pt2y, mValueLinePaint);
        }
    }
    

    Instead of following code:

    if (dataSet.getValueLineColor() != ColorTemplate.COLOR_NONE) {
        c.drawLine(pt0x, pt0y, pt1x, pt1y, mValueLinePaint);
        c.drawLine(pt1x, pt1y, pt2x, pt2y, mValueLinePaint);
    }