MPAndroidChart v3.x.x:从2.x.x标签升级

正如标题中所提到的,我正在开发一个使用MP AndroidChart 2.2.3版的项目.在此项目中,目前使用条形图.

我正在将版本升级到3.0.1.升级后,以下几件事情不再适用:

1.

mBarChart.setDescription("");

2.

xAxis.setSpaceBetweenLabels(1);

3.

BarData barData = new BarData(ArrayList<String>,ArrayList<IBarDataSet>);

我环顾四周,但似乎没有解释这些问题,即使在发行说明中也是如此.

解决方法

一个问题

mBarChart.setDescription(); doesn’t work anymore

根据this answer here,现在设置描述的正确方法是:

mChart.getDescription().setText("Description of my chart);

第三个问题

BarData barData = new BarData(ArrayList<String>,ArrayList<IBarDataSet>); doesn’t work anymore

添加标签方法与以前不同.在MPAndroidChart 2.x.x中,您将xIndex标签作为ArrayList< String>传递. BarData构造函数的参数.如果您在3.x.x中尝试,那么您将收到如下消息:

BarData (com.github.mikephil.charting.interfaces.datasets.IBarDataSet...) in BarData cannot be applied to (java.lang.String[],java.util.ArrayList<com.github.mikephil.charting.interfaces.datasets.IBarDataSet>)

这适用于许多流行但过时的教程,如Truition tutorial for MPAndroidChart here

相反,在MPAndroidChart 3.x.x中,执行此操作的方法是使用IAxisValueFormatter.此接口具有单个方法getFormattedValue(浮点值,AxisBase轴),您可以通过该方法以编程方式生成标签.

sample projectanswer here都有一个例子

总而言之,在MPAndroidChart3.x.x中向BarChart添加数据的正确方法如下(根据示例项目中的示例):

ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
 dataSets.add(set1);
 BarData data = new BarData(dataSets);
 mChart.setData(data);
 mChart.getXAxis().setValueFormatter(new MyCustomValueFormatter()); // your own class that implements IAxisValueFormatter

注意:如果必须使用ArrayList< String>对于您的标签,您可以使用便利类IndexAxisValueFormatter

你像这样消费它:

List<String> labels;
//Todo: code to generate labels,then
mChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));

你会需要:

mChart.getXAxis().setGranularity(1);
mChart.getXAxis().setGranularityEnabled(true);

模仿MPAndroidChart 2.x.x的行为,其中只有整数xIndices接收标签.

至于你的第二个问题,因为标签功能由IAxisValueFormatter非常精细地控制,你将不再需要setSpaceBetweenLabels().

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...