Set Color of RadChartItem by its Value

2 posts, 0 answers
  1. Bhaskar Khaund
    Bhaskar Khaund avatar
    12 posts
    Member since:
    May 2010

    Posted 29 Nov 2010 Link to this post


    I am using a RadChart in my silvelight application, and I am using a 2-d pie-chart. I need to set the color of the item based in its value.

    My current code is:


    var series = new DataSeries();
    var myDataSource = <my data source>;
    var pointCount = 0;
    DataPoint point = null;
    RiskChartArea.PaletteBrushes.Add(new SolidBrushColor(Colors.Red));
    RiskChartArea.PaletteBrushes.Add(new SolidBrushColor(Colors.Blue));
    RiskChartArea.PaletteBrushes.Add(new SolidBrushColor(Colors.Green));
    RiskChartArea.PaletteBrushes.Add(new SolidBrushColor(Colors.Black));
    RiskChartArea.PaletteBrushes.Add(new SolidBrushColor(Colors.Yellow));
    myCollection.ToList().ForEach(x =>
                    pointCount = openItem.Where(y => y["Status"].GetString().Equals(x.Code, StringComparison.CurrentCultureIgnoreCase)).Count();
                    point = new DataPoint(pointCount);
                    point.LegendLabel = x.Code;
                    point.Tooltip = x.Code;
    DataSeries doughnutSeries = series;
    doughnutSeries.LegendLabel = "Doughnut Series";
    doughnutSeries.Definition = new DoughnutSeriesDefinition();
    doughnutSeries.Definition.InteractivitySettings.HoverScope = InteractivityScope.Item;
    doughnutSeries.Definition.InteractivitySettings.SelectionScope = InteractivityScope.Item;
    doughnutSeries.Definition.InteractivitySettings.SelectionMode = ChartSelectionMode.Single;
    ((DoughnutSeriesDefinition)doughnutSeries.Definition).LabelSettings.LabelOffset = 0.7d;
    //doughnutSeries.Definition.ItemLabelFormat = "#%{p0}";
    MyChartArea.SmartLabelsEnabled = false;
    MyChartLegend.Header = " ";

    What I want is, if the value of the DataPoint is "ABC", I wnat its slice red in color, and if its valye is "XYZ" I wnat its slice to be green in color.

    Can you please help me, how can I acheive this?

  2. Ves
    Ves avatar
    2927 posts

    Posted 01 Dec 2010 Link to this post

    Hi Bhaskar Khaund,

    You can find a similar example here. While the example does not specifically address the requirement for a pie chart, it demonstrates how to achieve conditional fill settings for each item within the chart.

    Here is an example of a pie slice template:

    <ControlTemplate TargetType="telerikCharting:Pie">
        <Canvas x:Name="PART_MainContainer">
            <Ellipse Clip="{TemplateBinding FigurePath}"
                 Width="{TemplateBinding ItemActualWidth}"
             Height="{TemplateBinding ItemActualHeight}"
             Style="{TemplateBinding ItemStyle}" />
            <Path x:Name="PART_DefiningGeometry"
                Data="{TemplateBinding FigurePath2}"
            Style="{TemplateBinding ItemStyle}" />
        <Ellipse Clip="{TemplateBinding FigurePath3}"
             Fill="{Binding DataItem.MyColor}"
             Width="{TemplateBinding ItemActualWidth}"
             Height="{TemplateBinding ItemActualHeight}" />

    Best regards,
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
Back to Top