MPAndroidChart, how to remove decimal percentages and not show percentages below 10?

I am using MPAndroidChart and I have two questions:

  • MPAndroid Pie Chart Remove Decimal Percentages
  • Do not show values ​​in a pie chart that have values ​​less than 10%, but show a fragment; just the text should not be displayed for percent less than 10%.
+8
source share
2 answers

Update for version 3.0. 0+

Values ​​are now formatted by extending the class ValueFormatterand overriding the necessary methods for formatting. You can also insert your own logic here (for example, show only labels for values> 10).

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

.

+9

(3.0.0) MPAndroidChart :

setData():

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

DecimalRemover:

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 PercentFormatter

0

Source: https://habr.com/ru/post/1613728/


All Articles