2

我异步获取数据并尝试通过 LineSeries 填充绘图,但更新绑定集合(ObservableCollection)时绘图不会刷新。注意:当绑定集合更改时,我有一个调用 InvalidatePlot(true) 的 XAML 行为。

谁能解释为什么情节没有按预期更新?

WPF .Net 4.0 OxyPlot 2014.1.293.1

我有以下 XAML 数据模板,您可以看到 LineSeries ItemsSource 绑定到 ViewModel 中的属性 (PlotData):

<DataTemplate DataType="{x:Type md:DataViewModel}">

    <Grid>

        <oxy:Plot x:Name="MarketDatePlot"
                    Margin="10">
            <oxy:Plot.Axes>
                <oxy:DateTimeAxis Position="Bottom"
                                    StringFormat="dd/MM/yy"
                                    MajorGridlineStyle="Solid"
                                    MinorGridlineStyle="Dot"
                                    IntervalType="Days"
                                    IntervalLength="80" />
                <oxy:LinearAxis Position="Left"
                                MajorGridlineStyle="Solid"
                                MinorGridlineStyle="Dot"
                                IntervalLength="100" />
            </oxy:Plot.Axes>
            <oxy:LineSeries ItemsSource="{Binding Path=PlotData, Mode=OneWay}" />
            <i:Interaction.Behaviors>
                <behaviors:OxyPlotBehavior ItemsSource="{Binding Path=PlotData, Mode=OneWay}" />
            </i:Interaction.Behaviors>
        </oxy:Plot>
    </Grid>

</DataTemplate>

正如我所说,ViewModel 请求并异步填充绑定集合(绑定集合的实际填充发生在 UI 线程上):

public sealed class DataViewModel : BaseViewModel, IDataViewModel
{
    private readonly CompositeDisposable _disposable;
    private readonly CancellationTokenSource _cancellationTokenSource;
    private readonly RangeObservableCollection<DataPoint> _plotData;

    public DataViewModel(DateTime fromDate, DateTime toDate, IMarketDataService marketDataService, ISchedulerService schedulerService)
    {
        _plotData = new RangeObservableCollection<DataPoint>();
        _disposable = new CompositeDisposable();

        if (fromDate == toDate)
        {
            // nothing to do...
            return;
        }

        _cancellationTokenSource = new CancellationTokenSource();

        _disposable.Add(Disposable.Create(() =>
        {
            if (!_cancellationTokenSource.IsCancellationRequested)
            {
                _cancellationTokenSource.Cancel();
            }
        }));

        marketDataService.GetDataAsync(fromDate, toDate)
            .ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    throw new Exception("Failed to get market data!", TaskHelper.GetFirstException(t));
                }

                return t.Result.Select(x => new DataPoint(DateTimeAxis.ToDouble(x.Time), x.Value));
            }, schedulerService.Task.Default)
            .SafeContinueWith(t => _plotData.AddRange(t.Result), schedulerService.Task.CurrentSynchronizationContext);
    }

    public void Dispose()
    {
        _disposable.Dispose();
    }

    public IEnumerable<DataPoint> PlotData
    {
        get { return _plotData; }
    }
}

XAML 行为如下所示:

(我似乎无法粘贴更多代码,所以在保存时不断抛出错误)

4

3 回答 3

6

添加数据时,OxyPlot 不会自动更新。

您必须调用 plotname.InvalidatePlot(true);

它必须在 UI 调度程序线程上运行,即

Dispatcher.InvokeAsync(() => 
{
    plotname.InvalidatePlot(true);
}
于 2014-10-12T12:14:25.613 回答
2

不知道人们是否仍然需要这个,但我在 itemsource 不更新图表时遇到了同样的问题。现有的解决方案都没有帮助我。

好吧,我终于找到了整个事情不起作用的原因。在我实际初始化它之前,我已经将我的集合分配给了 itemsource(新的 Observable ......)。

当我尝试将已经初始化的集合分配给我的 itemsource 时,整个事情开始工作了。

希望这可以帮助某人。

于 2015-02-25T11:39:12.643 回答
2

我知道这是一个老问题,但也许有人会在经过数小时的仔细检查后使用我的答案。我使用 MVVM。我正在使用 await Task.Run(()=> update()); 更新数据 这并没有在我的 UI 中呈现我的情节。我还在设置 PlotModel 之前对其进行了初始化。事实证明,在该 update() 方法中初始化 PlotModel 并没有在我的 UI 中注册。在调用该任务运行之前,我必须对其进行初始化。

public ViewModel()
{
     Plot = new PlotModel(); //(Plot is a property using 
                             // INotifyPropertyChanged)
     PlotGraph = new RelayCommand(OnPlotGraph);
}

public RelayCommand PlotGraph {get; set;}

private async void OnPlotGraph()
{
     await Task.Run(() => Update());
}

private void Update()
{
    var tempPlot = new PlotModel();
    //(set up tempPlot, add data to tempPlot)
    Plot = tempPlot;
}
于 2017-10-26T14:41:56.797 回答