1

我正在尝试构建一个返回图表的函数。我想要一个参数来说明我可能需要多个系列的时间。一个循环如何创建多个系列?我认为每个系列都需要一个变量。函数如下。我认为如果有 5 个系列,那么每个“Dataseries”变量都应该有自己的名称。然后我是否只通过索引来引用它们?

  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
4

1 回答 1

0

索引或字符串,所以是的,您可以通过名称引用该系列:

MessageBox.Show("First Series is " & SampleChart.Series(0).Name)

或者

MessageBox.Show("First Series is " & SampleChart.Series("Series0").Name)

但请注意:如果未找到该字符串,则控件将引发ArgumentException错误。

于 2012-05-12T16:55:17.477 回答