VB.net使用新数据条目自动更新图表 [英] VB.net Automatic chart update with new data entry

查看:186
本文介绍了VB.net使用新数据条目自动更新图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图表从DataGridView加载数据。



如果新值插入DataGridView,我想使用新数据自动更新图表。



我的图表绑定到我的DataGridView中的 table1 table2 从DataTable。下面是代码的一小部分:

  Dim myData作为新数据表
wcAdapter.SelectCommand = wcCommand
wcAdapter.Fill(myData)

-

  Chart1.DataSource = myData 

Chart1.Series Series1)。ValueMemberX =table1
Chart1.Series(Series1)。ValueMembersY =table2

以下是完整的代码:

  Try 
wcconn .Open()
Dim wcCommand As New MySqlCommand()
''telesales name


'Dim wcQuery =SELECT ID,Telesales,SUBSTRING(lastupdatedate,1 ,10)as'Day',SUBSTRING(lastupdatetime FROM -9 FOR 6)as'Time'FROM ratingout where Telesales ='& cbTelesales.Text& '和lastupdatedate ='&新日期& '和lastupdatedate不为null,lastupdatetime不为null ORDER BY lastupdatetime;
'wcCommand.Connection = wcconn
'wcCommand.CommandText = wcQuery

Dim newDate As String
newDate = dateWorkCheck.Text
newDate = newDate.Replace (/, - )

Dim y,m,d As String
y = newDate.Substring(6,4)
m = newDate.Substring )
d = newDate.Substring(0,2)

newDate = y& - & m& - & d

Dim wcQuery =SELECT ID,Telesales,lastupdatedate as'Day',SUBSTRING(lastupdatetime FROM -8 FOR 2)as'Time'FROM ratingout where Telesales ='& cbTelesales.Text& '和lastupdatedate ='&新日期& '和lastupdatedate不为null,lastupdatetime不为null ORDER BY lastupdatetime;
wcCommand.Connection = wcconn
wcCommand.CommandText = wcQuery


Dim wcData As New DataTable
wcAdapter.SelectCommand = wcCommand
wcAdapter。 Fill(wcData)



Dim i = 0
如果wcData.Rows.Count = 0那么
wcAdapter.Dispose()
尝试

Dim wQuery =SELECT ID,Telesales,lastupdatedate as'Day',SUBSTRING(lastupdatetime FROM -8 FOR 2)as'Time'FROM ratingout where Telesales ='& cbTelesales.Text& '和lastupdatedate ='& dateWorkCheck.Text& '和lastupdatedate不为null,lastupdatetime不为null ORDER BY lastupdatetime;
wcCommand.Connection = wcconn
wcCommand.CommandText = wQuery


Dim wData As New DataTable
wcAdapter.SelectCommand = wcCommand
wcAdapter。 Fill(wData)

wData.Columns.Add(tt)
wData.Columns.Add(num)


wcData。 Columns.Add(tt)
wcData.Columns.Add(num)
'dgvWorkCheck.AutoSizeRowsMo​​de = DataGridViewAutoSizeRowMode.AllCells
Dim dr As DataRow
For dr wData.Rows
如果lastV是Nothing OrElse Not ColumnEqual(lastV,dr(Time))然后
检查第一个值是否为空
如果lastV = Nothing然后
lastV =00
l =0
Else

dr(tt)= lastV
dr(num)= l
'wcData.Tables(ratingout)。Rows(I)(ID)= dr(ID)
End If
ListBox1.Items.Add(lastV& < - >& l)
lastV = dr(Time)
l = 1
ElseIf lastV Is Nothing OrElse ColumnEqual(lastV,dr(Time))then
l + = 1
'Dim series1 As New Series()
'series1.Points.Add(l)
结束如果


对于I = I To wData.Rows。计数
如果I<> wData.Rows.Count then
I + = 1
如果i = wData.Rows.Count则
$ b dr b(b) num)= l

ListBox1.BeginUpdate()
ListBox1.Items.Add(dr(Telesales)&在[& lastV&和17:00之间,]& l&[records])
ListBox1.EndUpdate()
End If

GoTo n
Else
MsgBox(last data)
End If
Next
n:
Next
txtRec.Text = wData.Rows.Count

dgvWorkCheck.DataSource = wData

''chart
Dim ChartArea2 As ChartArea = New ChartArea()
Dim Legend2作为Legend = New Legend()
Dim Series2 As Series = New Series()
Dim Chart2 = New Chart()
Me.Controls.Add(Chart2)

ChartArea2.AxisX.LabelStyle.Angle = -90
ChartArea2.AxisX.LabelStyle.Interval = 1

ChartArea2.AxisY.LabelStyle.Angle = -90
ChartArea2.AxisY.LabelStyle.Interval = 5

ChartArea2.Name =ChartArea2
Chart2.ChartAreas.Add(ChartArea2)
Legend2.Name =Legend2
Chart2.Legends.Add(Legend2)
Chart2.Location = New System.Drawing.Point(12,113)
Chart2.Name =Chart2
Series2.ChartArea =ChartArea2
Series2.Legend =Legend2
Series2 .Name =Series2
Chart2.Series.Add(Series2)
Chart2.Size = New System.Drawing.Size(1145,604)
Chart2.TabIndex = 0
Chart2.Text =Chart2

Chart2.Series(Series2)。XValueMember =tt
Chart2.Series(Series2)。YValueMembers =num

Chart2.DataSource = dgvWorkCheck.DataSource

Chart2.DataBind()

Catch ex As Exception
MessageBox.Show(ex.Message,错误,MessageBoxButtons.OK,MessageBoxIcon.Error)
结束尝试
退出尝试


解决方案

为了更新我的图表 bargraph 命名为 CashChart databound to a BindingSource )我不得不这样做:


  1. 要清除图表信息,


  2. 清除边界来源信息

    - 分配绑定源信息:例如:

      CashChart.Series(0).Points.Clear()
    CashChart.DataSource =
    CashChart.DataSource = ESTADOINSTANTANEOBindingSource


之前,只有我的 DataTable 正在更新,但在这些命令后,我能够得到 bargraph 更新表中的新值。


My chart loads data from a DataGridView.

I want to automatically update my chart with new data if new values are inserted into the DataGridView.

My chart is bound to table1 and table2 in my DataGridView which gets values from a DataTable. Here is a small portion of the code:

        Dim myData As New DataTable
        wcAdapter.SelectCommand = wcCommand
        wcAdapter.Fill(myData)

-

        Chart1.DataSource = myData

        Chart1.Series("Series1").ValueMemberX = "table1"
        Chart1.Series("Series1").ValueMembersY = "table2"

