如何在按钮单击时更新WPFToolkit AreaSeries图表

时间:2016-08-02 21:02:37

标签: c# .net wpf charts wpftoolkit

我正在制作基于WPFToolkit的Graph,我每次点击按钮时都会尝试更新AreaSeries。 我在我的数据类上实现了INotifyPropertyChanged。但是当我重新加载源对象中的数据时,id不会更新图表(目标对象)

代码如下:

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

    }



}

}

数据模型在这里

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

它在启动时完全加载,因为我在开始时调用了LoadData()。 问题是,当我点击刷新按钮时,它会加载源对象中的数据,但目标对象的数据未更新,即图表未更新,它保持与初始数据相同。

1 个答案:

答案 0 :(得分:1)

使用ObservableCollection<Ready4LOS>,而不是List<Ready4LOS>ObservableCollection<>已经实施INotifyPropertyChangedINotifyCollectionChanged。如果您要为集合中已有的INotifyPropertyChanged动态更改Ready4LOSCase的值,则可能只需要为Time Ready4LOS实施/* @pjs preload="C:\\Users\\Kevin\\Desktop\\August 1, 2016\\cropped\\Sky4.JPG"; */ PImage myImage; void setup(){ size(200,200); myImage = loadImage("C:\\Users\\Kevin\\Desktop\\August 1, 2016\\cropped\\Sky4.JPG"); } void draw(){ image(myImage, 50,50, 100,100); }

enter image description here