Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
744 views
in Technique[技术] by (71.8m points)

android - MPAndroidChart v3.x.x: upgraded from 2.x.x labels

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First question

mBarChart.setDescription(); doesn't work anymore

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

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

Third question

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

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

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.

There is an example in the sample project and in the answer here

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

Note: if you must use an ArrayList<String> for your labels you can use the convenience class IndexAxisValueFormatter

You consume it like this:

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

You will need:

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...