Thursday, August 30, 2012

Custom pie chart colors with legend in Silverlight Toolkit

Custom pie chart colors with legend in Silverlight Toolkit

Each data series in my pie chart should have a specific color that is defined in an object. Thanks to this question I've managed to achieve this, but there's a problem - legend doesn't have updated colors.

To this point I've created ControlTemplate for legend and bound Fill property to IndependentValue, just like in SliceTemplate. It looks like this:

<charting:PieSeries DependentValuePath="Value" IndependentValuePath="Key" x:Name="pieChart" ItemsSource="{Binding}">     <charting:PieSeries.Resources>         <local:ColorKeyToColorConverter x:Key="ColorKeyToColorConverter" />         <ControlTemplate x:Key="SliceTemplate" TargetType="charting:PieDataPoint">             <Path Data="{TemplateBinding Geometry}"                 Fill="{Binding RelativeSource={RelativeSource TemplatedParent},                 Path=IndependentValue,                  Converter={StaticResource ColorKeyToColorConverter} }"                 Stroke="{TemplateBinding BorderBrush}" />         </ControlTemplate>         <ControlTemplate x:Key="LegendTemplate" TargetType="charting:LegendItem">             <Border Background="{TemplateBinding Background}"                     BorderBrush="{TemplateBinding BorderBrush}"                     BorderThickness="{TemplateBinding BorderThickness}">                 <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="2,2,5,2">                     <Rectangle Width="8" Height="8"                                Fill="{Binding RelativeSource={RelativeSource AncestorType=charting:PieSeries, AncestorLevel=1},                                      Path=IndependentValue, Converter={StaticResource ColorKeyToColorConverter}}"                                Stroke="{TemplateBinding BorderBrush}"                                StrokeThickness="1" Margin="0,0,3,0" VerticalAlignment="Center" />                     <datavis:Title Content="{TemplateBinding Content}"  VerticalAlignment="Center"/>                 </StackPanel>             </Border>         </ControlTemplate>     </charting:PieSeries.Resources> 

Pie chart works fine, but unfortunately, colors in legend aren't updating and they're transparent (by default they are yellow, so template binding works, but it doesn't bind colors properly).

What's wrong then? SliceTemplate has a Path object, but I'm not sure how to use it in LegendTemplate. I don't know if it's the problem.

Any help would be appreciated.

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog