Harsha W
Harsha W

Reputation: 3366

Chart series clearing not working

I need to call CreateChart() in several times in my C# application. But an error says

Additional information: A chart element with the name 'Income' already exists in the 'SeriesCollection'.

I cleared the Chart series using the below piece of code.

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

Took the above code from stackoverflow Link

But still same issue comes. Here is the CreateChart(). It loads the chart for the first time without having an issue. If I load it multiple times, error occures.

foreach (var series in chart1.Series)
                {
                    series.Points.Clear();
                }
                chart1.Series[0].IsVisibleInLegend = false;
                var IncSeries = new Series("Income");
                var ExpSeries = new Series("Expense");
                IncSeries.Points.DataBindXY(new[] { "Today's Income" }, new[] { Income });
                ExpSeries.Points.DataBindXY(new[] { "Today's Expense" }, new[] { Expense });
                chart1.ChartAreas[0].AxisX.IsReversed = true;
                chart1.Series.Add(IncSeries);
                chart1.Series.Add(ExpSeries);

Upvotes: 0

Views: 861

Answers (1)

Paviel Kraskoŭski
Paviel Kraskoŭski

Reputation: 1419

You haven't cleared series. You've only cleared points in series.

chart1.Series.Clear();
var IncSeries = new Series("Income");
var ExpSeries = new Series("Expense");
IncSeries.Points.DataBindXY(new[] { "Today's Income" }, new[] { Income });
            ExpSeries.Points.DataBindXY(new[] { "Today's Expense" }, new[] { Expense });
chart1.ChartAreas[0].AxisX.IsReversed = true;
chart1.Series.Add(IncSeries);
chart1.Series.Add(ExpSeries);
chart1.Series[0].IsVisibleInLegend = false;

Upvotes: 2

Related Questions