Reputation: 508
I have an application that draws a dynamic chart and when it's done it saves some data and lets the user exit. (So there are multiple activities). Once the user exits, I want them to be able to come back and let the charts dynamically redraw themselves from 0. But for some reason, the chart always continues from the value where it left of.
Here's what I tried:
Clearing the XYSeries by calling clear(). Also, removed the series from my XYMultipleSeriesDataset, and cleared the Text Labels on my XYMultipleSeriesRenderer. Still, the graph continues where it left off.
Calling onDestroy() to get rid of all saved data. This worked to clear the chart, but when the user chose to exit, the app crashed because for some odd reason, the activity I was killing tried to recreate itself. When I tried onDestroy() together with finish(), there was no crash, but the graph saved it's data from the last run.
Calling super.onCreate(null) to avoid the savedInstanceState. The graph still saved it's data.
Can anyone help me please? Let me know if I need to post my code or anything else!
Thank you in advance!
Upvotes: 1
Views: 2557
Reputation: 855
You can have a method that does that each time you launch a new graph
private void removeGraph(){
//View item = lineChartLayout;
lineChartLayout.removeAllViews();
//((ViewGroup) item.getParent()).removeView(item);
}
Since the lineChartLayout
is a Layout
, then it is a ViewGroup
, hence we can remove all attached views on it by calling the removeAllViews
method on the layout and all views attached on it will be removed.
Cheers
Upvotes: 0
Reputation: 2529
I solved this problem by removing the view and then adding he view again.
Each time I build a new XYSeries and new dataset:
mDataset = new XYMultipleSeriesDataset(); //mDataset in my case is global
XYSeries series = new XYSeries("My Series Title");
series.add((double)y,Weight); //call this function several times if you want several points in the same series.
mDataset.addSeries(series);
mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer); //mChartView is also global
layout.removeAllViews(); //This remove previous graph
layout.addView(mChartView); //This loads the graph again
I don't know if this is the best way, but it works great and with almost no delay (I only have to update the graph when coming back from other activity, or the user enters data).
Hope this is useful. BR, Adrian.
Upvotes: 5