In my Android application I have a bar chart I made using MPAndroidChart
. My problem is when there are a number of bars in bar chart then the value labels above each bar are overlapping with other value labels as shown in the attached screen shot below. I know this is because there is no room for displaying the label, but I think this can be avoided if it is possible to rotate the value label by 90 degrees like in XAxis labels in my screen shot. So is that possible? If I zoomed then I can see all values clearly. I am using MPAndroidChart
v3.0.1.
Below is my code.
yVals1 = new ArrayList<BarEntry>();
xVals = new ArrayList<String>();
for (int i = 0; i < listChart.size(); i++){
BarEntry newBEntry = new BarEntry(i,listChart.get(i).getAmount());
xVals.add(listChart.get(i).getAltName());
yVals1.add(newBEntry);
}
BarDataSet set1;
if (bChartRepOne.getData() != null && bChartRepOne.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) bChartRepOne.getData().getDataSetByIndex(0);
set1.setValues(yVals1);
bChartRepOne.getData().notifyDataChanged();
bChartRepOne.notifyDataSetChanged();
} else {
set1 = new BarDataSet(yVals1, chart);
set1.setColors(ColorTemplate.MATERIAL_COLORS);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(8f);
bChartRepOne.setData(data);
}
XAxis xAxis = bChartRepOne.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setLabelCount(xList.size()-1);
xAxis.setGranularity(1f); // only intervals of 1 day
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
if(Math.round(value) >= xList.size()) {
return null;
} else {
return xList.get(Math.round(value));
}
}
});
xAxis.setLabelRotationAngle(-90);
YAxis leftAxis = bChartRepOne.getAxisLeft();
leftAxis.setLabelCount(8, false);
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
leftAxis.setSpaceTop(15f);
YAxis rightAxis = bChartRepOne.getAxisRight();
rightAxis.setEnabled(false);
Legend l = bChartRepOne.getLegend();
l.setEnabled(false);
bChartRepOne.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry entry, Highlight highlight) {
}
@Override
public void onNothingSelected() {
}
});