We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

How to customize the items in the ChartLegend?

 

The chart legend can be displayed by setting ShowLegend property as true. Essential Chart supports various properties to customize legend items. ItemsAlignment property is used to specify the alignment of Items in legend. ShowItemShadow property is used to display shadow in items. TextAlignment property is used to align text in legend.

C#

// Aligns Items in the legend

this.chartControl1.Legend.ItemsAlignment = StringAlignment.Center;

// Displays Shadow

this.chartControl1.Legend.ShowItemsShadow = true;

this.chartControl1.Legend.ItemsShadowColor = Color.Red;

this.chartControl1.Legend.ItemsShadowOffset = new Size(2, 2);

this.chartControl1.Legend.ItemsSize = new Size(20, 20);

// Aligns text in the legend

this.chartControl1.Legend.ItemsTextAligment = VerticalAlignment.Top;

VB

' Aligns Items in the legend

Me.ChartControl1.Legend.ItemsAlignment = StringAlignment.Center

' Displays Shadow

Me.ChartControl1.Legend.ShowItemsShadow = True

Me.ChartControl1.Legend.ItemsShadowColor = Color.Red

Me.ChartControl1.Legend.ItemsShadowOffset = New Size(2, 2)

Me.ChartControl1.Legend.ItemsSize = New Size(20, 20)

' Aligns text in the legend

Me.ChartControl1.Legend.ItemsTextAligment = VerticalAlignment.Top

It is also possible to show the custom legend items in Legend. To display custom legend items we need to create an instance of ChartLegendItem class. To display a custom legend item we need to set the visible property to false for the default legend item.

We can customize the style of the legend item also. ShowSymbol, ShowShadow allows us to display or hide the symbol and shadow respectively. We can edit the symbol by using Symbol.Color, Symbol.Shape etc. Using CustomItems under Legend property we can add the item into Legend.

C#

// Hidding default legend item

series2.LegendItem.Visible = false;

// Creating new custom legend item

ChartLegendItem item1 = new ChartLegendItem();

// Setting its properties

item1.ItemStyle.BorderColor = Color.Fuchsia;

item1.ItemStyle.ShowShadow = false;

item1.ItemStyle.ShowSymbol = true;

item1.ItemStyle.Symbol.Color = Color.LightCyan;

item1.ItemStyle.Symbol.Shape = ChartSymbolShape.Diamond;

item1.Text = "Server2";

item1.ItemStyle.TextColor = Color.Green;

// Adding it into chart legend

this.chartControl1.Legend.CustomItems = new ChartLegendItem[] { item1 };

VB

' Hidding default legend item

series2.LegendItem.Visible = False

' Creating new custom legend item

Dim item1 As ChartLegendItem = New ChartLegendItem()

' Setting its properties

item1.ItemStyle.BorderColor = Color.Fuchsia

item1.ItemStyle.ShowShadow = False

item1.ItemStyle.ShowSymbol = True

item1.ItemStyle.Symbol.Color = Color.LightCyan

item1.ItemStyle.Symbol.Shape = ChartSymbolShape.Diamond

item1.Text = "Server2"

item1.ItemStyle.TextColor = Color.Green

' Adding it into chart legend

Me.ChartControl1.Legend.CustomItems = New ChartLegendItem() {item1}

Article ID: Published Date: Last Revised Date: Platform: Control:
56 04/30/2011 07/10/2014 Windows Forms Chart
Tags:
Did you find this information helpful?
Add Comment
You must log in to leave a comment

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.