'clear the previously drawn chart MPAndroidChart - Android?

I am using from MPAndroidChart. How can I clear the previously drawn chart ?

I am using from (Line Chart (Dual YAxis))



Solution 1:[1]

You have to add the following line for the clear previous chart.

arraylist.clear();
mChart.invalidate();
mChart.clear();

Solution 2:[2]

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.

Solution 3:[3]

You just have to call

mChart.clear();

Solution 4:[4]

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.

Solution 5:[5]

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

Source : https://javadoc.jitpack.io/com/github/PhilJay/MPAndroidChart/v3.1.0/javadoc/com/github/mikephil/charting/charts/Chart.html#clear--

Solution 6:[6]

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)

Solution 7:[7]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mehul Kabaria
Solution 2 Kevin
Solution 3 Mathias
Solution 4 Lee Hounshell
Solution 5 Zhar
Solution 6 Parthi
Solution 7 Kishan Mevada