Solved - Rebuilding a dynamically generated chart every few seconds

Posted on 2014-02-15
Last Modified: 2014-02-17

I am using the following code to build a chart by plotting the points from data in a GridView
Every 10 seconds the GridViews will reload so I need to rebuild each chart. What  code do I use to clear the chart so that it may be rebuilt?

    Sub oChart(ByVal oConnectionString As String, ByVal SQL As String, ByVal RowFields As String, ByVal DataField As String, ByVal sAggregate As String, _
               ByVal ColumnFields As String, ByVal oChartType As String, ByVal oRow As Integer, ByVal oColumn As Integer, ByVal sWidth As String, _
               ByVal sHeight As String, ByVal blnShow_Data_With_Chart As Boolean, blnFlip_Axes As Boolean)

        ' Initialize the Chart object    
        Dim chart1 As New Chart()
        'chart1.ID = "chart1"
        'chart1.ID = "chart1" & CStr(Me.Table1.Controls.Count)

        'Add to do code here
        If Not IsPostBack Then
            ' Initialize objects and elements
            Dim chartArea1 As New ChartArea()
            Dim legend1 As New Legend()
            'Dim series1 As New Series()
            'Dim series2 As New Series()
            'Dim series3 As New Series()
            Dim arrSeries(1000) As Series

            ' Set the Chart Properties
            chart1.Width = CInt(sWidth)
            chart1.Height = CInt(sHeight)

            chart1.Palette = ChartColorPalette.EarthTones
            chart1.Palette = ChartColorPalette.Excel
            chart1.BackColor = System.Drawing.Color.Gray
            chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss

            chart1.BorderlineColor = System.Drawing.Color.Gray
            chart1.BorderlineDashStyle = ChartDashStyle.Solid
            chart1.BorderlineWidth = 4 'MDB

            ' Set the ChartArea properties
            chartArea1.Name = "Default"
            chartArea1.BackColor = System.Drawing.Color.Gray

            'Enable AJAX features such as zooming and scrolling for the ChartArea
            'chartArea1.CursorX.UserEnabled = True
            'chartArea1.CursorY.UserEnabled = True

            ' Add the ChartArea to the Chart

            ' Set the Legend properties
            legend1.Name = "Default"
            legend1.Docking = Docking.Bottom
            legend1.LegendStyle = LegendStyle.Row
            legend1.Alignment = System.Drawing.StringAlignment.Center

            legend1.BackColor = System.Drawing.Color.Transparent
            legend1.BorderColor = System.Drawing.Color.Black
            legend1.BorderWidth = 1

            ' Add the Legend to the Chart

            ' Set the Series properties
            'series1.BorderColor = System.Drawing.Color.DarkSlateGray
            'series1.Name = "Series1"
            'series1.ChartType = SeriesChartType.SplineArea

            'series2.BorderColor = System.Drawing.Color.DarkSlateGray
            'series2.Name = "Series2"
            'series2.ChartType = SeriesChartType.Column

            'series3.Name = "Series3"
            'series3.BorderWidth = 3
            'series3.ChartType = SeriesChartType.Spline

            ' Add the Series to the Chart

            ' Add points to each series.
            Dim rnd As New Random()

            'For Each ser As Series In chart1.Series
            'For i As Integer = 0 To 12
            'ser.Points.AddY(rnd.[Next](5, 40))

            'chart1.DataSource = dt

            'GridView1.DataSource = dt
            Dim oGridView As New GridView
            'oGridView.DataSource = dt
            'First get the Aggregate function
            Dim oAggregate As AggregateFunction
            If sAggregate = "Count" Then
                oAggregate = AggregateFunction.Count
            ElseIf sAggregate = "Sum" Then
                oAggregate = AggregateFunction.Sum
            ElseIf sAggregate = "First" Then
                oAggregate = AggregateFunction.First
            ElseIf sAggregate = "Last" Then
                oAggregate = AggregateFunction.Last
            ElseIf sAggregate = "Average" Then
                oAggregate = AggregateFunction.Average
            ElseIf sAggregate = "Max" Then
                oAggregate = AggregateFunction.Max
            ElseIf sAggregate = "Min" Then
                oAggregate = AggregateFunction.Min
            ElseIf sAggregate = "Exists" Then
                oAggregate = AggregateFunction.Exists
            End If

            Dim dt As DataTable = SQL_Data_Table.GetSQLDataTable(oConnectionString, SQL)
            Dim pvt As New Pivot(dt)
            oGridView.DataSource = pvt.PivotData(RowFields, DataField, oAggregate, ColumnFields)
            If blnShow_Data_With_Chart = True Then
                oGridView.Visible = True
                oGridView.Visible = False
            End If

            'First add declare Update Panel then add GridView to it - finally add the update panel to the table cell
            Dim T As Table
            'T = Me.Controls("Table" & oRow)
            If oRow = 0 Then
                T = Me.Table1
            ElseIf oRow = 1 Then
                T = Me.Table2
            ElseIf oRow = 2 Then
                T = Me.Table3
            ElseIf oRow = 3 Then
                T = Me.Table4
            ElseIf oRow = 4 Then
                T = Me.Table5
            ElseIf oRow = 5 Then
                T = Me.Table6
            ElseIf oRow = 6 Then
                T = Me.Table7
            ElseIf oRow = 7 Then
                T = Me.Table8
            ElseIf oRow = 8 Then
                T = Me.Table9
            ElseIf oRow = 9 Then
                T = Me.Table10
            End If
            Dim up As New UpdatePanel
            'up.ID = "up" & CStr(Me.Table1.Controls.Count + Me.Table2.Controls.Count + _
            'Me.Table3.Controls.Count + Me.Table4.Controls.Count + Me.Table5.Controls.Count + _
            'Me.Table6.Controls.Count + Me.Table7.Controls.Count + Me.Table8.Controls.Count + _
            'Me.Table9.Controls.Count + Me.Table10.Controls.Count)

            Dim oCount As Integer = 0
            Dim oData As Double
            Dim strRowHeader As String
            Dim strColumnHeader As String
            'For Each ser As Series In chart1.Series
            'ser.Label = ser.Name

            chart1.BackColor = Drawing.Color.Navy
            chart1.BackGradientStyle = GradientStyle.LeftRight
            chartArea1.BackColor = Drawing.Color.DarkGreen
            chartArea1.BackGradientStyle = GradientStyle.LeftRight

            For r As Integer = 0 To oGridView.Rows.Count - 1

                oCount = 0
                strRowHeader = oGridView.Rows(r).Cells(0).Text
                arrSeries(r) = New Series
                arrSeries(r).BorderColor = System.Drawing.Color.MidnightBlue
                arrSeries(r).BorderWidth = 4 'Changes width of ine in line chart
                'arrSeries(r).BackImageTransparentColor = Drawing.Color.Aqua
                arrSeries(r).Name = strRowHeader

                If oChartType = "Chart_Line" Then
                    arrSeries(r).ChartType = SeriesChartType.Line
                    chart1.BackColor = Drawing.Color.Navy
                    chartArea1.BackColor = Drawing.Color.DarkGreen
                ElseIf (oChartType = "Chart_Spline") Then
                    arrSeries(r).ChartType = SeriesChartType.Spline
                    chart1.BackColor = Drawing.Color.Black
                    chartArea1.BackColor = Drawing.Color.MintCream
                ElseIf (oChartType = "Chart_Bar") Then
                    arrSeries(r).ChartType = SeriesChartType.Bar
                    chart1.BackColor = Drawing.Color.Navy
                    chartArea1.BackColor = Drawing.Color.DarkRed
                ElseIf (oChartType = "Chart_StackedBar") Then
                    arrSeries(r).ChartType = SeriesChartType.StackedBar
                    chartArea1.BackColor = Drawing.Color.Orange
                    chart1.BackColor = Drawing.Color.DarkRed
                ElseIf (oChartType = "Chart_Column") Then
                    arrSeries(r).ChartType = SeriesChartType.Column
                    chart1.BackColor = Drawing.Color.Orange
                    chartArea1.BackColor = Drawing.Color.OrangeRed
                ElseIf (oChartType = "Chart_StackedColumn") Then
                    arrSeries(r).ChartType = SeriesChartType.StackedColumn
                    chart1.BackColor = Drawing.Color.DarkSlateGray
                    chartArea1.BackColor = Drawing.Color.MintCream      
                ElseIf (oChartType = "Chart_Pie") Then
                    arrSeries(r).ChartType = SeriesChartType.Pie
                    chart1.BackColor = Drawing.Color.Navy
                    chartArea1.BackColor = Drawing.Color.DarkSlateGray
                ElseIf (oChartType = "Chart_Doughnut") Then
                    arrSeries(r).ChartType = SeriesChartType.Doughnut
                    chart1.BackColor = Drawing.Color.DarkRed
                    chartArea1.BackColor = Drawing.Color.Chocolate
                ElseIf (oChartType = "Chart_Doughnut") Then
                    arrSeries(r).ChartType = SeriesChartType.Funnel
                    chart1.BackColor = Drawing.Color.Black
                    chartArea1.BackColor = Drawing.Color.Silver
                ElseIf (oChartType = "Chart_Area") Then
                    arrSeries(r).ChartType = SeriesChartType.Area
                    chart1.BackColor = Drawing.Color.DarkGreen
                    chartArea1.BackColor = Drawing.Color.DarkTurquoise
                ElseIf (oChartType = "Chart_Funnel") Then
                    arrSeries(r).ChartType = SeriesChartType.Funnel
                    chart1.BackColor = Drawing.Color.Black
                    chartArea1.BackColor = Drawing.Color.Silver
                ElseIf (oChartType = "Chart_Point") Then
                    arrSeries(r).ChartType = SeriesChartType.Point
                    chart1.BackColor = Drawing.Color.Navy
                    chartArea1.BackColor = Drawing.Color.White
                ElseIf (oChartType = "Chart_Polar") Then
                    arrSeries(r).ChartType = SeriesChartType.Polar
                    chart1.BackColor = Drawing.Color.DodgerBlue
                    chartArea1.BackColor = Drawing.Color.Red
                ElseIf (oChartType = "Chart_Radar") Then
                    arrSeries(r).ChartType = SeriesChartType.Radar
                    chart1.BackColor = Drawing.Color.Olive
                    chartArea1.BackColor = Drawing.Color.OliveDrab
                End If

                'arrSeries(r).ChartType = SeriesChartType.Spline

                'arrSeries(r).ChartType = SeriesChartType.Radar
                'chart1.BackColor = Drawing.Color.Olive
                'chartArea1.BackColor = Drawing.Color.OliveDrab

                'arrSeries(r).ChartType = SeriesChartType.Pyramid
                'arrSeries(r).ChartType = SeriesChartType.Doughnut
                'arrSeries(r).ChartType = SeriesChartType.Pie
                'arrSeries(r).ChartType = SeriesChartType.Bubble
                'arrSeries(r).ChartType = SeriesChartType.StackedArea100
                'arrSeries(r).ChartType = SeriesChartType.Funnel 'Goes with next one
                arrSeries(r).BackGradientStyle = GradientStyle.DiagonalLeft

                'arrSeries(r).ChartType = SeriesChartType.Spline
                'arrSeries(r).BackGradientStyle = GradientStyle.HorizontalCenter


                For c As Integer = 1 To oGridView.Rows(0).Cells.Count - 1
                    'only add a point if the item is numeric
                    If IsNumeric(oGridView.Rows(r).Cells(c).Text) Then

                        oData = CDbl(oGridView.Rows(r).Cells(c).Text)
                        'strRowHeader = oGridView.Rows(r).Cells(0).Text
                        strColumnHeader = oGridView.HeaderRow.Cells(c).Text
                        'ser.Points.Item(oCount).AxisLabel = strRowHeader & " " & strColumnHeader
                        arrSeries(r).Points.Item(oCount).AxisLabel = strColumnHeader
                        'ser.Points.Item(oCount).LabelAngle = 10
                        oCount = oCount + 1
                    End If

            'chartArea1.Area3DStyle.Rotation = 180

            chartArea1.AxisX.LabelStyle.Enabled = True
            chartArea1.AxisX.IsLabelAutoFit = True
            chartArea1.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep30

            'Only show GridView if specified

            'Preserve the chart's state during callbacks
            'chart1.CallbackStateContent = CallbackStateContent.All
        End If

        ' You MUST add the Chart to a form. By default, form1 is created on aspx pages.

    End Sub

Open in new window

Question by:murbro
LVL 83

Accepted Solution

CodeCruiser earned 500 total points
ID: 39863353
Based on my understanding of your code, all you need is to call this method again when refreshing the charts as the charts  pick up their data from grids which will be refreshed as well. You will need to recall this method on every postback anyway as the charts are dynamic.

Author Closing Comment

ID: 39864636
Great answer. Thanks a lot!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Authentication of Web Services 3 53
Error in page 3 46
Allow user to hide and show datagridview columns 4 17
SQL Exceptions 3 38
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now