user4813855
user4813855

Reputation:

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

Upvotes: 7

Views: 14607

Answers (8)

Naveen Kumar M
Naveen Kumar M

Reputation: 7557

You can clear the chart value by adding these lines

chart.data = null
chart.invalidate()

Upvotes: 0

Kishan Mevada
Kishan Mevada

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

Parthi
Parthi

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

Zhar
Zhar

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

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

Upvotes: 0

Mathias
Mathias

Reputation: 212

You just have to call

mChart.clear();

Upvotes: 3

Kevin
Kevin

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

Mehul Kabaria
Mehul Kabaria

Reputation: 6622

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

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

Upvotes: 24

Lee Hounshell
Lee Hounshell

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

Related Questions