C# .net 图表清除并重新添加
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11019086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.net chart clear and re-add
提问by sd_dracula
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.
我有一个图表,我需要清除它才能用不同的值填充它。该图表有 3 个系列,均在 .aspx 页面中定义。
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?
它不保留 3 个初始系列的任何属性。如何只清除值并保留系列属性?
采纳答案by James Hill
This should work:
这应该有效:
foreach(var series in chart.Series) {
series.Points.Clear();
}
回答by user2954518
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); }
回答by G?KAY G?K
This should work
这应该工作
chartnameHERE.Series["SeriesNameHERE"].Points.Clear();
回答by user3939328
This works for me
这对我有用
foreach(var series in chart.Series)
{
series.Points.Clear();
}
reloadData();
this.chart.DataBind();
回答by ajinkya
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.
我遇到了一些问题(但对于 Windows 窗体应用程序)。所以你能告诉我你什么时候将数据源传递给图表。
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;

