MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

Bar chart bars do not align with x-axis labels

Open kalkrishnan opened this issue 9 years ago • 5 comments

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.

kalkrishnan avatar Dec 11 '16 21:12 kalkrishnan

+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.

tristanvda avatar Apr 21 '17 08:04 tristanvda

+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.

sevistatic avatar Dec 13 '17 16:12 sevistatic

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 "";  } });

Snehalsixthsense avatar Apr 21 '18 07:04 Snehalsixthsense

It helps me:

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

OlehBozhko avatar Nov 20 '19 12:11 OlehBozhko

HI! In my case, I have to put an empty string in the first and last index of the label array, this made labels aligned with the bars. Hope this can work for you as well.

qasim313 avatar Oct 19 '22 15:10 qasim313