NativeScript Core

Chart Axes Pan & Zoom

If you followed the getting started article, you now know how to create a chart and add it to a NativeScript page. In this article, you will learn how to enable the pan and zoom feature that allows users to zoom in and out of the chart through pinch gesture and pan through swipe.

Getting Started

There are two boolean properties that enable this feature:

  • allowZoom - Used to enable zooming by the axis.
  • allowPan - Used to enable panning by the axis.

Zoom Factor

You can programmatically define a zoom factor by which the chart will zoom. This is done via two properties exposed by RadCartesianChart:

  • horizontalZoom - Defines the zoom factor applied to the horizontal axis if zoom is enabled on it.
  • verticalZoom - Defines the zoom factor applied to the vertical axis if zoom is enabled on it.

For example, if you set one of these properties to 1.5 this will apply a 50% zoom relative to the original zoom factor of 1.

Events

To notify you when the selection state of an item is changed, the chart exposes the following events:

  • chartZoomed - Fired multiple times when the chart is zooming. The event data argument provides information about the event name and the chart that is zoomed.
  • chartPanned - Fired multiple times when the chart is panning. The event data argument provides information about the event name and the chart that is panned.

With the following example you can see that pan & zoom properties could be used for any axis assigned to a series or to the chart along with events handling.

Example 1: Pan and Zoom Enabled

<chart:RadCartesianChart id="PanZoomChart" >
    <chart:RadCartesianChart.series>
       <chart:BarSeries items="{{ categoricalSource }}" categoryProperty="Country" valueProperty="SecondVal">
            <chart:BarSeries.verticalAxis>
                <chart:LinearAxis horizontalLocation="Right" allowZoom="true" allowPan="false"/>
            </chart:BarSeries.verticalAxis>
        </chart:BarSeries>

        <chart:LineSeries items="{{ categoricalSource }}" categoryProperty="Country" valueProperty="Amount">
            <chart:LineSeries.verticalAxis>
                <chart:LinearAxis horizontalLocation="Left" allowPan="false" allowZoom="true"/>
            </chart:LineSeries.verticalAxis>
        </chart:LineSeries>
    </chart:RadCartesianChart.series>

    <chart:RadCartesianChart.horizontalAxis>
        <chart:CategoricalAxis allowPan="false" allowZoom="true"/>
    </chart:RadCartesianChart.horizontalAxis>

</chart:RadCartesianChart>

References

Want to see this scenario in action? Check our SDK Examples repository on GitHub. You will find this and many other practical examples with NativeScript UI.

Examples used in this article:

Related articles you might find useful: