Reputation: 841
I am using MPAndroid library. implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3' I need to create BarChart with labels. My code is bellow but it's not working: This is array of bar to display.
public ArrayList<BarEntry> getBarEntryArrayList() {
ArrayList<BarEntry> barEntries = new ArrayList<BarEntry>();
barEntries.add(new BarEntry(2f, 0));
barEntries.add(new BarEntry(4f, 1));
barEntries.add(new BarEntry(6f, 2));
barEntries.add(new BarEntry(8f, 3));
barEntries.add(new BarEntry(7f, 4));
barEntries.add(new BarEntry(3f, 5));
return barEntries;
}
This is label array to display at bottom.
public ArrayList<String> getBarEntryLabels() {
ArrayList<String> BarEntryLabels = new ArrayList<String>();
BarEntryLabels.add("J");
BarEntryLabels.add("F");
BarEntryLabels.add("M");
BarEntryLabels.add("A");
BarEntryLabels.add("M");
BarEntryLabels.add("J");
BarEntryLabels.add("J");
BarEntryLabels.add("A");
BarEntryLabels.add("S");
BarEntryLabels.add("O");
BarEntryLabels.add("N");
BarEntryLabels.add("D");
return BarEntryLabels;
}
Problem to set labels array and entry array
private void setBarChartData() {
BarDataSet barDataSet = new BarDataSet(getBarEntryArrayList(), getBarEntryLabels() );
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
BarData barData = new BarData(barDataSet);
barChart.setData(barData);
barChart.animateY(3000);
}
This line is not working.
BarDataSet barDataSet = new BarDataSet(getBarEntryArrayList(), getBarEntryLabels() );
If make code without labels it's working.
BarDataSet barDataSet = new BarDataSet(getBarEntryArrayList(), "Dummy text" );
I have to use array of lables. Thanks in advance.
I need to created graph like this. Please help me.
Upvotes: 1
Views: 2369
Reputation: 141
You can set it as following way,
In Kotlin,
val xAxis = chart.xAxis
xAxis.valueFormatter = IndexAxisValueFormatter(labels)
If you are still facing issue, make sure you have not hidden x-Axis. If you'll hide x-Axis, label will not be shown. If you want x-Axis hidden then, you have to make x-Axis visible and give it a transparent color.
Upvotes: 2
Reputation: 3189
You need to do following:
barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(BarEntryLabels));
Happy Coding :)
Upvotes: 3
Reputation: 13027
When you create bar entries, add the label as third parameter:
barEntries.add(new BarEntry(2f, 0, "J"));
barEntries.add(new BarEntry(4f, 1, "F"));
barEntries.add(new BarEntry(6f, 2, "M"));
barEntries.add(new BarEntry(8f, 3, "A"));
barEntries.add(new BarEntry(7f, 4, "M"));
barEntries.add(new BarEntry(3f, 5, "P"));
After barChart.setData(barData);
you can set Labels above or below bars with setValueFormatter
:
barChart.getBarData().setValueFormatter(new IValueFormatter() {
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
return entry.getData().toString();
}
});
Also increase font size:
barChart.getData().setValueTextSize(15);
P.S. you don't need getBarEntryLabels
method.
Here is the result:
Upvotes: 4