Shop
Anmelden
MindFusion

Q: I am building a bar chart with a legend and I want my legend to read the titles of my series. I create the legend and assign the titles, like this:

 myLegend.LabelsSource = new List() { series0.Title, series1.Title };
 
I add and remove series dynamically and must update the labels for the legend. Is this how you do it?

A: From what we see in your code you are using the SeriesLegend. This is good when you want to create a legend where you have complete control over the labels and brushes for the items. In your case, however, a better approach is to use the ChartLegend. With it you don’t set the brushes and titles for the items, but you set a Series collection, which provides the data. The labels for the ChartLegend are taken from the Title property of the ChartSeries in the Series. The brushes for the legend are taken from the Fills property of the ChartSeries in the Series. Here is a sample code:

 ChartLegend legend = new ChartLegend();
 //assign the Series
 legend.Series = barChart1.Series;

 //customize the position of the legend
 LayoutPanel.SetDock(legend, Dock.Right);
 legend.VerticalAlignment = System.Windows.VerticalAlignment.Center;
 barChart1.Legends.Add(legend);


Copyright © 2001-2024 MindFusion LLC. Alle Rechte vorbehalten.
Nutzungsbedingungen - Kontakt