SciChart WPF 2D Charts > ChartModifier API > Cursors, Tooltips and legends > LegendModifier

SciChart features a rich, customisable legend API which is based on our powerful ChartModifier API. To add a Legend to a chart, you need two elements – a LegendModifier – which is the datasource, and a SciChartLegend, or ItemsControl, to show the data.

SciChart legends support the following:

  • Placement of legend Top, Left, Right, Bottom, Inside the chart
  • Auto-creation of Legend UI and binding to Legend Data Source via the LegendModifier.
  • Horizontal or Vertical orientation of Legends
  • Showing SeriesName, Line Color/Marker, Visibility Checkboxes
  • Templating of Legend Items to allow full customization of the legend including adding color-pickers or other controls
  • Placing legends anywhere in your application

Showing a Legend with ShowLegend=True

The simplest way to declare a legend is to declare a LegendModifier and set LegendModifier.ShowLegend=True. This will autocreate a SciChartLegend control and place it inside the chart automatically.


Showing a Legend with ShowLegend=True
Copy Code

   <!-- XAxis, YAxis, RenderableSeries omitted for brevity -->

   <!--  Declare ChartModifiers  -->
         <s:LegendModifier ShowLegend="True" Orientation="Horizontal" Margin="10"
                           LegendPlacement="Inside" GetLegendDataFor="AllSeries"



