I am creating a dashboard in Silverlight. I am trying to create a pie chart as follows:
&l开发者_StackOverflowt;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>
My data source has hundreds of orders. The orders span three months. I want to group the pie slices by month. Because of this reason, I set the IndependentValueBinding to OrderDate.Month. However, I noticed that the items are not automatically grouped. How do I declaratively group the data by month to show the amounts in this fashion?
Thank you so much for your help!
The Charting in the toolkit is designed only to present a chart of the data supplied. It does not do any aggregation for you, you will need to have done that to the data before supplying it to the chart.
In this case you need to create new query method that does the summarising that returns a collection contain only one object per month with the total amount for the month.
Lets assume there are two columns named OrderDate and TotalAmount in your OrderTable (as DataSource).
Now you need to write a query to group your data by Month. As you have the data stored for 3 months of a particular year, you may not have to take care of year here. So the LINQ query for calculating monthly total amount can be like below.
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)));
As I worked in Visifire before so I have created this Pie Chart example using Visifire
Here in this example, the results are extracted form the Orders as a name value pair and cumulatively stored in the totalAmountByMonth. this is then bound to the chart by making AxisXLabel as Month and Amount as the YValue. Here is the code below.
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>
Output :
精彩评论