
All the sample data is correctly formatted for the stack chart when it is set as the items source, why is the chart display all wrong, what did I do wrong?
C#:
public partial class MainPage : UserControl
{
public MainPage()
{
var costs = new List<CostInfoCustom>();
costs.Add(new CostInfoCustom { Qty = 6, CostDate = new DateTime(2012, 02, 01), TaskInfoKey = new TaskInfo { TaskNo = 3 } });
costs.Add(new CostInfoCustom { Qty = 2, CostDate = new DateTime(2012, 03, 04), TaskInfoKey = new TaskInfo { TaskNo = 1 } });
costs.Add(new CostInfoCustom { Qty = 2, CostDate = new DateTime(2012, 02, 01), TaskInfoKey = new TaskInfo { TaskNo = 2 } });
costs.Add(new CostInfoCustom { Qty = 0, CostDate = new DateTime(2012, 02, 01), TaskInfoKey = new TaskInfo { TaskNo = 2 } });
costs.Add(new CostInfoCustom { Qty = 0, CostDate = new DateTime(2012, 05, 03), TaskInfoKey = new TaskInfo { TaskNo = 3 } });
costs.Add(new CostInfoCustom { Qty = 0, CostDate = new DateTime(2012, 02, 01), TaskInfoKey = new TaskInfo { TaskNo = 1 } });
costs.Add(new CostInfoCustom { Qty = 6, CostDate = new DateTime(2012, 02, 01), TaskInfoKey = new TaskInfo { TaskNo = 1 } });
costs.Add(new CostInfoCustom { Qty = 5, CostDate = new DateTime(2012, 06, 02), TaskInfoKey = new TaskInfo { TaskNo = 1 } });
costs.Add(new CostInfoCustom { Qty = 5, CostDate = new DateTime(2012, 02, 03), TaskInfoKey = new TaskInfo { TaskNo = 2 } });
costs.Add(new CostInfoCustom { Qty = 5, CostDate = new DateTime(2012, 02, 04), TaskInfoKey = new TaskInfo { TaskNo = 3 } });
costs.Add(new CostInfoCustom { Qty = 5, CostDate = new DateTime(2012, 02, 04), TaskInfoKey = new TaskInfo { TaskNo = 2 } });
costs.Add(new CostInfoCustom { Qty = 5, CostDate = new DateTime(2012, 07, 07), TaskInfoKey = new TaskInfo { TaskNo = 1 } });
costs.Add(new CostInfoCustom { Qty = 5, CostDate = new DateTime(2012, 02, 02), TaskInfoKey = new TaskInfo { TaskNo = 3 } });
costs.Add(new CostInfoCustom { Qty = 3, CostDate = new DateTime(2012, 02, 05), TaskInfoKey = new TaskInfo { TaskNo = 1 } });
costs.Add(new CostInfoCustom { Qty = 1, CostDate = new DateTime(2012, 10, 02), TaskInfoKey = new TaskInfo { TaskNo = 2 } });
costs.Add(new CostInfoCustom { Qty = 1, CostDate = new DateTime(2012, 02, 01), TaskInfoKey = new TaskInfo { TaskNo = 3 } });
SetChartData(costs);
}
private void SetChartData(List<CostInfoCustom> costs)
{
InitializeComponent();
//Set the X origin to 0
HourChart.YAxes.Add(new XYDecimalAxis { Minimum = 0 });
//Group costs by date and get all the task numbers from the data
var costsByDates = costs.GroupBy(c => c.CostDate);
var allTaskNumbers = costs.Select(x => x.TaskInfoKey.TaskNo);
//For each day add qty = 0 records where a task is missing
foreach (var costsByDay in costsByDates)
{
var missingTaskNumbers = allTaskNumbers.Except(costsByDay.Select(x => x.TaskInfoKey.TaskNo)).ToList();
foreach (int taskNumber in missingTaskNumbers)
{
costs.Add(new CostInfoCustom { Qty = 0, CostDate = costsByDay.Key, TaskInfoKey = new TaskInfo { TaskNo = taskNumber } });
}
}
//group the costs by task
var costsGroupedByTaskNo = costs.GroupBy(c => c.TaskInfoKey.TaskNo);
//for each task's costs, create a data series.
foreach (var taskCosts in costsGroupedByTaskNo)
{
Debug.WriteLine("TaskNo: " + taskCosts.Key + " - Total Hours: " + taskCosts.Sum(x => x.Qty) + " On Days: " + string.Join(",", taskCosts.Select(cg => cg.CostDate.Day).Distinct()));
HourChart.Series.Add(new BarSeries
{
ItemsSource = taskCosts.ToList(),
YPath = "Qty",
XPath = "CostDate",
StackKind = XYSeriesStackKind.Normal
});
}
}
}
XAML:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:charts="http://schemas.actiprosoftware.com/winfx/xaml/charts"
xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
x:Class="HoursChart.MainPage">
<UserControl.Resources>
<Style TargetType="charts:XYChart">
<Setter Property="Width" Value="1000" />
<Setter Property="Height" Value="900" />
</Style>
</UserControl.Resources>
<Grid>
<charts:XYChart Name="HourChart"/>
</Grid>
</UserControl>
Console Output:
TaskNo: 3 - Total Hours: 17 On Days: 1,3,4,2,7,5
TaskNo: 1 - Total Hours: 21 On Days: 4,1,2,7,5,3
TaskNo: 2 - Total Hours: 13 On Days: 1,3,4,2,7,5
Chart Output: