Chart for .NET / User's Guide / Legend / Legend Data

Legend Data

The integrated legend of Nevron Chart for .NET can operate in three modes - Disabled, Automatic and Manual. In disabled mode the legend is not displayed. When you set the mode to Automatic or Manual the legend will display the data it is attached to. This topic will discuss these two legend modes.

 Manual Legend Mode

In this mode you're responsible to feed the data displayed by the legend. The following code switches the legend mode to manual:

C#
Copy Code
legend.Mode = LegendMode.Manual;
Visual Basic
Copy Code
legend.Mode = LegendMode.Manual

Feeding data to the legend is easy. You have to create an instance of the NLegendItemCellData class and add it to legend.Data.Items collection:

C#
Copy Code
NLegendItemCellData legendItemCellData = new NLegendItemCellData();

legendItemCellData.Text = "My First Legend Item";
legendItemCellData.MarkFillStyle = new NColorFillStyle(Color.Azure);
legendItemCellData.MarkShape = LegendMarkShape.Star;
legend.Data.Items.Add(legendItemCellData);
Visual Basic
Copy Code
Dim legendItemCellData As New NLegendItemCellData

legendItemCellData.Text = "My First Legend Item"
legendItemCellData.MarkFillStyle = New NColorFillStyle(Color.Azure)
legendItemCellData.MarkShape = LegendMarkShape.Star
legend.Data.Items.Add(legendItemCellData)

The NLegendItemCellData exposes properties allowing you to configure the item appearance. The mark style is controlled with the help of the MarkShape property accepting values from the LegendMarkShape enumeration. The following table shows the possible mark shapes:

Rectangle Circle Diamond Triangle
Inverted Triangle Cross Diagonal Cross Star


The filling of the mark is controlled from the MarkFillStyle property accepting styles that derive from the NFillStyle class.

When you have line charts you may want to use the mark item line style and disable the shape. For instance:

C#
Copy Code
NLegendItemCellData item = new NLegendItemCellData();

item.Text = "Custom legend item";
item.MarkShape = LegendMarkShape.None;
item.MarkLineStyle.Width = new NLength(2, NGraphicsUnit.Pixel);
item.MarkLineStyle.Color = Color.Red;
legend.Data.Items.Add(item);
Visual Basic
Copy Code
Dim item As New NLegendItemCellData

item.Text = "Custom legend item"
item.MarkShape = LegendMarkShape.None
item.MarkLineStyle.Width = New NLength(2, NGraphicsUnit.Pixel)
item.MarkLineStyle.Color = Color.Red
legend.Data.Items.Add(item)

The TextStyle property controls the text style used to display the legend data item text:

C#
Copy Code
item.TextStyle.FontStyle = new NFontStyle("Arial", 13);
Visual Basic
Copy Code
item.TextStyle.FontStyle = New NFontStyle("Arial", 13)
 Automatic Legend Mode

You switch the legend in automatic mode by settings the legend Mode property to LegendMode.Automatic:

C#
Copy Code
legend.Mode = LegendMode.Automatic;
Visual Basic
Copy Code
legend.Mode = LegendMode.Automatic

Each series derived from NSeriesBase has an associated NSeriesLegend object controlling the information displayed for the series in the legend. It is accessible with the help of the Legend property of the NSeriesBase class.

C#
Copy Code
NSeriesLegend seriesLegend = seriesBase.Legend;
Visual Basic
Copy Code
Dim seriesLegend As NSeriesLegend  = seriesBase.Legend
 Controlling the Series Legend Mode

The series legend mode is controlled by the Mode property of the NSeriesLegend object accepting values from the SeriesLegendMode enumeration. The following table describes the different series legend modes.

SeriesLegendMode Description
None No information will be added to the legend for this series.
Series Only one legend item will be added. The legend mark will be displayed with the fill effect and line properties defined by the series. The legend item text will be extracted from the series name (specified by the Name property of the NSeriesBase class).
DataPoints The series will insert a legend item for each data point displayed by the series. The legend mark will be displayed with the fill effect and line properties of the data point it represents.
SeriesLogic Custom series dependent information will be inserted in the legend, which defines the logic used by the series.

The following code will display information about the data points of a bar chart:

C#
Copy Code
bar.Legend.Mode = SeriesLegendMode.DataPoints;
Visual Basic
Copy Code
bar.Legend.Mode = SeriesLegendMode.DataPoints
 Controlling the Legend Text Format

When the series legend is operating in DataPoints mode it inserts information about the data points of the series. The user can control what information is displayed for each individual data point with the help of a simple formatting string. It is specified from the Format property. The following example will display the bar value and label in the legend items.

C#
Copy Code
bar.Legend.Format = "<value> <label>";
Visual Basic
Copy Code
bar.Legend.Format = "<value> <label>"

For a list of the formatting commands and detail instruction how to format the values they represent please refer to the Series Texts Formatting topic.

 Controlling the Series Legend Order

Sometimes you may want to invert or prepend the legend data items automatically generated by the series to the data added by another series (in case you have multiple series that present data on the legend). This is done by changing the Order property of the NSeriesLegend object accepting values from the SeriesLegendOrder enumeration. The following table shows the possible values legend orders:

SeriesLegendOrder Description
Append Items are added after the existing legend items. (This is the default mode)
Prepend Items are added before the existing legend items.
AppendInverted Items are added after the existing legend items in inverted order
PrependInverted Items are added before the existing legend in inverted order

The following code will tell the series to prepend the legend items generated by it to the legend:

C#
Copy Code
bar.Legend.Order = SeriesLegendOrder.Prepend;
Visual Basic
Copy Code
bar.Legend.Order = SeriesLegendOrder.Prepend
 Related Examples

Windows forms: Series Attributes\Legend
Windows forms: Panels\Legend\Custom Legend Items


Web forms: Series Attributes\Legend
Windows forms: Panels\Legend\Custom Legend Items

See Also