Lon*_*Dao 4 android mpandroidchart
正如标题中所提到的,我正在开发一个使用MPAndroidChart 2.2.3版的项目.在此项目中,目前使用条形图.
我正在将版本升级到3.0.1.升级后,以下几件事情不再适用:
1.
mBarChart.setDescription("");
Run Code Online (Sandbox Code Playgroud)
2.
xAxis.setSpaceBetweenLabels(1);
Run Code Online (Sandbox Code Playgroud)
3.
BarData barData = new BarData(ArrayList<String>, ArrayList<IBarDataSet>);
Run Code Online (Sandbox Code Playgroud)
我环顾四周,但似乎没有解释这些问题,即使在发行说明中也是如此.
mBarChart.setDescription();不再起作用了
根据这个答案,现在设置描述的正确方法是:
mChart.getDescription().setText("Description of my chart);
Run Code Online (Sandbox Code Playgroud)
BarData barData = new BarData(ArrayList<String>, ArrayList<IBarDataSet>);不再起作用了
添加标签的方法与以前不同.在MPAndroidChart 2.xx中,您将传递xIndex标签作为ArrayList<String>构造函数的参数BarData.如果您在3.xx中尝试,那么您将收到如下消息:
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>)
Run Code Online (Sandbox Code Playgroud)
这适用于许多流行但过时的教程,例如MPAndroidChart的Truition教程
相反,在MPAndroidChart 3.xx中,这样做的方法是使用a IAxisValueFormatter.此接口有一个方法getFormattedValue(float value, AxisBase axis),您可以通过该方法以编程方式生成标签.
总而言之,在MPAndroidChart3.xx中向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
Run Code Online (Sandbox Code Playgroud)
注意:如果必须使用ArrayList<String>标签,则可以使用便捷类IndexAxisValueFormatter
你像这样消费它:
List<String> labels;
//TODO: code to generate labels, then
mChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));
Run Code Online (Sandbox Code Playgroud)
你会需要:
mChart.getXAxis().setGranularity(1);
mChart.getXAxis().setGranularityEnabled(true);
Run Code Online (Sandbox Code Playgroud)
模仿MPAndroidChart 2.xx的行为,其中只有整数xIndices接收标签.
至于你的第二个问题,因为标签功能非常精细,IAxisValueFormatter你不再需要了setSpaceBetweenLabels().
| 归档时间: |
|
| 查看次数: |
2017 次 |
| 最近记录: |