具有新数据输入的自动图表更新 [英] Automatic chart update with new data entry

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

问题描述

我的图表从DataGridView加载数据。

My chart loads data from a DataGridView.

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

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

我的图表绑定到获取值的DataGridView中的 table1 table2 从数据表。这是一小段代码:

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"

这是完整的代码:

 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


推荐答案

以更新我的图表条形图名为 CashChart (已绑定到 BindingSource )请执行以下操作:

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


  1. 要清除图表信息,

  1. To clear the chart information,

清除边界源信息

然后重新分配边界ng源信息:例如:

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

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


之前,只有我的 DataTable 正在更新,但是在执行这些命令之后,我能够将条形图获取到使用表中的新值更新。

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.

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

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