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

Welcome to the SciChart Community Forums!

Please use the forums below to ask questions about SciChart. Take a moment to read our Question asking guidelines on how to ask a good question and our support policy

We also have a tag=SciChart on Stackoverflow.com where you can earn rep for your questions!

Answered Closed
1
1

We keep getting asked What’s the best practice way to suspend the SciChartSurface in a multi-pane stock chart demo.

We have multiple ways of doing this.

By far the most effective and thread-safe way to do this is to call SuspendUpdates directly on the SciChartSurface. But how to do this in a ViewModel?

  • You must to post comments
Best Answer
1
0

We know this breaks MVVM, but we recommend in really high performance scenarios, passing the SciChartSurface directly to your viewmodel and calling SciChartSurface.SuspendUpdates().

The interface that has SuspendUpdates() is actually ISuspendable. SciChartSurface implements ISuspendable.

// SciChartSurface implements ISciChartSurface and ISuspendable interfaces
public class SciChartSurface : ISciChartSurface, ISuspendable 
{
    // ... 

In order to not break testability of your ViewModels, we recommend you cast to ISuspendable before passing your SciChartSurface to a ViewModel. Then, you can call SuspendUpdates directly (on any thread) and you can still unit test your ViewModels.

Worked Example – Create Multi Pane Stock Charts example

  1. Create a property in your chart pane ViewModel to hold the ISuspendable

     public abstract class BaseChartPaneViewModel : BaseViewModel, IChildPane
     {
         private ISuspendable _scs; 
         public ISuspendable Suspendable
         {
             get { return _scs; } 
             set { _scs = value; }
         }
    
  2. Create an Attached Property to inject the SciChartSurface as ISuspendable

    public class Injector
    {
        public static readonly DependencyProperty PassSurfaceToViewModelProperty = DependencyProperty.RegisterAttached(
           "PassSurfaceToViewModel", typeof(bool), typeof(Injector), new PropertyMetadata(default(bool), OnPassSurfaceToViewModelPropertyChanged));
    
        private static void OnPassSurfaceToViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var scs = d as SciChartSurface;
            if (scs == null || ((bool)e.NewValue) == false) return;
    
            scs.DataContextChanged += (s, arg) => UpdateProperty(scs, scs.DataContext as BaseChartPaneViewModel);
    
            UpdateProperty(scs, scs.DataContext as BaseChartPaneViewModel);
        }
    
        private static void UpdateProperty(SciChartSurface scs, BaseChartPaneViewModel vm)
        {
            if (vm == null) return;
            vm.Suspendable = scs;
        }
    
        public static void SetPassSurfaceToViewModel(DependencyObject element, ISuspendable value)
        {
            element.SetValue(PassSurfaceToViewModelProperty, value);
        }
    
        public static bool GetPassSurfaceToViewModel(DependencyObject element)
        {
            return (bool)element.GetValue(PassSurfaceToViewModelProperty);
        }        
    }
    
  3. Set the attached property in your view. This even works inside SciChartGroup

    <s:SciChartGroup ItemsSource="{Binding ChartPaneViewModels}">
                <s:SciChartGroup.ItemTemplate>
                    <DataTemplate>
    
                        <!-- A lot has been omitted for brevity here. Just set the property on the SciChartSurface -->          
    
                        <s:SciStockChart multiPane:Injector.PassSurfaceToViewModel="True">
    
  4. When the application starts, the SciChartSurface (or SciStockChart) will be passed to the ViewModel, cast to ISuspendable.

You can now call SuspendUpdates() directly ūüôā

  • You must to post comments
Showing 1 result