VB.net中的chart控件绘制曲线遇到了麻烦,希望各位大神帮帮忙!
本帖最后由 bcrun 于 2013-09-25 08:15:06 编辑 各位前辈,小弟最近学习VB.net,想通过从数据库中查询到的数据显示在dategridview中,并利用chart控件绘制所查询到的数据其中两列数据的折线图,前边的部分都已实现,但是在用chart绘制折线时遇到麻烦了,写了一些代码,但是运行之后显示不出折线,不知道哪里出了问题,希望各位前辈指教一下,小弟感激不尽!
下面是写的一些代码:
Imports System.Windows.Forms.DataVisualization.Chartingvb.net 控件
Public Class 绘制曲线
Dim DataMain As New DataTable
Dim Column As DataColumn
Dim ColumnCount As Integer
Dim Series(256) As Series
Dim ColumnName(256) As String
Dim maxlen As Integer
Private grid As DataGridView
Private DataGridView1 As Object
Private Sub iniChart()
Chart1.Series.Clear()
Timer1.Enabled = False
For i As Integer = 1 To ColumnCount Step 1
Chart1.Series.Add(Series(i))
Series(i).YValueMembers = "混合流量"
Series(i).XValueMember = "测试日期"
Next
Timer1.Enabled = True
End Sub
Private Sub 绘制曲线_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.grid = grid
Call iniChart()
Chart1.DataSource = DataMain 'dt作为chart1的数据源
Chart1.Series.Clear()
Chart1.Legends.Clear()
Chart1.ChartAreas.Clear()
Chart1.ChartAreas.Add("")
For i As Integer = 0 To 100
Series(i) = New Series
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Chart1.ChartAreas.Clear()
Dim ChartAreas1 As New ChartArea("混合流量") '定义新的ChartArea
Chart1.ChartAreas.Add(ChartAreas1) '将新定义的ChartArea加入Chart1
Chart1.ChartAreas(0).AxisX.Title = "测试日期" '设置ChartArea里坐标轴标题
Chart1.ChartAreas(0).AxisY.Title = "混合流量"
Chart1.Series.Clear()
Dim series1 As New Series("混合流量")
series1.ChartType = SeriesChartType.Line '设置Series的绘图类型
series1.IsXValueIndexed = True
series1.XValueType = ChartValueType.DateTime
Chart1.Series.Add(series1)
Dim i, l(i) As Integer
For i = 1 To maxlen
Chart1.Series("混合流量").Points.AddXY(i, l(i)) '为这个Series加数据点
Next
Me.grid = grid
Chart1.Visible = False
Dim dt As DataTable = New DataTable
dt.Columns.Add("测试日期", GetType(System.Single))
dt.Columns.Add("混合流量", GetType(System.Single))
For i = 1 To DataGridView1.Rows.Count - 1
dt.Rows.Add(New Object() {DataGridView1.Rows(i).Cells(1).Value.ToString(), DataGridView1.Rows(i).Cells(5).Value, DataGridView1.Rows(i).Cells(6).Value, DataGridView1.Rows(i).Cells(7).Value})
Next
Me.Chart1.DataSource = dt
End Sub