简体   繁体   English

MPChart BarChart X轴标签问题

[英]MPChart BarChart X-Axis label issue

I have implemented MPChart library's BarChart. 我已经实现了MPChart库的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. 当条形图中的条形数量小于9时,标签将与条形对齐,但是一旦计数增加到9以上,则会跳过标签,并且x轴上的9个标签而不是9个标签将显示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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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