我正在将版本升级到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 project和answer 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().