Solved

ASP.net Timer wipes out charts

Posted on 2014-02-15
3
252 Views
Last Modified: 2014-02-17
Hi

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
        Else
            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
            'chartArea1

            ' Add the ChartArea to the Chart
            chart1.ChartAreas.Add(chartArea1)

            ' 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
            chart1.Legends.Add(legend1)

            ' 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
            'chart1.Series.Add(series1)
            'chart1.Series.Add(series2)
            'chart1.Series.Add(series3)

            ' 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))
            'Next
            'Next


            'chart1.DataSource = dt
            'chart1.DataBind()

            'GridView1.DataSource = dt
            'GridView1.DataBind()
            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)
            oGridView.DataBind()
            If blnShow_Data_With_Chart = True Then
                oGridView.Visible = True
            Else
                oGridView.Visible = False
            End If


            If Me.lblChartGridViews.Text = "Chart GridViews" Then
                Me.lblChartGridViews.Text = oGridView.ID
            Else
                Me.lblChartGridViews.Text = Me.lblChartGridViews.Text & oGridView.ID
            End If
            'SO I AM UPDATING JUST THE GRIDVIEW - MIGHT HAVE TO DO THE SAME WITH THE CHART
            '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)
            up.ContentTemplateContainer.Controls.Add(oGridView)
            T.Rows(0).Cells(oColumn).Controls.Add(up)
            T.Rows(0).Cells(oColumn).Controls.Add(oGridView)


            Dim oCount As Integer = 0
            Dim oData As Double
            Dim strRowHeader As String
            Dim strColumnHeader As String
            'SO LOOP THROUGH ALL THE NUMERIC CELLS AND PLOT ON A CHART
            '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

                chart1.Series.Add(arrSeries(r))

                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.Add(oData)
                        arrSeries(r).Points.AddY(oData)
                        '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
                Next
            Next
            'Next

            'chartArea1.Area3DStyle.Rotation = 180


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




            T.Rows(0).Cells(oColumn).Controls.Add(chart1)
            '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.
        'Me.form1.Controls.Add(chart1)



    End Sub

Open in new window

0
Comment
Question by:murbro
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39863346
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.
0
 

Author Closing Comment

by:murbro
ID: 39864606
Thanks very much
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

18 Experts available now in Live!

Get 1:1 Help Now