React Spline Band Chart

Demonstrates how to create a React Spline Band Chart or High-Low Fill using SciChart.js, our High Performance JavaScript Chart Software

src/drawExample.ts
import {
    AxisMarkerAnnotation,
    EAxisAlignment,
    ECoordinateMode,
    EDragMode,
    EHorizontalAnchorPoint,
    ELegendOrientation,
    ELegendPlacement,
    EllipsePointMarker,
    LegendModifier,
    MouseWheelZoomModifier,
    NativeTextAnnotation,
    NumberRange,
    NumericAxis,
    ScaleAnimation,
    SciChartSurface,
    SplineBandRenderableSeries,
    XyyBezierRenderDataTransform,
    XyyDataSeries,
    YAxisDragModifier,
} from "scichart";
// import { XyyBezierRenderDataTransform } from "scichart/Charting/Visuals/RenderableSeries/RenderDataTransforms/BezierRenderDataTransform";
import { ExampleDataProvider } from "./ExampleDataProvider";
import { appTheme } from "./appTheme";
//import { xValues } from "../StackedMountainChart/data/stackedMountainChartData";

export const drawExample = async (rootElement: string | HTMLDivElement) => {

    // Create a SciChartSurface
    const { wasmContext, sciChartSurface } = await SciChartSurface.create(rootElement, {
        theme: appTheme.SciChartJsTheme,
    });

    // Add an XAxis, YAxis
    sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { axisAlignment: EAxisAlignment.Top }));
    sciChartSurface.yAxes.add(
        new NumericAxis(wasmContext, {
            axisAlignment: EAxisAlignment.Left,
            visibleRange: new NumberRange(-1.75, 0.75),
            backgroundColor: appTheme.VividSkyBlue + "33",
            labelStyle: { color: appTheme.VividOrange },
            majorTickLineStyle: { color: appTheme.VividOrange },
        })
    );
    sciChartSurface.yAxes.add(
        new NumericAxis(wasmContext, {
            axisAlignment: EAxisAlignment.Right,
            id: "y2",
            visibleRange: new NumberRange(-0.9, 1.6),
            backgroundColor: appTheme.VividPurple + "33",
            labelStyle: { color: appTheme.VividRed },
            majorTickLineStyle: { color: appTheme.VividRed },
        })
    );

    // The spline bandseries requires a special dataseries type called XyyDataSeries
    // This stores X, Y1, Y2 point data for the two lines in the band
    const yData = ExampleDataProvider.getDampedSinewave(0, 0.9, 0, 0.002, 1000, 17);
    const y1Data = ExampleDataProvider.getDampedSinewave(0, 0.9, 0, 0.002, 1000, 16);

    const xValues: number[] = [];
    const yValues: number[] = [];
    const y1Values: number[] = [];

    for (let i = 0; i < 20; i++) {
        const index = i * 50;
        xValues.push(yData.xValues[index]);
        yValues.push(yData.yValues[index]);
        y1Values.push(y1Data.yValues[index]);
    }

    // Create the band series and add to the chart
    const rendSeries = new SplineBandRenderableSeries(wasmContext, {
        dataSeries: new XyyDataSeries(wasmContext, { xValues, yValues, y1Values, dataSeriesName: "Default Spline" }),
        strokeThickness: 3,
        fill: appTheme.VividOrange + "33",
        fillY1: appTheme.VividSkyBlue + "33",
        stroke: appTheme.VividOrange,
        strokeY1: appTheme.VividSkyBlue,
        interpolationPoints: 20, // Choose the number of points to interpolate for smoothing
        pointMarker: new EllipsePointMarker(wasmContext, {
            width: 7,
            height: 7,
            fill: appTheme.PaleSkyBlue,
            stroke: appTheme.VividSkyBlue,
        }),
        animation: new ScaleAnimation({ duration: 800, zeroLine: 0, fadeEffect: true }),
    });

    sciChartSurface.renderableSeries.add(rendSeries);

    const bezierSeries = new SplineBandRenderableSeries(wasmContext, {
        dataSeries: new XyyDataSeries(wasmContext, {
            xValues,
            yValues,
            y1Values,
            dataSeriesName: "XyyBezier (Range Restricted)",
        }),
        stroke: appTheme.VividPurple,
        strokeY1: appTheme.VividRed,
        fill: appTheme.VividPurple + "33",
        fillY1: appTheme.VividRed + "33",
        pointMarker: new EllipsePointMarker(wasmContext, {
            width: 7,
            height: 7,
            fill: appTheme.PaleSkyBlue,
            stroke: appTheme.VividSkyBlue,
        }),
        yAxisId: "y2",
        strokeThickness: 3,
        interpolationPoints: 20, // Choose the number of points to interpolate for smoothing
        animation: new ScaleAnimation({ duration: 800, zeroLine: 0, fadeEffect: true }),
    });
    const bezierTransform = new XyyBezierRenderDataTransform(
        bezierSeries,
        wasmContext,
        [bezierSeries.drawingProviders[0]],
        { interpolationPoints: 20, curvature: 0.5 }
    );
    bezierSeries.renderDataTransform = bezierTransform;
    sciChartSurface.renderableSeries.add(bezierSeries);

    const curvatureAnnotation = new AxisMarkerAnnotation({
        yCoordinateMode: ECoordinateMode.Relative,
        y1: 0.5,
        yAxisId: "y2",
        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);

    sciChartSurface.annotations.add(
        new NativeTextAnnotation({
            xCoordinateMode: ECoordinateMode.Relative,
            yCoordinateMode: ECoordinateMode.Relative,
            x1: 0.9,
            y1: 0.9,
            horizontalAnchorPoint: EHorizontalAnchorPoint.Right,
            text: "Drag the left or right y axis to overlay and compare the series",
            fontSize: 16,
        })
    );

    // Optional: Add some interactivity modifiers
    sciChartSurface.chartModifiers.add(
        new YAxisDragModifier({ dragMode: EDragMode.Panning }),
        new MouseWheelZoomModifier(),
        new LegendModifier({
            orientation: ELegendOrientation.Vertical,
            placement: ELegendPlacement.TopRight,
            showCheckboxes: true,
        })
    );

    return { wasmContext, sciChartSurface };
};

