Chart控件绘制实时折线图表——Spline
1、首先,将chart控件添加到窗口。
2、设置chart - series 主要t属性:
2.1 IsXValueIndexed = true;
2.2 XValueType = Time;
3、编写初始化函数:
Private Sub InitChart() Dim r As Random = New Random() Dim series As Series = Chart1.Series(0) Chart1.ChartAreas(0).AxisX.LabelStyle.Format = "HH:mm" Chart1.ChartAreas(0).AxisX.ScaleView.Size = 5 series.ChartType = SeriesChartType.Spline; Chart1.ChartAreas(0).AxisX.ScrollBar.IsPositionedInside = True Chart1.ChartAreas(0).AxisX.ScrollBar.Enabled = True series.Points.AddXY(DateTime.Now.ToString("HH:mm"), r.Next(1, 100)) If DateTime.Now.Second = 0 Then '因定时器与当前时间不准确,故判断当前时间秒数是否为0 Timer1.Interval = 1000 * 60 Else Timer1.Interval = 1000 * (60 - DateTime.Now.Second) End If Timer1.Start() End Sub
4.编写Time函数:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Timer1.Stop() 'Chart1.Series(0).Points.Clear() '每次清除上一次绘制的数据 Dim random As Random = New Random() Dim series As Series = Chart1.Series(0) series.Points.AddXY(DateTime.Now, random.Next(1, 20)) If DateTime.Now.Second = 0 Then '初始化定时器 Timer1.Interval = 1000 * 60 End If Timer1.Start() End Sub
5:在From_Load事件调用InitChart()方法
以上,借鉴博客
本文来自博客园,作者:云辰,转载请注明原文链接:https://www.cnblogs.com/yunchen/p/12485223.html