Legend

Legends (Legend) map colors or symbols to a chart. A Chart contains a Legends property which consists of a LegendList. Add one or more Legend instances to the LegendList to add multiple legends to a chart. A series can also add a Legend to a chart. For example, a ColumnSeries has a Legend property.

A chart has a LegendList property for adding multiple legends. A series has a Legend property allowing adding and accessing only one Legend.

Properties

A Legend has the following properties for customizing its appearance and layout.

Property Description
Align Gets or sets the legend labels text alignment.
BackgroundColor Gets or sets the BackgroundColor.
BorderColor Gets or sets the BorderColor.
BorderStyle Gets or sets the LineStyle.
BorderWidth Gets or sets the LineWidth.
BottomPadding Gets or sets the BottomPadding.
Font Gets or sets the Font.
FontSize Gets or sets the FontSize.
Height Gets or sets the height.
LabelSpacing Gets or sets the LabelSpacing.
LeftPadding Gets or sets the LeftPadding.
LegendLabelList Gets or sets the LegendLabelList.
RequiredHeight Gets the RequiredHeight.
RequiredWidth Gets the RequiredWidth.
RightPadding Gets or sets the RightPadding.
SymbolSpacing Gets or sets the SymbolSpacing.
TextColor Gets or sets the TextColor.
TopPadding Gets or sets the TopPadding.
Visible Gets or sets the visible property.
Width Gets or sets the width.
X Gets or sets the x position.
Y Gets or sets the y position.

Formatting and Use

A Legend is automatically created and added to a Chart instance's LegendList if the first series added to the Chart instance's PlotArea does not already have a legend. If a Chart instance's AutoLayout property isTrue, the Legend visually occupies 35% of the chart's width. If a Chart instance's AutoLayout property is False then set the Legend instance's X, Y, Width and Height properties manually to control Legend placement.

Setting a Chart instance's AutoLayout property requires careful placement of different elements on the chart, not only a legend's placement.

A Legend has other properties, such as BackgroundColor. For example, the following example sets a legend's background color

Legend myLegend = chart.Legends.Add(2, 3, 10, 10);
myLegend.BackgroundColor = RgbColor.Pink;

You can also change the properties of a chart's legend.

Legend legend = chart.Legends[0];
legend.BackgroundColor = RgbColor.GreenYellow;       
Dim MyLegend As Legend = MyChart.Legends(0)
MyLegend.BackgroundColor = RgbColor.GreenYellow

The actual location of the placement of the legend(s) is controlled using the LegendList instance's Placement property. The LegendPlacement property is an enumeration specifying the location to place the legend. Values are BottomCenter, BottomLeft, BottomRight, LeftCenter, RightCenter, TopCenter, TopLeft, and TopRight. By default, the LegendPlacement is set to RightCenter.

LegendLabel

When adding a Legend to a series, the Name property is used as the text to list on the legend. For example, the following code would add Website A, Website B, and Website C as the legend items.

DateTimeLineSeries lineSeries1 = new DateTimeLineSeries("Website A");
DateTimeLineSeries lineSeries2 = new DateTimeLineSeries("Website B");
DateTimeLineSeries lineSeries3 = new DateTimeLineSeries("Website C");

Multiple Legends Example

Add legends through a Chart instance's LegendsList property's Add method. When adding the first Legend to a LegendList, by default all series titles are added to the legend. The first Legend instance is the default legend. All series are added to that Legend. To change this behavior, create a new Legend (which assigns it to the LegendList) and then explicitly assign the series to that new legend. The following example illustrates.

IndexedBarSeries barSeries1 = new IndexedBarSeries("Website A", xAxis, yAxis);
IndexedBarSeries barSeries2 = new IndexedBarSeries("Website B", xAxis, yAxis);
IndexedBarSeries barSeries3 = new IndexedBarSeries("Website C", xAxis, yAxis);

Legend myLegend = chart.Legends.Add();
myLegend.BackgroundColor = RgbColor.Pink;
myLegend.BorderColor = RgbColor.Black;
myLegend.BorderStyle = LineStyle.DashSmall;
barSeries1.Legend = myLegend;

DateTimeLineSeries lineSeries1 = new DateTimeLineSeries("Website N");
DateTimeLineSeries lineSeries2 = new DateTimeLineSeries("Website M");
DateTimeLineSeries lineSeries3 = new DateTimeLineSeries("Website P");

Legend legend2 = chart.Legends.Add();
chart.Legends[1].BackgroundColor = RgbColor.Green;
legend2.BorderColor = RgbColor.Black;
legend2.BorderStyle = LineStyle.DashSmall;
lineSeries1.Legend = chart.Legends[1];
lineSeries2.Legend = legend2;
lineSeries3.Legend = chart.Legends[1];

In this code: barSeries1, barSeries2, and barSeries3 are all added to myLegend, as myLegend is the first Legend added to the Chart instance's LegendList. However, to use a different Legend with lineSeries1, lineSeries2, and lineSeries3, they are assigned legend2 as the legend. By default, any series not explicitly assigned to a Legend gets assigned to the default legend (the 0th legend in the LegendList). Access a Legend either through the Chart that it belongs to or through the Series belonging to that Legend.

In this topic