Zooming and Panning
29 Nov 20229 minutes to read
Enable Zooming
Chart supports zooming to view the data clearly. To enable this feature, you need to add an instance of SFChartZoomPanBehavior to the Behaviors collection using the AddChartBehavior method of SFChart.
Following properties are used to configure the zooming feature,
-
EnableZooming– used to enable/disable the pinch zooming. Default value is true. -
EnableDirectionalZooming- Enables or disables the pinch zooming based on pinch gesture direction. The default value of this property is false. -
EnableDoubleTap– when you enable this property, you can double tap on the chart to reset it to the original size or zoom in by one level. -
EnablePanning– used to enable/disable the panning. Default value is true. -
MaximumZoomLevel- used to determine the maximum zoom level of the chart.
Following code snippet illustrates how to enable zooming.
SFChartZoomPanBehavior zoomPan = new SFChartZoomPanBehavior ();
chart.AddChartBehavior (zoomPan);Selection zooming
By specifying EnableSelectionZooming property to true, you can double tap and drag to select a range on the chart to be zoomed in.
Following code snippet illustrates how to enable the box selection zooming,
SFChartZoomPanBehavior zoomPan = new SFChartZoomPanBehavior();
zoomPan.EnableSelectionZooming = true;
chart.AddChartBehavior (zoomPan);Following screenshot shows the box selection on chart area,

Following screenshot shows the zoomed area,

