我认为我的主题总结了我有兴趣了解的内容。我希望创建一个图表,其中Series1来自Table1,Series2来自Table2中给定的数据集。
我的下面的代码不会抛出任何错误,但它似乎是从第一个表中获取每个系列的数据。
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Public Sub New(ByVal ChartData As DataSet)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim ChartArea1 As ChartArea = New ChartArea()
Dim Legend1 As Legend = New Legend()
Dim DataSeries(ChartData.Tables.Count - 1) As Series
Dim Chart1 = New Chart()
Me.Controls.Add(Chart1)
Chart1.ChartAreas.Add(ChartArea1)
Chart1.Legends.Add(Legend1)
Chart1.DataSource = ChartData
For index As UShort = 0 To ChartData.Tables.Count - 1
DataSeries(index) = New Series()
DataSeries(index).Name = ChartData.Tables(index).TableName
Chart1.Series.Add(DataSeries(index))
Chart1.Series(index).XValueMember = ChartData.Tables(index).Columns(0).ColumnName
Chart1.Series(index).YValueMembers = ChartData.Tables(index).Columns(1).ColumnName
Next
Chart1.Dock = DockStyle.Fill
Chart1.TabIndex = 0
End Sub
End Class
编辑:显然我可以通过迭代并从数据中设置点来获得我期望的结果......但是这种方法对于大型数据集来说似乎不会很快。有没有更好的方法来做到这一点?
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Public Sub New(ByVal ChartData As DataSet)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim ChartArea1 As ChartArea = New ChartArea()
Dim Legend1 As Legend = New Legend()
Dim DataSeries(ChartData.Tables.Count - 1) As Series
Dim Chart1 = New Chart()
Me.Controls.Add(Chart1)
Chart1.ChartAreas.Add(ChartArea1)
Chart1.Legends.Add(Legend1)
For index As UShort = 0 To ChartData.Tables.Count - 1
DataSeries(index) = New Series()
DataSeries(index).Name = ChartData.Tables(index).TableName
Chart1.Series.Add(DataSeries(index).Name)
For RowIndex As UShort = 0 To ChartData.Tables(index).Rows.Count - 1
Chart1.Series(DataSeries(index).Name).Points.AddXY(ChartData.Tables(index).Rows(RowIndex).Item(0), ChartData.Tables(index).Rows(RowIndex).Item(1))
Next
Next
Chart1.Dock = DockStyle.Fill
Chart1.TabIndex = 0
End Sub
End Class
答案 0 :(得分:1)
显示这个问题的答案。显然你必须手动添加数据点。
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Public Sub New(ByVal ChartData As DataSet)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim ChartArea1 As ChartArea = New ChartArea()
Dim Legend1 As Legend = New Legend()
Dim DataSeries(ChartData.Tables.Count - 1) As Series
Dim Chart1 = New Chart()
Me.Controls.Add(Chart1)
Chart1.ChartAreas.Add(ChartArea1)
Chart1.Legends.Add(Legend1)
For index As UShort = 0 To ChartData.Tables.Count - 1
DataSeries(index) = New Series()
DataSeries(index).Name = ChartData.Tables(index).TableName
Chart1.Series.Add(DataSeries(index).Name)
For RowIndex As UShort = 0 To ChartData.Tables(index).Rows.Count - 1
Chart1.Series(DataSeries(index).Name).Points.AddXY(ChartData.Tables(index).Rows(RowIndex).Item(0), ChartData.Tables(index).Rows(RowIndex).Item(1))
Next
Next
Chart1.Dock = DockStyle.Fill
Chart1.TabIndex = 0
End Sub
End Class