how to make a real time graph in visual studio , visual basic window form app

Hahhuh456 21 Reputation points
2022-09-06T09:04:14.147+00:00

Hello,
I am trying to make a real time line graph with visual basic in visual studio 2022 window form app.
the data of the graph is being pulled from a CSV file. the file updates every 10 sec.
I want to make the graph only portray around 10 data points at a time.
the graph also needs to update when new data point come in, the old data point should not be shown in the graph anymore

https://drive.google.com/file/d/1JwjiqNaGC3Jb3GmQuwSjqY2ekR-TTECc/view?usp=sharing

Above is the link for the CSV file

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,819 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,562 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 26,191 Reputation points Microsoft Vendor
    2022-09-08T05:54:51.287+00:00

    Hi @Hahhuh456 ,
    You can refer to the following code.

    Imports System.IO  
    Imports System.Windows.Forms.DataVisualization.Charting  
      
    Public Class Form1  
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
            Chart1.Series(0).ChartType = SeriesChartType.Line  
            Timer1.Interval = 1000  
            If Timer1.Enabled = False Then  
                Timer1.Enabled = True  
            Else  
                Timer1.Enabled = False  
            End If  
        End Sub  
      
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick  
            Chart1.Series(0).Points.Clear()  
            Dim fName = "C:\Yourfolder\Agriculture_Temp_Log.csv"  
            If File.Exists(fName) = True Then  
                Dim tmpstream As StreamReader = File.OpenText(fName)  
                Dim TextLine() As String = tmpstream.ReadToEnd().Split(Environment.NewLine)  
                For X = TextLine.Length - 12 To TextLine.Length - 2  'reading only the last 10 lines  
                    Dim strline = TextLine(X).Split(",")  
                    Chart1.Series(0).Points.AddXY(strline(1), CDbl(strline(2)))  
                Next  
                tmpstream.Close()  
            Else  
                MsgBox("file doesn't exist")  
            End If  
        End Sub  
    End Class  
    

    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful