I want to add my bar values to the left of the bar and the bar labels to the right of the bar.
Below is the code that initiliz
Yes, this is definitely possible using the ValueFormatter
and AxisValueFormatter
interfaces.
Create a formatter that adds the labels to the right of your bars:
public class LabelFormatter implements ValueFormatter {
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
// assuming that getHeader() returned the label
return (String) entry.getData();
}
}
Create an axis formatter that adds the values to the left of your bars:
public class BarValueFormatter implements AxisValueFormatter {
private final DataSet mData;
public BarValueFormatter(DataSet data) {
mData = data;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
return String.valueOf((int) mData.getEntryForXPos(value, DataSet.Rounding.CLOSEST).getY());
}
}
Set your formatters to the dataset and axis:
set1.setValueFormatter(new LabelFormatter());
mChart.getXAxis().setValueFormatter(new BarValueFormatter(set1));
Note that you need to remove the call to xl.setDrawLabels(false);
for the x-axis, so the values are properly rendered.