Mandroid Mandroid - 3 months ago 32
Android Question

Android MPAndroidChart: How to customise bar values

I am using MPAndroidChart in my android app. I use BarChart. So I prepare BarEntry for it. I also enable y value to be displayed on top of the bar. My issue is that I want values on top of the bars to be whole numbers,like 5.But currently it displays as 5.00.

enter image description here

So how to make 5.00 as 5?

Answer

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

public class IntValueFormatter implements ValueFormatter {

    @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 ValueFormatter, check the following links:

Comments