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

对着背影说爱祢 提交于 2019-12-23 08:01:30

问题


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%.

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/33424470/mpandroidchart-how-to-remove-decimal-percentages-and-not-show-percentages-below

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