MPAndroidChart v3.x.x:从2.x.x标签升级 [英] MPAndroidChart v3.x.x: upgraded from 2.x.x labels

查看:110
本文介绍了MPAndroidChart v3.x.x:从2.x.x标签升级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题中所述,我正在使用MPAndroidChart 2.2.3版的项目中工作.在此项目中,当前使用的是条形图.

As mentioned in the title, I am working on a project where MPAndroidChart version 2.2.3 is used. In this project, Bar chart is currently used.

我正在将版本升级到3.0.1.升级后,以下几项不再起作用:

I am doing the version upgraded to 3.0.1. After the upgrade, few things below does not work anymore:

1.

mBarChart.setDescription("");

2.

xAxis.setSpaceBetweenLabels(1);

3.

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

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

I looked around but seems like there is no explanation for those issue, even in the release note.

推荐答案

第一个问题

mBarChart.setDescription();不再起作用

根据此处的答案设置描述的正确方法是:

As per this answer here the correct way to set description is now:

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

第三个问题

BarData barData = new BarData(ArrayList<String>, ArrayList<IBarDataSet>);不再起作用

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

There is a different method of adding labels than before. In MPAndroidChart 2.x.x you would pass the xIndex labels as an ArrayList<String> parameter for the constructor of BarData. If you try that in 3.x.x then you will get a message like this:

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

这适用于许多流行但过时的教程,例如

This applies to many popular but outdated tutorials like the Truition tutorial for MPAndroidChart here

相反,在MPAndroidChart 3.x.x中,方法是使用IAxisValueFormatter.此接口具有单个方法getFormattedValue(float value, AxisBase axis),您可以实施该方法来以编程方式生成标签.

Instead, in MPAndroidChart 3.x.x the way to do this is by using a IAxisValueFormatter. This interface has a single method getFormattedValue(float value, AxisBase axis) which you implement to programatically generate your label.

示例项目,并在在此处答复

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

Altogether, the correct way to add data to a BarChart in MPAndroidChart3.x.x is the following (as per the example in the sample project):

 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>作为标签,则可以使用便捷类

Note: if you must use an ArrayList<String> for your labels you can use the convenience class 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可以接收标签.

to mimic the behaviour of MPAndroidChart 2.x.x where only whole number xIndices receive labels.

关于您的第二个问题,由于标签功能由IAxisValueFormatter很好地控制,因此您不再需要setSpaceBetweenLabels().

As for your second question, because the label functionality is very finely controlled by the IAxisValueFormatter you won't need the setSpaceBetweenLabels() any more.

这篇关于MPAndroidChart v3.x.x:从2.x.x标签升级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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