VB中如何使用mschart 控件画曲线图
表:LWL
字段是:日期、收入
现在就是想把表里的“收入”字画成曲线图
各位高手帮帮忙 展开
2024-09-04 广告
根据你的要求,我简单做了一个日期和收入的对应图,代码及调试结果如下所示
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 & "\lwl.mdb"
conn.Open
rs.Open "select * from lwl", conn, adOpenKeyset, adLockOptimistic
' Set MSChart1.DataSource = rs
With MSChart1
'// 以线条方式显示
.chartType = 3
'// 把刻录改为手工方式
.Plot.Axis(VtChAxisIdY).ValueScale.Auto = False
'// 设置最大值
.Plot.Axis(VtChAxisIdY).ValueScale.Maximum = 1000
'// 设置最小值
.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
参考资料: http://hi.baidu.com/mizuda/blog/item/ab8af02870fefff499250ac8.html
(1)在窗体上布置一个MSChart控件
(2)窗体代码
Option Explicit
Private Sub Form_Load()
'设置MSChart1
With MSChart1
'图上只画一条曲线
.ColumnCount = 1
'设置图表类型为二维曲线;默认为直方图
.chartType = VtChChartType2dLine
'X轴显示10个单位
.RowCount = 10
End With
'曲线数据
Dim v(1 To 10) As Single
Dim i As Integer
'曲线数据随机产生
For i = 1 To 10
v(i) = Rnd * 99 + 1
Next
' 显示曲线
With MSChart1
For i = 1 To 10
' 指定X轴的坐标
.Row = i
' X轴标签
.RowLabel = i
' 与x对应的值
.data = v(i)
Next
End With
End Sub
(3)运行结果
(4)其他注意事项
在设计状态,鼠标右键单击MSChart控件,在弹出菜单中单击“属性”,可以调出控件的属性页窗口。利用这个窗口,可以直观地设计MSChart