MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

Duplicate XAxis Labels

Open AdamMak opened this issue 10 years ago • 6 comments

I am trying to create a basic bar chart but am really struggling to get the XAxis labels display properly. The XAxis labels are duplicating for some reason. Here is my cide

final String[] quarters = new String[] { "Jan", "Feb", "March", "April" };

IAxisValueFormatter formatter = new IAxisValueFormatter() {

         @Override
         public String getFormattedValue(float value, AxisBase axis) {
             return quarters[(int) value];
         }

         @Override
         public int getDecimalDigits() {  return 0; }
     };

     XAxis xAxis = chart.getXAxis();
     xAxis.setValueFormatter(formatter);

     List<BarEntry> entries = new ArrayList<>();
     entries.add(new BarEntry(0f, 30f));
     entries.add(new BarEntry(1f, 80f));
     entries.add(new BarEntry(2f, 60f));
     entries.add(new BarEntry(3f, 50f));

     BarDataSet set = new BarDataSet(entries, "BarDataSet");
     BarData data = new BarData(set);
     chart.setData(data);
     chart.setFitBars(true);
     chart.invalidate(); // refresh

And the chart that is created looks like this

screen shot 2016-11-03 at 14 31 11

Any idea why this is happening? I just want one label per value. Any help would be much appreciated

AdamMak avatar Nov 03 '16 14:11 AdamMak

Same here. Moreover, I am also facing an overlapping issue. See below. screenshot_2016-11-04-11-43-46

sachinrana135 avatar Nov 04 '16 06:11 sachinrana135

I resolved this. just added one line.

xAxis.setGranularity(1f);

setGranularity(float gran): Sets the minimum interval between the axis values. This can be used to avoid value duplicating when zooming in to a point where the number of decimals set for the axis no longer allow to distinguish between two axis values.

sachinrana135 avatar Nov 04 '16 07:11 sachinrana135

Cheers, that seems to do the trick!

AdamMak avatar Nov 04 '16 09:11 AdamMak

I did the same but not working. The X axis labels are repeating. For y axis I have disabled the zooming already. ANy solution ?

rajeesh7 avatar Jan 18 '18 12:01 rajeesh7

xAxis.setGranularity(1f); saved me thank you

vaaniiaa avatar Apr 16 '18 07:04 vaaniiaa

I did the same but not working. The X axis labels are repeating. For y axis I have disabled the zooming already. ANy solution ?

Might be late, but you can try to activate the granularity before setting it at 1f. Moreover, you can set the label count following these lines :

XAxis xAxis = barChart.getXAxis();
barChart.getXAxis().setGranularityEnabled(true);
barChart.getAxisLeft().setGranularity(1f);
xAxis.setLabelCount(labelCount, false);

StevenTB avatar May 22 '19 11:05 StevenTB