Spline Band Series Chart Example - React

Overview

This example demonstrates how to render a Spline Band Series Chart using SciChart.js in a React environment. The chart displays two spline band series with smooth interpolations and dynamic annotations, enabling the visualization of overlapping high-low filled areas. The example leverages asynchronous initialization for efficient resource loading and incorporates interactive features for enhanced user engagement.

Technical Implementation

The chart is implemented using a React functional component that integrates the SciChart.js library through the <SciChartReact/> component. The asynchronous initialization of the SciChartSurface is achieved with async/await in the custom drawExample function, following practices outlined in Creating a SciChart React Component from the Ground Up. The series data is constructed using the XyyDataSeries which is then visualized via a SplineBandRenderableSeries. In addition, a Bezier transform, implemented through the XyyBezierRenderDataTransform, enables smooth interpolation of data points, enhancing the visual quality of the chart.

Features and Capabilities

The example showcases several advanced features including real-time updates via draggable annotations (AxisMarkerAnnotation) and interactive chart modifiers like YAxisDragModifier and MouseWheelZoomModifier. The inclusion of ScaleAnimation provides smooth transition effects when the chart is rendered. Moreover, multiple Y-axes are configured with custom styling to compare different data ranges side-by-side. For more details on spline band chart specifics, refer to The Spline (Smoothed) Band Series Type.

Integration and Best Practices

React integration is streamlined by using the dedicated SciChart React component, which encapsulates the complexity of chart initialization and rendering. The asynchronous model not only improves load times but also facilitates better resource management and cleanup. Developers are encouraged to explore advanced features such as interactive annotations and custom transforms for enhanced data visualization, while following best practices for component lifecycle and performance optimization as highlighted in the React Charts with SciChart.js documentation. Additionally, using built-in chart modifiers improves interactivity and usability, contributing to modern, responsive charting solutions.