SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and now iOS Charting & Android Chart Components

0
0

Hi there, How can i use ZoomExtents() on MVVM ?

  • You must to post comments
1
0

Hi there,

We have an article on various MVVM API’s here.

In order to ZoomExtents() in MVVM the simplest way is to call DataSeries.InvalidateParentSurface(RangeMode.ZoomToFit).

Best regards,
Andrew

  • Ari Sagiv
    Andrew — This doesn’t seem to work for me. I have a RenderableSeriesViewModel bound to a chart, and I would call this function on the renderableSeries’ “DataSeries” parameter (Maybe i’m doing it wrong). Is there an event that I can raise when the renderableSeries data is changed so that I can just do a ZoomExtents from the codebehind or a DelegateCommand? — Ari
  • Andrew
    Hi Ari,There is a known problem where if you update data in ViewModel then immediately call DataSeries.InvalidateParentSurface() it will not zoom to fit — why? Because SciChart has not yet received the data (via WPF’s binding).The best workaround at the moment is to invoke DataSeries.InvalidateParentSurface after a short time period, e.g. 100ms.Best regards, Andrew
  • You must to post comments
1
0

che1024 (and Andrew):

I’d like to offer an alternative, although I’m unsure if it’s appropriate since it violates the view-viewmodel decoupling.

In your ViewModel, make a static delegate, or an action

public class YourViewModel
{
    // This can be an event, if you prefer
    public static Action UpdateChartElements;

    public ObservableCollection<IRenderableSeriesViewModel> renderableSeriesList { get; set; }

    // Rest of class

    // method that plots data
    public void plotData()
    {
        // if you are creating multiple renderable series, make sure you invoke
        // this action OUTSIDE a for or foreach loop

        foreach //...
        {
            renderableSeriesList.Add(...) // your renderable series
        }

        // This is C# 6.0 code. If you're using anything before that, make sure you're
        // checking that your delegate handlers are not null before you invoke this action
        UpdateChartElements?.Invoke();
    }
}

In your codebehind, you can assign a method to the action:

public class YourViewCodeBehind
{
    // Assigning the method to the delegate handler in the constructor
    public YourViewCodeBehind()
    {
        InitializeComponent();
        // Note that because your action is static, it assigns the method on the class level
        ICPComparisonViewModel.UpdateChartElements += zoomExtentsMethod;
    }

    public void zoomExtentsMethod()
    {
        Dispatcher.Invoke(DispatcherPriority.Render, new Action(() =>
        {
            // You can add more UI stuff you want to update here

            // Make sure you label your chart in XAML
            yourChart.ZoomExtents();
        }));
    }
}

By calling the dispatcher, which is the UI thread, you are awaiting everything before rendering to be completed before the chart refreshes, which is why I would say be careful and make sure you’re not putting the dispatch invoke in the middle of a loop or between any possible CPU-intensive tasks.

Hope this helps!

— Ari

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.