Visual Basic Concepts

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.

Private Sub cmdFormat_Click()
   ' First, change the chart type to a 3D chart to
   ' see all the parts of the plot.
   MSChart1.chartType = VtChChartType3dArea

   ' Color the backdrop light blue using
   ' the Plot object.
   With MSChart1.Plot.Backdrop
      ' No color will appear unless you set the style
      ' property to VtFillStyleBrush.
      .Fill.Style = VtFillStyleBrush
      .Fill.Brush.FillColor.Set 100, 255, 200
      ' Add a border.
      .Frame.Style = VtFrameStyleThickInner
      ' Set style to show a shadow.
      .Shadow.Style = VtShadowStyleDrop
   End With

   ' Color the wall of the plot yellow.
   With MSChart1.Plot
      ' Set the style to solid.
      .Wall.Brush.Style = VtBrushStyleSolid
      ' Set the color to yellow.
      .Wall.Brush.FillColor.Set 255, 255, 0
   End With

   With MSChart1.Plot ' Color the plot base blue.
      .PlotBase.BaseHeight = 200
      .PlotBase.Brush.Style = VtBrushStyleSolid
      .PlotBase.Brush.FillColor.Set 0, 0, 255
   End With
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:

MSChart1.Title.Text = "Year End Summary"

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

Change the Scale Using the Type Property

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:

Private Sub Form_Load()
   ' Configure a ComboBox control named cmbScale.
   With cmbScale
      .AddItem "Log"
      .AddItem "Percent"
      .AddItem "Linear"
      .ListIndex = 0
   End With
End Sub

Private Sub cmbScale_Click()
   ' The ComboBox has three items: Log, Percent,
   ' and Linear (the default scale).

   Select Case cmbScale.Text
   Case "Log"
      MSChart1.Plot.Axis(VtChAxisIdY) _
      .AxisScale.Type = VtChScaleTypeLogarithmic

      ' You must specify a LogBase to be used when
      ' switching the scale to Log. The base can be
      ' set to any value between 2 and 200.
      MSChart1.Plot.Axis(VtChAxisIdY).AxisScale _
      .LogBase = 10

   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

Change the Color of MSChart Objects Using the CommonDialog Control

You may wish to allow the user to pick the colors assigned to chart elements (such as the color of series) using the CommonDialog control. In that case, you can use the following functions that mask the bytes returned by the CommonDialog control's Color property to return individual red, green, and blue values required by the Set method.

' Paste these functions into the Declarations section
' of the Form or Code Module.
Public Function RedFromRGB(ByVal rgb As Long) _
   As Integer
   ' The ampersand after &HFF coerces the number as a
   ' long, preventing Visual Basic from evaluating the
   ' number as a negative value. The logical And is
   ' used to return bit values.
   RedFromRGB = &HFF& And rgb
End Function

Public Function GreenFromRGB(ByVal rgb As Long) _
   As Integer
   ' The result of the And operation is divided by
   ' 256, to return the value of the middle bytes.
   ' Note the use of the Integer divisor.
   GreenFromRGB = (&HFF00& And rgb) \ 256
End Function

Public Function BlueFromRGB(ByVal rgb As Long) _
   As Integer
   ' This function works like the GreenFromRGB above,
   ' except you don't need the ampersand. The
   ' number is already a long. The result divided by
   ' 65536 to obtain the highest bytes.
   BlueFromRGB = (&HFF0000 And rgb) \ 65536
End Function

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:

Private Sub MSChart1_SeriesActivated(Series As _
   Integer, MouseFlags As Integer, Cancel As Integer)
   ' The CommonDialog control is named dlgChart.
   Dim red, green, blue As Integer
   With dlgChart ' CommonDialog object
      .ShowColor
      red = RedFromRGB(.Color)
      green = GreenFromRGB(.Color)
      blue = BlueFromRGB(.Color)
   End With

   ' NOTE: Only the 2D and 3D line charts use the
   ' Pen object. All other types use the Brush.

   If MSChart1.chartType <> VtChChartType2dLine Or _
   MSChart1.chartType <> VtChChartType3dLine Then
      MSChart1.Plot.SeriesCollection(Series). _
         DataPoints(-1).Brush.FillColor. _
         Set red, green, blue
   Else
      MSChart1.Plot.SeriesCollection(Series).Pen. _
         VtColor.Set red, green, blue
   End If
End Sub