English 中文(简体)
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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签