PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.48k stars 9k forks source link

Bar chart bars do not align with x-axis labels #2566

Open kalkrishnan opened 7 years ago

kalkrishnan commented 7 years ago

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.

tristanvda commented 7 years ago

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

sevistatic commented 6 years ago

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

Snehalsixthsense commented 6 years ago

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

OlehBozhko commented 4 years ago

It helps me:

xAxis.setAvoidFirstLastClipping(true)
xAxis.setCenterAxisLabels(true)
qasim313 commented 1 year ago

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.