We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to stop Primary and Secondary Axis changes when zoomed

I have a LineChart that uses the FastLineBitmapSeries and is updated with new data every 5 seconds.  My question is how do I stop the axis from being scaled for the new data when the user has the graph zoomed into a specific point.   Currently if the user zooms in and new data is added to the chart then the desired zoom position changes even though the ZoomFactor and ZoomPosition have not changed.  I want the axis to stay at its current scaling when zoomed so that the zoomed point does not move off the screen as new data is added.

Any help or hints would be great!  Thanks

6 Replies

SS Suresh S Syncfusion Team September 16, 2014 09:07 AM UTC

Hi Michael,


Thanks for using Syncfusion products.


We have prepared the sample based on your requirement. Please download it from the given below location.


Please let us know if you request further assistance on this.


Regards,

Suresh S


Attachment: RealtimeUpdate_2720b28f.zip


MH Michael Hamilton September 17, 2014 05:36 PM UTC

Thank you so much!


MH Michael Hamilton September 18, 2014 06:58 PM UTC

Hello,

I took the example from the previous reply and changed some things.  I wanted to throw these changes out to see if anyone thinks this is good or not.  It seems to be working but I'm still testing .  The LimitAxisZoom call just limits the value to 0 and 1.   Is this needed, or will the ZoomFactor and ZoomPosition automatically limit to 0 and 1?

private void NumericalAxis_ActualRangeChanged(object sender, Syncfusion.UI.Xaml.Charts.ActualRangeChangedEventArgs e)
{
    if (_graph != null)
    {
if (!e.IsScrolling && _graph.SecondaryAxis.ZoomFactor < 1 && _lastNumericalAxis != null)
{
   // Determine the range of the new axis
   Double newRange = (Double)e.ActualMaximum - (Double)e.ActualMinimum;

   // Determine the change for the max and min
   Double deltaMax = (Double)e.ActualMaximum - (Double)_lastNumericalAxis.ActualMaximum;
   Double deltaMin = (Double)e.ActualMinimum - (Double)_lastNumericalAxis.ActualMinimum;

   // Determine the total percent change in relation to the new range
   Double totalPercentChange = (deltaMax + deltaMin) / newRange;

   // Adjust the zoom factor based on the percent change of the total range
   _graph.SecondaryAxis.ZoomFactor = LimitAxisZoom(_graph.SecondaryAxis.ZoomFactor - (totalPercentChange * _graph.SecondaryAxis.ZoomFactor));

   // Determine the max and min percent change in relation to the new range
   Double maxPercentChange = deltaMax / newRange;
   Double minPercentChange = deltaMin / newRange;

   // Adjust the zoom position based on the max and min percent change of the total range
   _graph.SecondaryAxis.ZoomPosition =  LimitAxisZoom(_graph.SecondaryAxis.ZoomPosition + (minPercentChange * _graph.SecondaryAxis.ZoomPosition) - (maxPercentChange * _graph.SecondaryAxis.ZoomPosition));

}

// Remember for next time
_lastNumericalAxis = e;

    }
}



MH Michael Hamilton September 18, 2014 10:57 PM UTC

I think I made that way harder than it should have been.  This is my new approach and it seems to be working great for the X axis.


if (!e.IsScrolling && _graph.PrimaryAxis.ZoomFactor < 1 && _lastDateTimeAxis != null)
{
    // Determine the range of the new and old axis
    TimeSpan newRange = (DateTime)e.ActualMaximum - (DateTime)e.ActualMinimum;
    TimeSpan oldRange = (DateTime)_lastDateTimeAxis.ActualMaximum - (DateTime)_lastDateTimeAxis.ActualMinimum;

    // Adjust the zoom factor and position
    _graph.PrimaryAxis.ZoomFactor = LimitAxisZoom((_graph.PrimaryAxis.ZoomFactor * oldRange.TotalMilliseconds) / newRange.TotalMilliseconds);

    _graph.PrimaryAxis.ZoomPosition = LimitAxisZoom((_graph.PrimaryAxis.ZoomPosition * oldRange.TotalMilliseconds) / newRange.TotalMilliseconds);
}

// Remember for next time
_lastDateTimeAxis = e;



MH Michael Hamilton September 19, 2014 07:22 PM UTC

Ok, I don't think changing the ZoomFactor or ZoomPosition within the Axis_ActualRangeChanged event is working very well.  

I do have one request for a possible improvement.  That request would be to add the last ActualMinimum and last ActualMaximum to the ActualRangeChangedEventArgs class.  These would be the values that they were before the range changed.


All in all this is what I ended up with:

    if (_lastNumericalAxis != null && _graph.SecondaryAxis.ZoomFactor < 1.0)
    {
// Determine the range of the last axis and calculate the new visible minimum and maximum values
Double range = (Double)_lastNumericalAxis.ActualMaximum - (Double)_lastNumericalAxis.ActualMinimum;
Double minPosition = (Double)_lastNumericalAxis.ActualMinimum + (_graph.SecondaryAxis.ZoomPosition * range);
Double maxPosition = minPosition + (_graph.SecondaryAxis.ZoomFactor * range);

// Set the new visible minimum and maximum values
e.VisibleMinimum = minPosition;
e.VisibleMaximum = maxPosition;

    }

    // Remember for next time
    _lastNumericalAxis = new AxisRange { ActualMaximum = e.ActualMaximum, ActualMinimum = e.ActualMinimum };


SS Suresh S Syncfusion Team September 25, 2014 04:14 AM UTC

Hi Michael,


Sorry for the inconvenience caused.


Axis default ZoomPosition will be 0 and ZoomFactor is 1. Whenever chart zooming happens their limit will be changed between 0 to 1.


We have analysed your previous code snippet, we have acheived your requirement axis being scaled when adding new data. Also, we have prepared the sample based on your requirement. Please download it from the given below location.



Please let us know if you request further assistance on this.


Regards,
Suresh S

Attachment: AxisChanged_ddfcecb1.zip

Loader.
Live Chat Icon For mobile
Up arrow icon