Mpandroidchart: Bar chart bars do not align with x-axis labels

Created on 11 Dec 2016  ·  4Comments  ·  Source: PhilJay/MPAndroidChart

As shown below, the bar chart bars do not align with x-axis labels. I have tried a variety of potential solutions that I have seen here including:

1) chart.getRendererXAxis().getPaintAxisLabels().setTextAlign(Paint.Align.LEFT);
2) Setting the width of the bars and the size of the label text to potentially affect the spacing.

barchart

None of this has worked, please advice.

PS: This is awesome work by you. Hopefully I can contribute to the same soon.

Most helpful comment

It helps me:

xAxis.setAvoidFirstLastClipping(true)
xAxis.setCenterAxisLabels(true)

All 4 comments

+1 (having the same issue right now)
My first and last bar were cut off, so added these lines

barData.setBarWidth(0.4f);
combinedChart.getXAxis().setAxisMaximum(maxX + 0.2f);
combinedChart.getXAxis().setAxisMinimum(minX - 0.2f);

Now, the bars shift nicely inside the frame, but the gridlines and labels don't.. The labels keep their original positions (unlike the bars) and now don't align with the bars anymore.

+2 Having the same problem. I am using a bar for each month of the year. Jan and Dec bars only show half the bar if I use

xAxis.setAxisMinimum(0);
xAxis.setAxisMaximum(11);

I have tried setting the min and max to include half the bar height. I have also tried not setting them, but setting spaceMin and spaceMax to include half the bar height, but either results in the labels being misaligned, since they are drawn evenly across the range.

Edit:
I was able to fix this problem by setting

xAxis.setAxisMinimum(data.getXMin()-.5f);
xAxis.setAxisMaximum(data.getXMax()+.5f);

and changing
xAxis.setLabelCount(12, true);
to
xAxis.setLabelCount(12);

Noticed the subtle note in setLabelCount saying that it might misalign labels.
Hope this helps.

For the com.github.PhilJay:MPAndroidChart:v3.0.3

I am using a label list:

final List list_x_axis_name = new ArrayList<>();
list_x_axis_name.add("label1");
list_x_axis_name.add("label2");
list_x_axis_name.add("label3");
list_x_axis_name.add("label4");
list_x_axis_name.add("label5");

and setting the label like this:

BarChart chartBar = (BarChart) findViewById(R.id.chartBar);
XAxis xAxis = chartBar.getXAxis();
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);
xAxis.setLabelRotationAngle(-90);
xAxis.setValueFormatter(new IAxisValueFormatter() {
 @override
 public String getFormattedValue(float value, AxisBase axis) {
  if (value >= 0) {
   if (value <= list_x_axis_name.size() - 1) {
    return list_x_axis_name.get((int) value);
   }
   return "";
  }
  return "";
 }
});

It helps me:

xAxis.setAvoidFirstLastClipping(true)
xAxis.setCenterAxisLabels(true)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

blotfi picture blotfi  ·  3Comments

galex picture galex  ·  3Comments

tsengvn picture tsengvn  ·  3Comments

botondbutuza picture botondbutuza  ·  3Comments

rohitkumarbhagat picture rohitkumarbhagat  ·  3Comments