# Chart Legend

**ChartLegend** is used to provide information about the charts shown on the chart area. The **ChartLegend** has **Header**, usually some text and items - one for each chart series.

> tip To see how to style and template **ChartLegend** take a look at the [Styling the Chart Legend](https://www.telerik.com/products/wpf/documentation/controls/radchart/styles-and-templates/styling-and-appearance-styling-chart-legend) topic.

This topic will make you familiar with the following things:

* [Automatic Legend Items Generation](https://www.telerik.com/products/wpf/documentation/controls/radchart/features/chart-legend#Automatic-Legend-Items-Generation)

* [Manual Legend Items Generation](https://www.telerik.com/products/wpf/documentation/controls/radchart/features/chart-legend#Manual-Legend-Items-Generation)

* [Legend Display Modes](https://www.telerik.com/products/wpf/documentation/controls/radchart/features/chart-legend#Legend-Display-Modes)

* [Legend Positioning](https://www.telerik.com/products/wpf/documentation/controls/radchart/features/chart-legend#Legend-Positioning)

* [Legend Items Shape](https://www.telerik.com/products/wpf/documentation/controls/radchart/features/chart-legend#Legend-Items-Shape)

* [Reverse Legend Items Order](https://www.telerik.com/products/wpf/documentation/controls/radchart/features/chart-legend#Reverse-Legend-Items-Order)

## Automatic Legend Items Generation

In order to have legend items generated automatically, you have to use the property **UseAutoGeneratedItems**. When it is **True** (**Default** value), **ChartLegend** will add automatically one item for each chart series from the associated [ChartArea](https://www.telerik.com/products/wpf/documentation/controls/radchart/features/chart-area).

```XAML
	<telerik:RadChart x:Name="radChart"
	                  Width="511"
	                  Height="362"
	                  VerticalAlignment="Top">
	    <telerik:RadChart.DefaultView>
	        <telerik:ChartDefaultView>
	
	            <telerik:ChartDefaultView.ChartLegend>
	                <telerik:ChartLegend x:Name="chartLegend"
	                                     Header="Top 10 Manufacturers"
	                                     UseAutoGeneratedItems="True" />
	            </telerik:ChartDefaultView.ChartLegend>
	            <telerik:ChartDefaultView.ChartArea>
	                <telerik:ChartArea LegendName="chartLegend" />
	
	            </telerik:ChartDefaultView.ChartArea>
	
	        </telerik:ChartDefaultView>
	    </telerik:RadChart.DefaultView>
	</telerik:RadChart>
```

> tip By default, the **RadChart.DefaultView\.ChartLegend** is associated with the **RadChart.DefaultView\.ChartArea**. However, if you predefine the **DefaultView** (like it was done in the XAML above), be sure that the **ChartArea** has its property **LegendName** set to the name of the newly created **ChartLegend**. Otherwise, your legend items will not be auto generated.

> tip If you want to set the legend title only, it is better to do it in the code-behind not in the XAML, because you will have to predefine the whole [visual structure](https://www.telerik.com/products/wpf/documentation/controls/radchart/general-information/visual-structure) of the chart's default view.

```C#
	this.radChart.DefaultView.ChartLegend.UseAutoGeneratedItems = true;
	this.radChart.DefaultView.ChartLegend.Header = "Top 10 Manufacturers";
```

```VB.NET
	radChart.DefaultView.ChartLegend.UseAutoGeneratedItems = True
	radChart.DefaultView.ChartLegend.Header = "Top 10 Manufacturers"
```

As you can see, the **ChartLegend** on the right side of the **RadChart** is automatically generated and has one item for each manufacturer (series).

![WPF RadChart Chart Legend](https://www.telerik.com/products/wpf/documentation/assets/e5d8c65f0851a975c281f518ae4542e5/radchart_features_chartlegend_02.png)

## Manual Legend Items Generation

If you need to explicitly create legend items, set the value of the property **UseAutoGeneratedItems** to **False** and manually add the items which you need to the chart legend.

Here is a small example which demonstrates how to create and add new legend items to the chart legend of a chart.

```XAML
	<telerik:RadChart>
	    <telerik:RadChart.DefaultView>
	        <telerik:ChartDefaultView>
	            
	            <telerik:ChartDefaultView.ChartLegend>
	                <telerik:ChartLegend Header="Top 10 Manufacturers" UseAutoGeneratedItems="False">
	                    <telerik:ChartLegend.Items>
	                        <telerik:ChartLegendItem Label="Item 1" MarkerFill="Blue" />
	                        <telerik:ChartLegendItem Label="Item 2" MarkerFill="Green" />
	                    </telerik:ChartLegend.Items>
	                </telerik:ChartLegend>
	            </telerik:ChartDefaultView.ChartLegend>
	            
	        </telerik:ChartDefaultView>
	    </telerik:RadChart.DefaultView>
	</telerik:RadChart>
```

```C#
	Telerik.Windows.Controls.RadChart radChart = new Telerik.Windows.Controls.RadChart();
	radChart.DefaultView.ChartLegend.UseAutoGeneratedItems = false;
	
	ChartLegendItem item1 = new ChartLegendItem();
	item1.Label = "Item 1";
	item1.MarkerFill = new SolidColorBrush(Colors.Blue);
	radChart.DefaultView.ChartLegend.Items.Add( item1 );
	ChartLegendItem item2 = new ChartLegendItem();
	item2.Label = "Item 2";
	item2.MarkerFill = new SolidColorBrush(Colors.Green);
	radChart.DefaultView.ChartLegend.Items.Add( item2 );
```

```VB.NET
	Dim radChart As New Telerik.Windows.Controls.RadChart()
	radChart.DefaultView.ChartLegend.UseAutoGeneratedItems = False
	Dim item1 As New ChartLegendItem()
	item1.Label = "Item 1"
	item1.MarkerFill = New SolidColorBrush(Colors.Blue)
	radChart.DefaultView.ChartLegend.Items.Add(item1)
	Dim item2 As New ChartLegendItem()
	item2.Label = "Item 2"
	item2.MarkerFill = New SolidColorBrush(Colors.Green)
	radChart.DefaultView.ChartLegend.Items.Add(item2)
```

And here is the result:

![WPF RadChart Manual Legend Items Generation](https://www.telerik.com/products/wpf/documentation/assets/eb199202d95a7d6c75c0e02e308fe422/radchart_features_chartlegend_03.png)

## Legend Display Modes

The **SeriesDefinitions** expose the **LegendDisplayMode** property, which allows you to specify the way in which the Legend gets displayed. You can choose between three values:

* **DataPointLabel** - this will display an item for each **DataPoint** in your series and each series item will be marked with a different color.

* **None** - this will display no items in the legend.

* **SeriesLabel** - this will display an item for each series in your **RadChart**.

> tip The default value is different depending on the type of the series.

Here is an example of how the legend looks, when the **LegendDisplayMode** of the **DefaultSeriesDefinition** is set to **DataPointLabel**.

> tip When using dynamic data and you want the **LegendDisplayMode** to be set to **DataPointLabel**, you have to create an **ItemMapping** for the **LegendLabel** property of the **DataPoint**. More information about **ItemMappings** can be found [here](https://www.telerik.com/products/wpf/documentation/controls/radchart/populating-with-data/data-binding-with-manual-series-mapping).

```C#
	public LegendDisplayModeSample()
	{
	    InitializeComponent();
	    this.radChart.ItemsSource = new int[] { 1, 5, 6, 9, 5, 7 };
	    this.radChart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.SeriesLabel;
	}
```

```VB.NET
	Public Sub New()
	    InitializeComponent()
	    Me.radChart.ItemsSource = New Integer() {1, 5, 6, 9, 5, 7}
	    Me.radChart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.SeriesLabel
	End Sub
```

![WPF RadChart SeriesLabel Legend Display Mode](https://www.telerik.com/products/wpf/documentation/assets/eb4d7be113e1042e507518d19f2e0b71/radchart_features_chartlegend_04.png)

## Legend Positioning

You are able to specify the position of the Legend by just setting the **ChartLegendPosition** property of the **ChartDefaultView**. It can have one of the following values:

* **Bottom** - places the legend at the bottom of the **RadChart**.

* **Left** - places the legend at the left of the **RadChart.**

* **Right** - places the legend at the right of the **RadChart. (default)**

* **Top** - places the legend at the top of the **RadChart.**

Here is an example:

```C#
	this.radChart.DefaultView.ChartLegendPosition = Dock.Bottom;
```

```VB.NET
	Me.radChart.DefaultView.ChartLegendPosition = Dock.Bottom
```

![WPF RadChart Legend Positioning](https://www.telerik.com/products/wpf/documentation/assets/4bc64ac616ebdf8404df066b8ed5528b/radchart_features_chartlegend_05.png)

## Legend Items Shape

You are able to specify the shape of the Legend Item Markers just by setting the **LegendItemMarkerShape** property of the ChartLegend. It can be assigned to the following shapes:

* **Line**

* **Circle**

* **Diamond**

* **Hexagon**

* **Square**

* **SquareRounded (default)**

* **StarEightRay**

* **StarFiveRay**

* **Triangle**

```C#
	this.radChart.DefaultView.ChartLegend.LegendItemMarkerShape = MarkerShape.StarFiveRay;
```

```VB.NET
	Me.radChart.DefaultView.ChartLegend.LegendItemMarkerShape = MarkerShape.StarFiveRay
```

![WPF RadChart Legend Items Shape](https://www.telerik.com/products/wpf/documentation/assets/7fb26b7ccc25847922ba02142a304cd8/radchart_features_chartlegend_06.png)

## Reverse Legend Items Order

There are scenarios where you would like to have the Legend Items displayed in reverse order. For example when you have a *Horizontal Bar Series* the series will be displayed in the Legend in the way they are populated. The bottom Series will appear first on the Legend and the topmost will be last in the Legend. For such scenarios RadChart exposes a **ReverseLegendItemsOrder** boolean property to reverse the Legend Items order as its name implies.

## See Also

* [Chart Title](https://www.telerik.com/products/wpf/documentation/controls/radchart/features/chart-title)

* [Chart Area](https://www.telerik.com/products/wpf/documentation/controls/radchart/features/chart-area)

* [DefaultView](https://www.telerik.com/products/wpf/documentation/controls/radchart/features/default-view)

* [RadChart Visual Structure](https://www.telerik.com/products/wpf/documentation/controls/radchart/general-information/visual-structure)

* [Styling the Chart Legend](https://www.telerik.com/products/wpf/documentation/controls/radchart/styles-and-templates/styling-and-appearance-styling-chart-legend)
