Hide legend of WPF Toolkit chart with more than one data series

sprite picture sprite · Aug 29, 2010 · Viewed 24.6k times · Source

I am trying to use charts from the WPF Toolkit (with LineSeries) and I don't want a legend at all. I need this since I have 10 such charts each with data from a different source and I would like to draw one legend for all 10, to save screen real estate.

By default the legend appears the moment you add a second LineSeries. Is there any way to prevent it from even appearing?

Thanks,

sprite.

Answer

Quartermeister picture Quartermeister · Aug 29, 2010

There doesn't seem to be an especially clean way. One simple approach is to set the Legend's Width to zero using LegendStyle:

<charting:Chart>
    <charting:Chart.LegendStyle>
        <Style TargetType="datavis:Legend">
            <Setter Property="Width" Value="0" />
        </Style>
    </charting:Chart.LegendStyle>

A more drastic approach is to replace the ControlTemplate with one that does not include a Legend:

<charting:Chart>
    <charting:Chart.Template>
        <ControlTemplate TargetType="{x:Type charting:Chart}">
            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <datavis:Title Content="{TemplateBinding Title}" Style="{TemplateBinding TitleStyle}" />
                    <chartingprimitives:EdgePanel Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}" Grid.Row="1" Margin="0,15,0,15">
                        <Grid Panel.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
                        <Border Panel.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />
                    </chartingprimitives:EdgePanel>
                </Grid>
            </Border>
        </ControlTemplate>
    </charting:Chart.Template>

Use following namespaces:

xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:chartingprimitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit"