Legend objects represent legends. The Legend object and its children are shown in Figure 21-23.
The Chart object has a Legend property that returns the Legend object for that chart. As expected, the Legend object has Border, ChartFillFormat, Font, and Interior children that serve the same purpose here as they do for the ChartTitle, AxisTitle, ChartArea, and other objects.
Figure 21-1 shows a chart legend with two legend entries. Legend entries are represented by LegendEntry objects. The LegendEntry objects for a legend are kept in the LegendEntries collection object for the Legend object. This collection is accessed using the LegendEntries property of the Chart object.
Each legend entry has two parts: the text of the legend entry is the name of the series associated with that entry and the entry key (also called an entry marker) is a small copy of the associated series and its formatting.
Note that the text of a legend entry cannot be changed. However, the LegendEntry object does have a Font property that can be used to change the font of the legend entry, as in:
ActiveChart.Legend.Font.Italic = True
In addition, LegendEntry objects can be deleted. However, after a
legend entry has been deleted, the only way to restore it is to
remove and recreate the entire legend by setting the HasLegend
property for the chart first to False
and then to
True
.
Also, no pattern formatting is allowed for legend entries, nor can a legend entry's position or size be changed.
Note finally that there is no direct way to return the series corresponding to a given legend entry.
A legend key is represented by a LegendKey object. This object has Border, ChartFillFormat, and Interior children.
It is very important to note that formatting the LegendKey object will also automatically format the actual series that the legend entry represents. In other words, the series and its legend key always match.
Thus, for instance, the following code formats the first data series and its legend key with a red interior and a thick border:
With ActiveChart.Legend.LegendEntries(1).LegendKey .Interior.ColorIndex = 3 .Border.Weight = xlThick End With