Silverlight工具包;饼图颜色 [英] Silverlight Toolkit ; Pie Chart Colors

查看:159
本文介绍了Silverlight工具包;饼图颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想不出一个巨大的问题。比方说,我有五个不同的水果,我想每个人在某个特定颜色相关联。比方说,我有三个篮子,它包含零个或多个所述果实。



当我做饼图我的三个篮子,每个按钮都只是一些随机颜色即推测由控制拾取。我怎么会说,让蓝莓蓝,黄bannanas等图表中?



我知道这是一个奇怪的问题,但我不能想到一个更简单的方法来描述它。



如果该解决方案是在XAML或代码,就像只要它的作品我不在乎。


解决方案

我已经找到了解决办法,很多脏话和调查后。有很多在这里的运动部件,所以我要保持每个尽量简短,同时还传达的要点。



首先我有对象的集合我试图跟踪:

 公共类PileOfFruit 
{
公共字符串名称{ ;组; }
公众诠释计数{搞定;组; }
}

在我的视图模型,我有这些对象的集合。

 公共类BasketVM 
{

私人的ObservableCollection< PileOfFruit> _FruitBasket =新的ObservableCollection< PileOfFruit>();
公众的ObservableCollection< PileOfFruit>水果篮
{
{返回_FruitBasket; }
}

}

在我看来,我将定义饼图将显示在篮下成果的分位数。这里最重要的因素就是模板绑定为 SliceTemplate



<预类=郎咸平的XML prettyprint-覆盖> < ; chartingToolkit:图表X:NAME =ExampleChart>
< chartingToolkit:PieSeries将的ItemsSource = {结合水果篮
DependentValueBinding ={结合计数}
IndependentValueBinding ={绑定名称}>
< chartingToolkit:PieSeries.Palette>
< visualizationToolkit:ResourceDictionaryCollection>
<&ResourceDictionary中GT;
<风格X:键=DataPointStyle的TargetType =控制>
< setter属性=模板VALUE ={StaticResource的SliceTemplate}/>
< /样式和GT;

现在在App.xaml中,或者我们可以在模板的PieDataSeries对象拖放其他一些地方。在狠抓值填写这是必然的独立价值,这将是这样的'香蕉'葡萄'或什么的。这又传递给值转换器



<预类=郎咸平的XML prettyprint-覆盖> <转换器:FruitToColorConverter X:关键=FruitToColorConverter/>

<控件模板X:键=SliceTemplate的TargetType =图:PieDataPoint>
<路径数据={TemplateBinding几何}
填充={绑定的RelativeSource = {的RelativeSource TemplatedParent},路径= IndependentValue,转换器= {StaticResource的FruitToColorConverter}}
行程= {TemplateBinding BorderBrush}>
....



相关联的数据转换器是什么将最终设置颜色,我们想。因此,如果我们做这样的事......

 公共类FruitToColorConverter:的IValueConverter 
{
私人的SolidColorBrush默认值=新的SolidColorBrush(Colors.Black);

公共对象转换(对象的值,类型TARGETTYPE,对象参数,System.Globalization.CultureInfo文化)
{
如果(价值== NULL ||(!(价值字符串))){返回默认值; }

开关(值作为字符串)
{
案苹果:
返回新的SolidColorBrush(Colors.Red);
案蓝莓:
返回新的SolidColorBrush(Colors.Blue);
默认:
返回默认值;
......



这就是你如何得到正确的颜色每每次。如果我留下什么东西,或应该澄清,请让我知道这样我就可以进行更正。有很多在这里的运动部件.. = P


I have a huge problem that I can't figure out. Let's say that I have five different fruits, and I want each of them to be associated with a certain color. Let's say that I have three "baskets" which contain zero or more of said fruits.

When I Make Pie charts for my three baskets, Each wedge is just some random color that is presumable picked by the control. How would I say, make the blueberries blue, bannanas yellow, etc. in the chart?

I know this is a weird question, but I can't think of a simpler way to describe it.

I don't care if the solution is in XAML or code, just as long as it works.

解决方案

I have found the solution, after much swearing and investigation. There are a lot of moving parts here, so I am going to keep each as brief as possible while still conveying the main points.

For starters I have a collection of objects that I am trying to keep track of:

public class PileOfFruit
{
  public string Name {get; set; }
  public int Count { get; set; }
}

In my view model, I have a collection of those objects.

public class BasketVM
{
...
    private ObservableCollection<PileOfFruit> _FruitBasket = new ObservableCollection<PileOfFruit>();
    public ObservableCollection<PileOfFruit> FruitBasket
    {
      get { return _FruitBasket; }
    }
...
}

In my view I will define the pie chart that will display the quantites of the fruits in the basket. The most important factor here is the template binding to SliceTemplate

<chartingToolkit:Chart x:Name="ExampleChart">
    <chartingToolkit:PieSeries ItemsSource={Binding FruitBasket
                               DependentValueBinding="{Binding Count}" 
                               IndependentValueBinding="{Binding Name}">
        <chartingToolkit:PieSeries.Palette>
            <visualizationToolkit:ResourceDictionaryCollection>
            <ResourceDictionary>
                <Style x:Key="DataPointStyle" TargetType="Control">
                    <Setter Property="Template" Value="{StaticResource SliceTemplate}"/>
                </Style>

Now in app.xaml or some other place we can drop in the template for the PieDataSeries object. Pay close attention to the value on Fill It is bound to the independent value which will be something like 'banana' 'grape' or whatever. This in turn is passed off to a value converter.

<converters:FruitToColorConverter x:Key="FruitToColorConverter"/>

<ControlTemplate x:Key="SliceTemplate" TargetType="chart:PieDataPoint">
  <Path Data="{TemplateBinding Geometry}"
        Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IndependentValue, Converter={StaticResource FruitToColorConverter}}"
        Stroke="{TemplateBinding BorderBrush}">
    ....

The associated data converter is what will finally set the color that we want. So if we do something like this...

public class FruitToColorConverter : IValueConverter
{
  private SolidColorBrush Default = new SolidColorBrush(Colors.Black);

  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    if (value == null || (!(value is string))) { return Default; }

    switch (value as string)
    {
      case "Apple":
        return new SolidColorBrush(Colors.Red);
      case "BlueBerry":
        return new SolidColorBrush(Colors.Blue);
      default:
        return Default;
        ......

And that is how you get the correct colors each and every time. If I left anything out or should clarify, please let me know so I can make corrections. There are a lot of moving parts here.. =P

这篇关于Silverlight工具包;饼图颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