Reputation:
I am using from MPAndroidChart. How can I clear the previously drawn chart ?
I am using from (Line Chart (Dual YAxis))
Upvotes: 7
Views: 14607
Reputation: 7557
You can clear the chart value by adding these lines
chart.data = null
chart.invalidate()
Upvotes: 0
Reputation: 701
Actually, you do not need to clear chart. you need to clear the LIST you pass in, while setting up chart, it worked in my case.
Upvotes: 0
Reputation: 658
If you have already a dataset, Just remove it, before going to add new data
if (mDataSet != null) {
binding.chart.lineData.removeDataSet(mDataSet)
}
// create a dataset and give it a type
val set1 = LineDataSet(values, "Data1")
val dataSets: ArrayList<ILineDataSet> = ArrayList()
// add the data sets
dataSets.add(set1)
Upvotes: 0
Reputation: 3540
Sometimes you may not have reference to ArrayList, so you have to do something like that to to that properly.
if(chart.getData() != null)
chart.getData().clearValues();
chart.clear();
Note : chart.clear() assign null to mData and call invalidate on chart so you don't need multiple calls.
If you still have some zooming issues, you can use.
chart.setFitBars(true);
chart.fitScreen();
Upvotes: 0
Reputation: 1443
private fun resetChart() {
barChart.fitScreen()
barChart.data?.clearValues()
barChart.xAxis.valueFormatter = null
barChart.notifyDataSetChanged()
barChart.clear()
barChart.invalidate()
}
This is Kotlin but I found I needed to do all the steps to avoid crashing things.
Upvotes: 13
Reputation: 6622
You have to add the following line for the clear previous chart.
arraylist.clear();
mChart.invalidate();
mChart.clear();
Upvotes: 24
Reputation: 862
I am using the LineChart as a Fragment inside a ViewPager. All I had to do to clear the old data was assign the associated LineDataSet object to null.
Upvotes: 0