React Scatter Chart

We have created an example that demonstrates how to create a React Scatter Chart using SciChart.js

src/drawExample.ts
import { appTheme } from "./appTheme";
import {
    EllipsePointMarker,
    MouseWheelZoomModifier,
    NumericAxis,
    NumberRange,
    SciChartSurface,
    SweepAnimation,
    TrianglePointMarker,
    XyDataSeries,
    XyScatterRenderableSeries,
    ZoomExtentsModifier,
    ZoomPanModifier,
} from "scichart";

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

    // Create X,Y Axis
    sciChartSurface.xAxes.add(new NumericAxis(wasmContext));
    sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { growBy: new NumberRange(0.05, 0.05) }));

    // Create some xValues, yValues arrays
    const xValues = Array.from({ length: 250 }, (x, i) => i);
    const yValues = xValues.map((x) => 3 * x + x * Math.random());
    const y2Values = xValues.map((x) => x + x * Math.random());

    // Create a Scatter Series with EllipsePointMarker
    // Multiple point-marker types are available including Square, Triangle, Cross and Sprite (custom)
    sciChartSurface.renderableSeries.add(
        new XyScatterRenderableSeries(wasmContext, {
            dataSeries: new XyDataSeries(wasmContext, { xValues, yValues }),
            pointMarker: new EllipsePointMarker(wasmContext, {
                width: 14,
                height: 14,
                strokeThickness: 0,
                fill: appTheme.VividSkyBlue,
            }),
            opacity: 0.67,
            animation: new SweepAnimation({ duration: 600, fadeEffect: true }),
        })
    );

    // Add a second scatter chart with a different pointmarker
    sciChartSurface.renderableSeries.add(
        new XyScatterRenderableSeries(wasmContext, {
            dataSeries: new XyDataSeries(wasmContext, { xValues, yValues: y2Values }),
            pointMarker: new TrianglePointMarker(wasmContext, {
                width: 15,
                height: 15,
                strokeThickness: 0,
                fill: appTheme.VividOrange,
            }),
            opacity: 0.77,
            animation: new SweepAnimation({ duration: 600, fadeEffect: true, delay: 200 }),
        })
    );

    // Optional: Add Interactivity Modifiers
    sciChartSurface.chartModifiers.add(new ZoomPanModifier({ enableZoom: true }));
    sciChartSurface.chartModifiers.add(new ZoomExtentsModifier());
    sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier());

    sciChartSurface.zoomExtents();

    return { sciChartSurface, wasmContext };
};

React Scatter Chart Example

Overview

This example demonstrates how to create a high performance scatter chart within a React application using SciChart.js. It sets up a chart with two distinct scatter series that use custom point markers and animated transitions to enhance data visualization.

Technical Implementation

The chart is created asynchronously by invoking the SciChart.js API via a WASM context for optimized rendering performance. In this example, NumericAxis are added to a SciChartSurface and two XyScatterRenderableSeries are plotted using pointmarkers provided by EllipsePointMarker and TrianglePointMarker classes. The implementation follows asynchronous programming practices as described in Creating a SciChart React Component from the Ground Up, ensuring efficient initialization and resource management.

Features and Capabilities

The chart offers several advanced features including custom point markers and smooth animations. Each scatter series employs SweepAnimation to create animated transitions for the data points, as detailed in the SweepAnimation Documentation. Interactive modifiers such as ZoomPanModifier, ZoomExtentsModifier, and MouseWheelZoomModifier enable intuitive chart navigation, with further insights provided by the ZoomPanModifier Documentation. Additionally, the use of different marker types is aligned with the practices outlined in Drawing PointMarkers on Series.

Integration and Best Practices

Integrating SciChart.js into a React application is streamlined by the use of the <SciChartReact/> component, which encapsulates and abstracts away the complexities of chart creation. The example demonstrates how to effectively manage component lifecycle and asynchronous operations, while leveraging the WASM context for enhanced performance. Developers are encouraged to explore best practices for React integration and performance optimization as shared in React Charts with SciChart.js: Introducing “SciChart React” and Performance Optimisation of JavaScript Applications & Charts.