简体   繁体   中英

MPChart BarChart X-Axis label issue

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: 在此处输入图片说明 在此处输入图片说明

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);

您可以将标签数量设置为图表中的条形数量:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM