2

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 image

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

Yurets
  • 3,999
  • 17
  • 54
  • 74
stab
  • 55
  • 1
  • 11
  • Possible duplicate of [How to format values inside MPAndroidChart?](http://stackoverflow.com/questions/26883298/how-to-format-values-inside-mpandroidchart) – David Rawson Feb 03 '17 at 22:37
  • @DavidRawson, It shouldn't be a matter for several reasons: 1, Post will not be removed even if it is a duplicate, so should be edited; 2. The post appeared in "Help & improvement" queue, but not in "Close votes". – Yurets Feb 03 '17 at 23:15

6 Answers6

7

set ValueFormatter as follows

  pieData.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return String.valueOf((int) Math.floor(value));
            }
        });
user780277
  • 81
  • 1
  • 4
3

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());
David Rawson
  • 20,912
  • 7
  • 88
  • 124
  • 1
    I am so glad that you support me and taught me how to avoid downvotes. Its perfect and proper solution for issue!!...thanx – stab Feb 04 '17 at 05:57
1

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:

 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
0

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.

insa_c
  • 2,851
  • 1
  • 16
  • 11
0

return String.valueOf(Math.floor(value)).replace(".0","") This worked for me

-3

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 !