user6415785
user6415785

Reputation:

How to update a Chart

I've just finished developing a piece of code where I am able to create a Chart and query from my database. Here I insert values from the database and it will show to the user.

I will post here the code that I've done to create a chart:

Public Sub BuildChart()
    Try
        SQLCon = New SqlConnection
        SQLCon.ConnectionString = "........."
        Dim sqlStatis As String = "SELECT Top 5 Filename, Filesize FROM infofile"
        Dim Chart1 As New Chart()
        Dim da As New SqlDataAdapter(sqlStatis, SQLCon)
        Dim ds As New DataSet()
        da.Fill(ds, "infofile")

        Dim ChartArea1 As ChartArea = New ChartArea()
        Dim Legend1 As Legend = New Legend()
        Dim Series1 As Series = New Series()
        Me.Controls.Add(Chart1)

        ChartArea1.Name = "ChartArea1"
        Chart1.ChartAreas.Add(ChartArea1)
        Legend1.Name = "Legend1"
        Chart1.Legends.Add(Legend1)
        Chart1.Location = New System.Drawing.Point(12, 12)
        Chart1.Name = "Chart1"
        Series1.ChartArea = "ChartArea1"
        Series1.Legend = "Legend1"
        Series1.Name = "Tamanho do ficheiro"
        Chart1.Series.Add(Series1)
        Chart1.Size = New System.Drawing.Size(600, 300)
        Chart1.TabIndex = 0
        Chart1.Text = "Chart1"

        Chart1.Series("Tamanho do ficheiro").XValueMember = "Filename"
        Chart1.Series("Tamanho do ficheiro").YValueMembers = "Filesize"

        Chart1.DataSource = ds.Tables("infofile")
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    Finally
        SQLCon.Dispose()
    End Try
End Sub

As you can see I've created a method which will be called in the form and the info will be shown there. Outside of everything I declared a variable Dim Chart1 As New Chart(). Now I want to create a method which allows me with a timer to update automatically the chart. So I should create another method called UpdateChart where I could insert there this:

Timer1.Interval = 3000
Timer1.Start()

But now I have no idea what should I use to update it every 3 secs or 3000 milliseconds.

Upvotes: 1

Views: 1156

Answers (1)

Bugs
Bugs

Reputation: 4489

On Load you want to call the BuildChart method and start the timer:

Private Sub frmTest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    BuildChart()

    With Timer1
        .Enabled = True
        .Interval = 3000
        .Start()
    End With

End Sub

For BuildChart you only need to create the chart itself and not bind the data.

Public Sub BuildChart()
    Try
        Dim Chart1 As New Chart()

        Dim ChartArea1 As ChartArea = New ChartArea()
        Dim Legend1 As Legend = New Legend()
        Dim Series1 As Series = New Series()
        Me.Controls.Add(Chart1)

        ChartArea1.Name = "ChartArea1"
        Chart1.ChartAreas.Add(ChartArea1)
        Legend1.Name = "Legend1"
        Chart1.Legends.Add(Legend1)
        Chart1.Location = New System.Drawing.Point(12, 12)
        Chart1.Name = "Chart1"
        Series1.ChartArea = "ChartArea1"
        Series1.Legend = "Legend1"
        Series1.Name = "Tamanho do ficheiro"
        Chart1.Series.Add(Series1)
        Chart1.Size = New System.Drawing.Size(600, 300)
        Chart1.TabIndex = 0
        Chart1.Text = "Chart1"

        Chart1.Series("Tamanho do ficheiro").XValueMember = "Filename"
        Chart1.Series("Tamanho do ficheiro").YValueMembers = "Filesize"

     Catch ex As Exception
        MessageBox.Show(ex.ToString())
    Finally
        SQLCon.Dispose()
    End Try
End Sub

We then want UpdateChart to be called from the Timer.Tick event.

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    UpdateChart()

End Sub

Private Sub UpdateChart()

    Chart1.Series(0).Points.Clear()
    Chart1.DataSource = ""

    SQLCon = New SqlConnection
    SQLCon.ConnectionString = "............."
    Dim sqlStatis As String = "SELECT Top 5 Filename, Filesize FROM infofile"

    Dim da As New SqlDataAdapter(sqlStatis, SQLCon)
    Dim ds As New DataSet()
    da.Fill(ds, "infofile")

    Chart1.DataSource = ds.Tables("infofile")    
End Sub

Be aware though that this will create a lot of hits to your database.

Upvotes: 1

Related Questions