JavaScript 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 } };
};

Style Animation - JavaScript

Overview

This example, "Style Animation", demonstrates how to implement dynamic style transition animations using SciChart.js with JavaScript. It focuses on animating both the data and visual properties of a band series to provide high-performance, real-time updates.

Technical Implementation

The implementation begins with asynchronous initialization of the SciChartSurface using a WebAssembly context, as detailed in the Creating a new SciChartSurface and loading Wasm guide. Numeric axes are set up with custom NumberRange values, and data is generated using trigonometric functions in a standard JavaScript loop. A FastBandRenderableSeries is then created with an XyyDataSeries, and the style animation is executed through the runAnimation method of the BandAnimation class. This process smoothly transitions properties such as stroke, fill, and stroke thickness over a 1000ms duration, aligning with the techniques described in the Style Transition Animations documentation.

Features and Capabilities

Key features of this example include real-time updates of both chart data and styles, interactive enhancements such as ZoomExtentsModifier, ZoomPanModifier, and MouseWheelZoomModifier modifiers, and efficient memory management by registering disposable data series via addDeletable in SciChartSurface. The configuration of the band series is aligned with the best practices highlighted within the The Band Series type guide.

Integration and Best Practices

By relying solely on JavaScript, this example demonstrates how to integrate SciChart.js efficiently without additional frameworks or helpers. Developers are encouraged to follow the asynchronous initialization methods using WebAssembly for optimal performance and to use built-in interactive modifiers to boost user interactivity. Moreover, the example’s approach to managing temporary data via SciChartSurface.addDeletable() reflects recommended Memory Best Practices in SciChart.js. This ensures that resources are managed effectively even during intensive animation operations.