how to update ItemsSource of sfChart.Series

Is it possible to redraw sfChart when Series.ItemsSource changes?

It's OK when adding data to ItemsSource, but sfChart is not redraw when changing the data of ItemsSource already drawn to sfChart (ex. when ItemsSource has 5 drawn data, the change of ItemsSource[0] is not reflected to the chart).

My app keeps getting data. So I want to delete old data and draw only the latest 50 data in sfChart.


6 Replies

DD Devakumar Dhanapoosanam Syncfusion Team November 17, 2021 12:10 PM UTC

Hi Akina, 
 
Greetings from Syncfusion.

We have analyzed your query and we would like to let you know that when dynamically changing the existing datapoint value in ItemsSource we need to enable the ListenPropertyChange property for chart series as true and ChartDataModel should implement the INotifyPropertyChanged as per in the below code example,
 
 
<chart:ColumnSeries  
        ItemsSource="{Binding Data}"  
        XBindingPath="Name"  
        YBindingPath="YValue" ListenPropertyChange="True"  />  
 
public class ChartDataModel : INotifyPropertyChanged  
 
    private double yValue;  
    public double YValue  
    {  
        get { return yValue; }  
        set  
        {  
            if (value != yValue)  
            {  
                yValue = value;  
                RaisePropertyChanged(nameof(YValue));  
            }  
        }  
    }  
  
    private string name;  
    public string Name  
    {  
        get { return name; }  
        set  
        {  
            if (value != name)  
            {  
                name = value;  
                RaisePropertyChanged(nameof(Name));  
            }  
        }  
    }  
 
    public event PropertyChangedEventHandler PropertyChanged;  
  
    void RaisePropertyChanged(string name)  
    {  
        PropertyChanged?.Invoke(thisnew PropertyChangedEventArgs(name));  
    }  
}  
 
 
Please let us know if you need any further assistance on this. 
 
Regards, 
Devakumar D 



AK Akina November 18, 2021 07:26 AM UTC

Hi Devakumar D, 


Thank you for your reply.


I've already implemented that, and I've also checked that PropertyChanged is actually called, but the graph only redraws when a value is added.

Is there anything else I should check, such as how to call ResumeNotification() or SuspendNotification()?


Regards, 

Akina



AK Akina November 19, 2021 07:35 AM UTC

There was a problem to update data.

It works good now.

Thank you for your advice.



DD Devakumar Dhanapoosanam Syncfusion Team November 19, 2021 10:02 AM UTC

Hi Akina, 
 
Thanks for the update. We are glad to know that you have managed to resolve reported problem. 
 
Also, we would like to share using SuspendSeriesNotification() and ResumeSeriesNotification() code example for adding new data points collection. 
 
public class ViewModel   
{   
        ….   
        public Action BeginDataUpdate;   
        public Action EndDataUpdate;   
}   
   
public partial class MainPage : ContentPage   
{   
        ViewModel viewModel;   
        ….   
        public MainPage()   
        {   
            InitializeComponent();   
   
            viewModel = new ViewModel();   
            this.BindingContext = viewModel;   
   
            viewModel.BeginDataUpdate = () => chart.SuspendSeriesNotification();   
            viewModel.EndDataUpdate = () => chart.ResumeSeriesNotification();   
        }   
        ….   
        public async void LoadData()   
        {   
                await Task.Delay(500);   
                 //dynamic change data using suspend and resume    
                 Device.BeginInvokeOnMainThread(() =>   
                 {   
                        //chart.SuspendSeriesNotification() is called when using below code.   
                        viewModel.BeginDataUpdate();   
                         for (int i = 0; i < 100; i++)   
                         {   
                               //Add Data   
                         }   
                         //chart.ResumeSeriesNotification() is called when using below code.   
                         viewModel.EndDataUpdate();    
                  });    
               ….   
         }   
}   
 
Please refer the below help document for Chart performance.     
   
Please refer the below link for increasing chart performance:       
https://www.syncfusion.com/blogs/post/7-tips-to-optimize-xamarin-charts-performance.aspx

Please let us know if you need any further assistance on this.
 
 
Regards, 
Devakumar D 



LU Luca January 7, 2022 10:46 AM UTC

hi,

thanks for this post!

one question, i have animations in my chart but works just on add and not when i update a value. How can i solve it? 

thanks



ME Manivannan Elangovan Syncfusion Team January 10, 2022 07:35 AM UTC

Hi Luca, 


You can enable the animation when updating the value of a data point by invoking the Animate method of the series as shown in the below code snippet. 


private void Button_Clicked(object sender, EventArgs e) 

        { 

            Random random = new Random(); 

            viewModel.Data[5].YValue = random.Next(50, 60); 

            series.Animate();         


Please let us know if you have any other queries.


Thanks,

Manivannan E


Loader.
Up arrow icon