JavaScript Vital Signs ECG/EKG Medical Demo

Showcases how SciChart.js can be used in a Medical context, drawing ECGs with our High Performance JavaScript Charts

ECG
0
V1 - 1.4MM
ST | +0.6 || +0.9
NIBP
AUTO
145/95
0/0
SV
ML 100
%**** 55
0.0
SPO2
18:06
71-
RESP
0
src/drawExample.ts
import {
    CategoryAxis,
    EllipsePointMarker,
    EventHandler,
    FastLineRenderableSeries,
    NumberRange,
    NumericAxis,
    RightAlignedOuterVerticallyStackedAxisLayoutStrategy,
    SciChartSurface,
    XyDataSeries,
} from "scichart";
import { vitalSignsEcgData } from "./vitalSignsEcgData";
import { appTheme } from "./appTheme";

const STEP = 10;
const TIMER_TIMEOUT_MS = 20;
const STROKE_THICKNESS = 4;
const POINTS_LOOP = 5200;
const GAP_POINTS = 50;
const DATA_LENGTH = vitalSignsEcgData.xValues.length;

const { ecgHeartRateValues, bloodPressureValues, bloodVolumeValues, bloodOxygenationValues } = vitalSignsEcgData;

// HELPER FUNCTIONS
const getValuesFromData = (xIndex: number) => {
    const xArr: number[] = [];
    const ecgHeartRateArr: number[] = [];
    const bloodPressureArr: number[] = [];
    const bloodVolumeArr: number[] = [];
    const bloodOxygenationArr: number[] = [];
    for (let i = 0; i < STEP; i++) {
        const dataIndex = (xIndex + i) % DATA_LENGTH;
        const x = xIndex + i;
        xArr.push(x);
        ecgHeartRateArr.push(ecgHeartRateValues[dataIndex]);
        bloodPressureArr.push(bloodPressureValues[dataIndex]);
        bloodVolumeArr.push(bloodVolumeValues[dataIndex]);
        bloodOxygenationArr.push(bloodOxygenationValues[dataIndex]);
    }
    return {
        xArr,
        ecgHeartRateArr,
        bloodPressureArr,
        bloodVolumeArr,
        bloodOxygenationArr,
    };
};

export type TDataUpdateInfo = {
    ecg: number;
    bloodPressure1: number;
    bloodPressure2: number;
    bloodVolume: number;
    bloodOxygenation: number;
};

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

    // Create a single, shared X-axis, pre-sized to fit the data in X, and is invisible

    // Note: For fifoSweeping mode to work, the X-Axis must be a CategoryAxis
    //      NumericAxis is also supported, but x-values must then be offsets from 0, ie do x % fifoCapacity.
    //      See more info in the docs
    const xAxis = new CategoryAxis(wasmContext, {
        visibleRange: new NumberRange(0, POINTS_LOOP),
        isVisible: false,
    });
    sciChartSurface.xAxes.add(xAxis);

    // Create multiple y-axis, one per trace. Using the stacked vertically layout strategy
    const yAxisHeartRate = new NumericAxis(wasmContext, {
        id: "yHeartRate",
        visibleRange: new NumberRange(0.7, 1.0),
        isVisible: false,
    });
    const yAxisBloodPressure = new NumericAxis(wasmContext, {
        id: "yBloodPressure",
        visibleRange: new NumberRange(0.4, 0.8),
        isVisible: false,
    });
    const yAxisBloodVolume = new NumericAxis(wasmContext, {
        id: "yBloodVolume",
        visibleRange: new NumberRange(0.1, 0.5),
        isVisible: false,
    });
    const yAxisBloodOxygenation = new NumericAxis(wasmContext, {
        id: "yBloodOxygenation",
        visibleRange: new NumberRange(0, 0.2),
        isVisible: false,
    });
    sciChartSurface.layoutManager!.rightOuterAxesLayoutStrategy =
        new RightAlignedOuterVerticallyStackedAxisLayoutStrategy();
    sciChartSurface.yAxes.add(yAxisHeartRate, yAxisBloodPressure, yAxisBloodVolume, yAxisBloodOxygenation);

    // Using the NEW fifoCapacity, fifoSweeping mode in SciChart.js v3.2 we specify a number of points
    // we want in the viewport. When the right edge of the viewport is reached, the series wraps around

    const fifoSweepingGap = GAP_POINTS;
    const dataSeries1 = new XyDataSeries(wasmContext, {
        fifoCapacity: POINTS_LOOP,
        fifoSweeping: true,
        fifoSweepingGap,
    });
    const dataSeries2 = new XyDataSeries(wasmContext, {
        fifoCapacity: POINTS_LOOP,
        fifoSweeping: true,
        fifoSweepingGap,
    });
    const dataSeries3 = new XyDataSeries(wasmContext, {
        fifoCapacity: POINTS_LOOP,
        fifoSweeping: true,
        fifoSweepingGap,
    });
    const dataSeries4 = new XyDataSeries(wasmContext, {
        fifoCapacity: POINTS_LOOP,
        fifoSweeping: true,
        fifoSweepingGap,
    });

    // A pointmarker with lastPointOnly = true will be used for all series to mark the last point
    const pointMarkerOptions = {
        width: 7,
        height: 7,
        strokeThickness: 2,
        fill: appTheme.MutedSkyBlue,
        lastPointOnly: true,
    };

    // Create four RenderableSeries which render the data
    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            yAxisId: yAxisHeartRate.id,
            strokeThickness: STROKE_THICKNESS,
            stroke: appTheme.VividOrange,
            dataSeries: dataSeries1,
            pointMarker: new EllipsePointMarker(wasmContext, { ...pointMarkerOptions, stroke: appTheme.VividOrange }),
        })
    );

    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            yAxisId: yAxisBloodPressure.id,
            strokeThickness: STROKE_THICKNESS,
            stroke: appTheme.VividSkyBlue,
            dataSeries: dataSeries2,
            pointMarker: new EllipsePointMarker(wasmContext, { ...pointMarkerOptions, stroke: appTheme.VividSkyBlue }),
        })
    );

    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            yAxisId: yAxisBloodVolume.id,
            strokeThickness: STROKE_THICKNESS,
            stroke: appTheme.VividPink,
            dataSeries: dataSeries3,
            pointMarker: new EllipsePointMarker(wasmContext, { ...pointMarkerOptions, stroke: appTheme.VividPink }),
        })
    );

    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            yAxisId: yAxisBloodOxygenation.id,
            strokeThickness: STROKE_THICKNESS,
            stroke: appTheme.VividTeal,
            dataSeries: dataSeries4,
            pointMarker: new EllipsePointMarker(wasmContext, { ...pointMarkerOptions, stroke: appTheme.VividTeal }),
        })
    );

    const dataUpdateEventHandler = new EventHandler<TDataUpdateInfo>();

    let timerId: NodeJS.Timeout;
    let currentPoint = 0;

    // The following code is run once per timer-step to update the data in the charts
    // Here you would subsitute your own callback to receive data from your data feed or sensors
    const runUpdateDataOnTimeout = () => {
        // Get data
        const { xArr, ecgHeartRateArr, bloodPressureArr, bloodVolumeArr, bloodOxygenationArr } =
            getValuesFromData(currentPoint);
        currentPoint += STEP;

        // appendRange when fifoSweepingMode = true and fifoCapacity is reached will cause the series to wrap around
        dataSeries1.appendRange(xArr, ecgHeartRateArr);
        dataSeries2.appendRange(xArr, bloodPressureArr);
        dataSeries3.appendRange(xArr, bloodVolumeArr);
        dataSeries4.appendRange(xArr, bloodOxygenationArr);

        // Update Info panel
        if (currentPoint % 1000 === 0) {
            const ecg = ecgHeartRateArr[STEP - 1];
            const bloodPressure = bloodPressureArr[STEP - 1];
            const bloodVolume = bloodVolumeArr[STEP - 1] + 3;
            const bloodOxygenation = bloodOxygenationArr[STEP - 1];

            const dataUpdateInfo = {
                ecg: Math.floor(ecg * 20),
                bloodPressure1: Math.floor(bloodPressure * 46),
                bloodPressure2: Math.floor(bloodPressure * 31),
                bloodVolume: bloodVolume + 8.6,
                bloodOxygenation: Math.floor(bloodOxygenation * 10 + 93),
            };
            dataUpdateEventHandler.raiseEvent(dataUpdateInfo);
        }
        timerId = setTimeout(runUpdateDataOnTimeout, TIMER_TIMEOUT_MS);
    };

    const subscribeToDataUpdates = (handler: (info: TDataUpdateInfo) => void) => {
        dataUpdateEventHandler.subscribe(handler);

        // automatically cleanup subscription whe surface is deleted
        sciChartSurface.addDeletable({ delete: () => dataUpdateEventHandler.unsubscribeAll() });
    };

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

    const startUpdate = () => {
        if (timerId) {
            stopUpdate();
        }
        runUpdateDataOnTimeout();
    };

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