Here is the complete code:

 Try
        wcconn.Open()
        Dim wcCommand As New MySqlCommand()
        ''telesales name


        ' Dim wcQuery = "SELECT ID, Telesales, SUBSTRING(lastupdatedate, 1, 10) as 'Day', SUBSTRING(lastupdatetime FROM -9 FOR 6) as 'Time' FROM ratingout where Telesales='" & cbTelesales.Text & "' and lastupdatedate= '" & newDate & "' and lastupdatedate is not null and lastupdatetime is not null ORDER BY lastupdatetime ;"
        ' wcCommand.Connection = wcconn
        ' wcCommand.CommandText = wcQuery

        Dim newDate As String
        newDate = dateWorkCheck.Text
        newDate = newDate.Replace("/", "-")

        Dim y, m, d As String
        y = newDate.Substring(6, 4)
        m = newDate.Substring(3, 2)
        d = newDate.Substring(0, 2)

        newDate = y & "-" & m & "-" & d

        Dim wcQuery = "SELECT ID, Telesales, lastupdatedate as 'Day', SUBSTRING(lastupdatetime FROM -8 FOR 2)  as 'Time' FROM ratingout where Telesales='" & cbTelesales.Text & "' and lastupdatedate= '" & newDate & "' and lastupdatedate is not null and lastupdatetime is not null ORDER BY lastupdatetime ;"
        wcCommand.Connection = wcconn
        wcCommand.CommandText = wcQuery


        Dim wcData As New DataTable
        wcAdapter.SelectCommand = wcCommand
        wcAdapter.Fill(wcData)



        Dim i = 0
        If wcData.Rows.Count = 0 Then
            wcAdapter.Dispose()
            Try

                Dim wQuery = "SELECT ID, Telesales, lastupdatedate as 'Day', SUBSTRING(lastupdatetime FROM -8 FOR 2)  as 'Time' FROM ratingout where Telesales='" & cbTelesales.Text & "' and lastupdatedate= '" & dateWorkCheck.Text & "' and lastupdatedate is not null and lastupdatetime is not null ORDER BY lastupdatetime ;"
                wcCommand.Connection = wcconn
                wcCommand.CommandText = wQuery


                Dim wData As New DataTable
                wcAdapter.SelectCommand = wcCommand
                wcAdapter.Fill(wData)

                wData.Columns.Add("tt")
                wData.Columns.Add("num")


                wcData.Columns.Add("tt")
                wcData.Columns.Add("num")
                'dgvWorkCheck.AutoSizeRowsMode = DataGridViewAutoSizeRowMode.AllCells
                Dim dr As DataRow
                For Each dr In wData.Rows
                    If lastV Is Nothing OrElse Not ColumnEqual(lastV, dr("Time")) Then
                        ''check if first value is nothing
                        If lastV = Nothing Then
                            lastV = "00"
                            l = "0"
                        Else

                            dr("tt") = lastV
                            dr("num") = l
                            'wcData.Tables("ratingout").Rows(I)("ID") = dr("ID")
                        End If
                        ListBox1.Items.Add(lastV & " <--> " & l)
                        lastV = dr("Time")
                        l = 1
                    ElseIf lastV Is Nothing OrElse ColumnEqual(lastV, dr("Time")) Then
                        l += 1
                        'Dim series1 As New Series()
                        'series1.Points.Add(l)
                    End If


                    For I = I To wData.Rows.Count
                        If I <> wData.Rows.Count Then
                            I += 1
                            If i = wData.Rows.Count Then

                                dr("tt") = lastV
                                dr("num") = l

                                ListBox1.BeginUpdate()
                                ListBox1.Items.Add(dr("Telesales") & " between[" & lastV & " and 17:00, ] <--> " & l & "[ records ]")
                                ListBox1.EndUpdate()
                            End If

                            GoTo n
                        Else
                            MsgBox("last data")
                        End If
                    Next
 n:
                Next
                txtRec.Text = wData.Rows.Count

                dgvWorkCheck.DataSource = wData

                ''chart
                Dim ChartArea2 As ChartArea = New ChartArea()
                Dim Legend2 As Legend = New Legend()
                Dim Series2 As Series = New Series()
                Dim Chart2 = New Chart()
                Me.Controls.Add(Chart2)

                ChartArea2.AxisX.LabelStyle.Angle = -90
                ChartArea2.AxisX.LabelStyle.Interval = 1

                ChartArea2.AxisY.LabelStyle.Angle = -90
                ChartArea2.AxisY.LabelStyle.Interval = 5

                ChartArea2.Name = "ChartArea2"
                Chart2.ChartAreas.Add(ChartArea2)
                Legend2.Name = "Legend2"
                Chart2.Legends.Add(Legend2)
                Chart2.Location = New System.Drawing.Point(12, 113)
                Chart2.Name = "Chart2"
                Series2.ChartArea = "ChartArea2"
                Series2.Legend = "Legend2"
                Series2.Name = "Series2"
                Chart2.Series.Add(Series2)
                Chart2.Size = New System.Drawing.Size(1145, 604)
                Chart2.TabIndex = 0
                Chart2.Text = "Chart2"

                Chart2.Series("Series2").XValueMember = "tt"
                Chart2.Series("Series2").YValueMembers = "num"

                Chart2.DataSource = dgvWorkCheck.DataSource

                Chart2.DataBind()

            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            Exit Try

解决方案

In order to update my chart bargraph named CashChart (which was databound to a BindingSource) I had to do the following:

  1. To clear the chart information,

  2. Clear the bounding source information

  3. And then re-assign the bounding source information: for example:

    CashChart.Series(0).Points.Clear()
    CashChart.DataSource = ""                       
    CashChart.DataSource = ESTADOINSTANTANEOBindingSource
    

Before, only my DataTable was updating, but after these commands, I was able to get the bargraph to update with new values in the table.

这篇关于VB.net使用新数据条目自动更新图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