Zoom and Pan with Overview Chart

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

src/drawExample.ts
import {
    EAutoRange,
    ECoordinateMode,
    EExecuteOn,
    EHorizontalAnchorPoint,
    EllipsePointMarker,
    EMultiLineAlignment,
    EVerticalAnchorPoint,
    EWrapTo,
    FastLineRenderableSeries,
    MouseWheelZoomModifier,
    NativeTextAnnotation,
    NumberRange,
    NumericAxis,
    RubberBandXyZoomModifier,
    SciChartOverview,
    SciChartSurface,
    XyDataSeries,
    XyScatterRenderableSeries,
    ZoomExtentsModifier,
    ZoomPanModifier,
} from "scichart";
import { appTheme } from "./appTheme";
import { RandomWalkGenerator } from "./RandomWalkGenerator";

export const overviewOptions = {
    theme: appTheme.SciChartJsTheme,
};

export const drawExample = async (rootElement: string | HTMLDivElement) => {
    const { wasmContext, sciChartSurface } = await SciChartSurface.create(rootElement, {
        theme: appTheme.SciChartJsTheme,
    });

    // Create and add an XAxis and YAxis
    sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { visibleRange: new NumberRange(500, 600) }));
    sciChartSurface.yAxes.add(
        new NumericAxis(wasmContext, {
            autoRange: EAutoRange.Always,
            growBy: new NumberRange(0.1, 0.1),
        })
    );

    const POINTS = 1000;

    const data0 = new RandomWalkGenerator().Seed(1337).getRandomWalkSeries(POINTS);

    // Add a line series to the chart
    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            dataSeries: new XyDataSeries(wasmContext, { xValues: data0.xValues, yValues: data0.yValues }),
            strokeThickness: 3,
            stroke: appTheme.VividSkyBlue,
        })
    );

    const data1 = new RandomWalkGenerator().Seed(42069).getRandomWalkSeries(POINTS);

    // Add a scatter series to the chart
    sciChartSurface.renderableSeries.add(
        new XyScatterRenderableSeries(wasmContext, {
            dataSeries: new XyDataSeries(wasmContext, { xValues: data1.xValues, yValues: data1.yValues }),
            pointMarker: new EllipsePointMarker(wasmContext, { fill: appTheme.VividPink, strokeThickness: 0 }),
            strokeThickness: 3,
        })
    );

    // Add an annotation with instructions over the chart
    sciChartSurface.annotations.add(
        new NativeTextAnnotation({
            x1: 0.02,
            y1: 0.02,
            xCoordinateMode: ECoordinateMode.Relative,
            yCoordinateMode: ECoordinateMode.Relative,
            horizontalAnchorPoint: EHorizontalAnchorPoint.Left,
            verticalAnchorPoint: EVerticalAnchorPoint.Top,
            fontSize: 18,
            opacity: 0.55,
            textColor: appTheme.ForegroundColor,
            text: "SciChart.js supports an Overview scrollbar. Zoom the main chart or drag the overview to see it update",
            wrapTo: EWrapTo.ViewRect,
            multiLineAlignment: EMultiLineAlignment.Left,
        })
    );

    // This is the primary approach o. This will automatically bind to the parent surface
    // displaying its series. Zooming the chart will zoom the overview and vice versa
    // const overview = await SciChartOverview.create(sciChartSurface, divOverviewId, {
    //     theme: appTheme.SciChartJsTheme,
    // });

    // Optional: add some zoom pan interaction
    sciChartSurface.chartModifiers.add(new ZoomPanModifier({ enableZoom: true }));
    sciChartSurface.chartModifiers.add(
        new RubberBandXyZoomModifier({ executeCondition: { button: EExecuteOn.MouseRightButton } })
    );
    sciChartSurface.chartModifiers.add(new ZoomExtentsModifier());
    sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier());

    return { sciChartSurface };
};

export const drawOverview = async (sciChartSurface: SciChartSurface, divOverviewId: string | HTMLDivElement) => {
    const overview = await SciChartOverview.create(sciChartSurface, divOverviewId, overviewOptions);

    return { sciChartSurface: overview.overviewSciChartSurface };
};

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.