(MPAndroidChart-LineChart) 我没有在我的应用程序中得到最后一个标签

Adr*_*ian 2 android linechart mpandroidchart

我现在使用 MPAndroidChart v3.0.2。问题是最后一个数据('05?')没有显示......

这是我进入 Debug xVals 时的值:1?, 2?, 5?

它是来源和结果截图。

我认为“5?” 进入'红圈'...但我不知道原因..

  LineChart chart = (LineChart) findViewById(R.id.chart);
    if (ExRegList.length() == 0) {
        chart.clear();
        chart.setDescription(null);
        chart.setNoDataText("???? ???? ????."); // There is no data.
        chart.invalidate();
    } else {
        ArrayList<Entry> entries = new ArrayList<Entry>();
        final String[] xVals = new String[ExRegList.length()];

        try {
            for (int i = 0; i < ExRegList.length(); i++) {
                JSONObject obj = ExRegList.getJSONObject(i);
                String date = obj.getString("REG_DT").split("-")[2] + "?";
                xVals[i] = date;
                int score = obj.getInt("ANSWER02");
                switch (score) {
                    case 1:
                        entries.add(new Entry(i,3f));//????
                        break;
                    case 2:
                        entries.add(new Entry(i,2f));
                        break;
                    case 3:
                        entries.add(new Entry(i,1f));
                        break;
                    case 4:
                        entries.add(new Entry(i,0f));//?????
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        LineDataSet lineDataSet = new LineDataSet(entries, "");
        lineDataSet.setLineWidth(2);
        lineDataSet.setDrawCircleHole(true);
        lineDataSet.setDrawCircles(true);
        lineDataSet.setDrawHorizontalHighlightIndicator(false);
        lineDataSet.setDrawHighlightIndicators(false);
        lineDataSet.setDrawValues(false);

        LineData lineData = new LineData(lineDataSet);
        chart.setData(lineData);

        XAxis xAxis = chart.getXAxis();
        xAxis.setValueFormatter(new IndexAxisValueFormatter(xVals));
        XAxis bottomAxis = chart.getXAxis();
        bottomAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        bottomAxis.setDrawLabels(true);
        bottomAxis.setDrawGridLines(false);
        bottomAxis.setDrawAxisLine(true);
        bottomAxis.setLabelCount(entries.size());

        YAxis yAxis = chart.getAxisLeft();
        yAxis.setLabelCount(4, true);
        yAxis.setAxisMinimum(0.0f);
        yAxis.setAxisMaximum(3.0f);

        yAxis.setValueFormatter(new IAxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase yAxis) {
                String format = "";
                switch ((int) value) {
                    case 3:
                        format = "?? ??";
                        break;
                    case 2:
                        format = "??";
                        break;
                    case 1:
                        format = "???";
                        break;
                    case 0:
                        format = "?? ???";
                        break;
                    default:
                        break;
                }
                return format;
            }
        });
        yAxis.setTextColor(Color.BLACK);

        YAxis yRAxis = chart.getAxisRight();
        yRAxis.setDrawLabels(false);
        yRAxis.setDrawAxisLine(false);
        yRAxis.setDrawGridLines(false);

        chart.setFocusable(false);
        chart.setPinchZoom(false);
        chart.setDoubleTapToZoomEnabled(false);
        chart.setDrawGridBackground(false);
        chart.setDescription(null);
        chart.getLegend().setEnabled(false);
        chart.animateY(2000, Easing.EasingOption.EaseInCubic);
        chart.invalidate();
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Mis*_*pov 5

取而代之的是:

bottomAxis.setLabelCount(entries.size());
Run Code Online (Sandbox Code Playgroud)

使用这个重载版本:

bottomAxis.setLabelCount(entries.size(), true);
Run Code Online (Sandbox Code Playgroud)

第二个参数,boolean force强制具有确切数量的标签。没有它,有时该方法不起作用,就像您的情况一样。从源代码可以看到:

设置 y 轴的标签条目数 max = 25, min = 2, default: 6, 请注意这个数字不是固定的(如果 force == false),只能近似。