SciChart WPF 2D Charts > Advanced 2D Charting Topics > ViewportManager - Full Control over Axis Ranges and Viewport
ViewportManager - Full Control over Axis Ranges and Viewport

The ViewportManager API provides fine-grained control over the SciChartSurface viewport, allowing you to configure the result of VisibleRange calculations on a per-axis basis, invalidate the chart from a ViewModel and call methods like ZoomExtents() programmatically from a ViewModel.

Declaring a Binding to a ViewportManager in Code

You can declare a viewport manager and assign to a SciChartSurface using the following code. By default SciChart has a DefaultViewportManager assigned. You can also inherit this type and provide your own Viewport Managers.

Declaring a Binding to a ViewportManager
Copy Code
var sciChartSurface = new SciChartSurface();
var viewportManager = new DefaultViewportManager();
sciChartSurface.ViewportManager = viewportManager;

Declaring a Binding to a ViewportManager in MVVM

The same thing can be achieved in MVVM, by declaring a ViewportManager instance in a ViewModel and binding to it in the view.

Declaring a Binding to a ViewportManager in MVVM
Copy Code
<!-- View (XAML) -->

<!-- where xmlns:s="http://schemas.abtsoftware.co.uk/scichart" -->
<s:SciChartSurface ViewportManager="{Binding ViewportManager}">
   <!-- XAxis, YAxis, RenderableSeries omitted for brevity -->
</s:SciChartSurface>

 

Declaring a Binding to a ViewportManager in MVVM
Copy Code
// Viewmodel
private DefaultViewportManager _viewportManager = new DefaultViewportManager();
public IViewportManager ViewportManager
{
   get { return _viewportManager; }
   set
   {
      _viewportManager = value;
         OnPropertyChanged("ViewportManager");
   }
}

Manipulating the Chart from the ViewModel

You can call the following functions on a ViewportManager from a ViewModel. Use these to have direct control over the SciChartSurface in absence of a SciChartSurface instance itself.

Assuming you have bound a ViewportManager as per Declaring a Binding to a ViewportManager in MVVM

Manipulating the Chart from the ViewModel
Copy Code
public class MyViewModel : INotifyPropertyChanged
{
    // Viewmodel
       private DefaultViewportManager _viewportManager = new DefaultViewportManager();
       public IViewportManager ViewportManager
       {
          get { return _viewportManager; }
          set
          {
               _viewportManager = value;
               OnPropertyChanged("ViewportManager");
          }
       }
      
       public void Foo()
       {
             // All things you can do from the ViewModel with a ViewportManager
             // We don't recommend doing them all at once! :)
             _viewportManager.ZoomExtents();
              _viewportManager.AnimateZoomExtents(TimeSpan.FromMilliseconds(500));
             _viewportManager.ZoomExtentsY();
             _viewportManager.AnimateZoomExtentsY(TimeSpan.FromMilliseconds(500));
             _viewportManager.ZoomExtentsX();
             _viewportManager.AnimateZoomExtentsX(TimeSpan.FromMilliseconds(500));
       }
}

Overriding Axis VisibleRanges

The most powerful feature of the ViewportManager is the abilty to intercept the Axis VisibleRange calculations and return your own custom range. Use this if you want to have full control over the axis, not just set a new VisibleRange.

Define your own ViewportManager as follows:

Overriding Axis VisibleRanges
Copy Code
public class MyViewportManager : DefaultViewportManager
{
       protected override IRange OnCalculateNewXRange(IAxis xAxis)
       {
             var baseRange = base.OnCalculateNewXRange(xAxis);
            
             // Do some manipulation to the range
            
             return baseRange;
       }
      
    protected override IRange OnCalculateNewYRange(IAxis yAxis, RenderPassInfo renderPassInfo)
    {
        var baseRange = base.OnCalculateNewYRange(yAxis, renderPassInfo);
            
              // Do some manipulation to the range
            
             return baseRange;
    }
}

Then attach it to a chart. If you put a breakpoint in your OnCalculateNewXRange / OnCalculateNewYRange methods you will see these being hit when the chart resizes or redraws and you can manipulate the calculated visible range passed to SciChart as you wish.