Read Time Chart With Data Updation

Lalit Acharekar 0 Reputation points
2023-05-14T06:09:34.0833333+00:00

I am getting data in sql server at every 5 sec i want to view that data in grpahical format for which I have written the below code my graph should be moving/update as data is been added in sql table in below code it is generating but not having a data which is added aft generation of data please help

 Private Sub finaltest()
        Chart1.Update()
        Chart1.Series.Clear()

        Chart1.Series.Add("Timestamp")
        Chart1.Series.Add("ZONE1C1")
        Chart1.Series.Add("ZONE2C1")
        Chart1.Series.Add("ZONE3C1")

        Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM [MPLC]", con)
        Dim dr As SqlDataReader = cmd.ExecuteReader
        While dr.Read
            Chart1.Series("ZONE1C1").Points.AddXY(dr("Timestamp").ToString, dr("ZONE1C1").ToString)
            Chart1.Series("ZONE1C1").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline
            Chart1.Series("ZONE1C1").Color = Color.Green
            Chart1.Series("ZONE1C1").BorderWidth = 1
            Chart1.Series("ZONE1C1").LegendText = "ZONE1C1"
            ' Chart1.ChartAreas("ChartArea1").AxisX.Interval = DateTimeIntervalType.Days
        End While
        dr.Close()
        cmd.Dispose()

        cmd1 = New SqlCommand("SELECT * FROM [MPLC]", con)
        dr = cmd1.ExecuteReader
        While dr.Read
            Chart1.Series("ZONE2C1").Points.AddXY(dr("Timestamp").ToString, dr("ZONE2C1").ToString)
            Chart1.Series("ZONE2C1").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline
            Chart1.Series("ZONE2C1").Color = Color.Red
            Chart1.Series("ZONE2C1").BorderWidth = 1
            Chart1.Series("ZONE2C1").LegendText = "ZONE2C1"

        End While
        dr.Close()
        cmd2 = New SqlCommand("SELECT * FROM [MPLC]", con)
        dr = cmd2.ExecuteReader
        While dr.Read
            Chart1.Series("ZONE3C1").Points.AddXY(dr("Timestamp").ToString, dr("ZONE3C1").ToString)
            Chart1.Series("ZONE3C1").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline
            Chart1.Series("ZONE3C1").BorderWidth = 1
            Chart1.Series("ZONE3C1").Color = Color.Blue
            Chart1.Series("ZONE3C1").LegendText = "ZONE3C1"
        End While

        dr.Close()

        Chart1.ChartAreas(0).AxisY.Minimum = 300
        Chart1.ChartAreas(0).AxisY.Maximum = 600
        Chart1.ChartAreas(0).AxisY.Interval = 50
        Chart1.ChartAreas(0).AxisX.Interval = 50
        Chart1.Legends("Legend1").Docking = Docking.Bottom

        If Chart1.ChartAreas(0).AxisX.Maximum > Chart1.ChartAreas(0).AxisX.ScaleView.Size Then
            Chart1.ChartAreas(0).AxisX.ScaleView.Scroll(Chart1.ChartAreas(0).AxisX.Maximum)
        End If

        'Me.Chart1.Printing.PrintDocument.DefaultPageSettings.Landscape = True

        'Me.Chart1.Printing.PrintDocument.DefaultPageSettings.Margins.Left = 0.25
        'Me.Chart1.Printing.PrintDocument.DefaultPageSettings.Margins.Right = 0.25
        'Me.Chart1.Printing.PrintDocument.DefaultPageSettings.Margins.Top = 0.25
        'Me.Chart1.Printing.PrintDocument.DefaultPageSettings.Margins.Bottom = 0.25
        'Me.Chart1.Printing.PrintPreview()



    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        finaltest()
    End Sub

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,071 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,631 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 27,666 Reputation points Microsoft Vendor
    2023-05-15T07:14:24.06+00:00

    Hi @Lalit Acharekar

    I tested with the code you provided and it was able to update to the chart whenever I added new data to the database.

    Could you please provide more information about your problem?

        Dim con As SqlConnection = New SqlConnection(ConString)
        Private Sub finaltest()
    		...
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            finaltest()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            con.Open()
            Timer1.Start()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Timer1.Stop()
            con.Close()
        End Sub
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments