代码之家  ›  专栏  ›  技术社区  ›  usr30911

MPChart条形图X轴标签问题

  •  1
  • usr30911  · 技术社区  · 7 年前

    我已经实现了MPChart库的条形图。

    当条形图中的条数小于9时,标签将与条对齐,但一旦计数增加到9以上,标签将被跳过,x轴上的9个标签将仅显示4-5。 截断标签字符串大小没有帮助。

    截图: enter image description here enter image description here

    我的代码:

                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 回复  |  直到 7 年前
        1
  •  0
  •   Danny Buonocore DTM    7 年前

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

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