• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

ASP.net Timer wipes out charts


I am using the following code to bind data to GridViews and then dynamically build charts off each GridView. I have just add a timer to the page that has no code at all on it's tick
event but it bizarrely wipes the charts clean. Why would this be?

 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()

        If Me.lblCharts.Text = "Charts" Then
            Me.lblCharts.Text = chart1.ID
            Me.lblCharts.Text = Me.lblCharts.Text & chart1.ID
        End If
        '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

            If Me.lblChartGridViews.Text = "Chart GridViews" Then
                Me.lblChartGridViews.Text = oGridView.ID
                Me.lblChartGridViews.Text = Me.lblChartGridViews.Text & oGridView.ID
            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

Murray Brown
Murray Brown
1 Solution
As mentioned in your other question, dynamic controls need to be recreated on every page postback. Timer is causing a postback to execute its tick event which then removes the charts because these are not being readded. Refer to links provided in other question.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now