Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
When you create a multi-dimensioned array, the first series can be assigned a string; when the
array is assigned to the ChartData property, the strings become the labels for the rows. The
following code shows this feature.
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
As you can see, creating a chart using the ChartData property can be quick and simple. However,
the problem with using an array is getting the data into the array. Most users of this kind of data
will probably prefer to use a spreadsheet program, such as Microsoft Excel, or perhaps a database
program, such as Microsoft Access, to store and retrieve the data.
If you've started to explore the MSChart control, you will notice that it has a large number of
events. These events allow you to program the chart to respond to practically any action of the
user. As an example of this programmability, the PointActivated event is used in the following
example to show how a data point can be changed using the Series and DataPoint parameters. (The
PointActivated event occurs whenever a data point is double-clicked.) The Series and DataPoint
parameters correspond to the Column and Row properties, and can thus be used to set the Data
property:
1
Manipulating the Chart Appearance
The MSChart control has many visible parts, all of which can be programmed. To get a grasp on
how this is accomplished, it's useful to examine the following figure pointing out the parts of a
chart.
Each of these parts has a corresponding object in the MSChart control which can be used to change
the format of practically any element of the chart. For example, the code below dramatically
changes the look of a chart by changing the colors of the Plot object.
2
End Sub
Formatting Fonts
A very basic task with fonts might be to set the text of the chart's title. In order to do this, use the
Title object's Text property:
This is simple enough. The next question is how to change the font's attributes.
In order to format any text attribute on the chart, you must use the VtFont object. For example, to
format the title, the following code will work:
With MSChart1.Title.VtFont
.Name = "Algerian"
.Style = VtFontStyleBold
.Effect = VtFontEffectUnderline
.Size = 14
.VtColor.Set 255, 0, 255
End With
You can use the same technique with other parts of chart. The only difference lies in the object
model. For example, to format the text attributes of the Legend area use the following code:
MSChart1.Legend.VtFont.Size = 18
To change the scale of the plot, you must specify that the y axis of the chart is going to be changed
(changing the x axis has no visible effect). A convenient way to change the scale is to use a
ComboBox control, as shown in the following code:
3
Case "Percent"
MSChart1.Plot.Axis(VtChAxisIdY).AxisScale _
.Type = VtChScaleTypePercent
' Set the PercentBasis to one of six types. For
' the sake of expediency, only one is shown.
MSChart1.Plot.Axis(VtChAxisIdY).AxisScale _
.PercentBasis = VtChPercentAxisBasisMaxChart
Case "Linear"
MSChart1.Plot.Axis(VtChAxisIdY).AxisScale _
.Type = VtChScaleTypeLinear
End Select
End Sub
Using the functions in the preceding examples, you can take the Long value returned by the
CommonDialog object, and set the color of MSChart objects. The example below allows the user
to change the color of a Series by double-clicking it:
Before Rotation
5
After Rotation
You can also rotate a 3D chart by using the Set method of the View3D object.
MSChart1.Plot.View3D.Set 10,100
By default, the chart comes with one LightSource. The following code will set the four parameters
that affect the LightSource. These will be explained later. In the meantime, paste the code into a
standard EXE project, and run it.
6
Private Sub Form_Load()
With MSChart1
.chartType = VtChChartType3dArea
.Plot.Light.LightSources(1).Set 10, 10, 10, 1
End With
End Sub
This code results in a chart that appears to have a light source shining on it from the lower left of
the screen. As the chart is rotated, surfaces which face the light directly get brighter.
Light Basics
The LightSource feature is comprised of two parts:
Ambient light: As its name suggests, this is light that has no specific source and thus no
directionality.
LightSources: a directional light that can be shone on the chart from any angle, with
variable intensity. More than one light source can be created.
Ambient Light