sfchart binding dynamic object

i don't konw  how many LineSeries until the program started. i must binding  XBindingPath and  YBindingPath dynamically.so I create a dynamic object in viewmodel

The flowing is an example:In viewmodel,the dynamic object has two properties(Time and A).In view , XBindingPath ="Time" , YBindingPath ="A".but it doesn't work,why?

view :

<chart:SfChart

            x:Name="lineChart"

            Margin="10"

            Header="Efficiency of oil-fired power production">

            <chart:SfChart.Behaviors>

                <syncfusion:ChartZoomPanBehavior EnableSelectionZooming="True" ZoomMode="X">

                </syncfusion:ChartZoomPanBehavior>

            </chart:SfChart.Behaviors>


            <chart:SfChart.PrimaryAxis>

                <chart:CategoryAxis

                    ShowGridLines="False"

                    Header="Time"

                    />

            </chart:SfChart.PrimaryAxis>


            <chart:SfChart.SecondaryAxis>

                <chart:NumericalAxis

                    HorizontalAlignment="Center"

                    Header="Value"

                    Interval="1"

                    Maximum="2"

                    Minimum="0"/>

            </chart:SfChart.SecondaryAxis>


            <chart:LineSeries

                ItemsSource="{Binding DataPoints}"

                XBindingPath="Time"

                YBindingPath="A"

                Interior="Red">

                <chart:LineSeries.AdornmentsInfo>

                    <chart:ChartAdornmentInfo

                        ShowMarker="True"

                        Symbol="Ellipse">

                    </chart:ChartAdornmentInfo>

                </chart:LineSeries.AdornmentsInfo>

            </chart:LineSeries>

        </chart:SfChart>

viewmodel:

public MainViewModel()

        {     

           //eg.

           dynamic item = new ExpandoObject();

            item.Time = 1;

            item.A = 1;

            DataPoints.Add(item);

            dynamic item1 = new ExpandoObject();

            item1.Time = 2;

            item1.A = 2;

            DataPoints.Add(item);

        }

        private ObservableCollection<dynamic> dataPoints = new ObservableCollection<dynamic>();

        public ObservableCollection<dynamic> DataPoints

        {

            get { return dataPoints; }

            set { SetProperty(ref dataPoints, value); }

        }


1 Reply

SA Saiyath Ali Fathima Bee Moidhin Abdhul Kathar Syncfusion Team November 29, 2023 11:10 AM UTC

Hi Chong,

 

We would need to share information about chart control, which is ExpandoObject does not support charts. However, we can convert the data into a table. For reference, we provided the KB and blog below.

 

• KB for How to Bind a List of Tuples in WPF Charts: https://support.syncfusion.com/kb/article/10198/How-to-Bind-a-List-of-Tuples-in-WPF

• KB for How to Bind KeyValuePair Collection in WPF Charts: https://support.syncfusion.com/kb/article/9234/How-to-Bind-KeyValuePair-Collection-in-WPF-SfChart

• Blog for how to incorporate the JSON data into WPF Charts: https://www.syncfusion.com/blogs/post/visualize-json-data-wpf-charts.aspx

 

Please note that both X and Y paths are required for populating the data on the chart.

 

Regards,

Saiyath Ali Fathima M


Loader.
Up arrow icon