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

2

4

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

Horologe answered 21/7, 2017 at 6:18 Comment(4)
I too am stuck with it, github.com/PhilJay/MPAndroidChart/pull/1703/commits/… this says issue is closed. But, I am not able to find method pieChart.setDrawEmptyXLabel(false); using MPAndroidChart:v3.0.2'Spaulding
have to change PieChartRenderer.java method drawValues() 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); } }Horologe
Hi can you please tell me how ur placing the text outside of the graph ?Trickle
you can set data through github.com/PhilJay/MPAndroidChart/wiki/Setting-DataHorologe
H
3

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);
}
Horologe answered 25/9, 2017 at 10:33 Comment(0)
E
1

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.

Edmon answered 25/9, 2017 at 10:36 Comment(7)
you can not directly change color by writing code for the line, its fix black color from that library.Horologe
@Horologe check the example github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/…Edmon
you can use setValueLineColor(), but only once, if you are going to use it as ColorTemplate.COLOR_NONE then no where you can see the lines, and look at the question i have asked that dont want to show lines where value is zero.Horologe
I understand what you are saying but your "solution" isn't good, you are saying that you will change a library ( or open a merge request of changes) that you will no longer get updates.Edmon
But its doing what i want.Horologe
ok open merge request to that library, to use custom library for you without updates will not be a good solutionEdmon
Let us continue this discussion in chat.Horologe

© 2022 - 2024 — McMap. All rights reserved.