Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm using the Data Visualization stuff in the WPF Toolkit to create a pie chart. I've got the chart displaying, but I haven't found a way to set the colors of the slices to my own color palette (as opposed to the default one used by the library). Does anyone have any clues as to how I can go about doing it?

I have my data in one collection, and a colour palette in a dictionary. The dictionary is keyed by an enumerator that each item in the data collection has.

I would like to keep it *OUT* of the Xaml if possible.
Posted
Updated 23-Aug-11 0:10am
v3
Comments
Smithers-Jones 18-Aug-11 15:31pm    
Sorry John, but I couldn't resist to do (three) minor changes (one typo, one missing bracket and another "correction") :-)
#realJSOP 18-Aug-11 17:14pm    
I wanted to use the word palette. The "correction" wasn't necessary.

This was the solution. I needed to set the colors based on a sorted list of values, and the colors had to match a specific "category" in my dataset (a List). I think they succeeded in making this as obscure as possible. There is no way to set the color of a pie slice directly - it MUST be done via the Chart.Palette property. If you want to use something other than the standard you must CREATE and populate your own object. Since I didn't know ahead of time the order in which a given category was going to be added to the chart, I had to write a method that would determine the order and add colors accordingly.

C#
private System.Collections.ObjectModel.Collection<ResourceDictionary> MakePalette(List<RepItem> list)
{
    System.Collections.ObjectModel.Collection<ResourceDictionary> palette = new System.Collections.ObjectModel.Collection<ResourceDictionary>();
    foreach (RepItem item in list)
    {
        ResourceDictionary rd = new ResourceDictionary();
        Style style = new Style(typeof(Control));
        SolidColorBrush brush = null;
        switch (item.Category)
        {
            case RepCategory.Author       : brush = new SolidColorBrush(Colors.DarkRed);   break;
            case RepCategory.Authority    : brush = new SolidColorBrush(Colors.DarkGreen); break;
            case RepCategory.Debator      : brush = new SolidColorBrush(Colors.Magenta);   break;
            case RepCategory.Editor       : brush = new SolidColorBrush(Colors.Goldenrod); break;
            case RepCategory.Enquirer     : brush = new SolidColorBrush(Colors.Purple);    break;
            case RepCategory.Organiser    : brush = new SolidColorBrush(Colors.Olive);     break;
            case RepCategory.Participant  : brush = new SolidColorBrush(Colors.LightSteelBlue); break;
        }
        style.Setters.Add(new Setter() { Property = Control.BackgroundProperty, Value = brush });
        rd.Add("DataPointStyle", style);
        palette.Add(rd);
    }
    return palette;
}
 
Share this answer
 
v2
Comments
BobJanova 22-Aug-11 12:02pm    
Are you sure? In the WinForms version you can set the pie slice colour by setting the Color property on the data point. That does mean that you have to run around after data binding in order to get the DataPoint objects and match them up with list items, so whether it's better than what you did is a matter of taste.
#realJSOP 23-Aug-11 6:09am    
Yeah - I'm sure. I've already done a WinForms version of the app, and you're right - it's a lot less hassle than in WPF.
Oludayo Alli 22-Aug-11 13:23pm    
JSOP, Take +5. There's one thing i want to ask: You've made a palette and the call to this method returns that, now how do you link this palette to the pie(though I've engaged my brain with it but I'm still curious).
#realJSOP 22-Aug-11 13:30pm    
Just assign the returned ResourceDictionary collection to the chart's Palette property. The BIG deal (in my case) is that the palette entries have to be in the same order as the data points that the colors are associated with. If I wasn't using the Codeproject reputation category colors, it wouldn't have been an issue, and I wouldn't have asked the question in the first place.
Oludayo Alli 22-Aug-11 13:39pm    
Thanks, exactly as I thought. I also noticed that the BIG deal was what necessitate the 'switch' block of code.
John, I know you want the solution *OUT* of xaml if possible but I think this can be of help (in xaml): Styling Charts[^]
 
Share this answer
 
Comments
#realJSOP 21-Aug-11 16:27pm    
I already had the page bookmarked. :/ I don't want to do it XAML unless that's the ONLY way to get it done. I'm thinking I have to create a palette in the code and set the chart to use it. Would it have killed them to allow us to set the color of a series item without having to do that? This is one of the reasons I don't care at all for WPF/Silverlight.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900