buy now
log in
MindFusion

Q: How can I replace the standard legend that shows titles of the series in my chart with two legends at the two sides of the chart?

A: You have two ways to do this. One is to create to separate lists with ChartSeries – to be used as data for the two separate legends. Here is your code:

 var firstSeries = new ChartSeriesCollection() { series0, series1 };
 var secondSeries = new ChartSeriesCollection() { series2, series3 };
 ChartLegend legend1 = new ChartLegend();
 legend1.Series = firstSeries;
 LayoutPanel.SetDock(legend1, Dock.Right);
 lineChart1.Legends.Add(legend1);

 ChartLegend legend2 = new ChartLegend();
 legend2.Series = secondSeries;
 LayoutPanel.SetDock(legend2, Dock.Left);
 lineChart1.Legends.Add(legend2);

The second option is to create two SeriesLegend objects for which you set the labels and brushes to the titles and strokes of your series. Here is how you do it:

 
 SeriesLegend legend1 = new SeriesLegend();
 legend1.LabelsSource = new List(){series0.Title, series1.Title};
 legend1.BrushesSource = new List() { series0.Fills[0], series1.Fills[0] };
 LayoutPanel.SetDock(legend1, Dock.Right);
 lineChart1.Legends.Add(legend1);

 SeriesLegend legend2 = new SeriesLegend();
 legend2.LabelsSource = new List() { series2.Title, series3.Title };
 legend2.BrushesSource = new List() { series2.Fills[0], series3.Fills[0] };
 LayoutPanel.SetDock(legend2, Dock.Left);
 lineChart1.Legends.Add(legend2); 


Here we assume the ChartSeries have their Title property set, and have at least one Brush added to their Fills list:

 LineSeries series1 = new LineSeries();
 series1.XData = new List() { 4, 7, 12, 16 };
 series1.YData = new List() { 45, 62, 18, 32 };
 series1.Fills.Add(Brushes.Orange);
 series1.Title = "Tokyo";
 lineChart1.Series.Add(series1);

Copyright © 2001-2024 MindFusion LLC. All rights reserved.
Terms of Use - Contact Us