程序代码:
'新建项目时,在默认窗体Form1上添加一个按钮,一个chart Public Class Form1 Dim x(10) As String, y(10) As Double '示例中显示11条数据 Dim yy() As Double Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '按钮单击事件模拟取数 yy = GetValue() Dim i As Integer For i = 0 To yy.Length - 1 y(i) = yy(i) Next Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line Chart1.Series(0).Points.DataBindXY(x, y) Chart1.Series(0).IsValueShownAsLabel = True Chart1.Series(0).LabelForeColor = Color.Red Chart1.ChartAreas(0).AxisX.Minimum = 0 Chart1.ChartAreas(0).AxisX.Maximum = yy.Length Chart1.ChartAreas(0).AxisX.Interval = 1 End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load Chart1.Dock = DockStyle.Fill Button1.BringToFront() Chart1.ChartAreas(0).AxisX.MajorGrid.Enabled = False Chart1.ChartAreas(0).AxisY.MajorGrid.Enabled = False End Sub Function GetValue() As Array '这是一个模拟取数的函数 Dim a As Integer = 0 If yy Is Nothing Then a = 0 Else a = yy.Length End If If a = 11 Then For i As Integer = 0 To 9 yy(i) = yy(i + 1) x(i) = x(i + 1) Next yy(10) = Math.Round(a * Rnd(100) * 1.0264, 2) x(10) = Format(Now, "hh:mm:ss") Else ReDim Preserve yy(a) yy(a) = Math.Round(a * Rnd(100) * 1.0264, 2) x(a) = Format(Now, "hh:mm:ss") End If Return yy End Function End Class
其他的你再琢磨琢磨,下面是运行测试程序的截图