我想从WPF工具包(使用LineSeries)使用图表,我根本不需要图例。我需要这个,因为我有10个这样的图表,每个都有来自不同来源的数据,我想为所有10个图画一个图例,以节省屏幕的空间。隐藏多个数据系列的WPF工具包图表的图例
默认情况下,图例在添加第二个LineSeries时出现。有什么办法可以防止它出现?
谢谢,
雪碧。
我想从WPF工具包(使用LineSeries)使用图表,我根本不需要图例。我需要这个,因为我有10个这样的图表,每个都有来自不同来源的数据,我想为所有10个图画一个图例,以节省屏幕的空间。隐藏多个数据系列的WPF工具包图表的图例
默认情况下,图例在添加第二个LineSeries时出现。有什么办法可以防止它出现?
谢谢,
雪碧。
似乎没有一种特别干净的方法。一个简单的方法是设置Legend的宽度用零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"
感谢Quarermeister。我采取了第二种方法。我只是自己发表了答案,但是你救了我麻烦。我还用它将绘图区域周围的边缘和标题减至最小,以便尽可能将所有图表放在一起。 – sprite 2010-08-30 07:52:44
嘿。你能否澄清一下:什么是datavis命名空间? – 2012-04-16 08:06:06
我试图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” – codekaizen 2011-09-16 07:59:26
更明智的做法......
<charting:LineSeries.LegendItemStyle >
<Style TargetType="{x:Type charting:LegendItem}">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</charting:LineSeries.LegendItemStyle>
对我来说比设定值更好地工作至0 ... 干杯!
我同意这看起来像一个更明智的方法,但我有几个问题。首先,上述方法只会折叠这些项目,所以Legend框仍然显示出来。 (通过将样式应用于图表来修复,请注意Legend类位于与图表不同的名称空间中)。然后我碰到了第二个问题:Collapsed似乎在设计模式下立即工作,但在刷新后(例如构建项目)以及运行应用程序时,图例显示出来。因此最后我不得不添加一个宽度= 0的setter也:( – Ben 2014-12-28 13:57:53
附加属性的干燥,容易使用:
<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
}
我使用WPF工具包的2010年2月发布。 – sprite 2010-08-29 15:01:35