Selection rectangle customization
You can customize the selection rectangle using the below properties.
-
SelectionRectBorderWidth- used to change the border width of selection rectangle. -
SelectionRectStrokeColor- used to change the border color of selection rectangle. -
SelectionRectBorderDashes- used to change the border dashes of selection rectangle. -
SelectionRectFillColor- used to change the fill color of selection rectangle.
Show axis tooltip
The axis tooltip on selection zooming can be enabled using the SFChartAxis.TrackballLabelStyle.Visible property. You can customize the appearance of the axis tooltip by the below properties of SFChartAxis.SFChartTrackballAxisLabelStyle.
-
LabelAlignment- used to change the position of the axis label. -
BorderColor– used to change the label border color. -
BorderWidth– used to change the label border width. -
BackgroundColor– used to change the label background color. -
Color– used to change the label text color. -
Font– used to change the label font size, family and weight. -
LabelFormatter– used to format the label. -
Margin- used to change the margin for label. -
Visible– used to control the visibility of axis tooltip label.
SFChart chart = new SFChart ();
SFNumericalAxis primaryAxis = new SFNumericalAxis();
primaryAxis.TrackballLabelStyle.Visible = true;
chart.PrimaryAxis = primaryAxis;
SFNumericalAxis secondaryAxis = new SFNumericalAxis();
secondaryAxis.TrackballLabelStyle.Visible = true;
chart.SecondaryAxis = secondaryAxis;
SFChartZoomPanBehavior zoomPan = new SFChartZoomPanBehavior();
zoomPan.EnableSelectionZooming = true;
chart.AddChartBehavior (zoomPan);
Zoom Mode
The ZoomMode property specifies whether chart should be allowed to scale along horizontal axis or vertical axis or along both axis. The default value of ZoomMode is SFChartZoomMode.XY (both axis).
Following code example illustrates how to restrict the chart to be zoomed only along horizontal axis,
SFChartZoomPanBehavior zoomPan = new SFChartZoomPanBehavior();
zoomPan.ZoomMode = SFChartZoomMode.X;
chart.AddChartBehavior(zoomPan);
Auto interval on zooming
EnableAutoIntervalOnZooming property determines the update of axis internal based on the current visible range while zooming the chart. Default value of this property is true. If this property is false, the nice internal will not be calculated for new range after zoom in and actual interval will be sustained.
Delegates
We need to implement delegate to deal with the user interactions in chart for zooming and panning. In order to do this,you need to adopt the SFChartDelegate protocol through the class extension as shown below.
public override void ViewDidLoad ()
{
chart.Delegate = new ChartDelegate ();
}
public class ChartDelegate : SFChartDelegate
{
public override void WillZoom (SFChart chart, SFChartZoomingInfo info)
{
}
}WillZoom
The WillZoom delegate is called when the axis is started zooming, the argument includes the SFChartZoomingInfo which is used to get the zooming state. The argument contains the following information.
-
Axis- the zoom start event will be triggered for all the axis in the chart. -
CurrentZoomFactor- used to get the new zoom factor of the corresponding axis. -
CurrentZoomPosition- used to get the new zoom position of the corresponding axis. -
PreviousZoomFactor- used to get the previous zoom factor of the corresponding axis. -
PreviousZoomPosition- used to get the previous zoom position of the corresponding axis. -
Cancel- used to set the value indicating whether the zooming should be canceled. -
State- used to get theSFChartZoomPanStateof the zooming.
DidZoom
The DidZoom delegate is called when the axis is started zooming, the argument includes the SFChartZoomInfo which is used to get the zooming state. The argument contains the following information.
-
Axis- the zoom start event will be triggered for all the axis in the chart. -
CurrentZoomFactor- used to get the new zoom factor of the corresponding axis. -
CurrentZoomPosition- used to get the new zoom position of the corresponding axis. -
PreviousZoomFactor- used to get the previous zoom factor of the corresponding axis. -
PreviousZoomPosition- used to get the previous zoom position of the corresponding axis. -
State- used to get theSFChartZoomPanStateof the zooming.
WillPan
The WillPan delegate is called when the axis is started zooming, the argument includes the SFChartPanningInfowhich is used to get the zooming state. The argument contains the following information.
-
Axis- the zoom start event will be triggered for all the axis in the chart. -
CurrentZoomPosition- used to get the new zoom position of the corresponding axis. -
PreviousZoomPosition- used to get the previous zoom position of the corresponding axis. -
Cancel- used to set the value indicating whether the zooming should be canceled. -
State- used to get theSFChartZoomPanStateof the zooming.
DidPan
The DidPan delegate is called when the axis is started zooming, the argument includes the SFChartPanInfo which is used to get the zooming state. The argument contains the following information.
-
Axis- the zoom start event will be triggered for all the axis in the chart. -
CurrentZoomPosition- used to get the new zoom position of the corresponding axis. -
PreviousZoomPosition- used to get the previous zoom position of the corresponding axis. -
State- used to get theSFChartZoomPanStateof the zooming.
WillSelectionZoom
The WillSelectionZoom delegate is called when the axis is started zooming, the argument includes the SFChartSelectionZoomingInfo which is used to get the zooming state. The argument contains the following information.
-
ZoomRect- contains the bounds of the currently selected region. -
Cancel- used to set the value indicating whether the box selection zooming should be canceled. -
State- used to get theSFChartZoomPanStateof the zooming.
DidSelectionZoom
The DidSelectionZoom delegate is called when the axis is started zooming, the argument includes the SFChartSelectionZoomInfo which is used to get the zooming state. The argument contains the following information.
-
ZoomRect- contains the bounds of the currently selected region. -
State- used to get theSFChartZoomPanStateof the zooming.
DidResetZoom
The DidResetZoom delegate is called when the axis is started zooming, the argument includes the SFChartResetZoomInfo which is used to get the zooming state. The argument contains the following information.
-
Axis– Instance of the axis whose range is changed because of this event. This event is triggered for each axis in the chart. -
PreviousZoomFactor– used to get the previous zoom factor. -
PreviousZoomPosition– used to get the previous zoom position.
Methods
Zooming and panning can be performed programmatically with the following methods:
ZoomIn
The ZoomIn method is used to increase the magnification of the plot area to view the data clearly.
SFChartZoomPanBehavior zoomPan = new SFChartZoomPanBehavior();
zoomPan.ZoomIn();ZoomOut
The ZoomOut method is used to decrease the magnification of the plot area to reset the default view.
SFChartZoomPanBehavior zoomPan = new SFChartZoomPanBehavior();
zoomPan.ZoomOut();Zoom
Zoom(factor)
The Zoom(factor) method is used to change the zoom level by using ZoomFactor.
SFChartZoomPanBehavior zoomPan = new SFChartZoomPanBehavior();
zoomPan.Zoom(0.5f);Zoom(CGRect)
The Zoom(CGRect) method is used to zoom the chart for a given rectangle value.
SFChartZoomPanBehavior zoomPan = new SFChartZoomPanBehavior();
zoomPan.Zoom(new CGRect(10, 10, 200, 350));Zoom(chartAxis, zoomPosition, zoomFactor)
The Zoom(chartAxis, zoomPosition, zoomFactor) method is used to change the zoom level by using ZoomPosition and ZoomFactor. Zoom position value specifies the starting point of zooming, and zoom factor value specifies the level of zooming.
SFChartZoomPanBehavior zoomPan = new SFChartZoomPanBehavior();
zoomPan.Zoom(axis, 0.5f, 0.5f);Zoom(chartAxis, start, end)
The Zoom(chartAxis, start, end) method is used to zoom the given axis to given range.
SFChartZoomPanBehavior zoomPan = new SFChartZoomPanBehavior();
zoomPan.Zoom(axis, 20, 25);ZoomWithCumulativeScale(cumulativeLevel, origin, chartAxis)
The ZoomWithCumulativeScale method is used to change the zoom level of given axis to the specified level and origin.
SFChartZoomPanBehavior zoomPan = new SFChartZoomPanBehavior();
zoomPan.ZoomWithCumulativeScale(0.5, 0.5, axis);Reset
The Reset method is used to return the plot area back to its original position after zooming.
SFChartZoomPanBehavior zoomPan = new SFChartZoomPanBehavior();
zoomPan.Reset();