刷新 OxyPlot 图表

Refreshing OxyPlot Chart

我正在开发 WPF 应用程序。此应用程序使用 OxyPlot chart control。我让用户执行计算。用户可以以网格和图表形式查看结果。为了做到这一点,我有以下内容:

home.xaml

<DataGrid x:Name="resultDataGrid" ItemsSource="{Binding Items}">
  <DataGrid.Columns>
    <DataGridTextColumn Header="#" Binding="{Binding Number}" />
    <DataGridTextColumn Header="Description" Binding="{Binding Description}" />
  </DataGrid.Columns>
</DataGrid>

<oxy:Plot x:Name="resultGraph" Background="Transparent" Height="400" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" Visibility="Collapsed">
  <oxy:Plot.Series>
    <oxy:LineSeries ItemsSource="{Binding Points}" MarkerType="Circle"  Title="Result" />
  </oxy:Plot.Series>
</oxy:Plot>

<StackPanel Orientation="Horizontal">
  <Button x:Name="executeButton" Click="executeButton_Click" />
  <Button x:Name="viewData" Click="viewData_Click" />
  <Button x:Name="viewChart" Click="viewChart_Click" />
</StackPanel>

home.xaml.cs

public partial class Home: Page
{
  private MyViewModel viewModel = null;
  public Home()
  {
    InitializeComponent();

    viewModel = new MyViewModel();
    this.DataContext = viewModel;
  }

  private void executeButton_Click(object sender, RoutedEventArgs e) {
    viewModel.Execute();
  }

  private void viewData_Click(object sender, RoutedEventArgs e) 
  {
    resultDataGrid.Visibility = System.Windows.Visibility.Visible;
    resultGraph.Visibility = System.Windows.Visibility.Collapsed;
  }

  private void viewChart_Click(object sender, RoutedEventArgs e)
  {
    resultDataGrid.Visibility = System.Windows.Visibility.Collapsed;
    resultGraph.Visibility = System.Windows.Visibility.Visible;
  }
}

MyViewModel.cs

public class MyViewModel : INotifyPropertyChanged
{
  public Dictionary<int, string> Items{ get; set; }
  public IList<DataPoint> Points{ get; set; }

  public void Execute() 
  {
    this.Items = new Dictionary<int, string>();
    this.Points = new List<DataPoint>();

    var randomNumber = GetRandomNumber();
    for (var i=0; i<10; i++) 
    {
      this.Items.Add((i+1), "Item #" + i);

      var dataPoint = new DataPoint(i, (randomNumber*i));
      this.Points.Add(dataPoint);
    }

    NotifyPropertyChanged("Items");
    NotifyPropertyChanged("Points");
  }
}

我的问题是,当用户单击 "Execute" 按钮时,DataGrid 中的数据会刷新,这正是我所期望的。但是,图表只有在图表首次可见时才会刷新。换句话说,如果我在 DataGrid 可见时单击 "execute",图表中的绘图点将变得陈旧。但是,如果 resultGraph 可见并单击 "Execute",则点会按预期刷新。我不明白我做错了什么。感谢任何帮助。

其实我找到了答案here:

OxyPlot is not observing property or collection changes. This means that the client application is responsible for refreshing the plot when the content has changed.

更改后必须使绘图无效。有几个possibilities.

最简单的可能是 dirty MVVM:在视图订阅 viewmodel PropertyChanged 事件中,检查此事件是否由 Points 改变并使情节无效。

在您的 ViewModel 中创建

private int _invalidateFlag = 0;
public int InvalidateFlag
{
   get {return _invalidateFlag;}
   set
   {
      _invalidateFlag = value;
      NotifyPropertyChanged();
   }
}

//and to the end of Execute() add

InvalidateFlag++;

也不要忘记绑定 InvalidateFlag

<oxy:Plot x:Name="resultGraph" InvalidateFlag="{Binding InvalidateFlag}" Background="Transparent" Height="400" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" Visibility="Collapsed">

这应该可以解决问题

通常 OxyPlot 提供 PlotModel.InvalidatePlot(true) 方法来使用新的输入数据刷新图表。 编辑:哦,我看到你的 ViewModel 中没有 PlotModel 属性。我的答案需要这样一个 属性 也可以绑定到 xaml。