MPAndroidChart:如何自定义条值标签 [英] MPAndroidChart: How to customise bar value labels

查看:179
本文介绍了MPAndroidChart:如何自定义条值标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用中使用MPAndroidChart.我使用由BarEntry组成的BarChart.我还使y值显示在栏的顶部.

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.

我的问题是我希望条形图顶部的值是像5这样的整数.但是当前值显示为5.00.

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.

那么如何使5.00显示为5?

推荐答案

使用IValueFormatter界面格式化值.这是一个简单的格式化程序,可将所有值转换为整数:

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);
    }
}

然后可以将此格式化程序用于BarData和单个BarDataSet对象,如下所示:

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

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

有关IValueFormatter的更多信息,请检查以下链接:

For more information on IValueFormatter, check the following links:

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

这篇关于MPAndroidChart:如何自定义条值标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