React Mountain Chart

Use our Mountain Chart example to learn how to create a React Mountain Chart using SciChart.js, our in-house built JavaScript Chart framework.

src/drawExample.ts
import {
    FastMountainRenderableSeries,
    GradientParams,
    MouseWheelZoomModifier,
    NumberRange,
    NumericAxis,
    Point,
    RubberBandXyZoomModifier,
    SciChartSurface,
    WaveAnimation,
    XyDataSeries,
    ZoomExtentsModifier,
} from "scichart";
import { RandomWalkGenerator } from "./RandomWalkGenerator";
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 and YAxis
    sciChartSurface.xAxes.add(new NumericAxis(wasmContext));
    sciChartSurface.yAxes.add(
        new NumericAxis(wasmContext, {
            growBy: new NumberRange(0.05, 0.05),
        })
    );

    const POINTS = 1000;

    // Create arrays of x, y values (just arrays of numbers)
    const { xValues, yValues } = new RandomWalkGenerator().Seed(0).getRandomWalkSeries(POINTS);

    // Create a Mountain Series and add to the chart
    sciChartSurface.renderableSeries.add(
        new FastMountainRenderableSeries(wasmContext, {
            dataSeries: new XyDataSeries(wasmContext, { xValues, yValues }),
            stroke: appTheme.VividSkyBlue,
            strokeThickness: 3,
            zeroLineY: 0.0,
            fill: appTheme.VividSkyBlue, // when a solid color is required, use fill
            // when a gradient is required, use fillLinearGradient
            fillLinearGradient: new GradientParams(new Point(0, 0), new Point(0, 1), [
                { color: appTheme.MutedSkyBlue, offset: 0 },
                { color: "Transparent", offset: 1 },
            ]),
            animation: new WaveAnimation({ duration: 1000, fadeEffect: true, zeroLine: 0 }),
        })
    );

    // Optional: Add some interactivity to the chart
    sciChartSurface.chartModifiers.add(
        new ZoomExtentsModifier(),
        new RubberBandXyZoomModifier(),
        new MouseWheelZoomModifier()
    );

    sciChartSurface.zoomExtents();

    return { wasmContext, sciChartSurface };
};

React Mountain (Area) Chart Example in React

Overview

This example demonstrates the creation of a React Mountain Chart using SciChart.js, showcasing how to integrate advanced charting functionality into a React application. The chart visualizes a dynamic dataset generated by a random walk and is rendered with a mountain series that features both a gradient fill and an animated wave effect.

Technical Implementation

The chart is initialized via an asynchronous function which creates a SciChartSurface and configures it with numeric X and Y axes. A mountain series is added using the FastMountainRenderableSeries with configurable properties such as stroke thickness, gradient fills (using GradientParams), and a wave animation effect. The component is integrated into React by passing the initialization function as a prop to the <SciChartReact/> component, following best practices from React Charts with SciChart.js: Introducing “SciChart React”.

Features and Capabilities

The example emphasizes several advanced features: interactive zooming and panning through modifiers like ZoomExtentsModifier, RubberBandXyZoomModifier, and MouseWheelZoomModifier; dynamic data generation using a RandomWalkGenerator; and visually appealing animations on the mountain series. This comprehensive feature set aligns with recommendations found in Creating a SciChart React Component from the Ground Up and the detailed mountain chart documentation available in the SciChart.js Mountain Chart Documentation.

Integration and Best Practices

Integrating SciChart.js into a React application is streamlined by the use of the <SciChartReact/> component, which accepts a function prop for asynchronous chart initialization. This pattern supports effective resource management, especially with WebGL and WebAssembly contexts, and allows for efficient performance optimization. Developers can further explore techniques in asynchronous React patterns through Using async/await inside a React functional component. Overall, this example serves as an instructive guide on implementing high-performance, interactive charts in React while following robust coding and integration best practices.