JavaScript Smooth Stacked Mountain Chart

Learn how to make a JavaScript Smooth Stacked Mountain Chart using with SciChart's powerful JavaScript Charts and it's range of features.

src/drawExample.ts
import {
    AUTO_COLOR,
    BaseDataSeries,
    ELegendOrientation,
    ELegendPlacement,
    LegendModifier,
    makeIncArray,
    MouseWheelZoomModifier,
    NumericAxis,
    ScaleAnimation,
    SciChartSurface,
    StackedMountainCollection,
    SmoothStackedMountainRenderableSeries,
    XyDataSeries,
    ZoomExtentsModifier,
    ZoomPanModifier,
} 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,
    });

    // Create an xAxis, yAxis
    sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { labelPrecision: 0 }));
    sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { labelPrecision: 0 }));

    // Group these StackedMountain series together in a StackedMountainCollection
    const stackedMountainCollection = new StackedMountainCollection(wasmContext);

    const xValues = makeIncArray(20);
    for (let i = 0; i < 10; i++) {
        const yValues = xValues.map(() => Math.random() * 10);
        const stackedMountain = new SmoothStackedMountainRenderableSeries(wasmContext, {
            id: i.toString(),
            dataSeries: new XyDataSeries(wasmContext, { xValues, yValues }),
            fill: AUTO_COLOR,
            stroke: AUTO_COLOR,
            strokeThickness: 2,
            // Stagger each series so the stack builds from bottom to top.
            animation: new ScaleAnimation({ duration: 500, delay: i * 200 }),
        });
        stackedMountainCollection.add(stackedMountain);
    }

    // Add the StackedMountainCollection to the chart
    sciChartSurface.renderableSeries.add(stackedMountainCollection);

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

    const legendModifier = new LegendModifier({
        placement: ELegendPlacement.TopLeft,
        orientation: ELegendOrientation.Vertical,
        showLegend: true,
        showCheckboxes: true,
        showSeriesMarkers: true,
    });
    // Add a legend to the chart to show the series
    sciChartSurface.chartModifiers.add(legendModifier);

    stackedMountainCollection.asArray().forEach((series) =>
        series.isVisibleChanged.subscribe((data) => {
            if (data.isVisible) {
                data.sourceSeries.runAnimation(
                    new ScaleAnimation({
                        duration: 500,
                    })
                );
            } else {
                // To animate out, we have to trick the series into remaining visible while the animation runs.
                // We set the backing value of isVisible to true, and only set it false when the animation completes
                // @ts-ignore
                data.sourceSeries.isVisibleProperty = true;
                data.sourceSeries.runAnimation(
                    new ScaleAnimation({
                        duration: 500,
                        reverse: true,
                        onCompleted: () => {
                            (data.sourceSeries.dataSeries as BaseDataSeries).revertAnimationVectors();
                            // @ts-ignore
                            data.sourceSeries.isVisibleProperty = false;
                            // Force the legend to update
                            legendModifier.sciChartLegend.invalidateLegend();
                        },
                    })
                );
            }
        })
    );

    sciChartSurface.zoomExtents();

    const toggleHundredPercentMode = (value: boolean) => {
        stackedMountainCollection.isOneHundredPercent = value;
        sciChartSurface.zoomExtents(200);
    };

    return { sciChartSurface, controls: { toggleHundredPercentMode } };
};

Smooth Stacked Mountain Chart Example using JavaScript

Overview

This example demonstrates how to create a high performance and interactive Smooth Stacked Mountain Chart using SciChart.js in a JavaScript environment. The chart showcases multiple smooth stacked mountain series with dynamic, randomized data, offering animated transitions and rich interactivity through various modifiers.

Technical Implementation

The chart is initialized asynchronously via SciChartSurface.create(), ensuring efficient bootstrapping of the WebGL-based rendering engine. A StackedMountainCollection groups several SmoothStackedMountainRenderableSeries, each configured with randomized Y values and animated using ScaleAnimation to achieve smooth state transitions. The series subscribe to visibility change events using the Series isVisible and isVisibleChanged API for real-time updates.

Features and Capabilities

The implementation builds upon several advanced features:

  • The use of StackedMountainCollection allows for effective stacking of multiple series to form a combined visual representation.
  • Animated transitions ensure changes in series visibility and data updates are smooth and visually appealing through the application of ScaleAnimation.
  • Interactive modifiers such as ZoomExtentsModifier, ZoomPanModifier, and MouseWheelZoomModifier offer enhanced user control over zooming and panning, making the chart highly responsive to user inputs.

Integration and Best Practices

The example leverages best practices for JavaScript integration by isolating the chart initialization logic and utilizing asynchronous loading for optimal performance. Developers are encouraged to explore the ZoomPanModifier for customizing interactivity and to follow performance optimization tips available in SciChart.js documentation when managing multiple animated series. This approach ensures that charts remain responsive and maintain high performance even under complex rendering scenarios.