Showing a Legend with ShowLegend=True
Copy Code
var sciChartSurface = new SciChartSurface();
sciChartSurface.ChartModifier = new ModifierGroup(new LegendModifier()
       ShowLegend = true,
       Orientation = Orientation.Horizontal,
       Margin = new Thickness(10, 10, 10, 10),
       LegendPlacement = LegendPlacement.Inside,
       GetLegendDataFor = SourceMode.AllSeries,
       ShowVisibilityCheckboxes = true,

Excluding a Single Series from the LegendModifier

You can exclude a series from the LegendModifier by setting the LegendModifier.IncludeSeries property on the LegendModifier:

Include Series in XAML
Copy Code
      <s:FastLineRenderableSeries s:LegendModifier.IncludeSeries="False"/>
Include Series in Code
Copy Code
var sciChartSurface = new SciChartSurface();
var lineSeries = new FastLineRenderableSeries();

LegendModifier.SetIncludeSeries(lineSeries, false);

Placing the Auto-Generated Legend Inside, Outside the SciChartSurface

When using auto-generated legends (when LegendModifier.ShowLegend = true), you can place the legend Inside, Outside, Left, Top, Right, Bottom on the SciChartSurface by using the LegendModifier.LegendPlacement property. Note that the legend placement can also be adjusted with the LegendModifier.Margin property.

Showing only Visible or only Selected series in the Auto-Generated Legend

You can change which series the Legend displays data for by using the LegendModifier.GetLegendDataFor property.

Showing a Horizontal Auto-Generated Legend

You can change the orientation of the legend from Vertical to Horizontal by using the LegendModifier.Orientation property.

Showing or Hiding the Visibility Checkboxes in an Auto-Generated Legend

Visibility Checkboxes can be hidden in the SciChartLegend control by setting LegendModifier.ShowVisibilityCheckboxes=False.

Showing or Hiding Scrollbars in the Legend Control

With the following code you can show or hide Scrollbars in the legend

<s:LegendModifier ShowLegend="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"/>

Templating the Legend Item Rows

You may template the Legend Item Rows by using the LegendModifier.LegendItemTemplate property. This accepts a simple DataTemplate. The binding for each Legend Item Row is to type SeriesInfo. You can see an example of using LegendItemTemplate below:

Templating the Legend Item Rows
Copy Code
<!-- May be set to the LegendModifier.LegendItemTemplate property -->
<!-- Allows per legend item styling. For instance, this template includes the VisibilityCheckbox, point marker and series name textblock-->
<!-- Each item is bound to a SeriesInfo. If you look at our docs on SeriesInfo you will see all the possibilities, including direct access to SeriesInfo.RenderableSeries -->
<DataTemplate x:Key="SciChartLegendItemTemplate" DataType="s:SeriesInfo">
         <ColumnDefinition Width="Auto" />
         <ColumnDefinition Width="Auto" />
         <ColumnDefinition Width="Auto" />

      <CheckBox Width="16" Margin="5,0,0,0"
         IsChecked="{Binding RenderableSeries.IsVisible, Mode=TwoWay}"
         Visibility="{Binding LegendData.ShowVisibilityCheckboxes, RelativeSource={RelativeSource AncestorType=visuals:SciChartLegend}, Converter={StaticResource BooleanToVisibilityConverter}}" />

      <r:PointMarker Grid.Column="1" Margin="5,0,0,0" Width="40" Height="10" VerticalAlignment="Center" HorizontalAlignment="Center"
                  DataContext="{Binding RenderableSeries}"
                  DeferredContent="{Binding LegendMarkerTemplate}"
                  Visibility="{Binding ShowSeriesMarkers, RelativeSource={RelativeSource AncestorType=visuals:SciChartLegend}, Converter={StaticResource BooleanToVisibilityConverter}}" />

      <TextBlock Margin="5,0,5,0"
         Text="{Binding SeriesName}" />


Templating the SciChartLegend Control

When the legend is AutoGenerated, you cannot access the legend instance. But you can specify how it is created. Use the following code to change how the auto-generated legend is instantiated.

Templating the SciChartLegend Control
Copy Code
<s:LegendModifier x:Name="legendModifier" ShowLegend="True" Orientation="Horizontal" Margin="10" >
         <!-- Change the AutoGenerated SciChartLegend -->
         <s:SciChartLegend ItemTemplate="{Binding LegendItemTemplate}"
            LegendData="{Binding LegendData}"
            Orientation="{Binding Orientation}"
            ScrollViewer.HorizontalScrollBarVisibility="{Binding Path=(ScrollViewer.HorizontalScrollBarVisibility)}"
            ScrollViewer.VerticalScrollBarVisibility="{Binding Path=(ScrollViewer.VerticalScrollBarVisibility)}"
            ShowSeriesMarkers="{Binding ShowSeriesMarkers}"
            ShowVisibilityCheckboxes="{Binding ShowVisibilityCheckboxes}" />

Alternatively – Binding LegendModifier.LegendData to SciChartLegend

You can alternatively bind LegendModifier.LegendData to a SciChartLegend and place anywhere in your application. You can also bind to an LegendModifier.LegendData to an ItemsControl.ItemsSource and template it as you wish.

Alternatively – Binding LegendModifier.LegendData to SciChartLegend
Copy Code
<s:SciChartSurface x:Name="sciChart" s:ThemeManager.Theme="Chrome">

    <!-- XAxis, YAxis, Omitted for brevity -->

    <!--  Declare ChartModifiers  -->
            <s:LegendModifier x:Name="legendModifier" GetLegendDataFor="AllSeries" ShowLegend="False"/>


<!-- Somewhere else in your application, you can declare a SciChartLegend and bind to LegendModifier.LegendData -->
<s:SciChartLegend x:Name="legendControl"
   LegendData="{Binding LegendData, ElementName=legendModifier, Mode=OneWay}"
   ShowVisibilityCheckboxes="True" />

The ControlTemplate for the SciChartLegend control

The auto-generated SciChartLegend control is simply an ItemsControl. The default Control Template for the SciChartLegend is included below:

The ControlTemplate for the SciChartLegend control
Copy Code
<ControlTemplate TargetType="s:SciChartLegend">
      HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
      VerticalAlignment="{TemplateBinding VerticalAlignment}"
      Background="{TemplateBinding Background}"
      BorderBrush="{TemplateBinding BorderBrush}"
      BorderThickness="{TemplateBinding BorderThickness}"
      Padding="{TemplateBinding Padding}">
      <ScrollViewer Common:CompatibleFocus.IsFocusable="False" HorizontalScrollBarVisibility="{Binding (ScrollViewer.HorizontalScrollBarVisibility), RelativeSource={RelativeSource TemplatedParent}}" VerticalScrollBarVisibility="{Binding (ScrollViewer.VerticalScrollBarVisibility), RelativeSource={RelativeSource TemplatedParent}}">
         <ItemsPresenter />