JavaScript Chart with Central Axes

Demonstrates how to create a JavaScript Chart with central axes using SciChart.js, High Performance JavaScript Charts

src/drawExample.ts
import { appTheme } from "./appTheme";
import {
    CentralAxesLayoutManager,
    EAxisAlignment,
    ECoordinateMode,
    EHorizontalAnchorPoint,
    EInnerAxisPlacementCoordinateMode,
    ELineDrawMode,
    FastLineRenderableSeries,
    ICentralAxesLayoutManagerOptions,
    MouseWheelZoomModifier,
    NumericAxis,
    NumberRange,
    SciChartSurface,
    TextAnnotation,
    TSciChart,
    XyDataSeries,
    ZoomExtentsModifier,
    ZoomPanModifier,
    FadeAnimation,
} from "scichart";

export const drawExample = async (rootElement: string | HTMLDivElement) => {
    const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
        theme: appTheme.SciChartJsTheme,
    });

    // Optional parameters to control exact placement of the axis
    // These are defaults: which fix the axes in the center of the chart
    // Relative coordinate mode and 0.5 means 'place half way'
    // const options: ICentralAxesLayoutManagerOptions = {
    //     horizontalAxisPositionCoordinateMode: EInnerAxisPlacementCoordinateMode.Relative,
    //     verticalAxisPositionCoordinateMode: EInnerAxisPlacementCoordinateMode.Relative,
    //     horizontalAxisPosition: 0.5,
    //     verticalAxisPosition: 0.5
    // };

    // These options keep the axes crossing at (0,0), so the axes pan with the chart
    const options: ICentralAxesLayoutManagerOptions = {
        horizontalAxisPositionCoordinateMode: EInnerAxisPlacementCoordinateMode.DataValue,
        verticalAxisPositionCoordinateMode: EInnerAxisPlacementCoordinateMode.DataValue,
        horizontalAxisPosition: 0,
        verticalAxisPosition: 0,
    };

    // Control the placement of the axis by specifying CentralAxesLayoutManager
    // and isInnerAxis property
    sciChartSurface.layoutManager = new CentralAxesLayoutManager(options);

    // Configure x,y axis with central layout - oscilloscope style
    sciChartSurface.xAxes.add(
        new NumericAxis(wasmContext, {
            visibleRange: new NumberRange(-5, 5),
            isInnerAxis: true, // required for central axis
            axisAlignment: EAxisAlignment.Top,
            labelStyle: {
                color: appTheme.TextColor,
            },
            axisBorder: {
                borderTop: 1,
                color: appTheme.VividSkyBlue,
            },
        })
    );

    sciChartSurface.yAxes.add(
        new NumericAxis(wasmContext, {
            visibleRange: new NumberRange(-5, 5),
            isInnerAxis: true, // required for central axis
            axisAlignment: EAxisAlignment.Left,
            labelStyle: {
                color: appTheme.TextColor,
            },
            axisBorder: {
                borderLeft: 1,
                color: appTheme.VividSkyBlue,
            },
        })
    );

    // Add a line series
    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            drawNaNAs: ELineDrawMode.PolyLine,
            dataSeries: getButterflyCurve(wasmContext, 20000),
            isDigitalLine: false,
            stroke: appTheme.VividTeal,
            animation: new FadeAnimation({ duration: 500 }),
        })
    );

    // Add title annotation
    sciChartSurface.annotations.add(
        new TextAnnotation({
            text: "SciChart.js allows axis layout customisation including Central axis",
            fontSize: 16,
            textColor: appTheme.ForegroundColor,
            x1: 0,
            xCoordShift: 10,
            y1: 0,
            yCoordShift: 10,
            opacity: 0.77,
            horizontalAnchorPoint: EHorizontalAnchorPoint.Left,
            xCoordinateMode: ECoordinateMode.Relative,
            yCoordinateMode: ECoordinateMode.Relative,
        })
    );

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

    return { sciChartSurface, wasmContext };
};

function getButterflyCurve(wasmContext: TSciChart, count: number = 2000) {
    const temp = 0.01;
    const dataSeries = new XyDataSeries(wasmContext);
    for (let i = 0; i < count; i++) {
        const t = i * temp;

        const multiplier = Math.pow(Math.E, Math.cos(t)) - 2 * Math.cos(4 * t) - Math.pow(Math.sin(t / 12), 5);

        const x = Math.sin(t) * multiplier;
        const y = Math.cos(t) * multiplier;
        dataSeries.append(x, y);
    }
    return dataSeries;
}

Central Axes JavaScript Chart

Overview

This example demonstrates how to create a high-performance chart with central axes using SciChart.js in a JavaScript framework. The chart positions the x and y axes to cross at the data value (0,0), producing an oscilloscope-like layout. It leverages the advanced customization available in SciChart.js for axis placement, as detailed in the Central Axis Layout documentation.

Technical Implementation

The implementation begins by initializing a SciChartSurface with an integrated WebAssembly context, following guidelines from the Tutorial 01 - Including SciChart.js in an HTML Page using CDN documentation. Both the x and y axes are configured as inner axes by enabling the axis.isInnerAxis property and aligned to the center by setting the sciChartSurface.layoutManager property equal to a CentralAxesLayoutManager instance with data value based positioning. A dynamically generated butterfly curve is rendered using the FastLineRenderableSeries and utilizes a fade animation as provided by the Animations API for performance and visual enhancement. Interaction is further enriched with modifiers such as ZoomPanModifier, MouseWheelZoomModifier, and ZoomExtentsModifier, which support smooth zooming and panning.

Features and Capabilities

This example showcases advanced chart capabilities such as real-time data rendering for complex curves and dynamic interactivity. The efficient use of WebAssembly ensures high performance even when visualizing large data sets. Additionally, the chart demonstrates clear separation of concerns by isolating initialization, axis configuration, data rendering, and user interaction, which allows for robust customization based on the application’s needs.

Integration and Best Practices

Integrating SciChart.js into a JavaScript application is streamlined by employing a modular approach where the chart initialization function is directly invoked with a designated root element. Resource cleanup is handled by returning a destructor function that calls the delete() method on the SciChartSurface, in line with best practices suggested in the Memory Best Practices documentation. Developers looking to extend or optimize their chart applications can also refer to the Getting Started with SciChart JS guide for additional insights on integration and performance optimization.