我正在Silverlight中创建一个仪表板。我正在尝试创建一个饼图,如下所示:
<ria:DomainDataSource AutoLoad="True" Name="orderDomainDataSource"
QueryName="GetOrdersQuery">
<ria:DomainDataSource.DomainContext>
<my:PresentationDomainContext />
</ria:DomainDataSource.DomainContext>
</ria:DomainDataSource>
<toolkit:Chart Name="chart1" Title="">
<toolkit:Chart.Series>
<toolkit:PieSeries
ItemsSource="{Binding ElementName=orderDomainDataSource, Path=Data}"
IndependentValueBinding="{Binding Path=OrderDate.Month}"
DependentValueBinding="{Binding Path=TotalAmount}" />
</toolkit:Chart.Series>
</toolkit:Chart>
我的数据源有数百个订单。订单跨越三个月。我想按月分组饼图。由于这个原因,我将IndependentValueBinding设置为OrderDate.Month。但是,我注意到这些项目没有自动分组。如何按月以声明方式对数据进行分组以便以这种方式显示数量?
非常感谢你的帮助!
答案 0 :(得分:2)
工具包中的图表仅用于显示所提供数据的图表。它不会为您进行任何聚合,您需要在将数据提供给图表之前对数据执行此操作。
在这种情况下,您需要创建一个新的查询方法,该方法执行汇总,该汇总返回一个集合,每月只包含一个对象,并包含该月的总金额。
答案 1 :(得分:1)
假设您的OrderTable中有两列名为OrderDate和TotalAmount(作为DataSource)。
现在您需要编写一个查询来按月对数据进行分组。由于您将数据存储在特定年份的3个月中,因此您可能无需在此处注意年份。因此,用于计算月总金额的LINQ查询可以如下所示。
var totalAmountByMonth = (from record in orders
group record by record.Date.Month
into groupedOrder
select new KeyValuePair<String, Double>
(groupedOrder.First().Date.ToString("MMMM"),
groupedOrder.Sum(a => a.Amount)));
因为我之前在Visifire工作,所以我使用Visifire
创建了这个饼图示例在此示例中,结果从Orders中提取为名称值对,并累积存储在totalAmountByMonth中。然后通过将AxisXLabel设置为Month并将Amount设置为YValue将其绑定到图表。这是下面的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Visifire.Charts;
namespace BindingDemo
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
// Temp list of orders
List<Order> orders = new List<Order>();
orders.Add(new Order() { Date = new DateTime(2010, 1, 1), Amount = 1213 });
orders.Add(new Order() { Date = new DateTime(2010, 1, 3), Amount = 1273 });
orders.Add(new Order() { Date = new DateTime(2010, 1, 4), Amount = 1253 });
orders.Add(new Order() { Date = new DateTime(2010, 1, 5), Amount = 213 });
orders.Add(new Order() { Date = new DateTime(2010, 1, 5), Amount = 1300 });
orders.Add(new Order() { Date = new DateTime(2010, 2, 1), Amount = 1213 });
orders.Add(new Order() { Date = new DateTime(2010, 2, 3), Amount = 1255 });
orders.Add(new Order() { Date = new DateTime(2010, 2, 4), Amount = 1290 });
orders.Add(new Order() { Date = new DateTime(2010, 2, 5), Amount = 1731 });
orders.Add(new Order() { Date = new DateTime(2010, 2, 5), Amount = 2173 });
orders.Add(new Order() { Date = new DateTime(2010, 3, 1), Amount = 1213 });
orders.Add(new Order() { Date = new DateTime(2010, 3, 3), Amount = 1243 });
orders.Add(new Order() { Date = new DateTime(2010, 3, 4), Amount = 1263 });
orders.Add(new Order() { Date = new DateTime(2010, 3, 5), Amount = 1273 });
orders.Add(new Order() { Date = new DateTime(2010, 3, 5), Amount = 1233 });
var totalAmountByMonth = (from record in orders
group record by record.Date.Month
into groupedOrder
select new KeyValuePair<String, Double>(groupedOrder.First().Date.ToString("MMMM"),
groupedOrder.Sum(a => a.Amount)));
// Setting DataContext of Chart
chart.DataContext = totalAmountByMonth;
}
public class Order
{
public DateTime Date;
public int Amount;
}
}
}
:XAML:
<vc:Chart Name="chart" Width="500" Height="300" Theme="Theme1" View3D="True">
<vc:Chart.Series>
<vc:DataSeries RenderAs="Pie" DataSource="{Binding}">
<vc:DataSeries.DataMappings>
<vc:DataMapping MemberName="AxisXLabel" Path="Key"></vc:DataMapping>
<vc:DataMapping MemberName="YValue" Path="Value"></vc:DataMapping>
</vc:DataSeries.DataMappings>
</vc:DataSeries>
</vc:Chart.Series>
</vc:Chart>
输出: