Just started using the MPAndroidChart version 3.0.0 beta and i have created a project that can show my values in a bar chart. My question is where do i add and display labels. Each Bar should have its own label eg. "Flue", "Cheese" etc at the bottom. Not sure what function does this, am actively searching and reading the Docs/Wiki but no joy currently.
Asked
Active
Viewed 8,875 times
1 Answers
4
Depending on your preferences, you can use the data
property of an Entry
to store the label and then return it in your IAxisValueFormatter
implementation:
public class LabelValueFormatter implements IAxisValueFormatter {
private final DataSet mData;
public LabelValueFormatter(DataSet data) {
mData = data;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
// return the entry's data which represents the label
return (String) mData.getEntryForXPos(value, DataSet.Rounding.CLOSEST).getData();
}
}
This approach allows you to use the Entry
constructor (or BarEntry
in this case) to add the labels, which can improve the readability of your code:
ArrayList<BarEntry> entries = new ArrayList<>();
for (int i = 0; i < length; i++) {
// retrieve x-value, y-value and label
entries.add(new BarEntry(x, y, label));
}
BarDataSet dataSet = new BarDataSet(entries, "description");
BarData data = new BarData(dataSet);
mBarChart.setData(data);
mBarChart.getXAxis().setValueFormatter(new LabelValueFormatter(data));
Also check out this answer for more information and an alternative approach on using labels with the BarChart
and the new 3.0.0
version of the library.

Community
- 1
- 1

TR4Android
- 3,200
- 16
- 21
-
I cant get IAxisVlueFormatter, is this in version 3 of the lib?. I have v3 beta specified in gradle. Only AxisValueFormatter recognized. – brucen Aug 26 '16 at 15:07
-
Yes, that's the correct class. With the next release of the library it will be renamed though. – TR4Android Aug 26 '16 at 15:12
-
Thanks , it works perfectly now. just had to cast (BarData) data , because i had BarData as my variable to my Bar Chart adapter and used getDataSetByIndex(position). – brucen Aug 28 '16 at 05:27
-
Using ```v3.0.1``` the third param is not working :/ – JCarlosR Jan 26 '17 at 15:26
-
@JCarlos According to the [javadocs](https://jitpack.io/com/github/PhilJay/MPAndroidChart/v3.0.1/javadoc/com/github/mikephil/charting/data/BarEntry.html#BarEntry-float-float-java.lang.Object-) it's still there. What exactly is your problem? – TR4Android Jan 28 '17 at 09:09