Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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() {

        }
    });
like image 440
KJEjava48 Avatar asked Dec 23 '16 10:12

KJEjava48


2 Answers

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.
like image 141
qwertygamer Avatar answered Sep 24 '22 14:09

qwertygamer


Try This

// where 12 is the number of bars in your chart.
xAxis.setLabelCount(12)
like image 31
Vishal Chhodwani Avatar answered Sep 23 '22 14:09

Vishal Chhodwani