这是一个简单的VB6代码段,该代码段使用MSChart控件在VB6.0中显示图表。
要使用此示例,请按照以下步骤操作
在VB6中创建一个新项目
下拉“项目”菜单,然后选择“组件”。
选中Microsoft ChartControl 6.0(OLEDB)旁边的复选框,单击“确定”。
向现有表单添加一个组合框,一个Mschart控件和一个复选框。
将以下代码粘贴到代码窗口中
Private Sub Form_Load()
'Fill the combo box with different types of chart types.
With Combo1
.AddItem "3D Bar"
.AddItem "2D Bar"
.AddItem "3D Line"
.AddItem "2D LIne"
.AddItem "3D Area"
.AddItem "2D Area"
.AddItem "3D Step"
.AddItem "2D Step"
.AddItem "3D Combination"
.AddItem "2D Combination"
End With
'Declare 2D array to store values for the chart
'Variant ----so that can store both text as well as numbers
Dim X(1 To 7, 1 To 6) As Variant
X(1, 2) = "Steel"
X(1, 3) = "Aluminium"
X(1, 4) = "Copper"
X(1, 5) = "Buxite"
X(1, 6) = "Lead"
X(2, 1) = "JAN"
X(2, 2) = 2
X(2, 3) = 3
X(2, 4) = 4
X(2, 5) = 5
X(2, 6) = 6
X(3, 1) = "FEB"
X(3, 2) = 4
X(3, 3) = 6
X(3, 4) = 3
X(3, 5) = 10
X(3, 6) = 18
X(4, 1) = "MAR"
X(4, 2) = 1
X(4, 3) = 3
X(4, 4) = 8
X(4, 5) = 7
X(4, 6) = 9
X(5, 1) = "APR"
X(5, 2) = 4
X(5, 3) = 6
X(5, 4) = 13
X(5, 5) = 10
X(5, 6) = 12
X(6, 1) = "MAY"
X(6, 2) = 2
X(6, 3) = 9
X(6, 4) = 9
X(6, 5) = 12
X(6, 6) = 7
X(7, 1) = "JUN"
X(7, 2) = 13
X(7, 3) = 20
X(7, 4) = 5
X(7, 5) = 18
X(7, 6) = 11
'2D array is the data for the chart control.
MSChart1.ChartData = X
'Default chart type is se to 2D bar chart.
MSChart1.chartType = 1
End Sub
Private Sub Check1_Click()
If Check1.Value = 1 Then
'Show Legends
MSChart1.ShowLegend = True
Check1.Caption = "&Hide Legends"
Else
'Hide Legends
MSChart1.ShowLegend = False
Check1.Caption = "&Show Legends"
End If
End Sub
Private Sub Combo1_Click()
'To change the chart type at run time.
MSChart1.chartType = Combo1.ListIndex
End Sub
注意:-上面的示例代码适用于控件的默认名称,如果控件的名称不同,则需要进行相应的修改。
From: https://bytes.com/topic/visual-basic/insights/701826-drawing-chart-using-mschart-control
VB6中MSChart控件绘图实例
本文提供了一个VB6环境下使用MSChart控件展示图表的代码示例,详细介绍了如何设置不同类型的图表,如3D条形图、2D折线图等,并通过数组填充数据,实现图例显示与隐藏及运行时更改图表类型的功能。
4791

被折叠的 条评论
为什么被折叠?



