High Performance SVG Cursor & Rollover

Demonstrates the Decoupled Render Loop. Even with 1 Million Points and a complex Gradient Palette causing frame drops on the main chart, the SVG Cursor remains smooth at 60 FPS because it does not trigger a WebGL redraw.

src/drawExample.ts
import {
    SciChartSurface,
    NumericAxis,
    XyDataSeries,
    XyScatterRenderableSeries,
    EllipsePointMarker,
    CursorModifier,
    ZoomExtentsModifier,
    MouseWheelZoomModifier,
    DefaultPaletteProvider,
    IPointMarkerPaletteProvider,
    parseColorToUIntArgb,
    RolloverModifier,
    ENumericFormat,
    NumberRange,
    ChartModifierBase2D,
    XySeriesInfo,
    EXyDirection,
} from "scichart";
import { appTheme } from "./appTheme";

// Helper to extract RGB channels from a UInt ARGB color
const extractColorChannels = (uintColor: number) => {
    return {
        a: (uintColor >> 24) & 0xff,
        r: (uintColor >> 16) & 0xff,
        g: (uintColor >> 8) & 0xff,
        b: uintColor & 0xff,
    };
};

class GradientPaletteProvider extends DefaultPaletteProvider implements IPointMarkerPaletteProvider {
    private readonly minY: number;
    private readonly maxY: number;

    // Color Channel Data
    private startColor: { r: number; g: number; b: number; a: number };
    private endColor: { r: number; g: number; b: number; a: number };

    constructor(minY: number, maxY: number, startHex: string, endHex: string) {
        super();
        this.minY = minY;
        this.maxY = maxY;

        // Pre-calculate color channels to avoid expensive parsing per point
        this.startColor = extractColorChannels(parseColorToUIntArgb(startHex));
        this.endColor = extractColorChannels(parseColorToUIntArgb(endHex));
    }

