.net chart clear and re-add

I have a chart and I need to clear it in order to populate it with different values.
The chart has 3 series, all defined in the .aspx page.

The problem is when I call

chart.Series.Clear();

and then re-add the series like:

chart.Series.Add("SeriesName");

It doesn’t keep any of the attributes of the 3 initial series.
How to just clear the values and keep the series attributes?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This should work:

foreach(var series in chart.Series) {
    series.Points.Clear();
}

Method 2

This should work

 chartnameHERE.Series["SeriesNameHERE"].Points.Clear();

Method 3

This will actually completely remove the series from the chart (not just remove the points from the series).

while (chart1.Series.Count > 0) { chart1.Series.RemoveAt(0); }

Method 4

I Faced kind of problem(but for windows form Application). So can you please tell me when are you passing the datasource to the charts.

I Used incorrect order while passing the data to the chart control

At first time I did this :

chart1.Series["Series1"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Auto;

chart1.DataSource = sqlChartTabel; //sqlChartTable is my DataTable
// Assigning new Data to the charts
chart1.Series.Clear();   // clearing chart series (after the DataTable is assigned with data)
chart1.Series.Add("Violations");
chart1.Series["Series1"].XValueMember = "Month_name";
chart1.Series["Series1"].YValueMembers = "Salary";
chart1.ChartAreas["ChartArea1"].AxisX.Title = "Months";
chart1.ChartAreas["ChartArea1"].AxisY.Title = "Salary";
chart1.DataBind();
chart1.Visible = true;

So, then I realized, I am Assigning the data before clearing the chart so I just changed my statement order, And It Worked :

My Working code :

chart1.Series.Clear();   // changed position of clear(before Assigning the datatable)
chart1.Series.Add("Violations");
chart1.Series["Series1"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Auto;
chart1.DataSource = sqlChartTabel;   //sqlChartTable is my DataTable

// Assigning new Data to the charts
chart1.Series["Series1"].XValueMember = "Month_name";
chart1.Series["Series1"].YValueMembers = "Salary";
chart1.ChartAreas["ChartArea1"].AxisX.Title = "Months";
chart1.ChartAreas["ChartArea1"].AxisY.Title = "Salary";
chart1.DataBind();
chart1.Visible = true;

Method 5

This works for me

foreach(var series in chart.Series)
{
    series.Points.Clear();
}
reloadData();
this.chart.DataBind();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x