【问题标题】:Hide legend of WPF Toolkit chart with more than one data series隐藏具有多个数据系列的 WPF Toolkit 图表的图例
【发布时间】:2010-08-29 14:59:26
【问题描述】:

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

默认情况下,图例会在您添加第二个 LineSeries 时出现。有什么办法不让它出现吗?

谢谢,

精灵。

【问题讨论】:

  • 我使用的是 2010 年 2 月发布的 WPF 工具包。

标签: wpf charts customization wpftoolkit


【解决方案1】:

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

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

【讨论】:

  • 感谢 Quarermeister。我采取了第二种方法。我只是自己来发布答案,但你省了我的麻烦。我还使用它来最小化绘图区域和标题周围的边距,这样我就可以在尽可能小的空间内将所有图表堆叠在一起。
  • 嘿。您能否澄清一下:什么是 datavis 命名空间?
【解决方案2】:

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

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

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

【讨论】:

  • 你有,你只需要添加命名空间: xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
【解决方案3】:

更明智的方法...

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

对我来说比将值设置为 0 更好... 干杯!

【讨论】:

  • 我同意这似乎是一种更明智的方法,但我遇到了一些问题。首先,上述方法仅折叠项目,因此仍会显示图例框。 (通过将样式应用于图表来修复,请注意 Legend 类与图表位于不同的命名空间中)。然后我遇到了第二个问题:Collapsed 似乎在设计模式下立即工作,但在刷新(例如构建项目)之后,当运行应用程序时,图例会显示出来。因此最后我也不得不添加一个 width=0 设置器:(
【解决方案4】:

DRY的附加属性,使用方便:

&lt;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
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多