Legend Class
Represents a chart’s legend.
Namespace: DevExpress.XtraCharts
Assembly: DevExpress.XtraCharts.v20.2.dll
NuGet Packages: DevExpress.Charts, DevExpress.WindowsDesktop.Charts
Declaration
public class Legend :
ChartElementNamed,
IDisposable,
IBackground,
IHitTest,
ISupportVisibilityControlElement,
ISupportTextAntialiasing,
IXtraSupportDeserializeCollectionItem
Remarks
The Legend class contains the settings that define the legend functionality within a chart control. An object of the Legend type can be accessed using a chart control’s ChartControl.Legend and ChartControl.Legendsproperties.
The legend identifies different chart elements (such as series, constant lines and strips). An element is represented in the legend when its ShowInLegend property is enabled (in particular, the SeriesBase.ShowInLegend, ConstantLine.ShowInLegend and Strip.ShowInLegend properties).
The properties provided by the Legend class allow you to control the following legend options.
- the legend’s title options (Legend.Title);
- the custom legend items (Legend.CustomItems) and their visibility mode (Legend.ItemVisibilityMode);
- the legend’s horizontal and vertical alignment within the chart control (Legend.AlignmentHorizontal and Legend.AlignmentVertical);
- the appearance settings of the legend’s area (Legend.BackColor, Legend.BackImage, Legend.Border, Legend.FillStyle and Legend.Shadow);
- the appearance settings of the legend’s contents (Legend.Antialiasing, Legend.Font and Legend.TextColor);
- the positioning of the legend’s entries (Legend.Direction, Legend.EquallySpacedItems, Legend.HorizontalIndent, Legend.VerticalIndent and Legend.TextOffset);
- the legend marker settings (Legend.MarkerSize and Legend.MarkerVisible);
- the legend’s visibility (Legend.Visible).
For more information, refer to Legends.
Example
This example demonstrates how a chart’s Legend can be accessed and customized at runtime.
To control whether series, indicators, Constant Lines and Strips should be shown in the legend, use their SeriesBase.ShowInLegend, Indicator.ShowInLegend, ConstantLine.ShowInLegend and Strip.ShowInLegend properties.
Legend legend = chartControl1.Legend;
// Display the chart control's legend.
legend.Visible = true;
// Define its margins and alignment relative to the diagram.
legend.Margins.All = 8;
legend.AlignmentHorizontal = LegendAlignmentHorizontal.RightOutside;
legend.AlignmentVertical = LegendAlignmentVertical.Top;
// Define the layout of items within the legend.
legend.Direction = LegendDirection.LeftToRight;
legend.EquallySpacedItems = true;
legend.HorizontalIndent = 8;
legend.VerticalIndent = 8;
legend.TextVisible = true;
legend.TextOffset = 8;
legend.MarkerVisible = true;
legend.MarkerSize = new Size(20, 20);
legend.Padding.All = 4;
// Define the limits for the legend to occupy the chart's space.
legend.MaxHorizontalPercentage = 50;
legend.MaxVerticalPercentage = 50;
// Customize the legend appearance.
legend.BackColor = Color.Beige;
legend.FillStyle.FillMode = FillMode.Gradient;
((RectangleGradientFillOptions)legend.FillStyle.Options).Color2 = Color.Bisque;
legend.Border.Visible = true;
legend.Border.Color = Color.DarkBlue;
legend.Border.Thickness = 2;
legend.Shadow.Visible = true;
legend.Shadow.Color = Color.LightGray;
legend.Shadow.Size = 2;
// Customize the legend text properties.
legend.Antialiasing = false;
legend.Font = new Font("Arial", 9, FontStyle.Bold);
legend.TextColor = Color.DarkBlue;