
While this data is correct there is an unessicary date duplicate on the X axis.
using System.Windows.Controls;
using Adapt.Model.TaskManagement;
using System;
using Adapt.Model.TaskManagement.Custom;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using ActiproSoftware.Windows.Controls.Charts.Primitives;
using ActiproSoftware.Windows.Controls.Charts;
using System.Linq;
using System.Diagnostics;
namespace HoursChart
{
public partial class MainPage : UserControl
{
public MainPage()
{
//Generate random cost data
var random = new Random();
var dataSize = 5;
var costs = new List<CostInfoCustom>();
for (int i = 0; i < 10; i++)
{
costs.Add(new CostInfoCustom { Qty = random.Next(1, dataSize), CostDate = new DateTime(2012, 01, random.Next(1, dataSize), 12, 0, 0), TaskInfoKey = new TaskInfo { TaskNo = random.Next(1, dataSize) } });
}
//Set the chart to use the generated cost data
SetChartData(costs);
}
private void SetChartData(List<CostInfoCustom> costs)
{
InitializeComponent();
//Set the X origin to 0, the interval to 1 hour and display a maximum of 24 hours
HourChart.YAxes.Add(new XYDecimalAxis { Minimum = 0, TickMajorInterval = 1, Maximum = 24 });
HourChart.IsLegendVisible = true;
//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 for that day
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
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, Description = "Task No " + taskCosts.Key });
}
}
}
}
<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>