Vital Signs Monitor Demo in JavaScript

Overview

This example, titled Vital Signs Monitor Demo, showcases how to build a high-performance real-time medical chart using SciChart.js with JavaScript. It simulates multiple medical signals including ECG, blood pressure, blood volume, and blood oxygenation, and is optimized for continuous data streaming and efficient updates.

Technical Implementation

The demo initializes a SciChartSurface with a hidden shared CategoryAxis for cyclic data management, which is essential for using FIFO-sweeping mode. Data is continuously appended to multiple XyDataSeries via the appendRange() method, ensuring smooth real-time updates. The use of fifoCapacity and fifoSweeping mode (see DataSeries Realtime Updates) allows the series to wrap around once the specified capacity is reached, optimizing performance. Additionally, a recursive setTimeout loop simulates real-time data streaming, while a custom EventHandler is used to update an information panel with computed metrics. For more detail on real-time updates, refer to Adding Realtime Updates.

Features and Capabilities

Real-Time Data Streaming: The demo employs a setTimeout loop to periodically fetch and append data points, demonstrating efficient real-time updating using JavaScript.

Performance Optimization: By configuring properties like fifoCapacity and fifoSweeping, the example highlights how FIFO buffers improve performance and simplify code for ECG style wrap-around charts when handling large volumes of data. For further insights, see the Performance Tips & Tricks guide.

Axis Configuration and Layout: The example uses a hidden CategoryAxis on the X-axis and multiple NumericAxis Y-axes that are vertically stacked with the RightAlignedOuterVerticallyStackedAxisLayoutStrategy. This approach simplifies the synchronization of multiple data series. More information is available in the Vertically Stacked Axis documentation.

Custom Event Handling and Memory Management: Custom event handlers are implemented using the EventHandler class to update the UI with the latest metrics. Additionally, sciChartSurface.addDeletable() is used to automatically clean up these subscriptions, ensuring optimal resource management during the chart's lifecycle.

Integration and Best Practices

Even though this demo is built using JavaScript, the underlying principles and performance optimizations can be applied across various frameworks. It emphasizes best practices for real-time data visualization including efficient data appending, optimized axis configurations, and sustainable memory management. By following these techniques, developers can create responsive, high-performance charting applications using SciChart.js.