React Realtime Surface Mesh 3D Chart

Learn how to create a realtime updating React 3D Surface Mesh Chart using SciChart.js, and our High Performance JavaScript 3D Chart Library

src/drawExample.ts
import {
    CameraController,
    EDrawMeshAs,
    GradientColorPalette,
    MouseWheelZoomModifier3D,
    NumberRange,
    NumericAxis3D,
    OrbitModifier3D,
    ResetCamera3DModifier,
    SciChart3DSurface,
    SurfaceMeshRenderableSeries3D,
    UniformGridDataSeries3D,
    Vector3,
    zeroArray2D,
} from "scichart";
import { appTheme } from "./appTheme";

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

    // Create and position the camera in the 3D world
    sciChart3DSurface.camera = new CameraController(wasmContext, {
        position: new Vector3(-150, 200, 150),
        target: new Vector3(0, 50, 0),
    });
    // Set the worlddimensions, which defines the Axis cube size
    sciChart3DSurface.worldDimensions = new Vector3(200, 100, 200);

    // Add an X,Y and Z Axis
    sciChart3DSurface.xAxis = new NumericAxis3D(wasmContext, { axisTitle: "X Axis" });
    sciChart3DSurface.yAxis = new NumericAxis3D(wasmContext, {
        axisTitle: "Y Axis",
        visibleRange: new NumberRange(-0.3, 0.3),
    });
    sciChart3DSurface.zAxis = new NumericAxis3D(wasmContext, { axisTitle: "Z Axis" });

    // Create a 2D array using the helper function zeroArray2D
    // and fill this with data
    const zSize = 50;
    const xSize = 50;
    const heightmapArray = zeroArray2D([zSize, xSize]);

    // Create a UniformGridDataSeries3D
    const dataSeries = new UniformGridDataSeries3D(wasmContext, {
        yValues: heightmapArray,
        xStep: 1,
        zStep: 1,
        dataSeriesName: "Uniform Surface Mesh",
    });

    const setData = (i: number) => {
        const f = i / 10;
        for (let z = 0; z < zSize; z++) {
            let zVal = z - zSize / 2;
            for (let x = 0; x < xSize; x++) {
                let xVal = x - xSize / 2;
                const y = (Math.cos(xVal * 0.2 + f) + Math.cos(zVal * 0.2 + f)) / 5;
                heightmapArray[z][x] = y;
            }
        }
        dataSeries.setYValues(heightmapArray);
    };

    // Create the color map
    const colorMap = new GradientColorPalette(wasmContext, {
        gradientStops: [
            { offset: 1, color: appTheme.VividPink },
            { offset: 0.9, color: appTheme.VividOrange },
            { offset: 0.7, color: appTheme.MutedRed },
            { offset: 0.5, color: appTheme.VividGreen },
            { offset: 0.3, color: appTheme.VividSkyBlue },
            { offset: 0.15, color: appTheme.Indigo },
            { offset: 0, color: appTheme.DarkIndigo },
        ],
    });

    // Finally, create a SurfaceMeshRenderableSeries3D and add to the chart
    const series = new SurfaceMeshRenderableSeries3D(wasmContext, {
        dataSeries,
        minimum: -0.3,
        maximum: 0.5,
        opacity: 0.9,
        cellHardnessFactor: 1.0,
        shininess: 0,
        lightingFactor: 0.0,
        highlight: 1.0,
        stroke: appTheme.VividBlue,
        strokeThickness: 2.0,
        contourStroke: appTheme.VividBlue,
        contourInterval: 2,
        contourOffset: 0,
        contourStrokeThickness: 2,
        drawSkirt: false,
        drawMeshAs: EDrawMeshAs.SOLID_WITH_CONTOURS,
        meshColorPalette: colorMap,
        isVisible: true,
    });

    sciChart3DSurface.renderableSeries.add(series);

    // Optional: Add some interactivity modifiers
    sciChart3DSurface.chartModifiers.add(new MouseWheelZoomModifier3D());
    sciChart3DSurface.chartModifiers.add(new OrbitModifier3D());
    sciChart3DSurface.chartModifiers.add(new ResetCamera3DModifier());

    let frame = 0;
    let timer: NodeJS.Timeout;
    const updateFunc = () => {
        setData(frame);
        frame++;
    };
    updateFunc();
    const startUpdate = () => {
        frame = 0;
        timer = setInterval(updateFunc, 20);
    };
    const stopUpdate = () => {
        clearInterval(timer);
    };

    return { sciChartSurface: sciChart3DSurface, wasmContext, controls: { startUpdate, stopUpdate } };
};

React Realtime Surface Mesh 3D Chart

Overview

This example demonstrates how to create a realtime updating 3D Surface Mesh Chart within a React application using SciChart.js. The implementation showcases dynamic data streaming and visualization in a high-performance 3D environment powered by WebGL.

Technical Implementation

The chart is initialized using the <SciChartReact/> component, which wraps the imperative API of SciChart3DSurface. The core logic is encapsulated in a function that sets up the 3D surface, configures axes, and generates a dynamic data series using a 2D array through the UniformGridDataSeries3D class. Realtime data updates are driven by a setInterval loop, and the realtime animation approach is detailed in the Adding Realtime Updates documentation.

Features and Capabilities

Key features of this example include realtime updates to the surface mesh, detailed customization of the mesh appearance with a GradientColorPalette, and interactivity enhancements such as mouse wheel zoom, orbit controls, and a camera reset modifier. The demo clearly illustrates how to achieve smooth animated changes in a 3D scene and offers tips on fine-tuning performance for realtime applications, as seen in the React Realtime 3D Surface Mesh Chart Demo.

Integration and Best Practices

Integration with React is streamlined using the <SciChartReact/> component, which leverages TypeScript generics for robust type safety and component reusability. The clear separation between chart setup and realtime update logic makes it easier to manage state and resources. Developers can follow best practices for React integration and adopt performance optimization techniques highlighted in the Performance Optimisation of JavaScript Applications & Charts article to ensure efficient rendering and cleanup, especially when managing intervals and resource-intensive operations.

Additional Technical Insights

The example also features advanced 3D camera control using the CameraController and explicitly defined world dimensions for a realistic scene. For more in-depth details on configuring and interacting with the 3D chart, refer to the SciChart3DSurface API Documentation.