VB中如何从Access数据库中提取出一个月温度数据并绘制成月曲线
以下是vb从数据库中提取每日对应的温度曲线图代码。
Private Sub Command1_Click()
Dim i As Integer
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\tep.mdb"
conn.Open
rs.Open "select * from mrwdb", conn, adOpenKeyset, adLockOptimistic
' Set MSChart1.DataSource = rs
With MSChart1
'// 以线条方式显示
.chartType = 3
'// 把刻录改为手工方式
.Plot.Axis(VtChAxisIdY).ValueScale.Auto = False
'// 设置最大值
.Plot.Axis(VtChAxisIdY).ValueScale.Maximum = 50
'// 设置最小值
.Plot.Axis(VtChAxisIdY).ValueScale.Minimum = 0
'// 设置每格为 1
.Plot.Axis(VtChAxisIdY).ValueScale.MinorDivision = 1
'// 增加测试数据
.ColumnCount = 1
'//轴坐标标题
.Plot.Axis(VtChAxisIdX, 0).AxisTitle = "日期"
.Plot.Axis(VtChAxisIdY, 0).AxisTitle = "温度"
'//轴坐标标题字体大小的设置
.Plot.Axis(VtChAxisIdX, 0).AxisTitle.VtFont.Size = 15
.Plot.Axis(VtChAxisIdY, 0).AxisTitle.VtFont.Size = 25
'//设置图表标题
.Title.Text = "日期和温度对应折线图"
'// 将图表作为图例的背景。
.ShowLegend = False
'// 标记每个点的值
For i = 1 To .Plot.SeriesCollection.Count
.Plot.SeriesCollection(i).DataPoints(-1).DataPointLabel.LocationType = VtChLabelLocationTypeAbovePoint
Next
If rs.RecordCount > 0 Then
rs.MoveFirst
Else
Exit Sub
End If
For i = 0 To rs.RecordCount - 1
.RowCount = rs.RecordCount
.Row = i + 1
.RowLabel = CStr(rs("日期"))
.Data = rs("温度")
rs.MoveNext
Next
End With
End Sub
最终调试效果图如下: