Pie Chart - Dynamic Slice Styles Issue

3 posts, 0 answers
  1. Tracy
    Tracy avatar
    29 posts
    Member since:
    Sep 2010

    Posted 08 Mar 2012 Link to this post

    I have a pie chart which can display up to 20 slices.  I have to manually set the color for each slice using a converter.

    My biggest issue is that the "object value" being passed into the converter isn't the slice datapoint but is the datacontext of the entire chart. Hence, the hard-coded converter parameter (0, 1, ...) so that I know which dataPoint slice is being converted.

    So my xaml looks like this (one style per slice (20) which is BAD for maintaining):
    <chartView:PieSeries.SliceStyles>
         
        <Style TargetType="Path">
            <Setter Property="Stroke" Value="Gray"/>
            <Setter Property="StrokeThickness" Value="1"/>
            <Setter Property="Fill" Value="{Binding Converter={StaticResource pieChartSliceToColorConverter}, ConverterParameter=0}"/>
             
        </Style>
     
        <Style TargetType="Path">
            <Setter Property="Stroke" Value="Gray"/>
            <Setter Property="StrokeThickness" Value="1"/>
            <Setter Property="Fill" Value="{Binding Converter={StaticResource pieChartSliceToColorConverter}, ConverterParameter=1}"/>
        </Style>

    Is there a different style which would eliminate the number of styles I have to maintain?  OR a different approach?

    Thanks in advance.

    Tracy
  2. Sia
    Admin
    Sia avatar
    667 posts

    Posted 13 Mar 2012 Link to this post

    Hi Tracy,

    What I can suggest is to try using StyleSelector as follows:
    <telerik:RadPieChart>
       <telerik:PieSeries SliceStyleSelector="{StaticResource selector}">
            <telerik:PieSeries.DataPoints>
                   <telerik:PieDataPoint Value="20" />
             </telerik:PieSeries.DataPoints>
       </telerik:PieSeries>
    </telerik:RadPieChart>

    where the static resource is:
    <local:MySliceSelector x:Key="selector" />

    and the selector is:
    public class MySliceSelector : StyleSelector
    {
       public override System.Windows.Style SelectStyle(object item, DependencyObject container)
        {
            return base.SelectStyle(item, container);
        }
    }

    Greetings,
    Sia
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Tracy
    Tracy avatar
    29 posts
    Member since:
    Sep 2010

    Posted 15 Mar 2012 Link to this post

    Thanks for getting back to me.

    Using the style selector works really well.

    Tracy
Back to Top