Unable to set different slice colors of RadPieChart via Code Behind

9 posts, 1 answers
  1. Igor
    Igor avatar
    6 posts
    Member since:
    Jun 2014

    Posted 30 Jun 2014 Link to this post

    Here's the function that creates RadPieChart.

    private RadPieChart BuildPieChart(ContentModel contentData) {
        // Chart
        var chart = new RadPieChart();
     
        // Chart's Pie Series
        var pieSeries = new PieSeries {
            Name = "PieSeries",
            ShowLabels = true
        };
     
        // Building Data Points
        GetDataPoints(contentData, ContentTypeFlag.PieChartStatic).ToList().ForEach(a => {
            pieSeries.DataPoints.Add(a);
            pieSeries.SliceStyles.Add(Util.GenerateSliceStyle());
        });
     
        chart.Series.Add(pieSeries);
     
        return chart;
    }

    GetDataPoints() returns the IEnumerable<PieDataPoint>, and Util.GenerateSliceStyle() returns Style.

    The color on the slice is set, however, all the slices always end-up being the same color. 
    Is there a way of making each slice different color via Code Behind?
    Any help (a code example would be the best) would be appreciated. Thank you.

    At the moment, this is the result I get (as I mentioned, all the slices are of same color):

  2. Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 02 Jul 2014 Link to this post

    Hello Igor,

    The easiest way to get different colors is to use the Palette property of the chart. Just set it and all slices will be colored from the palette.

    You can also set different colors by using the SliceStyles property of the series, where you can set 5 different styles (with 5 different Fills) and the 5 different slices will be assigned a different style. 

    Let us know if this is what you were looking for or if you have any other questions.

    Regards,
    Petar Marchev
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Igor
    Igor avatar
    6 posts
    Member since:
    Jun 2014

    Posted 02 Jul 2014 Link to this post

    Thanks Petar, however, if you look above, for each DataPoint I have created a SliceStyle with different color.
    Previous to setting SliceStyles I tried using Palette and got the same result. 
    I confirmed that each SliceStyle had a different Setter with a different color on Fill property.
  5. Igor
    Igor avatar
    6 posts
    Member since:
    Jun 2014

    Posted 02 Jul 2014 Link to this post

    Here're methods that should have taken care of different colors on the slices (in the meantime I added a color on stroke property as well). 


    public static class Util {
     
        public static Style GenerateSliceStyle() {
            var style = new Style(typeof(Path));
            style.Setters.Add(new Setter {
                Property = Path.FillProperty,
                Value = new SolidColorBrush(GenerateColor())
            });
            style.Setters.Add(new Setter {
                Property = Path.StrokeProperty,
                Value = new SolidColorBrush(Colors.White)
            });
            return style;
        }
     
        public static Color GenerateColor() {
            var r = new Random();
            var rand = r.Next(10);
            switch (rand) {
                case 0: return Colors.Red;
                case 1: return Colors.Green;
                case 2: return Colors.Blue;
                case 3: return Colors.CadetBlue;
                case 4: return Colors.Coral;
                case 5: return Colors.DarkViolet;
                case 6: return Colors.IndianRed;
                case 7: return Colors.LightGoldenrodYellow;
                case 8: return Colors.LightPink;
                case 9: return Colors.DarkCyan;
                default: return Colors.Lime;
            }
        }
    }



  6. Answer
    Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 03 Jul 2014 Link to this post

    Hi Igor,

    Thank you for the additional information. I created a small project based on the provided code.

    Surprisingly I got the same results as you described. Upon debugging, however, I got different results. The reason for this appears to be the Random number generator mechanism you have. It seems that the Random objects are created very fast one after another and the same random number is generated. Simply make the r a static object:
    static Random r = new Random();

    Let us know how it goes.

    Regards,
    Petar Marchev
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  7. Igor
    Igor avatar
    6 posts
    Member since:
    Jun 2014

    Posted 03 Jul 2014 Link to this post

    It didn't occur to me that this could be the issue. Thank you Petar, that fixed it (see attachment). 
    I am glad this is solved as I almost gave up on using RadPieChart for my app's solution. :)
    Also, it's nice that we documented everything here for anyone else, because this is a simple but effective example  for creating a pie chart via code behind. 

    ps: I could swear that I saw an option somewhere to mark this topic as "Answered". 

    ps2: Btw, if you are as me from former Yugoslavia (I'm guessing that by the way your name is written... "Petar" and not "Peter"), I want to say: Hvala ti brate gdje cuo i gdje ne cuo! Rijesio si mi veliki problem koji me mucio na poslu kompletnu ovu nedelju dana. :)
  8. Igor
    Igor avatar
    6 posts
    Member since:
    Jun 2014

    Posted 03 Jul 2014 in reply to Igor Link to this post

    Igor said:
    ps: I could swear that I saw an option somewhere to mark this topic as "Answered". 

    Found it again. :)
  9. Igor
    Igor avatar
    6 posts
    Member since:
    Jun 2014

    Posted 03 Jul 2014 Link to this post

    As a side note, if anyone else takes this approach, I would suggest adjusting the GenerateColor() method to use Color.FromRgb(byte r, byte g, byte b) because in the prior example same colors appear way too often. 

    public static Color GenerateColor() {
        return Color.FromRgb((byte)r.Next(0, 255), (byte)r.Next(0, 255), (byte)r.Next(0, 255));
    }
  10. Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 04 Jul 2014 Link to this post

    Igor,

    We are happy that we managed to help. Thank you for your kind words : ) 

    Regards,
    Petar Marchev
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WPF is Visual Studio 2017 Ready