JavaScript Mountain Chart

Use our Mountain Chart example to learn how to create a JavaScript 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 };
};

JavaScript Mountain Chart Example

Overview

This example demonstrates how to create a high-performance, interactive Mountain / Area Chart using SciChart.js in a JavaScript environment. The chart visualizes a dynamically generated random walk series with an engaging mountain chart rendering that includes gradient fills and a wave animation effect.

Technical Implementation

The chart is initialized asynchronously using async/await, which creates a SciChartSurface with a WebAssembly context for enhanced performance as explained in the WebAssembly Initialization Tutorial. Numeric axes are configured with the NumericAxis and custom range settings to ensure optimal scaling. The data series is generated using a random walk algorithm and rendered via the FastMountainRenderableSeries that supports both solid and gradient fill properties configured through GradientParams. A WaveAnimation is applied to bring fluid motion to the chart.

Features and Capabilities

The example offers advanced features including dynamic data updates implementing a smooth random walk, a gradient fill that transitions from a solid color to transparency, and interactive modifiers such as zoom extents, rubber band zoom, and mouse wheel zoom. These features are integrated to provide an intuitive, real-time experience, further supported by the [Interactive Chart Modifiers](Tutorial 03 - Adding Zooming, Panning Behavior) documentation.

Integration and Best Practices

By leveraging JavaScript, developers can easily integrate this example into any web application without additional frameworks. The asynchronous pattern ensures efficient resource management, and proper cleanup of the SciChartSurface prevents memory leaks, aligning with recommended Memory Best Practices. This implementation showcases how to harness the performance benefits of WebGL and WebAssembly for demanding charting applications while maintaining high levels of interactivity and visual appeal.