Pre loader

ZoomExtents() on MVVM


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

  • You must to post comments

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

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

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

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,

  • 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 Burnett-Thompson
    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
Showing 2 results
Your Answer

Please first to submit.

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies