This is a migrated thread and some comments may be shown as answers.

Unable to set different slice colors of RadPieChart via Code Behind

8 Answers 202 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Igor
Top achievements
Rank 1
Igor asked on 30 Jun 2014, 07:53 PM
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):

8 Answers, 1 is accepted

Sort by
0
Petar Marchev
Telerik team
answered on 02 Jul 2014, 10:05 AM
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.
 
0
Igor
Top achievements
Rank 1
answered on 02 Jul 2014, 01:44 PM
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.
0
Igor
Top achievements
Rank 1
answered on 02 Jul 2014, 01:47 PM
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;
        }
    }
}



0
Accepted
Petar Marchev
Telerik team
answered on 03 Jul 2014, 07:01 AM
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.
 
0
Igor
Top achievements
Rank 1
answered on 03 Jul 2014, 05:53 PM
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. :)
0
Igor
Top achievements
Rank 1
answered on 03 Jul 2014, 05:55 PM
[quote]Igor said:
ps: I could swear that I saw an option somewhere to mark this topic as "Answered". 
[/quote]
Found it again. :)
0
Igor
Top achievements
Rank 1
answered on 03 Jul 2014, 06:14 PM
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));
}
0
Petar Marchev
Telerik team
answered on 04 Jul 2014, 07:11 AM
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.
 
Tags
ChartView
Asked by
Igor
Top achievements
Rank 1
Answers by
Petar Marchev
Telerik team
Igor
Top achievements
Rank 1
Share this question
or