简体   繁体   English

WPF工具包饼图样式颜色

[英]WPF Toolkit Pie Chart Style Colors

i`d like to set colors in codebehind for every Pie Slice in my PieChart. 我想为PieChart中的每个Pie Slice在代码背后设置颜色。 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. 现在它从样式xaml获取颜色,但是我需要自己从代码隐藏中为每个值(饼图切片)分配颜色。

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. 请注意,必须将每个画笔(或调色板条目)创建为新的ResourceDictionary对象。 If you create 10 brushes and just add them all to the same ResourceDictionary the piechart will only use the first color. 如果创建10个画笔并将它们全部添加到同一ResourceDictionary中,则该饼图将仅使用第一种颜色。 (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. 对于颜色范围或固定的颜色列表,我相信您可以在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. 一种可能性是用HSL或HSV构造颜色,保持SL / SV部分固定,可以将整个色相范围划分为饼图的数量,这应确保它们在视觉上合理地分开。 different SL/SV values will make the chart vibrant colours or pastels or dark shades etc. 不同的SL / SV值将使图表具有鲜明的色彩,粉彩或深色阴影等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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