问题
I've a problem using MPAndroidChart: I have some entry data to display in a Bar Chart, but the xaxis labels aren't displaying on the position I want: I want to show them one per chart value, immediatly under the "bar".
This is what is happening right now:
And this is my code:
ArrayList<BarEntry> entries1 = my entries...;
final ArrayList<String> stringList1 = my strings...;
XAxis xAxis1 = chartCost.getXAxis();
xAxis1.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis1.setDrawGridLines(false);
xAxis1.setCenterAxisLabels(true);
/*
xAxis1.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return stringList1.get((int)value);
}
});
*/
BarDataSet set = new BarDataSet(entries1, "myData");
BarData data = new BarData(set);
chartCost.setData(data);
chartCost.setFitBars(true);
chartCost.invalidate();
entries1 and stringList1 are being correctly populated. Here's an example:
- x: 0, y: 22,90
- x: 1, y: 21,53
- x: 2, y: 18,49
If I decomment the IAxisValueFormatter, the float value is not corresponding at the x value of my entries, but, as you can see, there are all decimal values!
I want to obtain this (place labels under bars):
But how can I do that? Thanks!
回答1:
Include below lines in your code
xAxis1.setCenterAxisLabels(false);
xAxis1.setGranularity(1f);
setCenterAxisLabels, default value is false. So it is not mandatory. If you have set it true, make it false.Else your bar will lie between the successive x values
来源:https://stackoverflow.com/questions/43027652/mpandroidchart-xaxis-values-not-corresponding-to-data-values