Tải bản đầy đủ - 0 (trang)
Table 21-2. Members of the Axis Object

Table 21-2. Members of the Axis Object

Tải bản đầy đủ - 0trang

Border

Returns a Border object that can be used to set the color, line style, and width of the

border of the axis title.

Caption

Sets the text for the title.

Characters

Returns a Characters object, which is a range of characters in the caption. This allows us

to format a portion of the text in the caption.

Fill

Returns a ChartFillFormat object used to set fill-formatting properties for the axis title.

Font

Returns a Font object that can be used to set the font characteristics of the labels.

HorizontalAlignment and VerticalAlignment

Sets the alignment of axis title text.

Interior

Returns an Interior object that can be used to format the interior of the axis title area.

NumberFormat

Sets the number format code for the labels. This property returns Null if all labels do not

have the same number format. Note that since the format codes are the same as those used

by Excel to format worksheet cells, we can use the macro recorder to get appropriate

format codes.

Orientation

Returns or sets the orientation for the axis title. It can be any one of the following

constants:

Enum XlOrientation

xlUpward = -4171

xlDownward = -4170

xlVertical = -4166

xlHorizontal = -4128

End Enum



Let us take a closer look at the Characters object. A Characters object represents a contiguous

portion of text within a text string. The Characters property returns a Characters object. (Note that

the Characters property also applies to the ChartTitle object and the Range object.)

The syntax of the Characters property is:



364



AxisTitleObject.Characters(start, length)

where start is the start character number and length is the number of characters to return in

the Characters object. When start is missing, it is assumed to be equal to 1, and when length is

missing, all characters after the starting character are included.

To illustrate, the following code creates a title for the primary value axis and italicizes the word

"billions." (Note the setting of the HasTitle property, to avoid an error message.)

With ActiveChart.Axes(xlValue, xlPrimary)

.HasTitle = True

.AxisTitle.Text = "These are billions"

.AxisTitle.Characters(11, 8).Font.Italic = True

End With



Of course, if we wanted to italicize the entire title, we could simply use the Font property of the

AxisTitle object, as in:

ActiveChart.Axes(xlValue, xlPrimary). _

AxisTitle.Font.Italic = True



21.6.2.2 The Border property and the Border object

This property returns a Border object that represents the border of the object. The Border object

can be used to set the color, line style, and weight of the border of an object, such as an axis title.

The Border object has no methods. Its main properties are Color, ColorIndex, LineStyle, and

Weight.

The Color property can be set to any RGB value. For instance, the following code sets the major

axis color to blue:

ActiveChart.Axes(xlCategory, xlPrimary). _

MajorGridlines.Border.Color = RGB(0, 0, 255)



The ColorIndex property can be used to set the color via a color palette. For more information,

including the ColorIndex values, see the help documentation.

The LineStyle property can take on any of the following values:

Enum XlLineStyle

xlLineStyleNone = -4142

xlDouble = -4119

xlDot = -4118

xlDash = -4115

xlContinuous = 1

xlDashDot = 4

xlDashDotDot = 5

xlSlantDashDot = 13

End Enum



The Weight property can be set to one of the following XLBorderWeight constants:

Enum XlBorderWeight

xlMedium = -4138

xlHairline = 1



365



xlThin = 2

xlThick = 4

End Enum



To further illustrate axis-title formatting, here is the relevant code from the CreateChart procedure:

' Axis formatting

With co.Chart.Axes(xlCategory)

.HasTitle = True

.AxisTitle.Caption = "Types"

.AxisTitle.Border.Weight = xlMedium

End With

With co.Chart.Axes(xlValue)

.HasTitle = True

With .AxisTitle

.Caption = "Quantity for 1999"

.Font.Size = 6

.Orientation = xlHorizontal

.Characters(14, 4).Font.Italic = True

.Border.Weight = xlMedium

End With

End With



21.6.3 CategoryNames Property

This property returns or sets the category names for the axis. It can be set to either an array or a

Range object that contains the category names.

For instance, the code:

ActiveChart.Axes(xlCategory, xlSecondary). _

CategoryNames = Array("One", "Two", "Three", "Four", "Five", "Six")



changes the labels on the upper horizontal axis to "One", "Two", "Three", etc.



21.6.4 CategoryType Property and BaseUnit Property

The CategoryType property returns or sets the type for a category axis. (It applies only to

category-type axes.) It can be one of the following XlCategoryType constants:

Enum XlCategoryType

xlAutomaticScale = -4105

xlCategoryScale = 2

xlTimeScale = 3

End Enum



The default is xlAutomaticScale.

When the CategoryType property for the axis is set to xlTimeScale, the BaseUnit property

returns or sets the base unit for the specified category axis. It can be one of the following

XlTimeUnit constants:

Enum XlTimeUnit

xlDays = 0

xlMonths = 1



366



xlYears = 2

End Enum



Note that the value of this property takes effect only when the CategoryType property for the axis

is set to xlTimeScale, even though the value can be changed at any time.



