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

时间:2016-07-11 07:33:51

标签: android mpandroidchart

我在使用MPAndroid库的Android应用程序中显示一个简单的条形图。条形图有时会完美地显示数据。但是,有时候,它会显示消息“没有可用的图表数据'尽管DataSet有数据。

仅当我点击图表区域时才会显示图表。我搜索了这个,但我无法找到解决方案。以下是代码:

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");
    }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

最后,在您的情况之外,添加:

mBarChart.invalidate();
mBarChart.refreshDrawableState();

希望,这将解决您的问题。