Zoom and Pan with Overview Chart

Demonstrates how to zoom and pan with an Overview Chart using SciChart.js, High Performance JavaScript Charts

Fullscreen

Edit

 Edit

Docs

drawExample.ts

index.tsx

RandomWalkGenerator.ts

theme.ts

Copy to clipboard
Minimise
Fullscreen
1import {
2    EAutoRange,
3    ECoordinateMode,
4    EExecuteOn,
5    EHorizontalAnchorPoint,
6    EllipsePointMarker,
7    EMultiLineAlignment,
8    EVerticalAnchorPoint,
9    EWrapTo,
10    FastLineRenderableSeries,
11    MouseWheelZoomModifier,
12    NativeTextAnnotation,
13    NumberRange,
14    NumericAxis,
15    RubberBandXyZoomModifier,
16    SciChartOverview,
17    SciChartSurface,
18    TextAnnotation,
19    XyDataSeries,
20    XyScatterRenderableSeries,
21    ZoomExtentsModifier,
22    ZoomPanModifier,
23} from "scichart";
24import { appTheme } from "../../../theme";
25import { RandomWalkGenerator } from "../../../ExampleData/RandomWalkGenerator";
26
27export const overviewOptions = {
28    theme: appTheme.SciChartJsTheme,
29};
30
31export const drawExample = async (rootElement: string | HTMLDivElement) => {
32    const { wasmContext, sciChartSurface } = await SciChartSurface.create(rootElement, {
33        theme: appTheme.SciChartJsTheme,
34    });
35
36    // Create and add an XAxis and YAxis
37    sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { visibleRange: new NumberRange(500, 600) }));
38    sciChartSurface.yAxes.add(
39        new NumericAxis(wasmContext, {
40            autoRange: EAutoRange.Always,
41            growBy: new NumberRange(0.1, 0.1),
42        })
43    );
44
45    const POINTS = 1000;
46
47    const data0 = new RandomWalkGenerator().Seed(1337).getRandomWalkSeries(POINTS);
48
49    // Add a line series to the chart
50    sciChartSurface.renderableSeries.add(
51        new FastLineRenderableSeries(wasmContext, {
52            dataSeries: new XyDataSeries(wasmContext, { xValues: data0.xValues, yValues: data0.yValues }),
53            strokeThickness: 3,
54            stroke: appTheme.VividSkyBlue,
55        })
56    );
57
58    const data1 = new RandomWalkGenerator().Seed(42069).getRandomWalkSeries(POINTS);
59
60    // Add a scatter series to the chart
61    sciChartSurface.renderableSeries.add(
62        new XyScatterRenderableSeries(wasmContext, {
63            dataSeries: new XyDataSeries(wasmContext, { xValues: data1.xValues, yValues: data1.yValues }),
64            pointMarker: new EllipsePointMarker(wasmContext, { fill: appTheme.VividPink, strokeThickness: 0 }),
65            strokeThickness: 3,
66        })
67    );
68
69    // Add an annotation with instructions over the chart
70    sciChartSurface.annotations.add(
71        new NativeTextAnnotation({
72            x1: 0.02,
73            y1: 0.02,
74            xCoordinateMode: ECoordinateMode.Relative,
75            yCoordinateMode: ECoordinateMode.Relative,
76            horizontalAnchorPoint: EHorizontalAnchorPoint.Left,
77            verticalAnchorPoint: EVerticalAnchorPoint.Top,
78            fontSize: 18,
79            opacity: 0.55,
80            textColor: appTheme.ForegroundColor,
81            text: "SciChart.js supports an Overview scrollbar. Zoom the main chart or drag the overview to see it update",
82            wrapTo: EWrapTo.ViewRect,
83            multiLineAlignment: EMultiLineAlignment.Left,
84        })
85    );
86
87    // This is the primary approach o. This will automatically bind to the parent surface
88    // displaying its series. Zooming the chart will zoom the overview and vice versa
89    // const overview = await SciChartOverview.create(sciChartSurface, divOverviewId, {
90    //     theme: appTheme.SciChartJsTheme,
91    // });
92
93    // Optional: add some zoom pan interaction
94    sciChartSurface.chartModifiers.add(new ZoomPanModifier({ enableZoom: true }));
95    sciChartSurface.chartModifiers.add(
96        new RubberBandXyZoomModifier({ executeCondition: { button: EExecuteOn.MouseRightButton } })
97    );
98    sciChartSurface.chartModifiers.add(new ZoomExtentsModifier());
99    sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier());
100
101    return { sciChartSurface };
102};
103
104export const drawOverview = async (sciChartSurface: SciChartSurface, divOverviewId: string | HTMLDivElement) => {
105    const overview = await SciChartOverview.create(sciChartSurface, divOverviewId, overviewOptions);
106
107    return {sciChartSurface: overview.overviewSciChartSurface};
108};
109

