MPAndroidChart:当有更多条目出现时,x轴上仅显示替代标签

KJE*_*a48 4 android bar-chart axis-labels mpandroidchart

在我的Android应用程序中,我有一个使用MPAndroidChart的水平条形图。我的问题是我的条形图中有12个条形图,每个条形图表示从4月到3月的月份,但是我只能在x轴上看到替代月份的标签。少量的条,然后我可以看到x轴上的所有标签。我没有使用

xAxix.setLabelCount()

方法。那么为什么我看不到所有标签?如果我缩放了,那么我可以看到每个条的标签。我正在使用MPAndroidChart v3.0.1。上面的截图。请看这里,我只能看到'Apr,Jun,Aug,Oct,Dec,Feb'和所有其他月份未显示。如何查看其他所有月份。

水平条形图

下面是我的代码。

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

        }
    });
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以更改要在中显示的标签数量y-axis

XAxis xAxis=lineChart.getXAxis();

xAxis.setLabelCount(4,true); //4 is the number of values to be shown.
Run Code Online (Sandbox Code Playgroud)


Vis*_*ani 5

尝试这个

// where 12 is the number of bars in your chart.
xAxis.setLabelCount(12)
Run Code Online (Sandbox Code Playgroud)