在从多个文本框接收数据时更新的实时图表 VB Visual Studio

Live chart that updates as it recieves data from multiple textbox's VB Visual Studio

我正在处理的项目中有一个图表,我需要在输入每个文本框时更新其值,以便用户可以跟踪线性度。

根据我的基本知识,我能想到的唯一方法是在输入的每个文本框值处计算图表,但是超过 12 个文本框就太疯狂了。

目前我通过一个按钮Click让它工作(见下面的代码)。但是,我希望它在输入每个值时更新 - 任何对此的指示将不胜感激。

Private Sub ChartBtn_Click(sender As Object, e As EventArgs) Handles ChartBtn.Click

    'Average Repeatability at 5K Test Point
    Dim A5K1 As Integer = CInt(T5K1.Text)
    Dim A5K2 As Integer = CInt(T5K2.Text)
    Dim A5K3 As Integer = CInt(T5K3.Text)
    Dim average = (A5K1 + A5K2 + A5K3) / 3

    'Chart Setup
    With Testpoint_Chart.ChartAreas(0)
        .AxisX.Title = "Pressure (Psi)"
        .AxisX.Minimum = 1000
        .AxisX.Maximum = 10000

        .AxisY.Interval = 1000
        .AxisY.Title = "Test Points (ft.lb)"

        If T10K.Text > Math.Ceiling(tst_MaxOutput.Text / 1000) * 1000 Then
            .AxisY.Maximum = Math.Ceiling(T10K.Text / 1000) * 1000
        Else
            .AxisY.Maximum = Math.Ceiling(tst_MaxOutput.Text / 1000) * 1000
        End If

    End With

    Testpoint_Chart.Series.Clear()
    Testpoint_Chart.Series.Add("Test Data")
    Testpoint_Chart.Series.Add("Max Torque")

    'Max Torque
    With Testpoint_Chart.Series("Max Torque")
        .IsVisibleInLegend = True
        .ChartType = SeriesChartType.Line
        .IsValueShownAsLabel = False
        .Color = Color.Red

        Dim xmaxvals() As Integer = ({1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000})
        Dim ymaxvals() As Integer = {tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text, tst_MaxOutput.Text}

        .Points.DataBindXY(xmaxvals, ymaxvals)
    End With

    'Test Points
    With Testpoint_Chart.Series("Test Data")
        .IsVisibleInLegend = True
        .ChartType = SeriesChartType.Line
        .IsValueShownAsLabel = True

        Dim xvals() As Integer = ({1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000})
        Dim yvals() As Integer = {T1K.Text, T2K.Text, T3K.Text, T4K.Text, average, T6K.Text, T7K.Text, T8K.Text, T9K.Text, T10K.Text}

        .Points.DataBindXY(xvals, yvals)
    End With
End Sub

您可以为每个文本框订阅事件 "TextChanged"。这是一个带有四个文本框(A,B,C,D)的演示,您可以参考。在事件"TextChanged"中,可以使用"switch"语句来判断变化的textBox。然后重新绑定图表的数据并更新。

Public Class Form1
    Private Price As New List(Of Integer)
    Private Brand As New List(Of String)

    Private Sub Form1_Load(Sender As Form1, e As EventArgs) Handles MyBase.Load
        Me.Brand = New List(Of String) From {"A", "B", "C", "D"}
        Me.Price = New List(Of Integer) From {1, 2, 3, 4}

        ' Set initial data
        Chart1.Titles.Add("Line Chart")
        Chart1.ChartAreas(0).Axes(0).MajorGrid.Enabled = False
        Chart1.ChartAreas(0).Axes(1).MajorGrid.Enabled = False
        Chart1.Series.Clear()
        Chart1.Series.Add("Price")
        Chart1.Series("Price").Points.DataBindXY(Me.Brand, Me.Price)
        Chart1.Series(0).ChartType = SeriesChartType.Column
    End Sub

    ' subscribe to event "TextChanged"
    Private Sub TextBox_TextChanged(Sender As TextBox, e As EventArgs) Handles TextBoxA.TextChanged, TextBoxB.TextChanged, TextBoxC.TextChanged, TextBoxD.TextChanged
        Select Case Sender.Name
            Case TextBoxA.Name : Me.Price(0) = Convert.ToInt32(TextBoxA.Text)
            Case TextBoxB.Name : Me.Price(1) = Convert.ToInt32(TextBoxB.Text)
            Case TextBoxC.Name : Me.Price(2) = Convert.ToInt32(TextBoxC.Text)
            Case TextBoxD.Name : Me.Price(3) = Convert.ToInt32(TextBoxD.Text)
        End Select

        ' Rebind the data
        Chart1.Series("Price").Points.DataBindXY(Me.Brand, Me.Price)

        ' Update the chart
        Chart1.Update()
    End Sub
End Class