WPF Toolkit图表中有多个数据系列,如何隐藏图例?

21

我正在尝试使用WPF Toolkit中的图表(使用LineSeries),并且我完全不需要图例。我需要这样做是因为我有10个这样的图表,每个图表都来自不同的数据源,我希望为所有10个图表绘制一个图例,以节省屏幕空间。

默认情况下,只要添加第二个LineSeries,图例就会出现。是否有任何方法可以防止它甚至出现?

谢谢,

sprite.


我正在使用WPF Toolkit的2010年2月版本。 - sprite
4个回答

50

似乎没有特别简单的方法。一个简单的方法是使用LegendStyle将图例的宽度设置为零:

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

更激进的方法是用一个不包含图例的ControlTemplate来替换它:

<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
谢谢Quarermeister。我采用了第二种方法。我本来要自己发布答案,但你帮我省去了麻烦。我还利用这个方法将图表区域和标题周围的边距最小化,以便在尽可能少的空间中堆叠所有图表。 - sprite
3
可以的,datavis是数据可视化的命名空间。 - Arsen Zahray

12
我尝试了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
你已经有它了,你只需要添加命名空间:xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"。 - codekaizen

10

更为明智的做法是...

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

这对我比将值设置为0更有效...

Cheers!


1
我同意这似乎是一个更明智的方法,但我有几个问题。首先,上述方法仅折叠项目,因此图例框仍会显示出来。(通过将样式应用于图表来修复,注意Legend类位于与图表不同的命名空间中)。然后我遇到了第二个问题:Collapsed在设计模式下立即起作用,但在刷新(例如构建项目)和运行应用程序后,图例会显示出来。因此最终我不得不添加width=0 setter :( - Ben

7

为DRY提供的附加属性,便于使用:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接