最后我们再来看一下几个用来设置图表外观的函数。
SetChartOptions函数
该函数用来设置图表类型、大小。
Public Sub SetChartOptions(ByVal iXlChartType As xlChartType, _ ByVal iPlotAreaWidth As Integer, ByVal iPlotAreaHeight As Integer)With oExcelChart .ChartType = iXlChartType If (iPlotAreaHeight> 50) Then .PlotArea.Height = iPlotAreaHeight If (iPlotAreaWidth> 50) Then .PlotArea.Width = iPlotAreaWidthEnd WithEnd Sub
该函数的目的是让用户设置一些重要的图表属性。另外,该函数也为开发者示范了如何在必要的时候显露图表属性使得用户可以访问它。本函数允许用户设置图表类型、图形区的高度和宽度,这些属性的默认值已经由SetChartBaseProps() 和SetChartWithDataProps()这两个函数设置过了。
SetChartTitles函数
该函数设置图表标题、X-轴和Y-轴标题。
Sub SetChartTitles(strMainTitle, strXAxisTitle, strYAxisTitle)On Error Resume NextWith oExcelChart '--- 检查并设置图表标题 If (Not IsNull(strMainTitle) And Trim(strMainTitle) <> "") Then.HasTitle = True.ChartTitle.Caption = strMainTitle.ChartTitle.Font.Name = "Verdana".ChartTitle.Font.FontStyle = "Bold".ChartTitle.Font.Size = 14 End If'--- 检查并设置X-轴标题 If (Not IsNull(strXAxisTitle) And Trim(strXAxisTitle) <> "") Then.Axes(xlCategory).HasTitle = True.Axes(xlCategory).AxisTitle.Characters.Text = strXAxisTitle.Axes(xlCategory).AxisTitle.Font.Name = "Verdana".Axes(xlCategory).AxisTitle.Font.FontStyle = "Bold".Axes(xlCategory).AxisTitle.Font.Size = 12 End If'--- 检查并设置Y-轴标题 If (Not IsNull(strYAxisTitle) And Trim(strYAxisTitle) <> "") Then.Axes(xlValue).HasTitle = True.Axes(xlValue).AxisTitle.Characters.Text = strYAxisTitle.Axes(xlValue).AxisTitle.Font.Name = "Verdana".Axes(xlValue).AxisTitle.Font.FontStyle = "Bold".Axes(xlValue).AxisTitle.Font.Size = 12'--- 设置Y-轴标题的方向.Axes(xlValue).AxisTitle.HorizontalAlignment = xlCenter.Axes(xlValue).AxisTitle.VerticalAlignment = xlCenter.Axes(xlValue).AxisTitle.Orientation = xlVertical End IfEnd WithEnd Sub
我们不再具体介绍该函数和后面几个函数的每行代码。该函数的目的是设置图表的各种标题:通过ChartTitile对象设置主标题,通过Axes对象设置X轴和Y轴的标题。窍门在于要设置好Y-轴标题的方向,这通过设置Aces对象内AxisTitle对象的Orientation属性实现。