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

spr*_*ite 20 wpf charts customization wpftoolkit

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

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

谢谢,

精灵.

Qua*_*ter 46

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

<charting:Chart>
    <charting:Chart.LegendStyle>
        <Style TargetType="datavis:Legend">
            <Setter Property="Width" Value="0" />
        </Style>
    </charting:Chart.LegendStyle>
Run Code Online (Sandbox Code Playgroud)

更激进的方法是将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>
Run Code Online (Sandbox Code Playgroud)

使用以下命名空间:

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"
Run Code Online (Sandbox Code Playgroud)

  • 嘿.你能澄清一下:什么是datavis命名空间? (3认同)

Joh*_*ker 10

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

<charting:LineSeries.LegendItemStyle >
  <Style TargetType="{x:Type charting:LegendItem}">
     <Setter Property="Visibility" Value="Collapsed"/>
  </Style>
</charting:LineSeries.LegendItemStyle>
Run Code Online (Sandbox Code Playgroud)

为我工作比将值设置为0更好......干杯!


mtl*_*nch 9

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

<chartingToolkit:Chart.LegendStyle>
    <Style TargetType="Control">
        <Setter Property="Width" Value="0" />
        <Setter Property="Height" Value="0" />
    </Style>
</chartingToolkit:Chart.LegendStyle>
Run Code Online (Sandbox Code Playgroud)

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

  • 你有它,你只需要添加命名空间:xmlns:datavis ="clr-namespace:System.Windows.Controls.DataVisualization; assembly = System.Windows.Controls.DataVisualization.Toolkit" (3认同)

Jak*_*ger 6

干燥附属物,易于使用:

<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
    }
Run Code Online (Sandbox Code Playgroud)