    public override overridePointMarkerArgb(xValue: number, yValue: number, index: number) {
        // Calculate the fraction (0 to 1) of the current Y value within the range
        let fraction = (yValue - this.minY) / (this.maxY - this.minY);
        if (fraction < 0) fraction = 0;
        if (fraction > 1) fraction = 1;

        // Lerp
        const r = Math.floor(this.startColor.r + (this.endColor.r - this.startColor.r) * fraction);
        const g = Math.floor(this.startColor.g + (this.endColor.g - this.startColor.g) * fraction);
        const b = Math.floor(this.startColor.b + (this.endColor.b - this.startColor.b) * fraction);

        // Reassemble into UInt ARGB
        const colorUint = (255 << 24) | (r << 16) | (g << 8) | b;

        return {
            stroke: colorUint,
            fill: colorUint,
        };
    }
}

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

    sciChartSurface.xAxes.add(
        new NumericAxis(wasmContext, {
            labelFormat: ENumericFormat.Engineering,
        })
    );
    sciChartSurface.yAxes.add(
        new NumericAxis(wasmContext, {
            growBy: new NumberRange(0.01, 0.01),
        })
    );

    // 1. Data Generation (Bounded Cloud)
    const count = 1_000_000;
    const xValues = new Float64Array(count);
    const yValues = new Float64Array(count);

    const Y_MIN = -500;
    const Y_MAX = 500;

    for (let i = 0; i < count; i++) {
        xValues[i] = i;
        yValues[i] = Y_MIN + Math.random() * (Y_MAX - Y_MIN);
    }

    // 2. Create Scatter Series with Gradient Palette
    const ACCENT_COLOR = "#5e7dba";

    const scatterSeries = new XyScatterRenderableSeries(wasmContext, {
        dataSeries: new XyDataSeries(wasmContext, {
            xValues,
            yValues,
            containsNaN: false,
            isSorted: true,
            dataSeriesName: "Bounded Cloud Series",
        }),
        pointMarker: new EllipsePointMarker(wasmContext, {
            width: 1,
            height: 1,
            strokeThickness: 0.5,
        }),
        paletteProvider: new GradientPaletteProvider(
            Y_MIN,
            Y_MAX,
            appTheme.Indigo, // Start (Low Y)
            appTheme.VividOrange // End (High Y)
        ),
        stroke: ACCENT_COLOR
    });
    sciChartSurface.renderableSeries.add(scatterSeries);

    sciChartSurface.chartModifiers.add(
        new ZoomExtentsModifier(),
        new MouseWheelZoomModifier({
            xyDirection: EXyDirection.XDirection,
        })
    );

    // 3. State Management for Tool Modifiers
    let activeModifier: ChartModifierBase2D | null = null;
    let isCursorMode = true;
    let isSvgMode = true;

    const rebuildActiveModifier = () => {
        if (activeModifier) {
            sciChartSurface.chartModifiers.remove(activeModifier);
            activeModifier.delete();
            activeModifier = null;
        }

        if (isCursorMode) {
            activeModifier = new CursorModifier({
                isSvgOnly: isSvgMode,
                showTooltip: true,
                crosshairStroke: ACCENT_COLOR,
                axisLabelFill: ACCENT_COLOR,
                tooltipContainerBackground: ACCENT_COLOR,
            });
        } else {
            activeModifier = new RolloverModifier({
                isSvgOnly: isSvgMode,
                showTooltip: true,
                showAxisLabel: true,
                rolloverLineStroke: ACCENT_COLOR,
                tooltipDataTemplate: (seriesInfo: XySeriesInfo): string[] => {
                    const valuesWithLabels: string[] = [];
                    const xySeriesInfo = seriesInfo as XySeriesInfo;
                    valuesWithLabels.push(`X: ${xySeriesInfo.formattedXValue}`);
                    valuesWithLabels.push(`Y: ${xySeriesInfo.formattedYValue}`);
                    return valuesWithLabels;
                },
            });
            (activeModifier as RolloverModifier).rolloverLineAnnotation.axisLabelFill = ACCENT_COLOR;
        }

        sciChartSurface.chartModifiers.add(activeModifier);
    };
    rebuildActiveModifier();

    // 4. Control Functions
    const setSvgMode = (useSvg: boolean) => {
        isSvgMode = useSvg;
        rebuildActiveModifier();
    };
    const toggleUseCursorOrRollover = (useCursor: boolean) => {
        isCursorMode = useCursor;
        rebuildActiveModifier();
    };

    return {
        sciChartSurface,
        controls: {
            setSvgMode,
            toggleUseCursorOrRollover,
        },
    };
};

High Performance SVG Cursor - React

Overview

When rendering massive datasets (like the 1 Million Point Scatter chart in this demo), the WebGL rendering engine may drop below 60 FPS due to the sheer volume of data and pixel shading required. We have made it possible to use paletteProvider with much bigger datasets while retaining performance. And even if you push this to its limit, you can still get smooth tooltip behaviour thanks to SVG cursor.

In standard charting libraries, the Cursor or Tooltip is often part of the main render loop. This means if the chart draws at 15 FPS, your mouse cursor lags at 15 FPS.

SciChart.js solves this with a Decoupled Render Loop.

The Solution: SVG vs Native

This demo allows you to toggle between isSvgOnly: true (SVG Mode) and isSvgOnly: false (Native Mode) on the Cursor and Rollover Modifiers.

  • Native Mode: The crosshairs and tooltips are drawn on the main canvas (or composited). Every mouse move triggers a full chart re-render. If the data is heavy, the cursor feels "heavy" and laggy.
  • SVG Mode: The modifiers draw to a separate HTML/SVG layer floating above the WebGL canvas. Moving the mouse updates the SVG layer without invalidating the WebGL scene.

Result: The chart might be static or updating slowly, but your interaction remains buttery smooth at 60 FPS.

Integration

This example uses the <SciChartReact/> component. The modifiers are updated dynamically using a reference to the chart controls. Note how the rebuildActiveModifier function in the source code switches between CursorModifier and RolloverModifier while preserving the isSvgOnly state.