微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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().

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