20

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.

sprite
  • 3,724
  • 3
  • 28
  • 30

4 Answers4

49

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"
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
Quartermeister
  • 57,579
  • 7
  • 124
  • 111
  • 1
    Thanks Quarermeister. I took the second approach. I just came to post the answer myself, but you saved me the trouble. I also used it to minimize the margins around the plot area and title to a bare minimum so I could stack all my charts together in as little space as possible. – sprite Aug 30 '10 at 07:52
  • 3
    hey. Could you clarify: what's datavis namespace? – Arsen Zahray Apr 16 '12 at 08:06
10

Much more sensible approach...

<charting:LineSeries.LegendItemStyle >
  <Style TargetType="{x:Type charting:LegendItem}">
     <Setter Property="Visibility" Value="Collapsed"/>
  </Style>
</charting:LineSeries.LegendItemStyle>

Worked better for me than setting values to 0... Cheers!

JohnBlacker
  • 173
  • 2
  • 10
  • 1
    I agree that this seems like a more sensible approach, but I had a couple of issues. First the above approach only collapses the items and so the Legend box still shows up. (Fixed by applying the style to the chart instead, note the Legend class is in a different namespace to chart). Then I ran in to the second issue: Collapsed seems to work immediately in design mode but after a refresh (eg building the project) and when running the app the legend shows up. Thus in the end I had to add a width=0 setter too :( – Ben Dec 28 '14 at 13:57
10

I tried Quarermeister's approach but his has a reference to a "datavis" assembly in the TargetType attribute that I didn't have.

<chartingToolkit:Chart.LegendStyle>
    <Style TargetType="Control">
        <Setter Property="Width" Value="0" />
        <Setter Property="Height" Value="0" />
    </Style>
</chartingToolkit:Chart.LegendStyle>

I also had to add padding to the right side of the chart because without the legend, my x-axis interval labels were extending outside the chart area.

mtlynch
  • 3,403
  • 4
  • 31
  • 31
  • 3
    You do have it, you just need to add the namespace: xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit" – codekaizen Sep 16 '11 at 07:59
7

Attached Property for DRY, easy usage:

<charting:Chart helpers:ChartHelpers.IsLegendHidden="True" ...

public static class ChartHelpers
    {
        static ChartHelpers()
        {
            HideLegendStyle = new Style(typeof(Legend));
            HideLegendStyle.Setters.Add(new Setter(Legend.WidthProperty, 0.0));
            HideLegendStyle.Setters.Add(new Setter(Legend.HeightProperty, 0.0));
            HideLegendStyle.Setters.Add(new Setter(Legend.VisibilityProperty, Visibility.Collapsed));
        }

        /// <summary>Gets a <see cref="Style"/> to hide the legend.</summary>
        public static readonly Style HideLegendStyle;

        #region IsLegendHidden

        [Category("Common")]
        [AttachedPropertyBrowsableForType(typeof(Chart))]
        public static bool GetIsLegendHidden(Chart chart)
        {
            return (bool)chart.GetValue(IsLegendHiddenProperty);
        }
        public static void SetIsLegendHidden(Chart chart, bool value)
        {
            chart.SetValue(IsLegendHiddenProperty, value);
        }

        public static readonly DependencyProperty IsLegendHiddenProperty = 
            DependencyProperty.RegisterAttached(
                "IsLegendHidden",
                typeof(bool), // type
                typeof(ChartHelpers), // containing static class
                new PropertyMetadata(default(bool), OnIsLegendHiddenChanged)
                );

        private static void OnIsLegendHiddenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            OnIsLegendHiddenChanged((Chart)d, (bool)e.NewValue);
        }
        private static void OnIsLegendHiddenChanged(Chart chart, bool isHidden)
        {
            if (isHidden)
            {
                chart.LegendStyle = HideLegendStyle;
            }
        }

        #endregion IsLegendHidden
    }
Jake Berger
  • 5,237
  • 1
  • 28
  • 22