How to remove decimal places from project using Mikephil pie chart

冷暖自知 提交于 2019-12-25 05:18:07

问题


Here is static value of integer

  private int upload=14, bill=15, unbill=85, total=100, unupload=12, sign=10, unsign=90, print=12, unprint=88;

Set entry of pie chart

ArrayList<Entry> entries = new ArrayList<>();

entries.add(new Entry(bill, 0));
entries.add(new Entry(unbill, 1));
entries.add(new Entry(print, 2));
entries.add(new Entry(unprint, 3));
entries.add(new Entry(sign, 4));
entries.add(new Entry(unsign, 5));
entries.add(new Entry(upload, 6));
entries.add(new Entry(unupload, 7));

 int colors[] ={Color.rgb(84, 139, 221), Color.rgb(251, 249, 146),Color.rgb(151, 212, 153), Color.rgb(183, 144, 189),Color.rgb(226, 148, 188),Color.rgb(208, 189, 121),Color.rgb(185, 147, 134),Color.rgb(206, 139, 130)};

Here I am using Mikephil PieChart, below I added entries in dataset.

PieDataSet dataset = new PieDataSet(entries, "Graph");
dataset.setColors(colors);
dataset.setSliceSpace(3f);

I am getting this output. So i want to remove decimal places which I mentioned in

Output is perfect. I just want output without decimal places. How can I do that?


回答1:


if you can input integer value as parameter you can or if the library method doesnt accepts integer value you have to customize the library !




回答2:


The question is essentially the same as this one which has a good answer already. This relevant question also has canonical answer by the library author. It's great to ask questions, but in the future you can avoid downvotes to your questions by researching them before hand to make sure they have not already been asked.

There is absolutely no need to add the library as a .jar file and edit the library code as in the previous low-quality answer. This just defeats the whole purpose of using a library and using build tools like Gradle.

As per the exact inside the accepted answer to the duplicate linked above, write a class that implements IValueFormatter like this:

public class IntValueFormatter implements IValueFormatter {

    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        return String.valueOf((int) value);
    }
}

Then you consume it like this:

pieDataSet.setValueFormatter(new IntValueFormatter());



回答3:


set ValueFormatter as follows

  pieData.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return String.valueOf((int) Math.floor(value));
            }
        });



回答4:


You need to create a custom percentFormatter to format the percentage presentation on a chart.

public class Remover extends PercentFormatter {

private DecimalFormat mDecimalFormat;

public Remover (DecimalFormat format) {
    this.mDecimalFormat = format;
}

@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex,       ViewPortHandler viewPortHandler) {

return   "% " + mDecimalFormat.format(value) ;
 }
}

You can then assign this formatter to your chart by calling setValueFormatter() on the chart object.



来源:https://stackoverflow.com/questions/41979249/how-to-remove-decimal-places-from-project-using-mikephil-pie-chart

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