循环以在图表控件中创建多个系列

时间:2012-05-12 11:47:35

标签: vb.net mschart

我正在尝试构建一个返回图表的函数。我想要一个参数来说明我可能需要多个系列的时间。如何循环创建多个系列?我认为每个系列都需要一个变量。功能如下。我认为如果有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

1 个答案:

答案 0 :(得分:0)

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

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

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

但请注意:如果找不到该字符串,控件将抛出ArgumentException错误。