1

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
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 に答える