2011-11-19 46 views
1

我有一個Silverlight4圖表與一列系列。而不是顯示「系列1」的圖例,我反而希望圖例每列包含一個條目。具體來說,我希望圖例顯示列的從屬值。餅圖完全是這樣,而且正是我想要的。我曾嘗試手動設置圖例的Items屬性,但該屬性是隻讀的。我猜想編寫自己的LegendStyle是完成這個任務的最好方法,但是我對XAML相對來說比較新,所以如果有任何風格的專家能夠輕鬆做到這一點,我會永遠感激不盡。Silverlight圖例:模擬Pie系列與列系列

回答

1

可能有效的一個訣竅:通過LegendStyle使您當前的圖例不可見,並自己手動構建圖例。

您可以通過將可觀察集合與列名綁定並將其綁定到與您所需外觀匹配的控件(即列表視圖)來完成此操作。

2

好的,這是我如何解決這個問題。首先,這裏是我的簡單的類:

public class Student 
{ 
    public string Name { get; set; } 
    public int Age { get; set; } 
    public Brush FavoriteColor { get; set; } 
} 

接下來,顏色列從大衛Anson的博客這裏走來風格: http://blogs.msdn.com/b/delay/archive/2009/02/04/columns-of-a-different-color-customizing-the-appearance-of-silverlight-charts-with-re-templating-and-mvvm.aspx

使用大衛的餡餅系列傳奇的代碼作爲一個例子,我最終取得了這種風格對於聯想本身:

<Style x:Key="ColorByPreferenceLegend" TargetType="toolkit:Legend"> 
     <Setter Property="Margin" Value="15,25" /> 
     <Setter Property="VerticalAlignment" Value="Center" /> 
     <Setter Property="BorderBrush" Value="#FFDBDBDB" /> 
     <Setter Property="Background"> 
      <Setter.Value> 
       <LinearGradientBrush EndPoint="0.442,0.005" StartPoint="0.558,0.995"> 
        <GradientStop Color="#FFDBDBDB" /> 
        <GradientStop Color="#FFFFFFFF" Offset="1" /> 
       </LinearGradientBrush> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="ItemTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <Rectangle 
           Width="8" Height="8" 
           Fill="{Binding FavoriteColor}" 
           Stroke="{Binding FavoriteColor}" 
           StrokeThickness="1" Margin="3"/> 
         <TextBlock Margin="3" Text="{Binding Name}"/> 
        </StackPanel> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

而這裏的圖表標記:

<toolkit:Chart x:Name="chart" LegendStyle="{StaticResource ColorByPreferenceLegend}"> 
     <toolkit:ColumnSeries IndependentValueBinding="{Binding Name}" 
          DependentValueBinding="{Binding Age}" 
          DataPointStyle="{StaticResource ColorByPreferenceColumn}" /> 
    </toolkit:Chart> 

最後,這裏是如何獲取覆蓋圖例項目,其中_students是學生對象列表:

ColumnSeries cs = chart.Series[0] as ColumnSeries; 
    cs.ItemsSource = _students; 
    cs.LegendItems.Clear(); 
    foreach (Student s in _students) 
     cs.LegendItems.Add(s);