Articles in this section

How to add custom legend items to the legend instead of automated legend items?

In WinForms Chart control, it is possible to add custom ChartLegendItem to the Legend instead of the automated Legend items. For this we have to use the event, Legend.FilterItems of the ChartControl. This event is raised before the legend items are rendered.

Here is the code snippet to add the custom legend,

this.chartControl1 = new ChartControl();
. . .

this.chartControl1.Legend.FilterItems += new LegendFilterItemsEventHandler(Legend_FilterItems);
. . .
private void Legend_FilterItems(object sender, ChartLegendFilterItemsEventArgs e)
{
	e.Items.Clear();
	ChartLegendItemsCollection items = new ChartLegendItemsCollection();
	ChartLegendItem l = new ChartLegendItem();
	ChartLegendItem l1 = new ChartLegendItem();
	l.ItemStyle.TextColor = Color.Orange;
        l.Text = "Test1";
        l1.ItemStyle.TextColor = Color.DarkBlue;
	l1.Text = "Test2";
	item.Add(l);
	item.Add(l1);
	e.Items = items;
}
Me.ChartControl1 = New ChartControl()
. . .

AddHandler Me.ChartControl1.Legend.FilterItems, AddressOf ChartControl1_Legend_FilterItems
. . .

Private Sub ChartControl1_Legend_FilterItems(sender As Object, e As Syncfusion.Windows.Forms.Chart.ChartLegendFilterItemsEventArgs)
    e.Items.Clear()

    Dim items As New Syncfusion.Windows.Forms.Chart.ChartLegendItemsCollection()

    Dim l As New Syncfusion.Windows.Forms.Chart.ChartLegendItem()
    l.ItemStyle.TextColor = Color.Orange
    l.Text = "Test1"

    Dim l1 As New Syncfusion.Windows.Forms.Chart.ChartLegendItem()
    l1.ItemStyle.TextColor = Color.DarkBlue
    l1.Text = "Test2"

    items.Add(l)
    items.Add(l1)

    e.Items = items
End Sub
Output

CustomLegend

Conclusion

I hope you enjoyed learning about how to add custom legend items to the legend instead of automated legend items.

You can refer to our WinForms Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our WinForms Chart examples to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied