Zoom and Pan a Realtime React Chart

Zoom the real-time chart below by dragging on the surface. Right click and drag to pan. Then double-click to reset zoom and start automatically scrolling again.

src/drawExample.ts
import {
    EExecuteOn,
    EllipsePointMarker,
    EZoomState,
    FastLineRenderableSeries,
    NumberRange,
    NumericAxis,
    RubberBandXyZoomModifier,
    SciChartSurface,
    XyDataSeries,
    XyScatterRenderableSeries,
    ZoomExtentsModifier,
    ZoomPanModifier,
} from "scichart";

import { appTheme } from "./appTheme";

export const drawExample = async (rootElement: string | HTMLDivElement) => {
    // Create the SciChartSurface in the div 'scichart-root'
    // The SciChartSurface, and webassembly context 'wasmContext' are paired. This wasmContext
    // instance must be passed to other types that exist on the same surface.
    const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
        theme: appTheme.SciChartJsTheme,
    });

    // Create an X,Y Axis and add to the chart
    const xAxis = new NumericAxis(wasmContext, { labelPrecision: 0 });
    const yAxis = new NumericAxis(wasmContext);

    sciChartSurface.xAxes.add(xAxis);
    sciChartSurface.yAxes.add(yAxis);

    // Create a Scatter series, and Line series and add to chart
    const scatterSeries = new XyScatterRenderableSeries(wasmContext, {
        pointMarker: new EllipsePointMarker(wasmContext, { width: 7, height: 7, fill: "White", stroke: "SteelBlue" }),
    });
    const lineSeries = new FastLineRenderableSeries(wasmContext, { stroke: "#4083B7", strokeThickness: 2 });
    sciChartSurface.renderableSeries.add(lineSeries, scatterSeries);

    const initialDataPointsCount = 1000;

    // if a required size of data series is known or it is expected to grow,
    // setting a capacity value may give some efficiency boost
    const initialCapacity = 2000;

    // Create and populate some XyDataSeries with static data
    // Note: you can pass xValues, yValues arrays to constructors, and you can use appendRange for bigger datasets
    const scatterData = new XyDataSeries(wasmContext, { dataSeriesName: "Cos(x)", capacity: initialCapacity });
    const lineData = new XyDataSeries(wasmContext, { dataSeriesName: "Sin(x)", capacity: initialCapacity });

    for (let i = 0; i < initialDataPointsCount; i++) {
        lineData.append(i, Math.sin(i * 0.05));
        scatterData.append(i, Math.cos(i * 0.05));
    }

    // Assign these dataseries to the line/scatter renderableseries
    scatterSeries.dataSeries = scatterData;
    lineSeries.dataSeries = lineData;

    //  We disable ZoomExtends animation
    sciChartSurface.chartModifiers.add(new ZoomExtentsModifier({ isAnimated: false }));
    // Realtime zooming example
    sciChartSurface.chartModifiers.add(new RubberBandXyZoomModifier());
    // Realtime panning example
    sciChartSurface.chartModifiers.add(
        new ZoomPanModifier({ executeCondition: { button: EExecuteOn.MouseRightButton } })
    );

    // Part 2: Appending data in realtime
    //
    let timerId: NodeJS.Timeout;

    const updateDataFunc = () => {
        // Append another data-point to the chart. We use dataSeries.count()
        // to determine the current length before appending
        // NOTE: sometimes appending data points in bulk with appendRange can be more efficient
        const i = lineData.count();
        lineData.append(i, Math.sin(i * 0.05));
        scatterData.append(i, Math.cos(i * 0.05));

        // If the zoomState is not UserZooming, then we are viewing the extents of the data
        // In this case, we want to scroll the chart by setting visibleRange = NumberRange(i-1000, i)
        if (sciChartSurface.zoomState !== EZoomState.UserZooming) {
            xAxis.visibleRange = new NumberRange(i - 1000, i);
        }
    };

    const startUpdate = () => {
        // Repeat at 60Hz
        timerId = setInterval(updateDataFunc, 16);
        // Warning, this will repeat forever, it's not best practice!
    };

    const stopUpdate = () => {
        clearTimeout(timerId);
    };

    return { sciChartSurface, controls: { startUpdate, stopUpdate } };
};

Realtime Chart Zoom and Pan - React Example

Overview

This example demonstrates a realtime chart with dynamic data updates using SciChart.js within a React application. The chart continuously displays sine and cosine data while allowing users to interact through zooming and panning.

Technical Implementation

The chart is initialized using the <SciChartReact/> component, which calls an asynchronous function to create a SciChartSurface and associated WebAssembly context. Data is handled by updating two XyDataSeries in real time via a 60Hz interval. Interactive behaviors are added through modifiers such as the ZoomPanModifier for panning with the right mouse button and RubberBandXyZoomModifier for zooming. These modifiers ensure that the visible range of the x-axis is updated dynamically unless the user is actively zooming (determined via the zoomState property), ensuring smooth performance.

Features and Capabilities

The example highlights several advanced features, including realtime data updates and interactive chart manipulation. It efficiently appends new data points to both the line and scatter series, while maintaining performance by updating the visible range only when appropriate. The use of WebAssembly via the SciChartSurface greatly enhances rendering efficiency, ensuring that the chart remains responsive even with continuous data updates.

Integration and Best Practices

Integration with React is achieved by encapsulating the chart creation and update logic within the <SciChartReact/> component. The onInit and onDelete callbacks are used to start and stop the realtime data updates, ensuring that resources are managed correctly during the component lifecycle. This approach follows best practices for React integration and highlights efficient performance optimization techniques. For additional insights on realtime chart updates, refer to the Advanced Realtime React Charts documentation, and to understand more about state management with WebAssembly in SciChart, see the SciChartSurface API Documentation.