2010-08-29 29 views
19

我想從WPF工具包(使用LineSeries)使用圖表,我根本不需要圖例。我需要這個,因爲我有10個這樣的圖表,每個都有來自不同來源的數據,我想爲所有10個圖畫一個圖例,以節省屏幕的空間。隱藏多個數據系列的WPF工具包圖表的圖例

默認情況下,圖例在添加第二個LineSeries時出現。有什麼辦法可以防止它出現?

謝謝,

雪碧。

+0

我使用WPF工具包的2010年2月發佈。 – sprite 2010-08-29 15:01:35

回答

43

似乎沒有一種特別乾淨的方法。一個簡單的方法是設置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" 
+1

感謝Quarermeister。我採取了第二種方法。我只是自己發表了答案,但是你救了我麻煩。我還用它將繪圖區域周圍的邊緣和標題減至最小,以便儘可能將所有圖表放在一起。 – sprite 2010-08-30 07:52:44

+3

嘿。你能否澄清一下:什麼是datavis命名空間? – 2012-04-16 08:06:06

8

我試圖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 2011-09-16 07:59:26

9

更明智的做法......

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

對我來說比設定值更好地工作至0 ... 乾杯!

+1

我同意這看起來像一個更明智的方法,但我有幾個問題。首先,上述方法只會摺疊這些項目,所以Legend框仍然顯示出來。 (通過將樣式應用於圖表來修復,請注意Legend類位於與圖表不同的名稱空間中)。然後我碰到了第二個問題:Collapsed似乎在設計模式下立即工作,但在刷新後(例如構建項目)以及運行應用程序時,圖例顯示出來。因此最後我不得不添加一個寬度= 0的setter也:( – Ben 2014-12-28 13:57:53

5

附加屬性的乾燥,容易使用:

<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 
    } 
相關問題