Silverlight 工具包;饼图颜色
我有一个巨大的问题无法解决。假设我有五种不同的水果,我希望每种水果都与某种颜色相关联。假设我有三个“篮子”,其中包含零个或多个上述水果。
当我为我的三个篮子制作饼图时,每个楔形只是一些随机颜色,大概是由控件选择的。我该怎么说,将图表中的蓝莓、香蕉等设为蓝色?
我知道这是一个奇怪的问题,但我想不出更简单的方法来描述它。
我不在乎解决方案是 XAML 还是代码,只要它有效即可。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过多次咒骂和调查后,我找到了解决方案。这里有很多活动部分,所以我将尽可能简短地表达每个部分,同时仍然传达要点。
首先,我有一个想要跟踪的对象集合:
在我的视图模型中,我有这些对象的集合。
在我看来,我将定义饼图来显示篮子中水果的数量。这里最重要的因素是模板绑定到 SliceTemplate
现在,在 app.xaml 或其他地方,我们可以放入 PieDataSeries 对象的模板。请密切注意 Fill 上的值,它与独立值绑定,例如“香蕉”“葡萄”或其他值。这又被传递给值转换器。
相关的数据转换器将最终设置我们想要的颜色。因此,如果我们做这样的事情...
这就是您每次都能获得正确颜色的方法。如果我遗漏了任何内容或需要澄清,请告诉我,以便我进行更正。这里有很多活动部件..=P
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:
In my view model, I have a collection of those objects.
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
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.
The associated data converter is what will finally set the color that we want. So if we do something like this...
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
您可以为图表定义自己的调色板,但它会自动选择调色板中显示的数据项的颜色。所以没有(简单?)方法告诉图表显示蓝莓蓝色,香蕉黄色等。您可以做的就是告诉图表显示第一个项目蓝色,第二个项目黄色等,然后将蓝莓放在第一位,香蕉放在第二位等在数据源中。
一些定义您自己的调色板的示例:
编辑:有趣的名称规范:
You can define your own color palette for the chart, but it will automatically pick the colors for the dataitems as they are appears in the palette. So no (easy?) way to tell the chart to display blueberries blue, bannanas yellow, etc. What you can do is to tell the chart to display the first item blue, second item yellow etc, and put bluberries first, bannanas second etc. in the datasource.
Some sample to define your own paletta:
EDIT: The interesting namespeces:
这是我发现的:
颜色并不是真正随机的:有一系列调色板,每 5 次重复一次(实际上数量并不重要)。我的解决方法更简单:每次有新数据要显示时,我都会重新创建饼图。这样,调色板将始终从相同的颜色开始(意味着它永远不会改变)。这意味着您需要有一个以编程方式重建 PieChart 的方法(XAML 中没有它,只需有一个占位符并调用“PieChartBuilder”方法)。
我认为这是更少的代码(也更少的错误:))。
Here is what I've found:
The colors aren't really random: There is a sequense of pallettes which repeats every 5ish times (the number actually doesn't matter). My workaround is simpler: every time I have new data to be displayed I recreate the piechart. In this way the color pallette will always start from the same colors (meaning that it will never change). This means that you need to have a method that programatically rebuilds your PieChart (do not have it in XAML, just have there a placeholder and call the "PieChartBuilder" method ).
I think this is less code (also less bugs :) ).