React Point-Markers Chart

Demonstrates how to create custom data-point markers on scatter/line using SciChart.js, High Performance React Charts

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

import {
    SciChartSurface,
    XyDataSeries,
    NumericAxis,
    NumberRange,
    EllipsePointMarker,
    SquarePointMarker,
    CrossPointMarker,
    SpritePointMarker,
    TrianglePointMarker,
    createImageAsync,
    ZoomPanModifier,
    ZoomExtentsModifier,
    MouseWheelZoomModifier,
    SplineLineRenderableSeries,
    LegendModifier,
    ELegendOrientation,
    ELegendPlacement,
    TSciChart,
} from "scichart";

function createData(wasmContext: TSciChart) {
    // Create some dataseries
    const dataSeries1 = new XyDataSeries(wasmContext, { dataSeriesName: "Ellipse Marker" });
    const dataSeries2 = new XyDataSeries(wasmContext, { dataSeriesName: "Square Marker" });
    const dataSeries3 = new XyDataSeries(wasmContext, { dataSeriesName: "Triangle Marker" });
    const dataSeries4 = new XyDataSeries(wasmContext, { dataSeriesName: "Cross Marker" });
    const dataSeries5 = new XyDataSeries(wasmContext, { dataSeriesName: "Custom Marker" });

    // Append values
    const dataSize = 30;
    for (let i = 0; i < dataSize; i++) {
        dataSeries1.append(i, Math.random());
        dataSeries2.append(i, Math.random() + 1);
        dataSeries3.append(i, Math.random() + 1.8);
        dataSeries4.append(i, Math.random() + 2.5);
        dataSeries5.append(i, Math.random() + 3.6);
    }

    // Insert a break into th eline = we do this to test double.NaN for the point marker types
    dataSeries1.update(15, NaN);
    dataSeries2.update(15, NaN);
    dataSeries3.update(15, NaN);
    dataSeries4.update(15, NaN);
    dataSeries5.update(15, NaN);

    return [dataSeries1, dataSeries2, dataSeries3, dataSeries4, dataSeries5];
}

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

    const dataSeriesArr = createData(wasmContext);

    sciChartSurface.xAxes.add(new NumericAxis(wasmContext));
    sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { growBy: new NumberRange(0.1, 0.1) }));

    // Add a line series with EllipsePointMarker
    sciChartSurface.renderableSeries.add(
        new SplineLineRenderableSeries(wasmContext, {
            stroke: appTheme.VividSkyBlue,
            strokeThickness: 3,
            pointMarker: new EllipsePointMarker(wasmContext, {
                width: 13,
                height: 13,
                strokeThickness: 2,
                fill: appTheme.VividSkyBlue,
                stroke: appTheme.ForegroundColor,
            }),
            dataSeries: dataSeriesArr[0],
        })
    );

    // Add a scatter series with SquarePointMarker
    sciChartSurface.renderableSeries.add(
        new SplineLineRenderableSeries(wasmContext, {
            stroke: appTheme.VividPink,
            strokeThickness: 3,
            pointMarker: new SquarePointMarker(wasmContext, {
                width: 11,
                height: 11,
                strokeThickness: 2,
                fill: appTheme.MutedPink,
                stroke: appTheme.VividPink,
            }),
            dataSeries: dataSeriesArr[1],
        })
    );

    // Add a scatter series with TrianglePointMarker
    sciChartSurface.renderableSeries.add(
        new SplineLineRenderableSeries(wasmContext, {
            stroke: appTheme.VividOrange,
            strokeThickness: 3,
            pointMarker: new TrianglePointMarker(wasmContext, {
                width: 13,
                height: 13,
                strokeThickness: 2,
                fill: appTheme.VividOrange,
                stroke: appTheme.VividOrange,
            }),
            dataSeries: dataSeriesArr[2],
        })
    );

    // Add a scatter series with CrossPointMarker
    sciChartSurface.renderableSeries.add(
        new SplineLineRenderableSeries(wasmContext, {
            stroke: appTheme.VividPurple,
            strokeThickness: 3,
            pointMarker: new CrossPointMarker(wasmContext, {
                width: 13,
                height: 13,
                strokeThickness: 3,
                stroke: appTheme.VividPurple,
            }),
            dataSeries: dataSeriesArr[3],
        })
    );

    // Add a scatter series with Custom Image using SpritePointMarker
    const imageBitmap = await createImageAsync(customPointImage);

    sciChartSurface.renderableSeries.add(
        new SplineLineRenderableSeries(wasmContext, {
            stroke: appTheme.MutedOrange,
            strokeThickness: 2,
            pointMarker: new SpritePointMarker(wasmContext, {
                image: imageBitmap,
            }),
            dataSeries: dataSeriesArr[4],
        })
    );

    sciChartSurface.chartModifiers.add(new ZoomPanModifier({ enableZoom: true }));
    sciChartSurface.chartModifiers.add(new ZoomExtentsModifier());

    sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier());
    sciChartSurface.chartModifiers.add(
        new LegendModifier({
            orientation: ELegendOrientation.Horizontal,
            placement: ELegendPlacement.TopLeft,
        })
    );

    sciChartSurface.zoomExtents();

    return { sciChartSurface, wasmContext };
};

React Use Point Markers Example

Overview

This example demonstrates how to render custom data point markers in a React application using the SciChart.js library. It showcases various marker types including ellipse, square, triangle, cross, and a custom image marker loaded asynchronously.

Technical Implementation

The implementation leverages the SciChartReact component to integrate SciChart.js with React. An asynchronous initialization function sets up the SciChartSurface with numeric axes and multiple renderable series using SplineLineRenderableSeries. Each series uses a distinct point marker (such as EllipsePointMarker, SquarePointMarker, TrianglePointMarker, CrossPointMarker, and SpritePointMarker for custom images which can be loaded by createImageAsync()) to highlight customization. The example also intentionally introduces a gap in the data (using NaN values) to demonstrate proper handling of missing data. Additional chart modifiers including ZoomPanModifier, MouseWheelZoomModifier, and ZoomExtentsModifier enhance user interactivity. For in-depth details on implementing custom point markers, refer to the Drawing PointMarkers on Series documentation.

Features and Capabilities

Key features of this example include the ability to render multiple custom markers in a high-performance WebGL environment, asynchronous image handling for custom markers, and robust interactivity through integrated chart modifiers. These capabilities enable real-time updates and smooth rendering performance, as detailed in resources like the Performance Optimisation of JavaScript Applications & Charts article.

Integration and Best Practices

The example illustrates best practices for React integration by encapsulating chart logic within the <SciChartReact/> component and an async initialization function, ensuring modular and scalable code. The approach adheres to recommended techniques for asynchronous data handling and interactive chart controls in React. Developers are encouraged to explore further guidance in the React Charts with SciChart.js blog post to optimize their implementations.