React Spline Line Chart

Demonstrates how to create a React Spline Line Chart using SciChart.js, our feature-rich JavaScript Chart Library

src/drawExample.ts
import { appTheme } from "./appTheme";
import {
    ELegendOrientation,
    EllipsePointMarker,
    FastLineRenderableSeries,
    LegendModifier,
    MouseWheelZoomModifier,
    NumericAxis,
    NumberRange,
    SciChartSurface,
    SplineLineRenderableSeries,
    WaveAnimation,
    XyDataSeries,
    ZoomExtentsModifier,
    ZoomPanModifier,
    BezierRenderDataTransform,
    AxisMarkerAnnotation,
    ECoordinateMode,
} from "scichart";

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

    // Create the X,Y Axis
    const xAxis = new NumericAxis(wasmContext);
    sciChartSurface.xAxes.add(xAxis);

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

    const xValues = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
    const yValues = [50, 35, 61, 58, 50, 50, 40, 53, 55, 23, 45, 12, 59, 60];

    // Create an XyDataSeries as data source
    const xyDataSeries = new XyDataSeries(wasmContext, {
        dataSeriesName: "Original Data ",
        xValues,
        yValues,
    });

    const splineXyDataSeries = new XyDataSeries(wasmContext, {
        dataSeriesName: "Default Spline",
        xValues,
        yValues,
    });

    // Create and add a standard line series to the chart.
    // This will be used to compare the spline (smoothed) algorithm
    const lineSeries = new FastLineRenderableSeries(wasmContext, {
        stroke: appTheme.VividOrange,
        strokeThickness: 3,
        dataSeries: xyDataSeries,
        pointMarker: new EllipsePointMarker(wasmContext, {
            width: 11,
            height: 11,
            fill: appTheme.ForegroundColor,
            stroke: appTheme.VividSkyBlue,
        }),
        animation: new WaveAnimation({ zeroLine: 10, pointDurationFraction: 0.5, duration: 1000 }),
    });
    sciChartSurface.renderableSeries.add(lineSeries);

    // Create and add a Spline line series to the chart
    const splineSeries = new SplineLineRenderableSeries(wasmContext, {
        stroke: appTheme.VividSkyBlue,
        strokeThickness: 4,
        dataSeries: splineXyDataSeries,
        pointMarker: new EllipsePointMarker(wasmContext, {
            width: 11,
            height: 11,
            fill: appTheme.ForegroundColor,
            stroke: appTheme.VividSkyBlue,
        }),
        interpolationPoints: 10, // Set interpolation points to decide the amount of smoothing,
        animation: new WaveAnimation({ zeroLine: 10, pointDurationFraction: 0.5, duration: 1000, fadeEffect: true }),
    });
    sciChartSurface.renderableSeries.add(splineSeries);

    const bezierDataSeries = new XyDataSeries(wasmContext, {
        dataSeriesName: "Bezier (Range Restricted)",
        xValues,
        yValues,
    });

    const bezierSeries = new FastLineRenderableSeries(wasmContext, {
        stroke: appTheme.VividPurple,
        strokeThickness: 4,
        dataSeries: bezierDataSeries,
        pointMarker: new EllipsePointMarker(wasmContext, {
            width: 11,
            height: 11,
            fill: appTheme.ForegroundColor,
            stroke: appTheme.VividSkyBlue,
        }),
        animation: new WaveAnimation({ zeroLine: 10, pointDurationFraction: 0.5, duration: 1000, fadeEffect: true }),
    });
    const bezierTransform = new BezierRenderDataTransform(bezierSeries, wasmContext, [
        bezierSeries.drawingProviders[0],
    ]);
    bezierSeries.renderDataTransform = bezierTransform;
    sciChartSurface.renderableSeries.add(bezierSeries);

    const curvatureAnnotation = new AxisMarkerAnnotation({
        yCoordinateMode: ECoordinateMode.Relative,
        y1: 0.5,
        formattedValue: "Bezier Curvature 0.5",
        backgroundColor: appTheme.VividPurple,
        isEditable: true,
        onDrag: (args) => {
            bezierTransform.curvature = curvatureAnnotation.y1;
            curvatureAnnotation.formattedValue = "Bezier Curvature " + curvatureAnnotation.y1.toFixed(2);
        },
    });
    sciChartSurface.annotations.add(curvatureAnnotation);

    // OPTIONAL: Add some interactivity modifiers
    sciChartSurface.chartModifiers.add(new ZoomPanModifier({ enableZoom: true }));
    sciChartSurface.chartModifiers.add(new ZoomExtentsModifier());
    sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier());
    sciChartSurface.chartModifiers.add(
        new LegendModifier({ orientation: ELegendOrientation.Vertical, showCheckboxes: true })
    );

    sciChartSurface.zoomExtents();
    return { sciChartSurface, wasmContext };
};

React Spline Line Chart

Overview

The React Spline Line Chart example demonstrates how to integrate SciChart.js into a React application using the <SciChartReact/> component. The example showcases a traditional fast line series, a smoothed spline line series, and an enhanced Bezier line series. It leverages advanced features such as animated series with WaveAnimation, dynamic data transforms for smoothing using BezierRenderDataTransform, and interactive annotations to adjust chart parameters in real-time.

Technical Implementation

The chart creation logic is encapsulated in a dedicated function called drawExample which is initialized via the <SciChartReact/> component in the index.tsx file. This function creates a SciChartSurface, adds numeric axes and multiple renderable series, and configures interactive modifiers such as zoom, pan, and mouse wheel zoom. The implementation makes use of SciChart.js components such as FastLineRenderableSeries and SplineLineRenderableSeries, along with custom point markers (EllipsePointMarker) and animation (WaveAnimation). Developers can learn more about these features in the Spline Line Chart Documentation and the Tutorial for setting up SciChart React.

Features and Capabilities

The example not only compares a standard fast line series against a spline line series but also demonstrates advanced smoothing techniques with BezierRenderDataTransform. Users can interactively modify the curvature of the Bezier smoothing via an axis marker annotation. Each series is animated using WaveAnimation, which provides a dynamic transition effect. Custom point markers, enhanced series customization, and interactive modifiers enrich the visualization and allow for real-time updates.

Integration and Best Practices

Integration with React is achieved through the <SciChartReact/> component, which simplifies the creation, update, and cleanup processes for the SciChartSurface within the React lifecycle. This approach follows best practices for React integration and ensures optimal performance by efficiently managing WebGL contexts. Additionally, developers are encouraged to explore interactive modifiers like ZoomPanModifier and MouseWheelZoomModifier for improved user interaction along with annotation features for dynamic chart adjustments. The example emphasizes performance optimization techniques that are crucial for rendering multiple series in a React environment, as outlined in Creating a SciChart React Component from the Ground Up.