JavaScript Market Depth Chart

How to create a Market Depth (Order Book) JavaScript Chart using Mountain Series and a Custom Modifier

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

import {
    SciChartSurface,
    MouseWheelZoomModifier,
    ZoomExtentsModifier,
    XyDataSeries,
    NumericAxis,
    FastMountainRenderableSeries,
    NumberRange,
    EAutoRange,
    EXyDirection,
    EAxisAlignment,
} from "scichart";
import { DepthCursorModifier } from "./DepthCursorModifier";

// SCICHART EXAMPLE

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

    const xAxis = new NumericAxis(wasmContext, {
        axisAlignment: EAxisAlignment.Top,
        labelPrecision: 4,
        rotation: 90,
    });

    sciChartSurface.xAxes.add(xAxis);

    const yAxis = new NumericAxis(wasmContext, {
        autoRange: EAutoRange.Always,
        growBy: new NumberRange(0, 0.05),
    });
    sciChartSurface.yAxes.add(yAxis);

    const AAPL_data = {
        buy: [
            { price: 132.79743, volume: 339 },
            { price: 132.79742, volume: 713 },
            { price: 132.79741, volume: 421 },
            { price: 132.7974, volume: 853 },
            { price: 132.79739, volume: 152 },
            { price: 132.79738, volume: 243 },
            { price: 132.79737, volume: 296 },
            { price: 132.79736, volume: 123 },
            { price: 132.79735, volume: 158 },
            { price: 132.79734, volume: 238 },
            { price: 132.79733, volume: 164 },
            { price: 132.79732, volume: 273 },
            { price: 132.79731, volume: 35 },
            { price: 132.79729, volume: 30 },
            { price: 132.79726, volume: 29 },
            { price: 132.79722, volume: 484 },
            { price: 132.79721, volume: 458 },
            { price: 132.7972, volume: 244 },
            { price: 132.79719, volume: 10 },
            { price: 132.79698, volume: 124 },
        ],
        sell: [
            { price: 132.79744, volume: 847 },
            { price: 132.79745, volume: 2412 },
            { price: 132.79746, volume: 635 },
            { price: 132.79747, volume: 323 },
            { price: 132.79748, volume: 828 },
            { price: 132.79749, volume: 322 },
            { price: 132.7975, volume: 268 },
            { price: 132.79751, volume: 92 },
            { price: 132.79752, volume: 249 },
            { price: 132.79753, volume: 189 },
            { price: 132.79754, volume: 179 },
            { price: 132.79755, volume: 122 },
            { price: 132.79756, volume: 28 },
            { price: 132.7976, volume: 114 },
            { price: 132.79764, volume: 27 },
            { price: 132.79767, volume: 10 },
            { price: 132.79772, volume: 31 },
            { price: 132.79785, volume: 484 },
            { price: 132.79786, volume: 364 },
            { price: 132.79787, volume: 244 },
        ],
    };

    const buyValues: number[] = [];
    let totalVol = 0;
    for (const v of AAPL_data.buy) {
        totalVol += v.volume;
        buyValues.push(totalVol);
    }
    const sellValues: number[] = [];
    totalVol = 0;
    for (const v of AAPL_data.sell) {
        totalVol += v.volume;
        sellValues.push(totalVol);
    }

    const buySeries = new FastMountainRenderableSeries(wasmContext, {
        dataSeries: new XyDataSeries(wasmContext, { xValues: AAPL_data.buy.map((v) => v.price), yValues: buyValues }),
        stroke: "green",
        fill: "00890033",
        strokeThickness: 2,
        isDigitalLine: true,
    });
    const sellSeries = new FastMountainRenderableSeries(wasmContext, {
        dataSeries: new XyDataSeries(wasmContext, { xValues: AAPL_data.sell.map((v) => v.price), yValues: sellValues }),
        stroke: "red",
        fill: "89000033",
        strokeThickness: 2,
        isDigitalLine: true,
    });
    sciChartSurface.renderableSeries.add(buySeries, sellSeries);

    xAxis.tickProvider.getMajorTicks = (minor, major, visibleRange) => {
        const ticks: number[] = [];
        const threshold = 400;
        const buyYs = buySeries.dataSeries.getNativeYValues();
        const buyXs = buySeries.dataSeries.getNativeXValues();
        let lastY = 0;
        for (let i = 0; i < buySeries.dataSeries.count(); i++) {
            const y = buyYs.get(i);
            if (y - lastY > threshold) {
                ticks.push(buyXs.get(i));
            }
            lastY = y;
        }
        const sellYs = sellSeries.dataSeries.getNativeYValues();
        const sellXs = sellSeries.dataSeries.getNativeXValues();
        lastY = 0;
        for (let i = 0; i < sellSeries.dataSeries.count(); i++) {
            const y = sellYs.get(i);
            if (y - lastY > threshold) {
                ticks.push(sellXs.get(i));
            }
            lastY = y;
        }
        return ticks.sort((a, b) => a - b);
    };

    const depthModifier = new DepthCursorModifier({
        buySeries,
        sellSeries,
        crosshairStrokeDashArray: [3, 2],
        crosshairStrokeThickness: 3,
        axisLabelFill: "transparent",
    });
    depthModifier.highlightColor = appTheme.DarkIndigo;
    // Optional: Add some interactivity to the chart
    sciChartSurface.chartModifiers.add(
        new ZoomExtentsModifier(),
        new MouseWheelZoomModifier({ xyDirection: EXyDirection.XDirection }),
        depthModifier
    );

    sciChartSurface.zoomExtents();
    xAxis.visibleRangeLimit = xAxis.visibleRange;
    yAxis.visibleRangeLimit = yAxis.visibleRange;
    return { sciChartSurface };
};

JavaScript Depth Chart

Overview

This example demonstrates how to build a high-performance Market Depth Chart using JavaScript and SciChart.js. It visualizes cumulative buy and sell order book data with two mountain series and a bespoke custom modifier that enhances mouse interactions, annotations, and hit testing.

Technical Implementation

The chart is initialized via the drawExample function which creates a SciChartSurface, configures NumericAxis, and instantiates two FastMountainRenderableSeries for buy and sell data. The custom modifier, DepthCursorModifier, is implemented directly using the core SciChart.js API to manage mouse events, dynamic annotations, and coordinate transformations. Developers can explore advanced customization through the Custom Chart Modifier API, which is leveraged to handle tasks such as updating crosshair lines and markers based on real-time mouse movement. The implementation also employs accurate hit testing using the RenderableSeries Hit-Test API and transforms canvas coordinates to data coordinates using the Axis APIs - Convert Pixel to Data Coordinates.

Features and Capabilities

Key features include real-time updating of annotations, dynamic highlighting of data points, and robust mouse event handling that provides a seamless interactive experience. The chart efficiently computes the mid-price between buy and sell data points and highlights the interactive region with crosshair annotations.

Integration and Best Practices

This example follows best practices for resource management in JavaScript by providing a cleanup (destructor) function that disposes of the SciChartSurface when no longer needed. Additionally, performance optimizations are achieved through careful DPI scaling and efficient update routines as described in the Performance Tips & Tricks documentation. The implementation reinforces principles for developing high-performance, interactive charts while maintaining clear separation of concerns and robust handling of chart events.