Chart Doesn't Reflect Data

Charts for Silverlight Forum

Posted 8 years ago by Laura
Version: 15.1.0192
Avatar

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:

Comments (1)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Laura,

For complex scenarios like this that require debugging, we always ask that instead of posting code here in the forums where it's not complete, please make a zip of a new simple sample project like this example (but with all the source code) and email that to our support address.  The sample should contain as few data points as possible to repro the issue and have no references to external assemblies of your own.

In your email, mention this thread and tell us what you see and what you expect to see.  Also be sure to rename the .zip file extension so it doesn't get spam blocked.  Then we'll debug that and tell you what the problem is.  Thanks!


Actipro Software Support

The latest build of this product (v18.1 build 0233) was released 4 years ago, which was after the last post in this thread.