I am trying to build a function that returns a chart. I want to have a parameter to account for times where i may need more than one series. How does one loop through to create multiple series? I would think you would need a variable for each series. The function is below. I would think that if there were 5 series that each of the "Dataseries" variables should have their own name. Do I then refer to them by index only?
Public Shared Function MakeChart(ByVal form As Form, Optional ByVal numseries As Integer = 0) As Chart
' Add any initialization after the InitializeComponent() call.
Dim SampleChart As Chart = New Chart()
Dim MainChartArea As ChartArea = New ChartArea()
Dim ChartLegend As Legend = New Legend()
Dim Dataseries As Series = New Series()
Dim seriesname As String = ""
'add additonal series if the parameter exists
If numseries > 0 Then
For i As Integer = 0 To numseries - 1
seriesname = "Series" & Convert.ToString(i)
Dataseries = SampleChart.Series.Add(i)
Dataseries.Name = seriesname
Next
Else
DataSeries = SampleChart.Series.Add("Series1")
Dataseries.Name = "Series1"
End If
form.Controls.Add(SampleChart)
SampleChart.ChartAreas.Add(MainChartArea)
SampleChart.Legends.Add(ChartLegend)
SampleChart.Dock = DockStyle.Fill
SampleChart.TabIndex = 0
Return SampleChart
End Function