21.6.5 Crosses and CrossesAt Properties

The Crosses property returns or sets the point on the specified axis at which the "other" axis

crosses—that is (we surmise), the other axis that is in the same axis group. Thus, if the specified

axis is a primary axis, so is the other axis. If the specified axis is a secondary axis, so is the other

axis. For instance, the code:

ActiveChart.Axes(xlValue, xlPrimary).Crosses = xlAxisCrossesMaximum



instructs the primary category axis (the other axis) to cross the primary value axis (the specified

axis) at the maximum point of the primary value axis (the specified axis).

The Crosses property can assume one of the following XlAxisCrosses constants:



AM

FL

Y



Enum XlAxisCrosses

xlAxisCrossesCustom = -4114

xlAxisCrossesAutomatic = -4105

xlAxisCrossesMaximum = 2

xlAxisCrossesMinimum = 4

End Enum



TE



When set to xlAxisCrossesAutomatic, Excel determines the crossing point. When set to

xlMinimum , the other axis crosses at the minimum value when the specified axis is a value axis

or the far-left category when the specified axis is a category axis. When set to xlMaximum , the

other axis crosses at the maximum value when the specified axis is a value axis or the far-right

category when the specified axis is a category axis. (Unfortunately, this property does not always

seem to act as advertised. In particular, I have noticed that setting this property to xlMinimum

does not always adjust the crossing point properly.)

Finally, the xlAxisCrossesCustom setting applies only to value axes, in which case when set

to xlAxisCrossesCustom , the CrossesAt property determines the crossing point for the other

(category) axis. Note that setting the CrossesAt property automatically sets the Crosses property to

xlAxisCrossesCustom .

For instance, referring to the CreateChart procedure, the code:

co.Chart.Axes(xlValue).CrossesAt = 50



causes the category axis to cross the value axis at 50.

Finally, note that these properties do not apply to 3-D charts.



21.6.6 Display Units

New to Excel 9 are the display unit-related properties HasDisplayUnitLabel, DisplayUnitLabel,

DisplayUnit, and DisplayUnitCustom, and the DisplayUnitLabel object. These properties can be

used to display a unit's legend for a chart axis. This is useful when dealing with labels that contain

very large numbers. For instance, if a series of labels has the form 1000000, 2000000, 3000000,



367 ®

Team-Fly



and so on, we can create a legend with the word "millions" and then change the labels to 1, 2,

3, . . . .

As an example, the code:

Sub DisplayUnitLabel()

With ActiveChart.Axes(xlValue)

.DisplayUnit = xlMillions

.HasDisplayUnitLabel = True

With .DisplayUnitLabel

.Caption = "millions"

.Font.Size = 14

End With

End With

End Sub



produces the chart in Figure 21-11. Note that the value axis is labeled with millions as the legend

indicates.

Figure 21-11. Display unit labels



21.6.7 Gridline-Related Properties and the Gridline Object

The Axis object has several properties that relate to gridlines; they are described in the following

list:

HasMajorGridlines

Set this read-write property to True to show major gridlines for the axis, or False to

hide gridlines. Applies only to primary axes (not secondary axes).

HasMinorGridlines

Set this read-write property to True to show minor gridlines for the axis, or False to

hide gridlines. Applies only to primary axes (not secondary axes).

MajorGridlines



368



This read-only property returns a Gridlines object that represents the major gridlines for

the specified axis. Applies only to primary axes (not secondary axes).

MinorGridlines

This read-only property returns a Gridlines object that represents the minor gridlines for

the specified axis. Applies only to primary axes (not secondary axes).

Note that the Gridlines object is not a collection object; that is, there is no Gridline object. Instead,

the properties of the Gridlines object apply to all of the gridlines for the axis. For instance, the

following code adds major gridlines to the category axis and formats these lines:

ActiveChart.Axes(xlCategory, xlPrimary). _

HasMajorGridlines = True

With ActiveChart.Axes(xlCategory, xlPrimary). _

MajorGridlines.Border

.Color = RGB(0, 0, 255)

.LineStyle = xlDot

.Weight = xlThick

End With



21.6.8 Position- and Dimension-Related Properties

The Axis object has the following properties related to its position and dimensions. These

properties are read-only.

Height

Returns the height of the axis in points.

Width

Returns the width of the axis in points.

Left

Returns the distance from the left edge of the axis to the left edge of the chart area.

Top

Returns the distance from the top edge of the axis to the top of the chart area.

(Note that the width of a vertical axis is 0 and the height of a horizontal axis is 0.)



21.6.9 Tick Mark-Related Properties

The Axis object has several properties related to tick marks. The MajorTickMark property returns

or sets the type of major tick mark for the specified axis, and the MinorTickMark property does

the same for minor tick marks. Each of these properties can assume any value from the following

enum:

Enum XlTickMark

xlTickMarkNone = -4142

xlTickMarkInside = 2

xlTickMarkOutside = 3



369



xlTickMarkCross = 4

