Mpandroidchart: Duplicate XAxis Labels

Created on 3 Nov 2016  路  6Comments  路  Source: PhilJay/MPAndroidChart

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

Most helpful comment

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.

All 6 comments

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

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.

Cheers, that seems to do the trick!

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

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

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);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

galex picture galex  路  3Comments

thanhcly920 picture thanhcly920  路  3Comments

vishvendu picture vishvendu  路  3Comments

AndroidJiang picture AndroidJiang  路  3Comments

ChenZeFengHi picture ChenZeFengHi  路  3Comments