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. Image for the cookie policy date
close icon

SfChart add realtime data

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.

4 Replies

SS Simone Sarzano January 17, 2017 03:12 PM UTC

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) });
                }
            }
        }


DA Devi Aruna Maharasi Murugan Syncfusion Team January 18, 2017 11:15 AM UTC

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 






SS Simone Sarzano January 18, 2017 01:26 PM UTC

Thanks a lot for your example.

Regards,

Simone


DA Devi Aruna Maharasi Murugan Syncfusion Team January 19, 2017 03:42 AM UTC

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


Loader.
Live Chat Icon For mobile
Up arrow icon