The Chart Group Object

As you no doubt know, an Excel chart can contain more than one data series. We have remarked that each series (that is, the Series object) has a ChartType property that can be used to set the chart type of the series. Thus, a single chart may have one or more series with a column-type format and one or more series with a line-type format.

A ChartGroup object represents one or more series that are plotted on a chart with the same chart type. Note that a ChartGroup object is not a collection. To access the individual Series objects represented by a ChartGroup, we must use the SeriesCollection property (discussed later in this chapter).

The ChartGroup objects for a single chart are stored in the ChartGroups collection for the Chart object. This collection is accessed using the ChartGroups property.

The Excel object model provides a way to get "subcollections" of the ChartGroups collection that correspond to the major chart types (line, column, bar, etc.). To illustrate, the ColumnGroups method applies to a 2-D chart and returns the collection of all ChartGroup objects that correspond to the various column-type formats. The syntax is:

ChartObject .ColumnGroups

We can also access a single ChartGroup in this collection using the syntax:

ChartObject .ColumnGroups( Index)

Note, however, that there is no ColumnChartGroups collection per se. The ColumnGroups method actually returns a ChartGroups collection, but not the full collection that would be returned by:

ChartObject .ChartGroups

To illustrate, the chart in Figure 21-15 has two series. While each series has a line type, the subtypes are different. One series has been formatted with a line type with no data point markers, whereas the other has data markers. Accordingly, the code:

ActiveChart.LineGroups.Count returns the value 2, since there are two distinct ChartGroup objects that fit in the LineGroups collection.

The Chart object has the following methods that return corresponding collections of ChartGroup objects:

• AreaGroups

• ColumnGroups

• DoughnutGroups

• LineGroups

• RadarGroups

There are also some members of the Chart object that return chart groups for 3-D charts. They are: Area3DGroup, Bar3DGroup, Column3DGroup, Line3DGroup, Pie3DGroup, and SurfaceGroup. These members are singular because they return a single ChartGroup object.

The portion of the Excel object model that relates to ChartGroup objects is shown in Figure 21-16.

+3 -1


  • jali
    How to set chart group in excel vba?
    2 months ago

Post a comment