React Chart with Vertically Stacked Axes

Vertically-Stacked Axis in SciChart.js allows several traces with independent Y-axis to be placed on the same chart, stacking the Y-Axis and enabling an ECG/EEG-style trace. Great for neurological apps, medical apps, earthquake monitoring.

src/drawExample.ts
import { appTheme } from "./appTheme";

import { EWrapTo, NativeTextAnnotation, TSciChart } from "scichart";

import {
    EAxisAlignment,
    ECoordinateMode,
    EExecuteOn,
    EHorizontalAnchorPoint,
    EXyDirection,
    FastLineRenderableSeries,
    LeftAlignedOuterVerticallyStackedAxisLayoutStrategy,
    MouseWheelZoomModifier,
    NumberRange,
    NumericAxis,
    RubberBandXyZoomModifier,
    SciChartSurface,
    XAxisDragModifier,
    XyDataSeries,
    YAxisDragModifier,
    ZoomExtentsModifier,
} from "scichart";

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

    sciChartSurface.layoutManager.leftOuterAxesLayoutStrategy =
        new LeftAlignedOuterVerticallyStackedAxisLayoutStrategy();

    sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { axisTitle: "X Axis" }));

    // Add title annotation
    sciChartSurface.annotations.add(
        new NativeTextAnnotation({
            text: "Vertically Stacked Axis: Custom layout of axis to allow traces to overlap. Useful for ECG charts",
            fontSize: 16,
            textColor: appTheme.ForegroundColor,
            x1: 0.5,
            y1: 0,
            opacity: 0.77,
            horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
            xCoordinateMode: ECoordinateMode.Relative,
            yCoordinateMode: ECoordinateMode.Relative,
            wrapTo: EWrapTo.ViewRect,
        })
    );

    const seriesCount: number = 10;
    for (let i = 0; i < seriesCount; i++) {
        const range = 10 / seriesCount;
        const yAxis = new NumericAxis(wasmContext, {
            id: "Y" + i,
            visibleRange: new NumberRange(-range, range),
            axisAlignment: EAxisAlignment.Left,
            zoomExtentsToInitialRange: true,
            maxAutoTicks: 5,
            drawMinorGridLines: false,
            axisBorder: { borderTop: 5, borderBottom: 5 },
            axisTitle: `Y ${i}`,
        });
        sciChartSurface.yAxes.add(yAxis);

        const lineSeries = new FastLineRenderableSeries(wasmContext, {
            yAxisId: yAxis.id,
            stroke: "auto",
            strokeThickness: 2,
        });
        lineSeries.dataSeries = getRandomSinewave(wasmContext, 0, Math.random() * 3, Math.random() * 50, 10000, 10);
        sciChartSurface.renderableSeries.add(lineSeries);
    }

    // Optional: Add some interactivity modifiers to enable zooming and panning
    sciChartSurface.chartModifiers.add(
        new YAxisDragModifier(),
        new XAxisDragModifier(),
        new RubberBandXyZoomModifier({
            xyDirection: EXyDirection.XDirection,
            executeCondition: { button: EExecuteOn.MouseRightButton },
        }),
        new MouseWheelZoomModifier({ xyDirection: EXyDirection.YDirection }),
        new ZoomExtentsModifier()
    );

    return { sciChartSurface, wasmContext };
};

function getRandomSinewave(
    wasmContext: TSciChart,
    pad: number,
    amplitude: number,
    phase: number,
    pointCount: number,
    freq: number
) {
    const dataSeries = new XyDataSeries(wasmContext);

    for (let i = 0; i < pad; i++) {
        const time = (10 * i) / pointCount;
        dataSeries.append(time, 0);
    }

    for (let i = pad, j = 0; i < pointCount; i++, j++) {
        amplitude = Math.min(3, Math.max(0.1, amplitude * (1 + (Math.random() - 0.5) / 10)));
        freq = Math.min(50, Math.max(0.1, freq * (1 + (Math.random() - 0.5) / 50)));

        const time = (10 * i) / pointCount;
        const wn = (2 * Math.PI) / (pointCount / freq);

        const d = amplitude * Math.sin(j * wn + phase);
        dataSeries.append(time, d);
    }

    return dataSeries;
}

React Chart with Vertically Stacked Axes

Overview

This example demonstrates how to create a React-based chart using SciChart.js to display multiple data traces with independent Y-axes that are vertically stacked. The chart is particularly suited for applications such as ECG/EEG visualizations, where overlapping traces are required.

Technical Implementation

The implementation is built using React and integrates SciChart.js through the <SciChartReact/> component. The chart initialization is handled by passing a function (drawExample) to the initChart property of the <SciChartReact/> component. This function sets up a custom layout strategy using the LeftAlignedOuterVerticallyStackedAxisLayoutStrategy to align multiple Y-axes vertically, ensuring the traces can overlap appropriately. Interactive modifiers such as pan, zoom, and drag are added to enable advanced chart interactivity. The source leverages SciChart.js documentation on vertically stacked axes for guidance on configuring these layout strategies.

Features and Capabilities

The example highlights several advanced features:

  • Multiple Y-Axes: Each data series is associated with its own Y-axis, configured with a custom visible range and axis borders to emphasize clear separation, yet visually overlapping for ECG/EEG style charts.
  • High-Performance Rendering: FastLineRenderableSeries is used to efficiently render large numbers of data points, which is critical for performance-sensitive applications. For additional performance optimization details, refer to the Performance Tips & Tricks in SciChart.js.
  • Interactive Chart Modifiers: The chart employs several interactive modifiers including mouse wheel zoom, drag modifiers on both X and Y axes, and rubber band zoom. These modifiers provide a smooth and responsive user experience as explained in the Tutorial on Adding Zooming, Panning Behavior.
  • Annotations: A text annotation is added to provide context for the stacked layout, demonstrating how annotations can be used to incorporate additional information directly onto the chart.

Integration and Best Practices

The example illustrates best practices for integrating SciChart.js into a React application using the <SciChartReact/> component. Developers new to SciChart in React can learn how to initialize charts asynchronously using the initChart prop. This approach is further detailed in React Charts with SciChart.js: Introducing “SciChart React”. Furthermore, the custom layout strategies and interactive modifiers showcase how to tailor the chart behavior to specific application needs. By following these practices, developers can build highly interactive and performant charts in React.

Overall, this example serves as a comprehensive guide to implementing vertically stacked axes in a React environment using SciChart.js, emphasizing custom layouts, performance optimization, and advanced chart interactivity.