隐藏包含多个数据系列的WPF Toolkit图表的图例

时间:2010-08-29 14:59:26

标签: wpf charts customization wpftoolkit

我正在尝试使用WPF工具包中的图表(使用LineSeries),我根本不需要传奇。我需要这个,因为我有10个这样的图表,每个都有来自不同来源的数据,我想为所有10个绘制一个图例,以节省屏幕空间。

默认情况下,图例会在您添加第二个LineSeries时出现。有没有办法阻止它出现?

谢谢,

子画面。

4 个答案:

答案 0 :(得分:46)

似乎没有特别干净的方式。一种简单的方法是使用LegendStyle将Legend的宽度设置为零:

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

更激进的方法是将ControlTemplate替换为不包含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>

使用以下命名空间:

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"

答案 1 :(得分:10)

更明智的方法......

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

比将值设置为0更适合我... 干杯!

答案 2 :(得分:9)

我尝试过Quarermeister的方法,但他在TargetType属性中引用了一个我没有的“datavis”程序集。

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

我还必须在图表的右侧添加填充,因为没有图例,我的x轴间隔标签在图表区域之外延伸。

答案 3 :(得分:5)

干燥附属物,易于使用:

<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
    }