gaurang
gaurang

Reputation: 2240

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

Upvotes: 4

Views: 3501

Answers (2)

gaurang
gaurang

Reputation: 2240

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

Upvotes: 3

gmetax
gmetax

Reputation: 3973

if you don't want those lines to get drawn, you have to set the color of that line to ColorTemplate.COLOR_NONE.

I don't have enough of your original code to point out what exactly to do, but on the example https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java#L199 you can check how to do it.

Upvotes: 1

Related Questions