使用多个数据系列隐藏 WPF Toolkit 图表的图例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3595222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide legend of WPF Toolkit chart with more than one data series
提问by sprite
I am trying to use charts from the WPF Toolkit (with LineSeries) and I don't want a legend at all. I need this since I have 10 such charts each with data from a different source and I would like to draw one legend for all 10, to save screen real estate.
我正在尝试使用 WPF Toolkit(带有 LineSeries)中的图表,我根本不需要图例。我需要这个,因为我有 10 个这样的图表,每个图表都有来自不同来源的数据,我想为所有 10 个绘制一个图例,以节省屏幕空间。
By default the legend appears the moment you add a second LineSeries. Is there any way to prevent it from even appearing?
默认情况下,图例会在您添加第二个 LineSeries 时出现。有什么办法可以防止它出现吗?
Thanks,
谢谢,
sprite.
精灵。
回答by Quartermeister
There doesn't seem to be an especially clean way. One simple approach is to set the Legend's Width to zero using LegendStyle:
似乎没有特别干净的方法。一种简单的方法是使用 LegendStyle 将 Legend 的 Width 设置为零:
<charting:Chart>
<charting:Chart.LegendStyle>
<Style TargetType="datavis:Legend">
<Setter Property="Width" Value="0" />
</Style>
</charting:Chart.LegendStyle>
A more drastic approach is to replace the ControlTemplate with one that does not include a Legend:
一种更激进的方法是用不包含 Legend 的 ControlTemplate 替换 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>
Use following namespaces:
使用以下命名空间:
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"
回答by JohnBlacker
Much more sensible approach...
更明智的做法...
<charting:LineSeries.LegendItemStyle >
<Style TargetType="{x:Type charting:LegendItem}">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</charting:LineSeries.LegendItemStyle>
Worked better for me than setting values to 0... Cheers!
对我来说比将值设置为 0 更好......干杯!
回答by mtlynch
I tried Quarermeister's approach but his has a reference to a "datavis" assembly in the TargetType attribute that I didn't have.
我尝试了 Quarermeister 的方法,但他在 TargetType 属性中引用了我没有的“datavis”程序集。
<chartingToolkit:Chart.LegendStyle>
<Style TargetType="Control">
<Setter Property="Width" Value="0" />
<Setter Property="Height" Value="0" />
</Style>
</chartingToolkit:Chart.LegendStyle>
I also had to add padding to the right side of the chart because without the legend, my x-axis interval labels were extending outside the chart area.
我还必须在图表的右侧添加填充,因为没有图例,我的 x 轴间隔标签会延伸到图表区域之外。
回答by Jake Berger
Attached Property for DRY, easy usage:
DRY的附加属性,易于使用:
<charting:Chart helpers:ChartHelpers.IsLegendHidden="True" ...
<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
}