Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
703 views
in Technique[技术] by (71.8m points)

android - (MPAndroidChart-LineChart) I didn't get the last label in my app

I use MPAndroidChart v3.0.2 now. The problem is that lasst data('05?') is not showing...

This is the value while I get on Debug xVals : 1?, 2?, 5?

and It is the source and the result screenshot.

I think the '5?' enter the 'red circle'... but I don't know the reason..

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

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of this:

bottomAxis.setLabelCount(entries.size());

Use this overloaded version:

bottomAxis.setLabelCount(entries.size(), true);

Second parameter, boolean force forces to have exact number of labels. Without it, sometimes the method doesn't work, like in your case. From source code you can see:

sets the number of label entries for the y-axis max = 25, min = 2, default: 6, be aware that this number is not fixed (if force == false) and can only be approximated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...