|
|
Title | Graph data in a database using MSChart |
Keywords | MSChart, graph, database, chart |
Categories | Graphics, Controls, Database |
|
|
When the program starts, it calls subroutine LoadData to load the data in a text file. LoadData opens the database and sees how many entries it contains. It makes its data array big enough to hold the data and then reads the data.
|
|
Private Values() As Single
Private NumPoints As Integer
Private Sub LoadData()
Dim db As Database
Dim qdef As QueryDef
Dim rs As Recordset
Dim dbname As String
Dim i As Integer
' Open the database.
dbname = App.Path
If Right$(dbname, 1) <> "\" Then dbname = dbname & "\"
dbname = dbname & "data.mdb"
Set db = OpenDatabase(dbname)
' Get the records.
Set qdef = db.CreateQueryDef("", _
"SELECT Year, Value FROM YearlyValues")
Set rs = qdef.OpenRecordset(dbOpenSnapshot)
' See how many records there are.
rs.MoveLast
NumPoints = rs.RecordCount
ReDim Values(1 To NumPoints, 1 To 2)
' Load the data.
rs.MoveFirst
For i = 1 To NumPoints
Values(i, 1) = Format$(rs!Year, "yyyy")
Values(i, 2) = rs!Value
rs.MoveNext
Next i
rs.Close
db.Close
End Sub
|
|
After loading the data, the program uses the following code to send the data to the MSChart control.
|
|
' Send the data to the chart.
Chart1.chartType = VtChChartType2dXY
Chart1.RowCount = NumPoints
Chart1.ColumnCount = 2
Chart1.ChartData = Values
|
|
|
|
|
|