简体   繁体   English

c#chart1.Series.Clear()

[英]c# chart1.Series.Clear()

for (int i = 0; i < intarr.Length; i++)
{
    Series series = this.chart1.Series.Add(strarr[i]);

    series.Points.Add(intarr[i]);
    series.XValueType = ChartValueType.Int32;
}

This is my code and I have这是我的代码,我有

chart1.Series.Clear();

at the top of the function I'm calling.在我调用的函数的顶部。 The first time round, it's fine.第一轮,还好。 However the second time I get this error但是第二次我收到这个错误

A chart element with the name 'NSW' already exists in the 'SeriesCollection'.

at this line在这一行

Series series = this.chart1.Series.Add(strarr[i]);

Thanks谢谢

Apparently strarr has two elements with the name "NSW".显然, strarr有两个名为“NSW”的元素。 You'll either have to catch the exception (I assume it throws an exception) or check that there isn't already an element with that type.您要么必须捕获异常(我假设它会引发异常),要么检查是否还没有具有该类型的元素。

Well this error occurs when you try to add a serie with name that is already existed in series collection!好吧,当您尝试添加名称已存在于系列集合中的系列时,会发生此错误! Chart's series name should be unique .图表的系列名称应该是唯一的

Probably strarr's values are not unique It contains multiple instance of same values.可能 strarr 的值不是唯一的它包含相同值的多个实例。

So how to prevent it?Follow code bellow :-)那么如何预防呢?按照下面的代码:-)

chart1.Series.Clear();
        for (int i = 0; i < intarr.Length; i++)
        {
            if (chart1.Series.FindByName(strarr[i])== null)
            {
                Series series = this.chart1.Series.Add(strarr[i]);
                series.Points.Add(intarr[i]);
                series.XValueType = ChartValueType.Int32;
            }
        }

I suggest you to change scenario as well,If you want further aids comment me.我建议你也改变场景,如果你想要进一步的帮助,请评论我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM