MindFusion.Wpf Pack Programmer's Guide
Legend

1. There are no limits as to the count of legend you can add to the chart. Charting for WPF supports two types of legends.

1.1 ChartLegend takes its labels from the Title property of the ChartSeries it refers to. The brushes for the rectangles that are drawn before each label are taken from the Fills property of the ChartSeries. The series are set with the Series property.

Here is a sample code that creates a ChartLegend:

C#  Copy Code

ChartLegend legend = new ChartLegend();
legend.Series = barChart1.Series;
legend.BorderBrush = Brushes.LightGray;
legend.Background = Brushes.White;
barChart1.Legends.Add(legend);

VB.NET  Copy Code

Dim legend As New ChartLegend()
legend.Series = barChart1.Series
legend.BorderBrush = Brushes.LightGray
legend.Background = Brushes.White
barChart1.Legends.Add(legend)

1.2 SeriesLegend is designed to give details about data in a single ChartSeries. Its labels and brushes are set with LabelsSource and BrushesSource properties.

Here is a sample code that creates a SeriesLegend with two brushes and two labels:

C#  Copy Code

SeriesLegend legend1 = new SeriesLegend();
legend1.BrushesSource = new List<Brush>() {
    new SolidColorBrush(Color.FromArgb(100, 0, 255, 0)),
    Brushes.Yellow };
legend1.BorderBrush = Brushes.Silver;
legend1.Background = new SolidColorBrush(Color.FromRgb(240, 240, 240));
legend1.LabelsSource = new List<string>() { "Arina LLC", "Falderia Corp." };
lineChart1.Legends.Add(legend1);

VB.NET  Copy Code

Dim legend1 As New SeriesLegend()
legend1.BrushesSource = New List(Of Brush)() From { _
    New SolidColorBrush(Color.FromArgb(100, 0, 255, 0)), _
    Brushes.Yellow _
}
legend1.BorderBrush = Brushes.Silver
legend1.Background = New SolidColorBrush(Color.FromRgb(240, 240, 240))
legend1.LabelsSource = New List(Of String)() From { _
    "Arina LLC", _
    "Falderia Corp." _
}
lineChart1.Legends.Add(legend1)

2. Both types of legend descend from the Legend class and expose its style properties:

3. You can place the legend anywhere around the plot area where the chart is drawn. The docking of the legend is set with the Dock attached property of the LayoutPanel, which contains both the chart and the legend. The alignment of the legend depends on its position: if its docked to the bottom or the top of the plot area, the alignment is set with the HorizontalAlignment. If the legend is placed at the left or right side of the plot area, its alignment is set with VerticalAlignment.

Here is sample code that places a legend to the right of the plot area and aligns it in the middle:

C#  Copy Code

LayoutPanel.SetDock(legend, Dock.Right);
legend.VerticalAlignment = VerticalAlignment.Top;

VB.NET  Copy Code

LayoutPanel.SetDock(legend, Dock.Right)
legend.VerticalAlignment = VerticalAlignment.Top

Diagram of the Legend classes.