syncfusion wpf chart array of double - how to use with mvvm

185 Views Asked by At

I have a Model with an array[100] of double and I would like to use this directly as the data for the chart. The index value should be the X and the value at that index should be the Y on the chart.

Currently I have to convert the array to a list of points (in my ViewModel) where I store the index value (0-99) as the X of the point and the value of that array index as the Y value of the point.

My xaml looks like this :

<UserControl.DataContext>
        <local:ViewModel/>
    </UserControl.DataContext>
    <Grid>
        <syncfusion:SfChart>
            <chart:SfChart.PrimaryAxis>
                <chart:NumericalAxis/>
            </chart:SfChart.PrimaryAxis>
            <chart:SfChart.SecondaryAxis>
                <chart:NumericalAxis/>
            </chart:SfChart.SecondaryAxis>
            <chart:LineSeries ItemsSource="{Binding Data}" XBindingPath="X" YBindingPath="Y"/>
        </syncfusion:SfChart>
    </Grid>
</UserControl>

I cannot imagine that this is the best way to get the result ...

1

There are 1 best solutions below

2
On BEST ANSWER

The index value should be the X and the value at that index should be the Y on the chart.

Well, the only way to let the control know this is to create an object with a property that returns the X value and another that returns the Y value and then set the XBindingPath and YBindingPath to the names of these respectively. This is MVVM.

The control cannot really be supposed to figure out that you want the index to represent the Y value.