如何更新图表

时间:2016-06-03 09:39:14

标签: vb.net

我刚刚完成了一段代码的开发,我可以在我的数据库中创建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

正如您所看到的,我已经创建了一个将在表单中调用的方法,并且信息将在那里显示。在所有内容之外,我声明了一个变量Dim Chart1 As New Chart()。现在我想创建一个方法,允许我使用计时器自动更新图表。所以我应该创建另一个名为UpdateChart的方法,我可以在那里插入:

Timer1.Interval = 3000
Timer1.Start()

但现在我不知道应该用什么来每3秒或3000毫秒更新一次。

1 个答案:

答案 0 :(得分:0)

Load上,您要调用BuildChart方法并启动计时器:

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

对于BuildChart,您只需创建图表本身而不是绑定数据。

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

然后我们希望从UpdateChart事件中调用Timer.Tick

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

请注意,这会给您的数据库带来很多点击。