Hi--
I'm trying to figure out the most efficient way to load a LineChart with data.
I'd like to be able to load data directly from an array without needing to create and maintain a Collection of explicitly defined objects with dependency properties.
The following data binding descriptions and code sample from the 'Specifying Items' and 'Axes' documentation imply that this is possible:
The ItemsSource property on each series can be set to any collection of objects, including custom objects, as long as the collection implements IEnumerable.
....
We can specify that the Y values should be pulled from the Amount property by setting YPath to "Amount". If we want to use the index in the collection as our X values, then we do not need to set XPath.
<charts:XYChart Width="300" Height="200">
<charts:XYChart.XAxes>
<XYDateTimeAxis"/>
</charts:XYChart.XAxes>
<charts:XYChart.YAxes>
<XYDoubleAxis/>
</charts:XYChart.YAxes>
<charts:AreaSeries ItemsSource="{Binding}" XPath="MyDateProperty" YPath="MyDoubleProperty" />
</charts:XYChart>
When I try, however, I get no chart or data. Adding the XYDoubleAxis does not help.
I know that the Chart can get the data, however, because I can bind ItemSource directly to one of the Arrays (as shown in the example below), but this binds to both the X and Y axes instead of just Y. (btw, this behavior seems unusual to me. Charts in other visualization toolkits I have used, when displaying a single array of data, would bind data values to the YAxis and use the array offsets along the X axis).
Any advice would be appreciated.
Here's my XAML:
<Window x:Class="ArrayDataTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:charts="http://schemas.actiprosoftware.com/winfx/xaml/charts"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<charts:XYChart x:Name="chart1" >
<charts:LineSeries ItemsSource="{Binding}" XPath="X" YPath="Y" />
</charts:XYChart>
<charts:XYChart x:Name="chart2" Grid.Row="1">
<charts:LineSeries ItemsSource="{Binding Y}" />
</charts:XYChart>
</Grid>
</Window>
and the C#:
namespace ArrayDataTest
{
public class WpfViewModel : INotifyPropertyChanged
{
private Double[] x;
public Double[] X
{
get { return x; }
set
{
x = value;
OnPropertyChanged("X");
}
}
private Double[] y;
public Double[] Y
{
get { return y; }
set
{
y = value;
OnPropertyChanged("Y");
}
}
public WpfViewModel(int size)
{
x = new Double[size];
y = new Double[size];
initializeData(size);
}
private void initializeData(int size)
{
double m;
double dt = 1;
for (int i = 0; i < size; i++)
{
m = i * dt;
this.x[i] = m;
this.y[i] = Math.Sin(m) * 2 * Math.PI;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
Console.WriteLine("PropertyChangedEventHandler: " + name);
handler(this, new PropertyChangedEventArgs(name));
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
this.DataContext = new WpfViewModel(500);
InitializeComponent();
}
}
}
Thanks!
--jon