处理图表外观

MSChart控件有许多可视的部件,它们均可以编程。要掌握这写是如何实现的,可以考察下图,它说明了图表的各部分。

其中的每一部分在 MSChart 控件中都有一个相应的对象,通过这个对象可以改变图表中几乎任一元素的格式。例如,下面代码通过改变 Plot 对象的颜色显著地改变了图表的外观。

Private Sub cmdFormat_Click()
   ' 首先,将图表类型改为 3D 图表
   ' 这样可以看到图的所有部分。
   MSChart1.chartType = VtChChartType3dArea

   '  Plot 对象将 
   ' 背景变为浅蓝色。
   With MSChart1.Plot.Backdrop 
      ' 除非将样式属性正确地设置为VtFillStyleBrush 
      ' 否则不会有颜色显示。
      .Fill.Style = VtFillStyleBrush
      .Fill.Brush.FillColor.Set 100, 255, 200
      ' 添加边框。
      .Frame.Style = VtFrameStyleThickInner
      ' 将样式设置为显示阴影。 
      .Shadow.Style = VtShadowStyleDrop    
   End With

   '将绘图墙的颜色设置为黄色。
   With MSChart1.Plot 
      ' 将样式设置为实心。
      .Wall.Brush.Style = VtBrushStyleSolid 
      ' 将颜色设置为黄色。
      .Wall.Brush.FillColor.Set 255, 255, 0
   End With
   
   With MSChart1.Plot '将绘图底色设置为蓝色。
      .PlotBase.BaseHeight = 200
      .PlotBase.Brush.Style = VtBrushStyleSolid
      .PlotBase.Brush.FillColor.Set 0, 0, 255
   End With
End Sub

格式化字体

一种与字体有关的基本情况是设置标题的文本。要做到这一点,需要正确使用 Title 对象的 Text 属性:

MSChart1.Title.Text = "Year End Summary"

这很简单。下一个问题是怎样改变字体的属性。

为了格式化图表上所有文本的属性,必须使用 VtFont 对象。例如,要格式化标题,可以使用如下代码:

With MSChart1.Title.VtFont
   .Name = "Algerian"
   .Style = VtFontStyleBold
   .Effect = VtFontEffectUnderline
   .Size = 14
   .VtColor.Set 255, 0, 255
End With

对图表的其它部分也可以使用同样的技术。唯一的不同在于对象模型。例如,要格式化 Legend 区域的文本属性,可使用如下代码:

MSChart1.Legend.VtFont.Size = 18

用 Type 属性改变比例

要改变图的比例,必须通过修改图表的 y 轴来实现(改变 x 轴看不到什么影响)。一种改变比例的方便途径是使用 ComboBox 控件,如下面代码所示:

Private Sub Form_Load()
   ' 设定一个名为 cmbScale 的组合框。
   With cmbScale
      .AddItem "Log"
      .AddItem "Percent"
      .AddItem "Linear"
      .ListIndex = 0
   End With
End Sub

Private Sub cmbScale_Click()
   ' 组合框有三个项目:LogPercent'  Linear (缺省比例)Select Case cmbScale.Text
   Case "Log"
      MSChart1.Plot.Axis(VtChAxisIdY) _
      .AxisScale.Type = VtChScaleTypeLogarithmic

      '  scale 转换到 log 时必须特别 
      ' 指定一个 LogBase。基数可以设为 
      ' 2  200 间的任意值。
      MSChart1.Plot.Axis(VtChAxisIdY).AxisScale _
      .LogBase = 10    

   Case "Percent"
      MSChart1.Plot.Axis(VtChAxisIdY).AxisScale _
      .Type = VtChScaleTypePercent
      ' 设置 PercentBasis 为六种类型中的一种。
      ' 为了方便,只列出一种。
      MSChart1.Plot.Axis(VtChAxisIdY).AxisScale _
      .PercentBasis = VtChPercentAxisBasisMaxChart

   Case "Linear"
      MSChart1.Plot.Axis(VtChAxisIdY).AxisScale _
      .Type = VtChScaleTypeLinear
   End Select
End Sub

用 CommonDialog 控件改变 MSChart 对象的颜色

有时可能想要允许用户使用 CommonDialog 控件选择颜色分配给图表元素(如系列的颜色)。在这种情况下,可以使用如下的函数,它们对 CommonDialog 控件的 Color 属性返回的字节进行屏蔽,进而获得 Set 方法所需的单独的红、绿、蓝的值。

' 将这些函数粘贴到 Form  Code 
' 模块的Declarations部分中。
Public Function RedFromRGB(ByVal rgb As Long) _
   As Integer
   ' &HFF 后的“&”符号将数字强制 
   '为长整型,防止 Visual Basic 
   '将其视为负值。逻辑 And 操作用 
   '来返回位值。 
   RedFromRGB = &HFF& And rgb
End Function

Public Function GreenFromRGB(ByVal rgb As Long) _
   As Integer
   ' And 操作的结果是除以 256 ' 以便返回中部字节的值。
   ' 注意整型除数的用法。
   GreenFromRGB = (&HFF00& And rgb) \ 256
End Function

Public Function BlueFromRGB(ByVal rgb As Long) _
   As Integer
   ' 此函数和上面的 GreenFromRGB 函数 
   ' 功能相似,除了不需要使用“&”符号。 
   ' 数字已经是长整型了。结果除以 65536 
   ' 以便得到最高的几位字节。
   BlueFromRGB = (&HFF0000 And rgb) \ 65536
End Function

通过使用前面例子中的函数,可以得到 CommonDialog 对象返回的长整型值,并且可以设置 MSChart 对象的颜色。下面的例子允许通过双击一个序列来改变其颜色:

Private Sub MSChart1_SeriesActivated(Series As _
   Integer, MouseFlags As Integer, Cancel As Integer)
   ' CommonDialog 控件被命名为 dlgChart
   Dim red, green, blue As Integer
   With dlgChart ' CommonDialog 对象
      .ShowColor 
      red = RedFromRGB(.Color)
      green = GreenFromRGB(.Color)
      blue = BlueFromRGB(.Color)
   End With

   ' 注意:只有 2D  3D 线图使用 Pen 对象。
   ' 所有其它类型均使用 BrushIf 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