React Stacked Mountain Chart

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

src/drawExample.ts
import {
    ELegendOrientation,
    ELegendPlacement,
    LegendModifier,
    MouseWheelZoomModifier,
    NumericAxis,
    SciChartSurface,
    StackedMountainCollection,
    StackedMountainRenderableSeries,
    WaveAnimation,
    XyDataSeries,
    ZoomExtentsModifier,
    ZoomPanModifier,
} from "scichart";
import { appTheme } from "./appTheme";
import { xValues, y1Values, y2Values, y3Values, y4Values } from "./stackedMountainChartData";

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

    // A collection stacks its series in insertion order.
    const stackedMountainCollection = new StackedMountainCollection(wasmContext);
    for (const { yValues, dataSeriesName, fill } of [
        { yValues: y1Values, dataSeriesName: "Apples", fill: appTheme.VividPurple },
        { yValues: y2Values, dataSeriesName: "Pears", fill: appTheme.VividPink },
        { yValues: y3Values, dataSeriesName: "Bananas", fill: appTheme.VividSkyBlue },
        { yValues: y4Values, dataSeriesName: "Oranges", fill: appTheme.VividOrange },
    ]) {
        stackedMountainCollection.add(
            new StackedMountainRenderableSeries(wasmContext, {
                dataSeries: new XyDataSeries(wasmContext, { xValues, yValues, dataSeriesName }),
                fill: fill + "AA",
                stroke: appTheme.PaleSkyBlue,
                strokeThickness: 2,
            })
        );
    }
    stackedMountainCollection.animation = new WaveAnimation({ duration: 600, fadeEffect: true });

    // 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()
    );

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

    sciChartSurface.zoomExtents();

    return { wasmContext, sciChartSurface, stackedMountainCollection };
};

Stacked Mountain Chart Example with React

Overview

This example demonstrates a high performance stacked mountain chart built with SciChart.js in a React framework. It renders multiple data series representing different fruit categories such as Apples, Pears, Bananas, and Oranges, which are grouped together for a visually appealing chart presentation.

Technical Implementation

The chart is initialized by creating a SciChartSurface with custom theming and numeric axes. A collection of StackedMountainRenderableSeries is created using XyDataSeries to store the data for each category, and these series are grouped into a StackedMountainCollection. Setting isOneHundredPercent property on StackedMountainCollection toggles 100% stacked behaviour. The implementation includes a smooth WaveAnimation for animated transitions and interactive modifiers such as ZoomExtentsModifier, ZoomPanModifier, and MouseWheelZoomModifier, ensuring rich chart interactivity. React state management and refs are utilized to toggle the 100% stacked mode, enabling real-time update capabilities. For more details on the Stacked Mountain Series implementation, refer to the API documentation.

Features and Capabilities

The example offers advanced capabilities including grouping multiple series, animated transitions, and interactive features. With the integrated toggle for 100% mode, users can switch between standard and 100% stacked views, and the chart dynamically adapts its rendering. This approach emphasizes the use of React hooks for efficient state management and real-time interactive updates.

Integration and Best Practices

Integration with React is achieved using the <SciChartReact/> component, ensuring a clean and modular implementation. Developers can explore best practices for combining React state management with SciChart.js as demonstrated in this example and learn more from the SciChart React integration guide. Additionally, performance optimization techniques, including efficient use of WebGL for rendering, are inherent in SciChart.js, which is well documented in the Tutorial: Setting up a project with SciChartReact. Custom theming further enhances the visual appeal by allowing the chart's appearance to be aligned with application styling, as discussed in the Chart Styling - Creating a Custom Theme documentation.