【问题标题】:Refreshing OxyPlot Chart刷新 OxyPlot 图表
【发布时间】:2016-05-27 19:39:31
【问题描述】:

我正在开发一个 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");
  }
}

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

【问题讨论】:

  • 如果情节未更新,则更有可能是由于缺少通知。检查属性名称中的拼写错误(最好使用NotifyPropertyChanged(nameof(Points)),因为编译器会进行检查)。也许有一种特殊的方法来更新地块,尝试找到这种方法(Refresh?)。通过更改可见性(系列?情节?)来检查您对可见性更改的想法是否正确。
  • 请参阅"Should questions include “tags” in their titles?",其中的共识是“不,他们不应该”!

标签: wpf xaml oxyplot


【解决方案1】:

在您的 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">

这应该可以解决问题

【讨论】:

    【解决方案2】:

    其实我找到了答案here

    OxyPlot 没有观察属性或集合的变化。这意味着客户端应用程序负责在内容发生更改时刷新绘图。

    您必须在更改后使情节无效。有几个possibilities

    最简单的可能是 MVVM:在视图中订阅视图模型PropertyChanged 事件,检查由Points 引发的此事件是否已更改并使绘图无效。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2022-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 2014-06-25
        • 1970-01-01
        • 2017-07-13
        相关资源
        最近更新 更多