如何格式化MPAndroidChart中的值?

Den*_*nis 23 android mpandroidchart

2关于MPAndroidChart库的问题.我的所有yvalues都是整数,但显示为Decimals.如何将它们显示为整数(没有数字)?如何防止ylabels也显示为小数?我知道yvalues有一个setFormatter,只是不明白如何使用...

Phi*_*oda 36

看看库提供的IValueFormatter 界面.使用该界面,您可以根据自己的逻辑完全自定义图表上显示的内容.

用法:

chart.setValueFormatter(new YourValueFormatter());
YLabels yl = chart.getYLabels();
yl.setFormatter(new YourValueFormatter());
Run Code Online (Sandbox Code Playgroud)

更新(对于此库的2.0.0+版本):

现在,ValueFormatter可以为每个DataSet单独设置一个,或者ValueFormatter可以为整个数据对象包含所有设置相同DataSets.此外,YLabels现在调用该类YAxis.

例:

// set for whole data object (individual DataSets also possible)
LineData data = new LineData(...);
data.setValueFormatter(new YourValueFormatter());

// YLabels are now called YAxis
YAxis yAxis = mChart.getAxisLeft(); // get the left or right axis
yAxis.setValueFormatter(new YourAxisValueFormatter());
Run Code Online (Sandbox Code Playgroud)

更新(对于此库的3.0.0+版本):

格式化界面已​​在其功能中重命名和扩展.现在,IAxisValueFormatter可以用来格式化两者的价值观XAxisYAxis.该IValueFormatter界面用于自定义图表值.

链接到IValueFormatter文档.

链接到IAxisValueFormatter文档.

  • 第一.我刚刚注意到YLabel的方法名称已更改为setFormatter(new YourValueFormatter()).您也应该更新您的答案,以便其他人也不会分心.谢谢! (2认同)
  • 切断小数位置不会正确更改标签 - 在使用2.0 - 2.4后,它会更改为2 - 2(重复).关于如何纠正它的任何想法? (2认同)
  • _PercentFormatter_ 类有 2 个不同的构造函数。尽管我使用了百分比模式,但空构造函数对我没有帮助。如果 `pieData.setValueFormatter(new PercentFormatter());` 不显示百分比图标,您应该使用第二个构造函数,如下所示: `pieData.setValueFormatter(new PercentFormatter(pieChart));` (2认同)