Search code examples
androidmpandroidchart

MPAndroidChart, how to center XLabels above grid-lines?


I have a simple linechart with MPAndroidChart library:

chart.setDrawYValues(false);
chart.setDescription("");
chart.setDrawVerticalGrid(true);
chart.setDrawGridBackground(false);

XLabels xl = holder.chart.getXLabels();
xl.setCenterXLabelText(true);
xl.setPosition(XLabelPosition.BOTTOM);

YLabels yl = holder.chart.getYLabels();
yl.setLabelCount(5);

// set data
chart.setData((LineData) mChartData);

chart.setDrawYValues(true);
chart.setValueTextSize(20f);

// zooming on both axis
chart.setPinchZoom(true);
chart.animateX(1000);

But I have a visualization problem. Labels on the X axis aren't under the relative grid line, but between the two lines (on Y axis I don't have this problem). Is there a way to put Y labels exactly under the relative grid line?


Solution

  • Calling this:

    XLabels xl = chart.getXLabels();
    xl.setCenterXLabelText(true);
    

    will cause the labels to be drawn between the lines. If you call setCenterXLabelText(false); the labels should be exactly above the individual grid-lines.

    Also check the example project LineChart. There the labels are exactly above the lines.

    Update

    There is no getXLabels method in version 3.1.0. Use this:

    chart.xAxis.setCenterAxisLabels(true)