Excel: In which ways can one create ChartGroups?

2019-06-04 00:24发布

问题:

I have a Chart containing two ChartGroups. I never did this intentionally, so I cannot guess what caused this. Can anyone give all options (either VBA or else)?

If I know which are the possible actions that may have led to this, I can be aware of and control/avoid them.

PS: The downside of this is that I have quite a bit of code that uses the PlotOrder for identifying Series. When there is more than one ChartGroup, PlotOrder is not a unique identifier of a Series in a Chart (it is unique within a ChartGroup). In the long run, I Will have to adapt my code, anyway. But for the time being, I could take care of adapting my charts to having a single ChartGroup, as much as possible. Is there an easy way to reassign the ChartGroup of a Series? (this would make a single post with two questions...)

This question stemmed from

Excel: Duplicated PlotOrder for two Series in a Chart

EDIT: I list here some interesting findings (which revolve around this topic, but do not answer the specific question).

  1. I found this in the official site

    ChartGroups Collection: "A collection of all the ChartGroup objects in the specified chart. Each ChartGroup object represents one or more series plotted in a chart with the same format." Even if I did not find it explicitly stated, it appears that each XlChartType (the Enumeration type of .ChartType for a Series) would be obviously associated with a given format, e.g., xlXYScatter with scatter.

    ChartGroup Object: "Represents one or more series plotted in a chart with the same format."

  2. All Series in a ChartGroup should have the same "format". But the converse is not true: it is possible to have Series with the same format in different ChartGroups. See point 3 and example below.

  3. From ChartGroup Object: "Because the index number for a particular chart group can change if the chart format used for that group is changed, it may be easier to use one of the named chart group shortcut methods to return a particular chart group. The PieGroups method returns the collection of pie chart groups in a chart, the LineGroups method returns the collection of line chart groups, and so on." E.g., LineGroups Method.

    This allows for point 2: The PieGroups would return a collection of ChartGroups. There may be more than one such ChartGroup, and all of the series would have a Pie format.

  4. The available named chart group shortcut methods to return a particular chart group are not comprehensive. E.g., there is no XYScatterGroups method.

As an example of point 2, I have a chart with all series with format XYScatter. The 5 available XlChartTypes (with their numeric values and description, according to XlChartType Enumeration) are

xlXYScatter -4169   Scatter.
xlXYScatterSmooth   72  Scatter with Smoothed Lines.
xlXYScatterSmoothNoMarkers  73  Scatter with Smoothed Lines and No Data Markers.
xlXYScatterLines    74  Scatter with Lines.
xlXYScatterLinesNoMarkers   75  Scatter with Lines and No Data Markers.

In the chart, there are 2 ChartGroups, as shown in the dump (from a custom Sub)

ChartObject 'Chart 36', no. series: 6
* ChartGroup, .Index: 1, no. series: 2
** plot order: 1, type: -4169, chart: 'Chart 36', chart type: 73, no. points: 17
** plot order: 2, type: -4169, chart: 'Chart 36', chart type: -4169, no. points: 17
* ChartGroup, .Index: 2, no. series: 4
** plot order: 1, type: -4169, chart: 'Chart 36', chart type: 75, no. points: 2
** plot order: 2, type: -4169, chart: 'Chart 36', chart type: -4169, no. points: 24
** plot order: 3, type: -4169, chart: 'Chart 36', chart type: 75, no. points: 33
** plot order: 4, type: -4169, chart: 'Chart 36', chart type: -4169, no. points: 5

Note that there are series of type xlXYScatter (-4169) in both ChartGroups. I could change the format of Series 2 of ChartGroup 1 to any of the 5 types, and it would not change its ChartGroup.