简体   繁体   中英

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". 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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