Click here to Skip to main content
13,552,737 members
Rate this:
Please Sign up or sign in to 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 18-Aug-11 9:22am
Updated 23-Aug-11 0:10am
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") :-)
I wanted to use the word palette. The "correction" wasn't necessary.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

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);
    return palette;
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.
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 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).
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 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

John, I know you want the solution *OUT* of xaml if possible but I think this can be of help (in xaml): Styling Charts[^]
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180515.1 | Last Updated 23 Aug 2011
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100