9

I am using MPAndroidChart and I have two questions:

  • MPAndroid Pie Chart Remove decimal percentages
  • Not show values on the Pie Chart that have values less than 10%, but show the slice; just the text should not be shown for the percentages less than 10%.
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
abhishek
  • 259
  • 4
  • 11

4 Answers4

12

Update for version 3.0.0+

Formatting values is now done by extending the ValueFormatter class and overriding the required methods for formatting. This is also where custom logic (e.g. only show labels for values > 10) can be inserted.

class MyValueFormatter : ValueFormatter() {
    private val format = DecimalFormat("###,##0.0")

    // override this for e.g. LineChart or ScatterChart
    override fun getPointLabel(entry: Entry?): String {
        return format.format(entry?.y)
    }
}

More examples and a detailed explanation can be found in the new documentation.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
1

With the new version (3.0.0) of MPAndroidChart, use the following method:

In the setData() method:

 PieData data = new PieData(dataSet);
 data.setValueFormatter(new DecimalRemover(new DecimalFormat("###,###,###")));

Here is the DecimalRemover class:

public class DecimalRemover extends PercentFormatter {

protected DecimalFormat mFormat;

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

@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
    if(value < 10) return "";
    return mFormat.format(value) + " %";
}
}

ValueFormatter has been replaced with PercentFormatter

suku
  • 10,507
  • 16
  • 75
  • 120
0

for remove decimal percentages, easily use: PiedataSet.setValueFormatter(new PercentFormatter(new DecimalFormat("###,###,##0")));

abbasalim
  • 3,118
  • 2
  • 23
  • 45
0

First Step: Please put this below "PercentFormatter" class in your package folder:

public class PercentFormatter extends ValueFormatter {

    public DecimalFormat mFormat;
    private PieChart pieChart;

    public PercentFormatter() {
        mFormat = new DecimalFormat("###,###,##");
    }
    public PercentFormatter(PieChart pieChart) {
        this();
        this.pieChart = pieChart;
    }

    @Override
    public String getFormattedValue(float value) {
        return mFormat.format(value) + " %";
    }

    @Override
    public String getPieLabel(float value, PieEntry pieEntry) {
        if (pieChart != null && pieChart.isUsePercentValuesEnabled()) {
            // Converted to percent
            return getFormattedValue(value);
        } else {
            // raw value, skip percent sign
            return mFormat.format(value);
        }
    }

}

Second Step: In your activity where the Piedata set, add value format in the PieDataset that way:

 pieChart = (PieChart) findViewById(R.id.pieChart);
 PieDataSet pieDataSet = new PieDataSet(getData(approved, pending, rejected), "");
 
 pieDataSet.setValueFormatter(new PercentFormatter());
 PieData pieData = new PieData(pieDataSet);
 pieChart.setData(pieData);

That way, we can remove the decimal point in the Pie data.

Ali Ahmed
  • 1,159
  • 1
  • 8
  • 18