Create A Chart Using Recordset Data

 

 

Imports System.Windows.Forms.DataVisualization.Charting

    Private Function DisplayReport(ByVal strTitle As String, ByVal intMonths As Integer) As Boolean
        On Error GoTo Err_Fail
        DisplayReport = False
        'Create Connection To Report Data
        dbprovider = "PROVIDER=Microsoft.jet.OLEDB.4.0;"
        dbsource = "Data Source =" & System.AppDomain.CurrentDomain.BaseDirectory() & "/Training2020.mdb"
        con.ConnectionString = dbprovider & dbsource
        con.Open()
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter
        Dim sql As String
        sql = "SELECT * from tblReportData order by ID DESC"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "rptData")
        'Create Chart Object
        Dim BarChart As New Chart
        Dim intRptValue As Integer
        'Create Chart Series
        BarChart.Series.Add(New Series("Property1") With {.ChartType = SeriesChartType.Line, .borderWidth = 3, .color = Color.Green})
        'Set Minimum, Maximums & Intervals
        BarChart.ChartAreas.Add(New ChartArea)
        BarChart.ChartAreas(0).AxisY.Minimum = 0
        BarChart.ChartAreas(0).AxisY.Interval = 100
        'BarChart.ChartAreas(0).AxisY.Maximum = 1400
        BarChart.ChartAreas(0).AxisX.Minimum = 1
        BarChart.ChartAreas(0).AxisX.Interval = 1
        BarChart.ChartAreas(0).AxisX.Maximum = intMonths
        'Invert Y Axis
        BarChart.ChartAreas(0).AxisY.IsReversed = False
        'Set Chart Size
        BarChart.Width = 720
        BarChart.Height = 550

        'Loop Through Health Data
        Dim MaxRecCount As Integer
        Dim inc As Integer
        inc = -1
        MaxRecCount = ds.Tables("rptData").Rows.Count
        Dim intHighestValue As Integer = 0

        For inc = 0 To MaxRecCount - 1
            'Load Data Into Chart
            intRptValue = ds.Tables("rptData").Rows(inc).Item("TotalValue")
            'Get Highest Value
            If intRptValue > intHighestValue Then intHighestValue = intRptValue
            BarChart.Series("Property1").Points.Add(intRptValue)
        Next inc
        con.Close()
        'Set Chart Series To Suit Highest Data Value
        intHighestValue = Math.Ceiling(intHighestValue / 100) * 100
        BarChart.ChartAreas(0).AxisY.Maximum = intHighestValue
        'Final Report Details & Display
        BarChart.Titles.Add(strTitle)
        BarChart.Location = New Point(5, 20)
        Me.gbReports.Controls.Add(BarChart)
        DisplayReport = True
        Exit Function
Err_Fail:
        DisplayReport = False
        MsgBox(ErrorToString)
    End Function

 

 


 

Clear Previous Charts

        'Clear Previous Reports (If Any...)

        Dim ctl As Control
        For Each ctl In Me.gbReports.Controls
            If TypeOf ctl Is Chart Then
                ctl.Dispose()
                Exit For
            End If
        Next

 

Pie Charts

 

SpaceFooter
Website_Design_NetObjects_FusionWeb
 Counter

www.CarlsPlace.co.uk

This Is A United Kingdom Site...