trouble binding LineSeries to Array

Charts for WPF Forum

Posted 10 years ago by Jonathan Helfman
Version: 14.2.0611
Avatar

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&quot;/>
    </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

Comments (1)

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

Hi Jon,

Thanks for reporting this bug.  We agree that you should be able to do ItemsSource="{Binding Y}" and have it auto-bind the X-axis to the index.  This used to work however it appears that a change in a previous build broke it.  We have added code for the upcoming 2015.1 version (due in the next week or two) to fix this problem.

The reason the other thing you tried (ItemsSource="{Binding}" XPath="X" YPath="Y") didn't work is that the chart can't handle data in that fashion, where you effectively defined two items sources (your view model's X and Y array properties).  What will work instead, and what is a valid workaround until 2015.1, is if you make a new class with X and Y properties defined on it, both of which can be doubles.  Then have a single collection of this type property on your view model (call it "DataPoints") and set the chart's ItemsSource="{Binding DataPoints}".  That should get you going.

But again, in the next version, you'll be able to bind to a single array again as expected.


Actipro Software Support

The latest build of this product (v24.1.3) was released 27 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.