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

北城余情 提交于 2019-12-04 04:46:24

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/45230343/how-to-remove-lines-if-values-is-0-in-pie-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!