array of series in a chart

ffb.boy.30

Member
Joined
Jan 18, 2017
Messages
5
Programming Experience
1-3
Hi,
I'm creating a class that draw multiple serie on a chart.
Now I would like to pass a parameters to my class to create the number of series received byt the class.

Actually I 've created a dynamic Chart but I stuck to create the array of series.
The code to create a series is
C#:
System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();

series1.ChartArea = "ChartArea1";
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
series1.Legend = "Legend1";
series1.Name = "Serie 1";

And I tought I could do something like this
C#:
System.Windows.Forms.DataVisualization.Charting.Series[] ChartSeries = new System.Windows.Forms.DataVisualization.Charting.Series[10];

Array.Resize(ref ChartSeries, NbSeries);

for (int i = 0; i < NbCharts; i++)
{
    ChartSeries[i].ChartArea = "ChartArea1";
    ChartSeries[i].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
    ChartSeries[i].Legend = "Legend";
    ChartSeries[i].Name = "Serie Number" + Convert.ToString(i+1);
    this.DrawChartInfo.Series.Add(ChartSeries[i]);
}

But it don't work because the ChartSeries array is empty I start the loop.

If someone have a tips to share.

Thanks for your help
 
If you just used NbSeries on line 1 instead of 10, then you wouldn't need line 3.

C# is not like C++. It is more like Java. You not only have to allocate an array, but you also need to allocate each of the items of the array. So the first thing you need to do within your loop is to allocate an element like you were in your first chunk of code.
 
Thanks for the tips.
I've modified my loop like this and ti seems to works
C#:
for (int i = 0; i < NbCharts; i++)
{
    System.Windows.Forms.DataVisualization.Charting.Series _Serie = new System.Windows.Forms.DataVisualization.Charting.Series();
    _Serie.ChartArea = "ChartArea1";
    _Serie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
    _Serie.Legend = default;
    _Serie.Name = "Serie Number " + Convert.ToString(i + 1);

    ChartSeries[i] = _Serie;
    this.DrawChartInfo.Series.Add(ChartSeries[i]);
}
 

Latest posts

Back
Top Bottom