React Style Animation

Demonstrates how to run Style Transition Animations using SciChart.js, High Performance JavaScript Charts

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

// Colours used for style 1
const lineColor1 = appTheme.VividOrange;
const fillColor1 = appTheme.VividOrange + "33";
const lineColor2 = appTheme.VividSkyBlue;
const fillColor2 = appTheme.VividSkyBlue + "33";

// Colours used for style 2

const fillColor1b = appTheme.VividPink + "33";

const fillColor2b = appTheme.PaleTeal + "33";

const POINTS = 100;
const STEP = (3 * Math.PI) / POINTS;

export const drawExample = async (rootElement: string | HTMLDivElement) => {
    // create a chart with X, Y axis
    const { wasmContext, sciChartSurface } = await SciChartSurface.create(rootElement, {
        theme: appTheme.SciChartJsTheme,
    });
    sciChartSurface.xAxes.add(new NumericAxis(wasmContext));
    sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { growBy: new NumberRange(0.2, 0.2) }));

    // Generate some data
    let xValues: number[] = [];
    let yValues: number[] = [];
    let y1Values: number[] = [];
    for (let x = 0; x <= POINTS; x++) {
        const k = 1 - x / 2000;
        const y = Math.sin(x * STEP) * k * 0.7 + 1;
        const y1 = Math.cos(x * STEP) * k + 1;
        xValues.push(x);
        yValues.push(y);
        y1Values.push(y1);
    }
    // Create a band series with the data and initial stroke/fill colours
    const bandSeries = new FastBandRenderableSeries(wasmContext, {
        dataSeries: new XyyDataSeries(wasmContext, { xValues, yValues, y1Values }),
        strokeThickness: 4,
        stroke: lineColor1,
        strokeY1: lineColor2,
        fill: fillColor1,
        fillY1: fillColor2,
    });
    sciChartSurface.renderableSeries.add(bandSeries);

    // create a temp series for passing animation values
    const animationSeries = new XyyDataSeries(wasmContext);
    // register this so it is deleted along with the main surface
    sciChartSurface.addDeletable(animationSeries);
    // Animate both the data & the style of the chart, using a style animation
    const animateChartStyle = (isStyle1: boolean) => {
        xValues = [];
        yValues = [];
        y1Values = [];

        // Depending on the flag passed in, change the data. Just for eye candy
        if (isStyle1) {
            for (let x = 0; x <= POINTS; x++) {
                const k = 1 - x / 2000;
                const y = Math.sin(x * STEP) * k * 0.7 + 1;
                const y1 = Math.cos(x * STEP) * k + 1;
                xValues.push(x);
                yValues.push(y);
                y1Values.push(y1);
            }
        } else {
            for (let x = 0; x <= POINTS; x++) {
                const k = 1 - x / 2000;
                const y = Math.cos(x * STEP) * k * 0.7 + 1;
                const y1 = Math.sin(x * STEP) * k + 1;
                xValues.push(x);
                yValues.push(y);
                y1Values.push(y1);
            }
        }
        // Set the values on the temp series
        animationSeries.clear();
        animationSeries.appendRange(xValues, yValues, y1Values);

        // Running an animation on the series lets you change data as well as styles
        bandSeries.runAnimation(
            new BandAnimation({
                duration: 1000,
                styles: {
                    strokeThickness: isStyle1 ? 4 : 8,
                    stroke: isStyle1 ? lineColor1 : appTheme.VividPink,
                    strokeY1: isStyle1 ? lineColor2 : appTheme.PaleTeal,
                    fill: isStyle1 ? fillColor1 : fillColor1b,
                    fillY1: isStyle1 ? fillColor2 : fillColor2b,
                },
                // Don't create a new dataSeries here or it will leak and crash if run repeatedly
                dataSeries: animationSeries,
            })
        );
    };
    // Add some interactivity modifiers
    sciChartSurface.chartModifiers.add(
        new ZoomExtentsModifier(),
        new ZoomPanModifier({ enableZoom: true }),
        new MouseWheelZoomModifier()
    );

    setTimeout(() => animateChartStyle(false), 1000);
    setTimeout(() => animateChartStyle(true), 3000);

    sciChartSurface.zoomExtents();

    return { wasmContext, sciChartSurface, controls: { animateChartStyle } };
};

React Style Animation

Overview

This example, "Style Animation", demonstrates how to implement smooth style transition animations within a SciChart-based React application. The purpose is to showcase dynamic updates of chart styles and data using the <SciChartReact/> component along with shared CSS controls interactive controls.

Technical Implementation

The implementation begins with asynchronous initialization via the <SciChartReact/> component. A function called drawExample creates a SciChartSurface, adds numeric X and Y axes, and constructs a FastBandRenderableSeries using an XyyDataSeries. The example then uses the BandAnimation class to animate changes in both data and styles, toggling between two visual presets. This approach complies with the guidance provided in the Style Transition Animations documentation.

Features and Capabilities

The example features real-time style updates controlled by a shared CSS controls button group that allows users to switch between animation presets. It integrates interactive modifiers such as ZoomPanModifier, MouseWheelZoomModifier, and ZoomExtentsModifier to enhance user engagement. The onInit callback of <SciChartReact/> is used to update the React state with external animation control callbacks, reflecting best practices for React state management as outlined in Updating React component state from callback.

Integration and Best Practices

Developers will appreciate the seamless integration of SciChart.js with React in this example. The initialization leverages asynchronous techniques with React hooks, ensuring that the chart and its animations are both efficient and responsive. Further insights into integrating SciChart with React can be found in the React Charts with SciChart.js article. The example uses shared CSS controls for its interactive options. For overall performance considerations, developers are encouraged to review strategies for optimizing chart performance in Performance Optimisation of JavaScript Applications & Charts.