JavaScript Fan Chart

Here we demonstrate how to create a JavaScript Fan Chart using SciChart.js. Zoom in and out to see the detail you can go to using our JavaScript Charts

src/drawExample.ts
import { appTheme } from "./appTheme";
import { RandomWalkGenerator } from "./RandomWalkGenerator";

import {
    MouseWheelZoomModifier,
    ZoomExtentsModifier,
    ZoomPanModifier,
    XyyDataSeries,
    NumericAxis,
    SciChartSurface,
    XyDataSeries,
    ENumericFormat,
    WaveAnimation,
    SplineLineRenderableSeries,
    TextAnnotation,
    EVerticalAnchorPoint,
    SplineBandRenderableSeries,
} from "scichart";
// tslint:disable:max-line-length

const animation = new WaveAnimation({ duration: 700, fadeEffect: true });

export type TVarPoint = {
    date: number;
    actual: number;
    varMax: number;
    var4: number;
    var3: number;
    var2: number;
    var1: number;
    varMin: number;
};

export function getVarianceData(): TVarPoint[] {
    const varianceData: TVarPoint[] = [];
    const startDate = 1546300800; // 1st Jan 2019
    const dateStep = 1546387200 - startDate; // one day;

    const length: number = 10;
    const yValues: number[] = new RandomWalkGenerator().Seed(923478).getRandomWalkSeries(length).yValues;
    for (let i = 0; i < length; i++) {
        const date = startDate + dateStep * i;

        let varMax: number = NaN;
        let var4: number = NaN;
        let var3: number = NaN;
        let var2: number = NaN;
        let var1: number = NaN;
        let varMin: number = NaN;

        if (i > 4) {
            varMax = yValues[i] + (i - 5) * 0.3;
            var4 = yValues[i] + (i - 5) * 0.2;
            var3 = yValues[i] + (i - 5) * 0.1;
            var2 = yValues[i] - (i - 5) * 0.1;
            var1 = yValues[i] - (i - 5) * 0.2;
            varMin = yValues[i] - (i - 5) * 0.3;
        }

        varianceData.push({ date, actual: yValues[i], varMax, var4, var3, var2, var1, varMin });
    }

    return varianceData;
}

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, { labelFormat: ENumericFormat.Date_DDMMYYYY }));
    sciChartSurface.yAxes.add(new NumericAxis(wasmContext));

    // Generates some data for the example as an array of TVarPoint: {
    //     date: number;
    //     actual: number;
    //     varMax: number;
    //     var4: number;
    //     var3: number;
    //     var2: number;
    //     var1: number;
    //     varMin: number;
    // }
    const varianceData = getVarianceData();

    // To render the fan chart, we use a Line Chart with XyDataSeries
    // and three Band charts with XyyDataSeries
    const actualDataSeries = new XyDataSeries(wasmContext);
    const variance3DataSeries = new XyyDataSeries(wasmContext);
    const variance2DataSeries = new XyyDataSeries(wasmContext);
    const variance1DataSeries = new XyyDataSeries(wasmContext);

    actualDataSeries.appendRange(
        varianceData.map((v) => v.date),
        varianceData.map((v) => v.actual)
    );
    variance3DataSeries.appendRange(
        varianceData.map((v) => v.date),
        varianceData.map((v) => v.varMin),
        varianceData.map((v) => v.varMax)
    );
    variance2DataSeries.appendRange(
        varianceData.map((v) => v.date),
        varianceData.map((v) => v.var1),
        varianceData.map((v) => v.var4)
    );
    variance1DataSeries.appendRange(
        varianceData.map((v) => v.date),
        varianceData.map((v) => v.var2),
        varianceData.map((v) => v.var3)
    );

    // Add a line series with the Xy data (the actual data)
    // Note use FastLineRenderableSeries for non-spline version
    sciChartSurface.renderableSeries.add(
        new SplineLineRenderableSeries(wasmContext, {
            strokeThickness: 2,
            dataSeries: actualDataSeries,
            stroke: appTheme.VividPink,
            animation,
        })
    );

    // Add band series with progressively higher opacity for the fan variance data
    // Note use FastBandRenderableSeries for non-spline version
    sciChartSurface.renderableSeries.add(
        new SplineBandRenderableSeries(wasmContext, {
            dataSeries: variance3DataSeries,
            opacity: 0.15,
            fill: appTheme.VividPink,
            strokeY1: "#00000000",
            animation,
        })
    );
    sciChartSurface.renderableSeries.add(
        new SplineBandRenderableSeries(wasmContext, {
            dataSeries: variance2DataSeries,
            opacity: 0.33,
            fill: appTheme.VividPink,
            strokeY1: "#00000000",
            animation,
        })
    );
    sciChartSurface.renderableSeries.add(
        new SplineBandRenderableSeries(wasmContext, {
            dataSeries: variance1DataSeries,
            opacity: 0.5,
            fill: appTheme.VividPink,
            strokeY1: "#00000000",
            animation,
        })
    );

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

    // Optional: Add some annotations (text) to show detail
    sciChartSurface.annotations.add(
        new TextAnnotation({
            x1: varianceData[0].date,
            y1: varianceData[0].actual,
            verticalAnchorPoint: EVerticalAnchorPoint.Bottom,
            yCoordShift: -50,
            text: "Actual data",
            opacity: 0.45,
            textColor: appTheme.ForegroundColor,
        })
    );

    sciChartSurface.annotations.add(
        new TextAnnotation({
            x1: varianceData[5].date,
            y1: varianceData[5].actual,
            text: "Forecast Variance",
            verticalAnchorPoint: EVerticalAnchorPoint.Top,
            yCoordShift: 50,
            opacity: 0.45,
            textColor: appTheme.ForegroundColor,
        })
    );

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

Fan Chart Example in JavaScript

Overview

This example demonstrates how to create a sophisticated fan chart using SciChart.js with JavaScript. The chart visualizes actual data alongside forecast variance by combining a spline line series with multiple SplineBandRenderableSeries, providing an intuitive visualization of trend and variance data over time.

Technical Implementation

The chart is initialized asynchronously using SciChartSurface.create(), ensuring that the WebGL-based rendering engine and the underlying WebAssembly context are set up efficiently. This asynchronous approach is detailed in the Getting Started with SciChart JS guide. The fan chart effect is achieved by rendering an actual data line via SplineLineRenderableSeries and overlaying it with progressively opaque band series using SplineBandRenderableSeries, as described in The Fan Charts Type documentation.

Features and Capabilities

The implementation incorporates interactive modifiers such as ZoomPanModifier and MouseWheelZoomModifier, enabling smooth zooming and panning interactions. Additionally, it applies a WaveAnimation for smooth transition effects, while the use of XyyDataSeries enables rendering of the band charts that illustrate forecast variance. For a deeper understanding of the data series used, developers can refer to the XyyDataSeries API documentation.

Integration and Best Practices

Best practices such as efficient resource management are observed by encapsulating the chart creation in an asynchronous function and ensuring proper cleanup using sciChartSurface.delete(). Performance optimization is a key consideration, with high-performance WebAssembly rendering capabilities that are further explained in the Performance Tips & Tricks and Memory Best Practices guides. These practices help maintain responsiveness even when handling complex chart rendering and dynamic data updates.