End Enum



The TickMarkSpacing property returns or sets the number of categories or series between tick

marks. This applies only to category and series axes (for 3-D charts). To set the tick mark spacing

on a value axis, we can use the MajorUnit and MinorUnit properties (described later in this

chapter).

There are also several properties related to tick-mark labels. The TickLabels property returns a

TickLabels object that represents the tick-mark labels for the specified axis.

The TickLabelPosition property returns or sets the position of tick-mark labels on the specified

axis. It can be one of the following XlTickLabelPosition constants:

Enum XlTickLabelPosition

xlTickLabelPositionNone = -4142

xlTickLabelPositionLow = -4134

xlTickLabelPositionHigh = -4127

xlTickLabelPositionNextToAxis = 4

End Enum



The TickLabelSpacing property returns or sets the number of categories or series between tickmark labels. This property applies only to category and series axes (for 3-D charts). Note that

Excel determines label spacing on all value axes.

21.6.9.1 The TickLabels object

The TickLabels object represents the set of tick-mark labels for an axis. Note that this is not a

collection object; that is, there is no TickLabel object. Thus, the properties of the TickLabels

object affect all of the labels for an axis simultaneously.

The TickLabels object has several properties, the most prominent of which are the following:

Font

Returns a Font object that can be used to set the font characteristics of the labels.

AutoScaleFont

Scales the label text font size automatically when the chart size changes when this

property is True. The default value is True.

NumberFormat

Sets the number-format code for the labels. This property returns Null if all labels do not

have the same number format. Note that since the format codes are the same as those used

by Excel to format worksheet cells, we can use the macro recorder to get appropriate

format codes.

Orientation

Returns or sets the orientation for the labels and can be any one of the following constants:

Enum XlTickLabelOrientation

xlTickLabelOrientationUpward = -4171



370



xlTickLabelOrientationDownward = -4170

xlTickLabelOrientationVertical = -4166

xlTickLabelOrientationHorizontal = -4128

xlTickLabelOrientationAutomatic = -4105

End Enum



21.6.10 Units-Related Properties

The Axis object has several properties related to setting units and the scale factor on the axis.

MajorUnit and MinorUnit

Returns or sets (as a Double) the major units or minor units for the specified axis. Setting

this property sets the corresponding MajorUnitIsAuto or MinorUnitsIsAuto property to

False.

MajorUnitIsAuto and MinorUnitIsAuto

Calculates the major units or minor units for the axis if True. These properties are

read/write Boolean.

MajorUnitScale and MinorScaleUnit

Returns or sets the major unit scale value or minor unit scale value for the category axis

when the CategoryType property is set to xlTimeScale. It can be one of the following

XlTimeUnit constants:

Enum XlTimeUnit

xlDays = 0

xlMonths = 1

xlYears = 2

End Enum



MaximumScale and MinimumScale

Returns or sets the maximum or minimum value on the axis as a Double.

MaximumScaleIsAuto and MinimumScaleIsAuto

Calculates the maximum value or minimum value for the axis if True . This property is

read/write Boolean.

ScaleType

Returns or sets the value axis scale type; this property applies only to value axes. It can be

one of the following XlScaleType constants:

Enum XlScaleType

xlScaleLogarithmic = -4133

xlScaleLinear = -4132

End Enum



To illustrate, the following code:

With ActiveChart.Axes(xlValue, xlPrimary)



371



'Common logarithm



.MajorUnit = 1

.MaximumScale = 5

End With



will cause the axis to appear as on the left in Figure 21-12. On the other hand, the code:

With ActiveChart.Axes(xlValue, xlPrimary)

.MajorUnit = 5

.MaximumScale = 3

End With



results in the axis shown on the right in Figure 21-12.

Figure 21-12. Axis units and scale



21.6.11 ReversePlotOrder Property

This read/write Boolean property can be set to True to have Excel reverse the direction of an axis

(although the name is not very descriptive of the function). For instance, if the active chart is the

chart in Figure 21-13, then the code:

ActiveChart.Axes(xlValue, xlPrimary).ReversePlotOrder = True



changes this chart to the one in Figure 21-14, where the primary axis data is plotted from the top

down (so to speak).

Figure 21-13. Illustrating ReversePlotOrder (before)



372



Figure 21-14. Illustrating ReversePlotOrder (after)



21.6.12 Type Property

The Type property returns or sets the axis type. It can be one of the following XlAxisType

constants:

Enum XlAxisType

xlCategory = 1

xlValue = 2

xlSeriesAxis = 3

End Enum



21.7 The ChartArea Object

The ChartArea object represents the chart area of a chart. As shown in Figure 21-1, the chart area

on a 2-D chart contains essentially the entire chart: the axes, chart title, axis titles, and legend. On

the other hand, the chart area on a 3-D chart contains the chart title and the legend but does not

include the plot area, where the data is plotted.



373



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Table 21-2. Members of the Axis Object

Tải bản đầy đủ ngay(0 tr)

×