' 这段代码可以粘贴到一个Form的 Load
' 事件中,该Form包含名为“MSChart1”的
' MSChart 控件。
Dim arrPrices(1 to 10)
Dim i As Integer
For i = 1 to 10
arrPrices(i)= i * 2
Next i
MSChart1.ChartData = arrPrices
' 系列的数目是由第二个维数决定的。
' 在本例中,图表将有两个系列,
' 每个系列有五个数据点。
Dim arrPriceQuantity(1 to 5, 1 to 2)
Dim i as Integer
For i = 1 to 5
arrPriceQuantity(i, 1) = i ' Series 1
arrPriceQuantity(i, 2) = 0 - i ' Series 2
Next i
MsChart1.ChartData = arrPriceQuantity
Dim arrValues(1 to 5, 1 to 3)
Dim i as Integer
For i = 1 to 5
arrValues(i, 1) = "Label " & i ' Labels
arrValues(i, 2) = 0 + i ' Series 1 values.
arrValues(i, 3) = 2 * i ' Series 2 values.
Next i
MsChart1.ChartData = arrValues
上面的代码产生的图表如下:
正如所看见的那样,使用 ChartData 属性创建图表的方法快捷而且简便。但是,使用数组的问题是要将数据取到数组中。这类数据的大多数用户可能更想使用某种电子表格程序,例如 Microsoft Excel,或某种数据库程序,如 Microsoft Access,来存贮和检索数据。
设置或返回数据点
一旦使用来自电子表格或其它数据源的数组创建了图表,可能也希望设置或返回某个指定数据点的值。要做到这一点,可以首先设置 Row 和(如果可以的话) Column 属性,然后设置或返回 Data 属性。例如,在简单的(单系列的)图表中,下面的代码将会改变第三个数据点。
With MSChart1
' 将第三个数据点改为50。
.Row = 3
.Data = 50
End With
如果图表有不止一个系列,那么可以使用 Column 属性来指定系列,然后再象上面那样设置 Row 和 Data 属性。
With MSChart1
' 将第四个系列的第二
' 个数据点设为42。
.Column = 4
.Row = 2
.Data = 42
End With
Private Sub MSChart1_PointActivated(Series As _
Integer, DataPoint As Integer, MouseFlags As _
Integer, Cancel As Integer)
With MSChart1
.Column = Series
.Row = DataPoint
.Data = InputBox _
("Change the data point:", , .Data)
End With
End Sub