08-24-2012 10:04 AM
This is probably a very basic issue but I can't figure out how to graph more than one set of data with the WPF graph in VS2010. There is a DataSource property on the graph but I don't see the same on each Plot class. I can't find any documentation on the DataSource property of the graph. I can pass it an array of doubles and it plots it fine but now I need to plot two sets of data. Can anyone help?
Solved! Go to Solution.
08-24-2012 01:07 PM
The DataSource property is documented in the How to: Plot and Chart topic, and on the GraphBase class (which is the type that defines the property). However, it looks like these topics do not directly address your multi-plot issue; I have put in a request to improve this.
To answer your question, if you are using DataSource to pass an array of double values to one plot, then you can use a collection of arrays to pass data to multiple plots (e.g. a double[][], a List<double[]>, or an ObservableCollection<double[]>).
Alternatively, you could use the Data collection to supply each plot with data; e.g.:
graph.Data[0] = /* double[] for plot 0 */;
graph.Data[1] = /* double[] for plot 1 */;
~ Paul
08-24-2012 01:43 PM
Thanks for your quick response; this solution works.
As a side note, I think it would be intuitive for each Plot to have a DataSource property. That would keep the DataBinding nice and clean and play well with the MVVM pattern.
09-19-2012 09:47 PM
how can I bind the Data collection to my data in the viewmodel, I got a error as below:
'Data' property is read-only and cannot be set from markup.
and could you give a sample more clearly to show how to apply the Data collection to each plot in the graph?
Thank you very much !
09-20-2012 07:14 AM
Are you binding the Data property on the graph or somewhere else? My xaml graph looks something like this:
<ni:Graph x:Name="graphMain" IsEnabled="{Binding Path=IsEnabled}" DataSource="{Binding Path=GraphData}">
....stuff here....
</ni:Graph>
My GraphData property is a
List<double[]>
If you're still getting a read-only error, try setting the BindingMode to OneWay. Maybe the property you're binding to is read-only.
Does that all make sense? If you want to bind data to multiple plots then you have to have multiple arrays in your data source. You can't bind directly to a plot.
09-20-2012 10:06 AM
As danxia showed, you can populate the Data collection by binding to the DataSource property.
This works the same way as ItemsControl, which has an ItemsSource property used to populate the Items collection. Just like ItemsControl and Items, you can add individual objects to the Data collection on a graph, but you cannot bind the read-only Data property directly; you can only bind using the DataSource property.
Regarding your question about "how to apply the Data collection to each plot in the graph", all you need to do is supply the graph with data and (optionally) configure the corresponding plots. To make this more concrete, consider the following XAML:
<ni:Graph Name="graph">
<ni:Graph.Plots>
<ni:Plot Label="Plots[0]">
<ni:LinePlotRenderer Stroke="Green" />
</ni:Plot>
<ni:Plot Label="Plots[1]">
<ni:LinePlotRenderer Stroke="Blue" />
</ni:Plot>
</ni:Graph.Plots>
</ni:Graph>
This creates a graph containing two line plots, one green and one blue. If you initialize the Data collection directly in code, the data at index 0 will go to the green plot at index 0 ("Plots[0]"), and the data at index 1 will go to the blue plot at index 1:
graph.Data[0] = new[] { 0, 2, 1, 3 };
graph.Data[1] = new[] { 1, 3, 2, 4 };
Similarly, if you initialize DataSource with a collection containing that same data (i.e.
graph.DataSource = new[] { new[] { 0, 2, 1, 3 }, new[] { 1, 3, 2, 4 } };
in code, or exposed through a property with a binding), the graph will automatically populate Data at index 0 with the first array and at index 1 with the second, at which point it behaves identically to the first example.
09-22-2012 03:57 AM
Thanks for your answer, it's really helpful to me.