1
我想構建一個返回圖表的函數。我想有一個參數來說明我可能需要多個系列的時間。如何循環創建多個系列?我想你會需要每個系列的變量。該功能如下。我認爲如果有5個系列,每個「數據集」變量都應該有自己的名字。然後我是否只通過索引來引用它們?循環創建圖表控件中的多個系列
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