MPAndroid条形图仅在点击“图表”区域后才显示图形,否则显示“没有可用的图表数据”

Bot*_*Bot 2 android mpandroidchart

我正在使用MPAndroid库在Android应用程序中显示一个简单的条形图。条形图有时会完美显示数据。但是,有时即使数据集包含数据,它也会显示消息“没有可用的图表数据”。

仅当我点击“图表”区域时,才会显示该图表。我用谷歌搜索,但是找不到解决方案。以下是代码:

if (mCount > 0){mBarDataSet = new BarDataSet(mBarEntryAssessmentList, "Assessment Count");
        mBarDataSet.setBarSpacePercent(5f);
        mBarData = new BarData(trimmedSubjectNameList, mBarDataSet);
        mBarData.setValueFormatter(new BarEntryValueFormatter()); // Setting a Value formatter to show Integer data instead of Float
        mBarChart.setData(mBarData);
        mBarChart.setDescription("");
        mBarChart.setDrawGridBackground(false);
        mBarChart.setDragEnabled(true);
        mBarChart.setTouchEnabled(true);
        mBarChart.setClickable(true);
        mBarChart.setScaleXEnabled(false);
        mBarChart.setScaleYEnabled(false);
        mBarChart.setVisibleXRange(1, 4);
        mBarChart.setHighlightPerDragEnabled(false);
        mBarChart.setHighlightPerTapEnabled(true); // set this to true if we want to listen to click events
        mBarChart.setOnChartValueSelectedListener(StudentProgressActivity.this);

        XAxis xAxis = mBarChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawLabels(true);
        xAxis.setDrawGridLines(false);
        xAxis.setLabelsToSkip(0); // Shows all the labels as initially we had problems showing all the labels

        YAxis leftAxis = mBarChart.getAxisLeft();
        leftAxis.setDrawLabels(true);
        leftAxis.setDrawGridLines(false);
        leftAxis.setAxisMinValue(0f); // Removes padding below YAxis minimum value and XAxis labels

        YAxis rightAxis = mBarChart.getAxisRight();
        rightAxis.setDrawLabels(false);
        rightAxis.setDrawGridLines(false);
    } else {
        mBarChart.setDescription("");
        mBarChart.setNoDataText("No Assessments yet");
    }
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。

Dhr*_*ruv 6

最后,在您的条件之外,添加:

mBarChart.invalidate();
mBarChart.refreshDrawableState();
Run Code Online (Sandbox Code Playgroud)

希望这能解决您的问题。

  • 它确实解决了问题。谢谢。 (2认同)