VB.NET 使用CHART 控件做实时曲线
本人初学vb.net 编程,因一工程需要将现场数据采集过来,并以实时曲线的方式,显示出来,顾写了个例子程序,供大家参考,不对的地方请大家指正,谢谢!
废话不多说,下面是整个代码,希望能够帮到大家。
Imports System.Windows.Forms.DataVisualization.ChartingPublic Class Form1'//定义曲线1X轴变量为时间Private X1(19) As String'//定义曲线1Y轴变量为瞬时流量Private Y1(19) As Double'//定义曲线2X轴变量为时间Private X2(19) As String'//定义曲线2Y轴变量为瞬时流量Private Y2(19) As Double'//定义画图区域变量Private fluxArea As ChartArea'//定义Serial对象Private fluxLine1 As SeriesPrivate fluxLine2 As Series'//定义Legends对象Private fluxLegend As LegendPrivate Sub InitChartSet()'//初始化Chart对象设置'//设置控件背景色Chart1.BackColor = Color.Black'//定义标题对象变量Dim ChartTitle As New Title'//设置Title信息With ChartTitle.Text = "皮带秤流量实时曲线".ForeColor = Color.Yellow.Font = New System.Drawing.Font("微软雅黑", 16.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))End With'//清除所有标题对象Chart1.Titles.Clear()Chart1.Titles.Add(ChartTitle)'//清空画图区域Chart1.ChartAreas.Clear()'//创建chartarea对象实例fluxArea = New ChartArea("fluxArea")'//将定义的画图区域添加到chartChart1.ChartAreas.Add(fluxArea)'//设置显示区域With fluxArea'//设置背景颜色为黑色.BackColor = Color.Black'//设置X轴最小值.AxisX.Minimum = 1'//设置X轴最大值.AxisX.Maximum = 20'//设置X轴.AxisX.Interval = 1'//设置X轴标题.AxisX.Title = "时间"'//设置X轴标题字体.AxisX.TitleFont = New System.Drawing.Font("微软雅黑", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))'//设置X轴线宽.AxisX.LineWidth = 3'//设置X轴颜色.AxisX.LineColor = Color.Green'//设置X轴线型.AxisX.LineDashStyle = ChartDashStyle.Solid'//设置X轴标题对齐.AxisX.TitleAlignment = StringAlignment.Center'//设置X轴标题颜色.AxisX.TitleForeColor = Color.Yellow'//设置X轴网格刻度线.AxisX.MajorGrid.LineColor = Color.Green'//设置X轴网格刻度线宽度.AxisX.MajorGrid.LineWidth = 1'//设置X轴网格刻度线样式.AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash'//设置X轴刻度线颜色.AxisX.MajorTickMark.LineColor = Color.Green'//设置X轴刻度线长度.AxisX.MajorTickMark.Size = 2'//设置X轴刻度线宽度.AxisX.MajorTickMark.LineWidth = 2'//设置X轴刻度线样式.AxisX.MajorTickMark.LineDashStyle = ChartDashStyle.Solid'//设置X轴刻度线位置.AxisX.MajorTickMark.TickMarkStyle = TickMarkStyle.OutsideArea'//设置X轴标签颜色.AxisX.LabelStyle.ForeColor = Color.LawnGreen'//设置Y轴最小值.AxisY.Minimum = 0D'//设置Y轴最大值.AxisY.Maximum = 150D'//设置Y轴标题.AxisY.Title = "皮带秤瞬时流量"'//设置Y轴标题字体.AxisY.TitleFont = New System.Drawing.Font("微软雅黑", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))'//设置X轴线宽.AxisY.LineWidth = 3'//设置X轴颜色.AxisY.LineColor = Color.Green'//设置X轴线型.AxisY.LineDashStyle = ChartDashStyle.Solid'//设置Y轴标题对齐.AxisY.TitleAlignment = StringAlignment.Center'//设置Y轴标题颜色.AxisY.TitleForeColor = Color.Yellow'//设置Y轴网格刻度线.AxisY.MajorGrid.LineColor = Color.Green'//设置Y轴网格刻度线宽度.AxisY.MajorGrid.LineWidth = 1'//设置Y轴网格刻度线样式.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash'//设置X轴刻度线颜色.AxisY.MajorTickMark.LineColor = Color.Green'//设置X轴刻度线长度.AxisY.MajorTickMark.Size = 2'//设置X轴刻度线宽度.AxisY.MajorTickMark.LineWidth = 2'//设置X轴刻度线样式.AxisY.MajorTickMark.LineDashStyle = ChartDashStyle.Solid'//设置X轴刻度线位置.AxisY.MajorTickMark.TickMarkStyle = TickMarkStyle.OutsideArea'//设置Y轴标签颜色.AxisY.LabelStyle.ForeColor = Color.LawnGreenEnd With'//创建Legends对象实例fluxLegend = New Legend("fluxLegend")'//设置Legends对象With fluxLegend'//设置图例停靠位置为自动.Position.Auto = True'//设置图例对齐方式为居中.Alignment = StringAlignment.Center'//设置图例停靠位置为底部.Docking = Docking.Bottom'//设置图例的背景色为透明.BackColor = Color.Transparent'//设置图例的字体颜色为白色.ForeColor = Color.White'//设置图例字体.Font = New System.Drawing.Font("微软雅黑", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))End With'//清除默认LegendsChart1.Legends.Clear()Chart1.Legends.Add(fluxLegend)'//创建series对象实例fluxLine1 = New Series("fluxLine1")fluxLine2 = New Series("fluxLine2")'//设置Serial对象1With fluxLine1'//设置线型.ChartType = SeriesChartType.Spline'//设置画线区域.ChartArea = "fluxArea"'//设置画线的宽度.BorderWidth = 3'//设置画线颜色.Color = Color.LawnGreen'//设置画线阴影.ShadowOffset = 1'//设置图示文字.LegendText = "4A路皮带秤"'//设置图例属性.Legend = "fluxLegend"End With'//设置Serial对象2With fluxLine2'//设置线型.ChartType = SeriesChartType.Spline'//设置画线区域.ChartArea = "fluxArea"'//设置画线的宽度.BorderWidth = 3'//设置画线颜色.Color = Color.Red'//设置画线阴影.ShadowOffset = 1'//设置图示文字.LegendText = "4B路皮带秤"'//设置图例属性.Legend = "fluxLegend"End With'//清除默认seriesChart1.Series.Clear()'//将定义好的曲线对象添加到chartChart1.Series.Add(fluxLine1)Chart1.Series.Add(fluxLine2)End SubPrivate Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load'//InitChartSet()End SubPrivate Sub InitChartData()'// 设置初始数据Dim i As Integer = 0For i = 0 To 19X1(i) = Format(Now(), "HH:mm:ss")X2(i) = Format(Now(), "HH:mm:ss")Y1(i) = 0Y2(i) = 0Next'//显示数据Chart1.Series("fluxLine1").Points.DataBindXY(X1, Y1)Chart1.Series("fluxLine2").Points.DataBindXY(X2, Y2)End SubPrivate Sub RefreshChartData()'// 更新曲线数据Dim i As IntegerFor i = 0 To 18X1(i) = X1(i + 1)X2(i) = X2(i + 1)Y1(i) = Y1(i + 1)Y2(i) = Y2(i + 1)NextY1(19) = Math.Round(100 * Rnd(100) * 1.0264, 2)X1(19) = Format(Now(), "HH:mm:ss")Y2(19) = Math.Round(100 * Rnd(100) * 1.0264, 2)X2(19) = Format(Now(), "HH:mm:ss")Chart1.Series("fluxLine1").Points.DataBindXY(X1, Y1)Chart1.Series("fluxLine2").Points.DataBindXY(X2, Y2)End SubPrivate Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.ClickInitChartData()Timer1.Enabled = TrueEnd SubPrivate Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.TickRefreshChartData()End Sub
End Class