-1

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 ...

Geertie
  • 237
  • 4
  • 15
  • Wouldn't it be fantastic if people down voting questions would leave a small comment why. Maybe I could learn something from it or even modify my question. People asking questions do not do this because they feel like it that day, no, they have a problem or do not know something, that is why they ask questions. In my case I have a GDI+ and QML background and I'm trying to move over to WPF/xaml and mvvm and I'm trying to do things correct. I notice a lot that 'optimizations' are not the priority in both wpf and xaml and that is new for me coming from superfast GDI+ for example. – Geertie Jun 09 '21 at 15:31

1 Answers1

0

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • First of all thanks for the answer, but I do not want the index to be the y-value BUT the x-value (0-100, like in percentages - not very often used apparently) and to be honest that has nothing to do with mvvm as such. Since I have to say in xaml that the x-axis should be numeric, I think the control knows it can expect numeric values (such as an index for example). Is it therefore so far fetched that maybe a method could be present that allows binding directly to an array ? – Geertie Jun 09 '21 at 15:20
  • You can bind to an array of `T` where `T` is a type with a property that represents the X value *and* a property that represents the Y value, i.e. you cannot bind to an `int[]` or another array of primitive types and expect it work. – mm8 Jun 11 '21 at 12:46