New to Telerik UI for WinFormsStart a free 30-day trial

Apply a Custom Shape to ChartView's Legend Items

Updated over 6 months ago

Environment

Product VersionProductAuthor
2019.2.508RadChartView for WinFormsDesislava Yordanova

Description

The LegendItemElement in RadChartView consists of a LegendItemTitle and a LegendItemMarker:

custom-shape-in-chart-legend-items001

A common requirement is to change the square shape of the marker element. This article demonstrates how to achieve it.

Solution

Before populating RadChartView with data, subscribe to the ChartElement.LegendElement.VisualItemCreating event, create the default LegendItemElement and assign an ElementShape to the ItemElement.MarkerElement.Shape property:

custom-shape-in-chart-legend-items002

Apply a HeartShape to legend items

C#
        public RadForm1()
        {
            InitializeComponent();

            this.radChartView1.ChartElement.LegendElement.VisualItemCreating += LegendElement_VisualItemCreating;

            BarSeries barSeries = new BarSeries("Performance", "RepresentativeName");
            barSeries.LegendTitle = "Q1";
            barSeries.DataPoints.Add(new CategoricalDataPoint(177, "Harley"));
            barSeries.DataPoints.Add(new CategoricalDataPoint(128, "White"));
            barSeries.DataPoints.Add(new CategoricalDataPoint(143, "Smith"));
            barSeries.DataPoints.Add(new CategoricalDataPoint(111, "Jones"));
            barSeries.DataPoints.Add(new CategoricalDataPoint(118, "Marshall"));
            this.radChartView1.Series.Add(barSeries);
            BarSeries barSeries2 = new BarSeries("Performance", "RepresentativeName");
            barSeries2.LegendTitle = "Q2";
            barSeries2.DataPoints.Add(new CategoricalDataPoint(153, "Harley"));
            barSeries2.DataPoints.Add(new CategoricalDataPoint(141, "White"));
            barSeries2.DataPoints.Add(new CategoricalDataPoint(130, "Smith"));
            barSeries2.DataPoints.Add(new CategoricalDataPoint(88, "Jones"));
            barSeries2.DataPoints.Add(new CategoricalDataPoint(109, "Marshall"));
            this.radChartView1.Series.Add(barSeries2);

            this.radChartView1.ShowLegend = true;
        }

        private void LegendElement_VisualItemCreating(object sender, LegendItemElementCreatingEventArgs e)
        {
            e.ItemElement = new LegendItemElement(e.LegendItem);
            e.ItemElement.MarkerElement.Shape = new HeartShape();
        }
       

You can also create your own ElementShape. Create a derivative of the ElementShape class and override its CreatePath method. The following code snippet demonstrates how to draw a single line as it is demonstrated below:

custom-shape-in-chart-legend-items003

Apply a custom LineShape to legend items

C#

        private void LegendElement_VisualItemCreating(object sender, LegendItemElementCreatingEventArgs e)
        {
            e.ItemElement = new LegendItemElement(e.LegendItem);
            e.ItemElement.MarkerElement.Shape = new LineShape();
        }

        public class LineShape : ElementShape
        {
            public override GraphicsPath CreatePath(Rectangle bounds)
            {
                GraphicsPath path = new GraphicsPath();
                
                Point start = new Point(bounds.X, bounds.Y);
                Point end = new Point(bounds.X + bounds.Width, bounds.Y);
                path.AddLine(start, end);
                return path;
            }
        }
       
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support