Solved

ASP.net - Rebuilding a dynamically generated chart every few seconds

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

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 VB.net  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
            '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

            '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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 83

Accepted Solution

by:
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.
0
 

Author Closing Comment

by:murbro
ID: 39864636
Great answer. Thanks a lot!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net figure out error 3 26
VB.Net  Exporting Excel Woorkbook to PDF 7 60
Copying from a network share 3 26
Stop Git from being my repository 1 26
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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.…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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