MPAndroidChart: XAxis values not corresponding to Data values

喜欢而已 提交于 2020-01-02 07:04:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!