React Band Chart

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

src/drawExample.ts
import {
    MouseWheelZoomModifier,
    ZoomExtentsModifier,
    ZoomPanModifier,
    XyyDataSeries,
    NumericAxis,
    FastBandRenderableSeries,
    SciChartSurface,
    NumberRange,
    SweepAnimation,
    GradientParams,
    Point,
} from "scichart";

import { appTheme } from "./appTheme";

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));
    sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { growBy: new NumberRange(0.1, 0.1) }));

    // Create some data for the example. We need X, Y and Y1 values
    const xValues = [];
    const yValues = [];
    const y1Values = [];
    const POINTS = 1000;
    const STEP = (3 * Math.PI) / POINTS;
    for (let i = 0; i <= 1000; i++) {
        const k = 1 - i / 2000;
        xValues.push(i);
        yValues.push(Math.sin(i * STEP) * k * 0.7);
        y1Values.push(Math.cos(i * STEP) * k);
    }

    // Create the band series and add to the chart
    // The bandseries requires a special dataseries type called XyyDataSeries with X,Y and Y1 values
    sciChartSurface.renderableSeries.add(
        new FastBandRenderableSeries(wasmContext, {
            dataSeries: new XyyDataSeries(wasmContext, { xValues, yValues, y1Values }),
            strokeThickness: 3,
            fill: appTheme.VividOrange + "33",
            fillY1: appTheme.VividSkyBlue + "33",
            stroke: appTheme.VividOrange,
            strokeY1: appTheme.VividSkyBlue,
            animation: new SweepAnimation({ duration: 800 }),
        })
    );

    sciChartSurface.renderableSeries.add(
        new FastBandRenderableSeries(wasmContext, {
            dataSeries: new XyyDataSeries(wasmContext, {
                xValues,
                yValues: yValues.map((y) => y - 2),
                y1Values: y1Values.map((y) => y - 2),
            }),
            strokeThickness: 3,
            stroke: appTheme.VividOrange,
            strokeY1: appTheme.VividSkyBlue,
            fillLinearGradient: new GradientParams(new Point(0, 0.6), new Point(0, 0.9), [
                { color: appTheme.MutedRed + "99", offset: 0 },
                { color: appTheme.MutedOrange + "99", offset: 1 },
            ]),
            fillLinearGradientY1: new GradientParams(new Point(0, 0.6), new Point(0, 0.9), [
                { color: appTheme.MutedPurple + "99", offset: 0 },
                { color: appTheme.MutedTeal + "99", offset: 1 },
            ]),
            animation: new SweepAnimation({ duration: 800 }),
        })
    );

    // Optional: Add some interactivity modifiers
    sciChartSurface.chartModifiers.add(
        new ZoomExtentsModifier(),
        new ZoomPanModifier({ enableZoom: true }),
        new MouseWheelZoomModifier()
    );

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

React Band Series Chart

Overview

This example demonstrates a React implementation of a high performance Band Series Chart using SciChart.js. The chart renders two band series with distinct stroke and gradient fill configurations, and incorporates interactive modifiers for zooming and panning.

Technical Implementation

The chart is initialized asynchronously via the <SciChartReact/> component’s initChart property, which is linked to a drawExample function. This function sets up the SciChartSurface with numeric X and Y axes and creates two band series using the FastBandRenderableSeries class with data provided by the XyyDataSeries. The example leverages advanced sweep animations (via SweepAnimation) to animate the rendering of the series. Developers can refer to the Creating a SciChart React Component from the Ground Up guide for more details on asynchronous initialization in a React environment.

Features and Capabilities

The implementation features advanced gradient customizations through fill and fillLinearGradient parameters, allowing different color gradients for the upper and lower bounds of the bands. Interactive modifiers such as ZoomExtentsModifier, ZoomPanModifier, and MouseWheelZoomModifier enhance the user experience by supporting dynamic zooming and panning. The chart performance benefits from SciChart.js’s efficient WebGL rendering, which is crucial for real-time updates and smooth animations. Detailed information on gradient fills can be found in The Band Series type documentation.

Integration and Best Practices

This React-centric implementation follows best practices for integrating SciChart components into React applications. The use of the <SciChartReact/> wrapper ensures that the chart is created and managed in a React-friendly manner. Performance optimizations such as asynchronous chart creation and the reuse of the underlying WebGL context are key to maintaining responsiveness in complex applications. For additional best practices, developers can explore the React Charts with SciChart.js: Introducing “SciChart React” article and the interactive modifiers are further detailed in the ZoomPanModifier documentation to help optimize user interactions.