Updating Chart (Histogram) by binding ItemSources with viewmodel.

I'm now trying to update an energy histogram by using MVVM style WPF.

Code is 

[Model]
 public class Energy
{
     public double ScatterE { get; set; }
     public double AbsorberE { get; set; }
}

[ViewModel]

public class MainViewModel : INotifyPropertyChanged
{
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

          public MainViewModel()
        {
                 Task.Run(() => Loading());
        }

        public void Loading()
        {
               Random rand = new Random();
               var spect = new ObservableCollection<Energy>();
               for (int i = 0; i < 10000; i++)
               {
                     double num = rand.Next(0,1000);
                     spect.Add(new Energy {ScatterE = num, AbsorberE = 0 });

                     if(i % 5000 == 0)
                     {
                              SpectrumData = spect;
                     }
               }
         }

         public ObservableCollection<Energy> SpectrumData{ get; set; }
}

[View]

        <syncfusion:SfChart Grid.Column="1" Margin="5">
            <syncfusion:SfChart.PrimaryAxis>
                <syncfusion:NumericalAxis Header="Energy" Minimum="0" Maximum="1000"/>
            </syncfusion:SfChart.PrimaryAxis>
            <syncfusion:SfChart.SecondaryAxis>
                <syncfusion:NumericalAxis Header="Count" Minimum="0" Maximum="10000"/>
            </syncfusion:SfChart.SecondaryAxis>
            <syncfusion:HistogramSeries ListenPropertyChange="True"
                                                            ItemsSource="{Binding SpectrumData}" 
                                                            XBindingPath="ScatterE" 
                                                            YBindingPath="AbsorberE"  
                                                            HistogramInterval="2" 
                                                            ShowNormalDistributionCurve ="False">
            </syncfusion:HistogramSeries>
        </syncfusion:SfChart>

When I run the program, in the chart there is nothing change and no update of the histogram.

In the performance document, you mention setting ListenPropertyChange true, but It seems like not working.

My environment is Windows 10 and .Net 5. 




1 Reply 1 reply marked as answer

YP Yuvaraj Palanisamy Syncfusion Team January 4, 2021 04:38 PM UTC

Hi SeHoon,  
   
Greetings from Syncfusion.  
  
We have checked your reported problem “Updated data is not reflected in Histogram series” and it will resolved by adding notify property change to your Energy class as per in below code snippet  
   
Code Snippet:  
public class Energy : INotifyPropertyChanged  
{  
    private double scatterE;  
    public double absorberE;  
   
    public double ScatterE  
            {  
                get  
                {  
                    return scatterE;  
                }  
                set  
                {  
                    if(scatterE != value)  
                    {  
                        scatterE = value;  
                        OnPropertyChanged("ScatterE");  
                    }  
                }  
            }  
   
    public double AbsorberE  
            {  
                get  
                {  
                    return absorberE;  
                }  
                set  
                {  
                    if (absorberE != value)  
                    {  
                        absorberE = value;  
                        OnPropertyChanged("AbsorberE");  
                    }  
                }  
            }  
   
    protected void OnPropertyChanged(string propertyName)  
            {  
                if (PropertyChanged != null)  
                {  
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
                }  
            }  
    public event PropertyChangedEventHandler PropertyChanged;  
}  
   
Note: The ListenPropertyChange property of the series is used to improve the performance when we change the Model property value of data in large number of data points.  
  
For more details, please refer the below link.  
 
Regards, 
Yuvaraj 


Marked as answer
Loader.
Up arrow icon