MPAndroidChart : Only alternate labels are shown in x axis when more entries comes

10.3k views Asked by At

In my android application i have a horizontal bar chart using MPAndroidChart.My problem is there are 12 number of bars in my bar chart each represents month from April to March,but i can see only alternate months label in x-axis.If there are small number of bars then i can see all the labels in x-axis.I didn't set any label count for x-axis using

xAxix.setLabelCount()

method.Then why i can't see all the labels? If I zoomed then i can see labels for each bar.I am using MPAndroidChart v3.0.1.Attached is the screenshot of the above.See here I can see only 'Apr,Jun,Aug,Oct,Dec,Feb' and all the other months are not displayed.How can I see all the other months also.

Horizontal BarChart

Below is my code.

        yVals1 = new ArrayList<BarEntry>();
    xVals = new ArrayList<String>();
    for (int i = 0; i < listChart.size(); i++){
        BarEntry newBEntry = new BarEntry(i,listChart.get(i).getAmount());
        xVals.add(listChart.get(i).getAltName());
        yVals1.add(newBEntry);
    }

    BarDataSet set1;
    set1 = new BarDataSet(yVals1, "");
    set1.setColors(new int[] {Color.BLUE,Color.GREEN});
    ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
    dataSets.add(set1);
    BarData data = new BarData(dataSets);
    data.setValueTextSize(10f);
    barChartIncExp.setData(data);

    barChartIncExp.setDrawBarShadow(false);
    barChartIncExp.setDrawValueAboveBar(true);

    barChartIncExp.getDescription().setEnabled(false);
    barChartIncExp.setMaxVisibleValueCount(60);
    // scaling can now only be done on x- and y-axis separately
    barChartIncExp.setPinchZoom(true);
    barChartIncExp.setDrawGridBackground(false);
    barChartIncExp.setHighlightFullBarEnabled(false);
    barChartIncExp.setHighlightPerDragEnabled(false);

    XAxis xAxis = barChartIncExp.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    xAxis.setGranularity(1f); 
    xAxis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            if(Math.round(value) >= xVals.size()) {
                return null;
            } else {
                return xVals.get(Math.round(value));
            }
        }
    });


    YAxis leftAxis = barChartIncExp.getAxisLeft();
    leftAxis.setDrawGridLines(false);
    leftAxis.setLabelCount(8, false);
    leftAxis.setSpaceTop(15f);
    leftAxis.setDrawLabels(false);

    YAxis rightAxis = barChartIncExp.getAxisRight();
    rightAxis.setLabelCount(8, false);
    rightAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);

    Legend l = barChartIncExp.getLegend();
    l.setEnabled(false);

    barChartIncExp.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
        @Override
        public void onValueSelected(Entry entry, Highlight highlight) {

        }

        @Override
        public void onNothingSelected() {

        }
    });
4

There are 4 answers

8
qwertygamer On BEST ANSWER

You can change the number of labels you want to show in the y-axis.

XAxis xAxis=lineChart.getXAxis();

xAxis.setLabelCount(4,true); //4 is the number of values to be shown.
0
Vishal Chhodwani On

Try This

// where 12 is the number of bars in your chart.
xAxis.setLabelCount(12)
0
qwertygamer On
XAxis xAxis=lineChart.getXAxis();    
xAxis.setLabelCount(4,true);
xAxis.setGranularity(1f);
0
Tushar Varshney On
XAxis xAxis = mChart.getXAxis();
xAxis.setLabelCount(label.size()+1, true);

Set the label count to the size of the label + 1 in the xAxis.setLabelCount function. It worked for me!