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 (Angular)

Overview

This example demonstrates how to integrate SciChart.js into an Angular application to create high-performance, interactive 2D charts with a synchronized SciChartOverview scrollbar. The chart offers advanced zooming and panning capabilities, enabling users to interact with large datasets in real time. For an introductory guide, see Getting Started with SciChart JS.

Technical Implementation

The chart is initialized by creating a SciChartSurface with a WebAssembly context and configuring numeric axes for dynamic scaling. Two renderable series are added: a FastLineRenderableSeries and an XyScatterRenderableSeries, each leveraging random walk data to simulate dynamic input. Interactive modifiers such as ZoomPanModifier, RubberBandXyZoomModifier, ZoomExtentsModifier, and MouseWheelZoomModifier are incorporated to enable seamless zooming and panning, as detailed in Tutorial 03 - Adding Zooming, Panning Behavior.

Features and Capabilities

The example showcases real-time update capabilities, with the main chart and its overview scrollbar created via SciChartOverview.create() remaining synchronized during user interactions. Additional features include embedded annotations for enhanced user guidance and multiple renderable series for diverse data visualization. The built-in overview chart allows users to maintain a global perspective of chart data, an approach aligned with the practices described in the SciChartOverview documentation.

Integration and Best Practices

Integrating SciChart.js within an Angular project requires careful management of component lifecycles and dependency injection to optimize the WebAssembly context. Developers are encouraged to employ Angular lifecycle hooks and modular configuration from the scichart-angular package for maintaining chart performance and scalability.