I am showing a simple Bar Chart in my Android application using the MPAndroid library. The Bar chart shows data perfectly at times. However, sometimes, it shows the message 'No chart data available' despite the DataSet having data.
The chart is displayed only when I tap the Chart area. I googled this but I can't find a solution. Following is the code:
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");
}
Any assistance will be appreciated.
At the end, outside your condition, add:
mBarChart.invalidate();
mBarChart.refreshDrawableState();
Hope, this will solve your problem.