We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

SfChart add realtime data

Thread ID:

Created:

Updated:

Platform:

Replies:

128369 Jan 17,2017 09:32 AM Jan 18,2017 10:42 PM WPF 4
loading
Tags: SfChart
Simone Sarzano
Asked On January 17, 2017 09:32 AM

Hi guys,

I'd like to create and populate a serie using the code behind.

I've created a timer triggered wpf application that every 500 milliseconds execute the following code:

 private void timer_Tick(Object sender, EventArgs e)
        {
            var findSerie = sfChart.Series.Where(x => x.Name == "RealTime");
            if (findSerie.Count() == 0)
            {
                realtime.Name = "RealTime";
                RealTimeDataViewModel rtVM = new RealTimeDataViewModel();
                realtime.ItemsSource = rtVM;
                realtime.XBindingPath = "Speed";
                realtime.YBindingPath = "Rate";
                realtime.Interior = new SolidColorBrush(Colors.DarkGreen);
                sfChart.Series.Add(realtime);
            }
            int index = (sfChart.Series["RealTime"].ItemsSource as RealTimeDataViewModel).RealTimeDatas.Count - 1;
            if (index < 0)
            {
                index = 0;
                (sfChart.Series["RealTime"].ItemsSource as RealTimeDataViewModel).RealTimeDatas.Add(new RealTimeData() { Speed = i.Millisecond, Rate = rnd.Next(0, 1000) });
            }
            else
            {
                RealTimeData rtd = (sfChart.Series["RealTime"].ItemsSource as RealTimeDataViewModel).RealTimeDatas.Last<RealTimeData>();
                (sfChart.Series["RealTime"].ItemsSource as RealTimeDataViewModel).RealTimeDatas.Add(new RealTimeData() { Speed = rtd.Speed + 500, Rate = rnd.Next(0, 1000) });
            }
        }

where RealTimeData is a class
    public partial class RealTimeData
    {
        public int Speed { get; set; }
        public double Rate { get; set; }
    }
and RealTimeDatas is an  ObservableCollection of RealTimeData.

I'm not able to view my data on the sfchart.
Have you got any suggestions?

Regards.

Simone Sarzano
Replied On January 17, 2017 10:12 AM

I've improved my code as follow:

private void timer_Tick(Object sender, EventArgs e)
        {           
            int indice = (int)sfChart.Series.IndexOf(realtime);  
            if ( indice < 0 )
            {
                realtime.ItemsSource = rtVM;
                realtime.XBindingPath = "X";
                realtime.YBindingPath = "Y";
                realtime.Interior = new SolidColorBrush(Colors.DarkGreen);
                sfChart.Series.Add(realtime);
            } else {
                var findRealTime = (LineSeries)sfChart.Series[indice];
                int index = (findRealTime.ItemsSource as RealTimeDataViewModel).RealTimeDatas.Count - 1;
                if (index < 0)
                {
                    index = 0;
                    (findRealTime.ItemsSource as RealTimeDataViewModel).RealTimeDatas.Add(new RealTimeData() { X = i.Millisecond, Y = rnd.Next(0, 10000) });
                }
                else
                {
                    RealTimeData rtd = (findRealTime.ItemsSource as RealTimeDataViewModel).RealTimeDatas.Last();
                    (findRealTime.ItemsSource as RealTimeDataViewModel).RealTimeDatas.Add(new RealTimeData() { X = rtd.X + 1, Y = rnd.Next(0, 10000) });
                }
            }
        }

Devi Aruna Maharasi Murugan [Syncfusion]
Replied On January 18, 2017 06:15 AM

Hi Simone, 
  
Thanks for contacting Syncfusion Support. 
  
We have suspected from the provided code snippet that you have set the ItemsSource of series as RealTimeViewModel().RealTimeDatas. But the ItemsSource of chart series is ObservableCollection<RealTimeData>. Since the ItemsSource has not set, the chart series does not get visible. However, we have prepared a demo sample based on your requirement with the reference of provided code snippet and it can be downloaded from below link 
  
  
Regards, 
Devi 





Simone Sarzano
Replied On January 18, 2017 08:26 AM

Thanks a lot for your example.

Regards,

Simone

Devi Aruna Maharasi Murugan [Syncfusion]
Replied On January 18, 2017 10:42 PM

Hi Simone, 
  
Thanks for your update. 
  
Please let us know, if you need any further assistance on this. 
  
Regards, 
Devi 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;