Products
Chart Control for C# Windows Forms
Chart features scroll and zoom, selection, tooltip, trackball, title, legend, smart labels, drill down
Home > WinForms UI Controls > User Manual > Chart Legend in C#

Legend of Chart for WinForms C# Tutorial

The Windows Forms Chart control offers the support for the legends that is the descriptions of the charts on the plot. The items in the legend are series specific for the pie chart, and the data points will be displayed in the legend. There will be only one item displayed for line series for each series.

How to Display Legend in Chart

The property of DisplayLegend included in the Chart controls for Windows Forms determines if the legend will be displayed or not. The default value of the DisplayLegend is left as false. The legend for Chart is able to display a legend title and users can display the title text by using the property of LegendTitle.

this.chart1.DisplayLegend = true;
this.chart1.LegendTitle = "Title";

How to Customize Legend in Chart

Users can change the position of the Chart title by using the property of TitleLocation. If we want to customize more of the title, we can use the TitleElement

this.chart1.ChartElement.LegendElement.TitleLocation = TitleLocation.Bottom;
this.chart1.ChartElement.LegendElement.TitleElement.Font = new Font("Arial", 10, FontStyle.Italic);
this.chart1.ChartElement.LegendElement.TitleElement.ForeColor = Color.Red;
Users can also dock the legend to each side of the Chart control by customizing the LegendLocation property.

this.chart1.ChartElement.LegendLocation = LegendLocation.Bottom;
The property of LegendLocation can be set to float over the chart view. The below C# code shows how to set the legend to stay at the location 120, 10 over the chart area. The LegendOffset property is only taken into consideration when the LegendLocation is set to Float.

this.chart1.ChartElement.LegendLocation = LegendLocation.Float;
this.chart1.ChartElement.LegendOffset = new Point(120, 10);

How to Setup LegendItem in Chart

The individual data points are the elements that provide legend items in the Pie chart. Besides the Pie chart, the legend items will be provided by the series. Developers need to set two properties, DisplayInLegend and LegendTitle, to each provider so that to control the representation of legend items in the legend. These two properties are.

LineSeries line = new LineSeries();
line.DisplayInLegend = true;
line.LegendTitle = "Title";

How to Change Title of LegendItem for Chart

The Items property of the chart legend can be used to customize the title. When users change the text in the legend item, the text in the data point or series will also change. The below C# code can be used to add the line series to the chart and change the title of the legend item via the legend Items collection.

this.chart1.ChartElement.LegendElement.Items[0].Title = "New Title";

How to Add and Remove LegendItems

To add or remove items from the legend by using the Items collection, we need to create a new instance of LegendItem and add to the Items collection. The Element property of the LegendItem allows users customizing the style of the marker.

LegendItem item = new LegendItem();
item.Element.BorderColor = Color.Gray;
item.Element.BackColor = Color.Blue;
item.Title = "Custom item";
this.chart1.ChartElement.LegendElement.Items.Add(item);
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls