Using VisibleRangeLimit API
Posted by Admin - on 21 March 2014 11:20 AM

This API allows you to set range limit when you’re performing ZoomExtents on chart. You can use this feature when you don’t want to display all data range when performing ZoomExtents.

If you want to use this feature you should set VisibleRangeLimit of AxisBase and when you’re performing ZoomExtents on axis VisibleRange should be clipped to VisibleRangeLimit.

For example we have axis without any limits (VisibleRangeLimit = null). When we perform ZoomExtents, say the XAxis gets the visible range (0;100):

Actual data range = (0, 100) ; VisibleRange after ZoomExtents = (0, 100)

 

After setting VisibleRangeLimit = new DoubleRange(10, 90) and using ZoomExtendt we now get:

Actual data range = (0, 100) ; VisibleRange after ZoomExtents = (10, 90)

 

 

Example of using VisibleRangeLimit

XAML – MVVM

<s:NumericAxis VisibleRangeLimit="{Binding XLimit}" />


Cs

xAxis.VisibleRangeLimit = new DoubleRange(0.0, 100.0);



NOTE:  VisibleRangeLimit doesn’t clip data range when VisibleRangeLimit is greater than data range. In such case after ZoomExtents you’ll get the actual data range.

For example we have data range = (0, 10), VisibleRangeLimit = (-100, 100)  and we’ll get VisibleRange = (0, 10). So if data range intersects VisibleRangeLimit range it would be clipped to VisibleRangeLimit.

 

VisibleRangeLimitMode

Another property which can be used to change how the VisibleRangeLimit works is AxisBase.VisibleRangeLimitMode

Use this property if you wish to ensure that one side of the chart is always clipped, while the other side is not. For instance:

 

<!-- Ensures VisibleRangeLimit of 0 is applied to the VisibleRange.Min -->
<!-- The VisibleRange.Max is unbounded -->
<s:NumericAxis VisibleRangeLimit="0,0" VisibleRangeLimitMode="Min"/>

Advanced VisibleRange Clipping and Manipulation

So the VisibleRangeLimit is a useful API to ensure the axis clips the VisibleRange when zooming to extents. But it won't stop a user from scrolling outside of that range. 

What if you wanted to ensure that a certain range was never exceeded? Simple, just subscribe to AxisBase.VisibleRangeChanged and set the range to whatever you want

 

axis.VisibleRangeChanged += (s, e) => 
{
   // e is VisibleRangeChangedEventArgs
   // Assuming axis is NumericAxis

   if (e.NewVisibleRange != null && e.NewVisibleRange.Min < 0)
   {
      // Force minimum visiblerange to zero always
      ((NumericAxis)sender).VisibleRange = new DoubleRange(0, e.NewVisibleRange.Max);
   }
};

Great! How to do that in MVVM? 

Glad you asked! Well, you could use EventToCommand to push the VisibleRangeChanged event into a ViewModel. Or, our preferred method would be to create an Attached Behavior

public class AxisClippingBehavior : Behavior<AxisBase>
{
    protected override void OnAttached()
    {
        base.OnAttached();
 
        AssociatedObject.VisibleRangeChanged +=OnVisibleRangeChanged;
    }
 
    protected override void OnDetaching()
    {
        base.OnDetaching();
 
        AssociatedObject.VisibleRangeChanged -= OnVisibleRangeChanged;
    }
 
    private void OnVisibleRangeChanged(object sender, VisibleRangeChangedEventArgs visibleRangeChangedEventArgs)
    {
        var visibleRangeLimit = AssociatedObject.VisibleRangeLimit;
        if (visibleRangeLimit != null)
        {
            var limitMode = AssociatedObject.VisibleRangeLimitMode;
 
            var range = (IRange)AssociatedObject.VisibleRange.Clone();
            range.ClipTo(visibleRangeLimit, limitMode);
 
            AssociatedObject.SetCurrentValue(AxisBase.VisibleRangeProperty, range);
        }
    }
}

And to use it you just need to attach it to appropriate axis 

<s:NumericAxis>
    <i:Interaction.Behaviors>
        <clipAxis:AxisClippingBehavior />
    </i:Interaction.Behaviors>
</s:NumericAxis>

 

(2 vote(s))
Helpful
Not helpful

CONTACT US

Not sure where to start? Contact us, we are happy to help!


CONTACT US

SciChart Ltd, 16 Beaufort Court, Admirals Way, Docklands, London, E14 9XL. Email: Legal Company Number: 07430048, VAT Number: 101957725