8

I am using MPAndroidChart in my Android app. I use a BarChart composed of BarEntry. I also enabled the y-values to be displayed on top of the bar.

My issue is that I want the values on top of the bars to be whole numbers like 5. But currently the values display as 5.00.

the top of a single bar from a bar chart with the value label above it displaying "5.00"

So how do I make 5.00 display as 5?

David Rawson
  • 20,912
  • 7
  • 88
  • 124
Mandroid
  • 6,200
  • 12
  • 64
  • 134

2 Answers2

13

Values are formatted using the IValueFormatter interface. Here's a simple formatter that converts all values to integers:

public class IntValueFormatter implements IValueFormatter {

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

You can then use this formatter for both BarData and individual BarDataSet objects like this:

barData/barDataSet.setValueFormatter(new IntValueFormatter());

For more information on IValueFormatter, check the following links:

TR4Android
  • 3,200
  • 16
  • 21
0

In my opinion, the easiest option is the following

DecimalFormat decimalFormat = new DecimalFormat("0.##");

 mChartData.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
              return decimalFormat.format(value);
            }
        });
S.Iliev
  • 48
  • 1
  • 6