Zoom and Pan with Overview Chart - React

Overview

This example demonstrates how to integrate SciChart.js within a React application to build an interactive 2D chart with advanced zooming and panning capabilities. The implementation makes use of the <SciChartReact/> component for initializing the chart surface and the SciChartNestedOverview component from scichart-react to embed a synchronized overview chart, providing a comprehensive view of the chart's data.

Technical Implementation

The chart is set up by creating a SciChartSurface with a WebAssembly (WASM) context, numeric axes, and two types of renderable series: a FastLineRenderableSeries and an XyScatterRenderableSeries, both generated using random walk data. An annotation is added to guide user interactions, while multiple interactive modifiers such as ZoomPanModifier, RubberBandXyZoomModifier, ZoomExtentsModifier, and MouseWheelZoomModifier provide dynamic navigation. Detailed guidance on adding zoom and pan behavior can be found in the Tutorial 03 - Adding Zooming, Panning Behavior.

Features and Capabilities

The example offers dynamic update features by propagating user interactions between the main chart and the overview. It leverages performance enhancements from the FastLineRenderableSeries for efficient rendering, and the use of dynamic data visualization maintains a responsive experience. These capabilities make it ideal for applications that require immediate feedback when zooming and panning, ensuring that the entire series data remains in sync.

Integration and Best Practices

Focusing on React integration, the example utilizes the <SciChartReact/> component as outlined in the React Charts with SciChart.js: Introducing “SciChart React” article. The nested overview chart is automatically synchronized with the parent SciChartSurface, a technique aligned with practices described in the SciChartOverview documentation. Additionally, management of the WASM context and chart initialization follows established patterns from the Tutorial 01 - Setting up a project with scichart-react and config object to optimize performance. For deeper insights into rendering performance and optimization strategies, developers can refer to the Performance Optimisation of JavaScript Applications & Charts guide.

react Chart Examples & Demos

See Also: Zoom and Pan a Chart (9 Demos)

React Chart with Multiple X Axes | React Charts | SciChart.js

React Chart with Multiple X Axes

Demonstrates Multiple X & Y Axis on a React Chart using SciChart.js. SciChart supports unlimited left, right, top, bottom X, Y axis with configurable alignment and individual zooming, panning

React Chart with Secondary Y Axes | SciChart.js Demo

React Chart with Secondary Y Axes

Demonstrates Secondary Y Axis on a React Chart using SciChart.js. SciChart supports unlimited, multiple left, right, top, bottom X, Y axis with configurable alignment and individual zooming, panning

Drag React Chart Axis to Scale or Pan | SciChart.js Demo

Drag React Chart Axis to Scale or Pan

Demonstrates how to Zoom, Scale or Pan individual Axis on a React Chart with SciChart.js AxisDragModifiers

Zoom and Pan a Realtime React Chart | SciChart.js Demo

Zoom and Pan a Realtime React Chart

Demonstrates how to zoom and pan a realtime React Chart while it is updating, with SciChart.js ZoomState API

Zoom and Pan with React Chart multiple Modifiers | SciChart

Zoom and Pan with React Chart multiple Modifiers

Demonstrates how to use multiple Zoom and Pan Modifiers on a React Chart with SciChart.js

Virtualized React Charts: Load Data on Zoom/Pan | SciChart

Virtualized React Charts: Load Data on Zoom/Pan

shows how to load data on zoom/pan and how to create an overview chart for this case.

React Polar Modifiers | Polar Interactivity Modifiers

React Polar Modifiers | Polar Interactivity Modifiers Demo

Explore SciChart's Polar Interactivity Modifiers including zooming, panning, and cursor tracking. Try the demo to trial the Polar Chart Behavior Modifiers.

NEW!
High Precision Date Axis | React Charts | SciChart.js Demo

High Precision Date Axis

Demonstrates 64-bit precision Date Axis in SciChart.js handling Nanoseconds to Billions of Years

NEW!
React Overview for SubCharts with Range Selection | SciChart

React Overview for SubCharts with Range Selection

Demonstrates how to build synchronized multi-panel charts with an overview range selector using SciChart.js in React

SciChart Ltd, 16 Beaufort Court, Admirals Way, Docklands, London, E14 9XL.