Search code examples
vb.netlinechartdynamic-chart-series

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


I have a chart in a project that I am working on that I require to update its values as each textbox is input so the user can track the linearity.

The only way I can think of with my basic knowledge is to calculate the chart at each textbox value entered but this would be crazy over 12 textboxes.

Currently i have it working from a button Click (See Code below). However, I would like it to update as each value is entered - Any Direction with this would be greatly appreciated.

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

Solution

  • You can subscribe to the event "TextChanged" for each textbox. Here is a demo with four textBoxes(A,B,C,D) you can refer to. In the event "TextChanged", you can use "switch" statement to judge the changed textBox. Then rebind the chart's data and update it.

    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