我正在尝试构建一个返回图表的函数。我想要一个参数来说明我可能需要多个系列的时间。如何循环创建多个系列?我认为每个系列都需要一个变量。功能如下。我认为如果有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
答案 0 :(得分:0)
索引或字符串,所以是的,您可以通过名称来引用该系列:
MessageBox.Show("First Series is " & SampleChart.Series(0).Name)
或
MessageBox.Show("First Series is " & SampleChart.Series("Series0").Name)
但请注意:如果找不到该字符串,控件将抛出ArgumentException
错误。