MPAndroidChart: How to customise bar value labels

安稳与你 提交于 2019-12-04 01:18:41

问题


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.

So how do I make 5.00 display as 5?


回答1:


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:

  • IValueFormatter Wiki on Github
  • IValueFormatter Documentation on JitPack (v3.0.0-beta1)


来源:https://stackoverflow.com/questions/38922125/mpandroidchart-how-to-customise-bar-value-labels

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