Hi,
I need to bind a xamDataChart to a collection of objects of type "dynamic", but the chart doesn't draw the points. If I bind the collection to a DataGrid I can see the values of each dynamic object. My dynamic object has a class property "X", and for each serie that I add on runtime, I add a new property "Y" to my object:
X, Y1, Y2, ..., Yn
The xamDataChart supports binding to objects of type dynamic?
Thanks.
Are you just casting a normal class as dynamic in the collection, or are you using something like ExpandoObject? Is there a way you can share a sample of what you are trying to do? That would help us offer a solution. The chart does not currently try to inspect the IDynamicMetaObjectProvider interface for a truly dynamic object like ExpandoObject. If you tried to use reflection on this object yourself, you would have similar problems. We will have to add some extra functionality to allow you to use something like ExpandoObject with the chart directly. But there are various work arounds you could use. If you can provide a sample for me to look at we can discuss your options.
-Graham
Here's how you could proxy a list of ExpandoObjects to bind them to the chart:
<Window.Resources> <local:TestData x:Key="dynamicData" /> <local:ChartData x:Key="data" LabelPath="Label" ValuePath="Value" ItemsSource="{StaticResource dynamicData}" /> </Window.Resources> <Grid> <igChart:XamDataChart x:Name="theChart" > <igChart:XamDataChart.Axes> <igChart:CategoryXAxis x:Name="xAxis" ItemsSource="{StaticResource data}" Label="{}{Label}"/> <igChart:NumericYAxis x:Name="yAxis"/> </igChart:XamDataChart.Axes> <igChart:XamDataChart.Series> <igChart:LineSeries x:Name="line" XAxis="{Binding ElementName=xAxis}" YAxis="{Binding ElementName=yAxis}" ItemsSource="{StaticResource data}" ValueMemberPath="Value"/> </igChart:XamDataChart.Series> </igChart:XamDataChart> </Grid>
And Code behind:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } public class TestData : List<dynamic> { public TestData() { for (int i = 0; i < 10; i++) { dynamic newObj = new ExpandoObject(); newObj.Label = i.ToString(); newObj.Value = (double)i; Add(newObj); } } } public class ChartData : List<ChartDataItem> { private string _labelPath; public string LabelPath { get { return _labelPath; } set { _labelPath = value; Refresh(); } } private string _valuePath; public string ValuePath { get { return _valuePath; } set { _valuePath = value; Refresh(); } } private IEnumerable<dynamic> _itemsSource; public IEnumerable<dynamic> ItemsSource { get { return _itemsSource; } set { _itemsSource = value; Refresh(); } } public void Refresh() { Clear(); if (ValuePath != null && LabelPath != null && ItemsSource != null) { foreach (dynamic obj in ItemsSource) { if (obj is IDictionary<string, object>) { ChartDataItem item = new ChartDataItem(); item.Label = (string)((IDictionary<string, object>)obj)[LabelPath]; item.Value = (double)((IDictionary<string, object>)obj)[ValuePath]; Add(item); } } } } } public class ChartDataItem { public string Label { get; set; } public double Value { get; set; } }
If you would like the chart to natively work with something like ExpandoObject, you could make a feature request.