rvsingh42
rvsingh42

Reputation: 71

How To Update WPFToolkit AreaSeries Chart On Button Click

I am making WPFToolkit based Graph where i am trying to update AreaSeries every time i click on a button. I have implemented INotifyPropertyChanged on my data class. but when i reload the data in the source object id doesn't updates in chart(target object)

the code is as below:

 public partial class MainWindow : Window
{

   static List<Ready4LOS> Ready4LOS = new List<Data.Ready4LOS>();





    public MainWindow()
    {
        InitializeComponent();

        chart1.DataContext = Ready4LOS;
        InitChart();
        LoadData();
    }

    private void LoadData()
    {
        var path = @"zxzxzxz.log";
        Ready4LOS.Clear();
        List<APISTATDataModel> daa = APISTATDataModel.GetFromFile(path, new string[] { "|" }, "Ready4TOS");

        List<APISTATDataModel> lastn = daa.GetRange(daa.Count - 10, 10);

        foreach (APISTATDataModel d in lastn)
        {
            Ready4LOS.Add(new Ready4LOS() { Case = d.Current_Count, Time = d.Current_Time });

        }

    }

    private void InitChart()
    {
        System.Windows.Data.Binding indi = new System.Windows.Data.Binding("Case");
        System.Windows.Data.Binding dep = new System.Windows.Data.Binding("Time");
        dep.Mode = System.Windows.Data.BindingMode.OneWay;
        indi.Mode = System.Windows.Data.BindingMode.OneWay;
        AreaSeries ares = new AreaSeries();
        ares.ItemsSource = Ready4LOS;
        ares.IndependentValueBinding = dep;
        ares.DependentValueBinding = indi;
        ares.Title = "Ready4LOS";

        DateTimeAxis dta = new DateTimeAxis();
        dta.Interval = 10;
        dta.IntervalType = DateTimeIntervalType.Minutes;
        dta.Title = "Time";
        dta.Orientation = AxisOrientation.X;
     // dta.Minimum = DateTime.Now.AddMinutes(-90);
      //  dta.Maximum = DateTime.Now;

        LinearAxis yaxis = new LinearAxis();
        yaxis.Minimum = 0;
        yaxis.Interval = 2;
        yaxis.Title = "Case";
        yaxis.Orientation = AxisOrientation.Y;
        yaxis.ShowGridLines = true;
        chart1.Axes.Add(yaxis);
        chart1.Axes.Add(dta);
        chart1.Series.Add(ares);



    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        LoadData();
        chart1.UpdateLayout();

    }



}

}

the data model is here

    class Ready4LOS : INotifyPropertyChanged
{
    int _case;
    DateTime _time;

    public int Case
    {
        get
        {
            return _case;

        }

        set
        {
            _case = value;
            NotifyPropertyChanged("Case");
        }
    }

    public DateTime Time
    {
        get
        {
            return _time;
        }

        set
        {
            _time = value;
            NotifyPropertyChanged("Time");
        }
    }



    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

It loads perfectly when it starts as i've called the LoadData() in the beginning. The problem is when i click on the refresh button it loads the data in the source object but the target object's data in not updated i.e. chart is not updated it remains the same as of initial data.

Upvotes: 1

Views: 156

Answers (1)

jsanalytics
jsanalytics

Reputation: 13188

Use ObservableCollection<Ready4LOS>, not List<Ready4LOS>. ObservableCollection<> already implements INotifyPropertyChanged and also INotifyCollectionChanged. Your implementation of INotifyPropertyChanged for Ready4LOS may only be necessary if you're going to dynamically change values for Case and Time for existing Ready4LOS already in your collection.

enter image description here

Upvotes: 1

Related Questions