在VB mschart里面可以一个MSCHART同时显示曲线和状图吗?

2025-03-01 08:59:33
推荐回答(2个)
回答1:

代码及调试结果如下所示
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

回答2:

Private Sub Form_Load()
Randomize Timer
Dim i As Integer, jls As Integer
Dim Values(1 To 15, 1 To 3)

For i = 1 To 15
Values(i, 1) = "T" & Format(i, "00")
Next i

For i = 1 To 15
Values(i, 2) = Rnd * 100
Values(i, 3) = Rnd * 50
Next i

MSChart1.chartType = VtChChartType2dCombination
MSChart1.Plot.SeriesCollection.Item(1).SeriesType = VtChSeriesType2dLine
MSChart1.Plot.SeriesCollection.Item(2).SeriesType = VtChSeriesType2dBar
MSChart1.ChartData = Values

End Sub