Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a lot of charts, is it possible to loop through to initialise / set data? right now i have

chart1.series[0].points.clear()
chart1.series[1].points.clear()

etc. for multiple series and multiple charts. i'd love to loop through but i can't seem to set the text using a loop

chart(i).series[i].points.clear()

i just get an error. i've also tried the loop below, and whilst i get no errors, nothing happens

it probably matters that the charts are located in split panels, which are located in tabs. so there are two charts per tab for 14 tabs.

thanks

What I have tried:

foreach (Chart x in this.Controls.OfType<chart>())
            {
                x.ChartAreas[0].series[0].clear
                x.ChartAreas[0].series[1].clear
                ....
            }
Posted
Updated 27-Oct-16 5:14am
v2
Comments
Member 12815488 27-Oct-16 22:33pm    
thanks! that worked. appreciate it

1 solution

An issue causing the error when you did the Chart[i].Series[i].Points.Clear() is most likely caused by using the same iterator for Chart and Series. This would only ever work if you had the same number of each Chart and Series.

Also the loop posted only clears points, have you debugged through to ensure it is getting inside the loop?

So while looping through the tabs as I'm assuming the loop you provided does, you will need 2 additional for/foreach loops.

Your loop will iterate each Chart control found.
Then one loop will iterate each ChartArea within the Chart loop, and the second should iterate each Series within the ChartArea loop.

C#
foreach (Chart x in this.Controls.OfType(Chart)) {
    foreach (ChartArea area in x.ChartAreas) {
        foreach (Series series in area.Series){
             area.series.Points.Clear();

             // Add new points here
        }
    }
}


You should add to ensure that the ChartAreas and Series collections are not null!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900