Dynamic BarSeries

Charts for Silverlight Forum

Posted 8 years ago by Laura
Version: 15.1.0192
Avatar

Hello, I'm trying to generate a stack bar chart that has a BarSeries for each task in the taskGroup collection. How do I go about this?

C#:

var taskA = new TaskInfoCustom();

taskA.Costs.Add(new CostInfoCustom { Qty = 3, CostDate = DateTime.Now + new TimeSpan(3, 0, 0, 0) });
taskA.Costs.Add(new CostInfoCustom { Qty = 4, CostDate = DateTime.Now + new TimeSpan(3, 0, 0, 0) });
taskA.Costs.Add(new CostInfoCustom { Qty = 3, CostDate = DateTime.Now + new TimeSpan(2, 0, 0, 0) });
taskA.Costs.Add(new CostInfoCustom { Qty = 2, CostDate = DateTime.Now + new TimeSpan(5, 0, 0, 0) });
taskA.Costs.Add(new CostInfoCustom { Qty = 1, CostDate = DateTime.Now + new TimeSpan(4, 0, 0, 0) });

var taskB = new TaskInfoCustom();
taskB.Costs.Add(new CostInfoCustom { Qty = 3, CostDate = DateTime.Now + new TimeSpan(5, 0, 0, 0) });
taskB.Costs.Add(new CostInfoCustom { Qty = 3, CostDate = DateTime.Now + new TimeSpan(4, 0, 0, 0) });
taskB.Costs.Add(new CostInfoCustom { Qty = 2, CostDate = DateTime.Now + new TimeSpan(3, 0, 0, 0) });
taskB.Costs.Add(new CostInfoCustom { Qty = 4, CostDate = DateTime.Now + new TimeSpan(2, 0, 0, 0) });
taskB.Costs.Add(new CostInfoCustom { Qty = 2, CostDate = DateTime.Now + new TimeSpan(1, 0, 0, 0) });

var taskGroup = new ObservableCollection<TaskInfoCustom>();
taskGroup.Add(taskA);
taskGroup.Add(taskB);

InitializeComponent();

HourChart.DataContext = taskGroup;

 XAML:

<charts:XYChart Name="HourChart">
  <charts:BarSeries ItemsSource="{Binding Costs}" YPath="Qty" XPath="CostDate" StackKind="Normal"/>
</charts:XYChart>

Comments (1)

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

Hi Laura,

While you can bind data to a series' ItemsSource, there isn't an ItemsSource on chart so you have to add the series manually.  Instead of setting the chart's DataContext after InitializeComponent, I would recommend looping through your task group collection and programmatically create a series for each, and add that series to the chart.  The series you create should have the same options you had set for the sample series in XAML.  You should also remove the sample BarSeries from the XAML.


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.