3
votes

I have implemented MPChart library's BarChart.

When the number of bars in the bar chart are below 9, the labels are aligned to the bars but once the count increases above 9, the labels get skipped and instead of 9 labels on the x - axis it shows only 4-5. Truncating the label string size doesn't help.

Screenshots: enter image description here enter image description here

My code:

            float index = 0;
            ArrayList<String> xAxisLabel = new ArrayList<>();

            for (Map.Entry<String, Integer> callMapVal : callMap.entrySet()) {
                if (index < 8) {
                    barChartEntries.add(new BarEntry(index, callMapVal.getValue()));
                    if (callMapVal.getKey().length() > 10) {
                        xAxisLabel.add(getTruncated(callMapVal.getKey(), 9));
                    } else {
                        xAxisLabel.add(callMapVal.getKey());
                    }

                }
                index++;
            }

            BarDataSet dataSet = new BarDataSet(barChartEntries, selectedCallType.toString() + " CALLS");
            dataSet.setValueTextSize(8f);
            BarData chartData = new BarData(dataSet);
            dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
            barChart.setData(chartData);
            barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisLabel));
            //barChart.getXAxis().setLabelRotationAngle(20f);
            barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
            barChart.getXAxis().setTextSize(8f);
            //barChart.getXAxis().setAvoidFirstLastClipping(true);
            barChart.getXAxis().setGranularityEnabled(true);
            barChart.getXAxis().setCenterAxisLabels(false);
            barChart.getDescription().setEnabled(false);
            barChart.setFitBars(true); // make the x-axis fit exactly all bars
            barChart.invalidate(); // refresh
            barChart.setScaleEnabled(false);
            barChart.setDoubleTapToZoomEnabled(false);
            barChart.setBackgroundColor(Color.rgb(255, 255, 255));           
            barChart.setDrawBorders(false);
            barChart.setDrawValueAboveBar(true);
1
getTruncated remove this and try. - Ümañg ßürmån
I'm using that method to reduce the label size, it doesn't affect the chart. The params to this method is (String,StringSize) - ViVekH

1 Answers

0
votes

You can set the label count to the number of bars in your chart:

barChart.getXAxis().setLabelCount(9, true);