简体   繁体   中英

WPF Toolkit Pie Chart Style Colors

i`d like to set colors in codebehind for every Pie Slice in my PieChart. Anyone knows how to do this? Now it gets the colors from the styles xaml, but i need to assign the colors for each value (pie slice) by myself from codebehind.

Not sure if this is the best way of doing it but I struggled with the same thing and eventually got it working like this.

Declare a palette:

System.Windows.Controls.DataVisualization.ResourceDictionaryCollection pieSeriesPalette = new System.Windows.Controls.DataVisualization.ResourceDictionaryCollection();

Then create a brush (or brushes) with the color information:

Brush currentBrush = new SolidColorBrush(Color.FromRgb(128, 128, 128)); //Grey

With each brush, create a new Style and add it to the palette collection:

System.Windows.ResourceDictionary pieDataPointStyles = new ResourceDictionary();
Style stylePie = new Style(typeof(PieDataPoint));
stylePie.Setters.Add(new Setter(PieDataPoint.BackgroundProperty, currentBrush));
pieDataPointStyles.Add("DataPointStyle", stylePie);
pieSeriesPalette.Add(pieDataPointStyles);

Note that each brush (or palette entry) has to be created as a new ResourceDictionary object. If you create 10 brushes and just add them all to the same ResourceDictionary the piechart will only use the first color. (Or maybe I'm just doing something wrong).

Then set the chart palette to the custom palette:

this.yourChartsName.Palette = pieSeriesPalette;

This is only useful if you want to customise the palette to specific colors. For a color range or fixed list of colors I believe you can just definie the palette in XAML. Hope it helps.

One possiblitiy is to construct your colours from HSL or HSV , keeping the SL/SV part fixed you can divide the entire hue range into the number of pie slices you have, this should ensure they are reasonably visually seperated. different SL/SV values will make the chart vibrant colours or pastels or dark shades etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM