React Chart with Multiple X Axes

Demonstrates how to create a React Chart with multiple X,Y axis using SciChart.js, High Performance JavaScript Charts

src/drawExample.ts
import {
    EAxisAlignment,
    ECoordinateMode,
    EHorizontalAnchorPoint,
    ELabelAlignment,
    EllipsePointMarker,
    EMultiLineAlignment,
    ENumericFormat,
    EVerticalAnchorPoint,
    EWrapTo,
    FastLineRenderableSeries,
    MouseWheelZoomModifier,
    NativeTextAnnotation,
    NumberRange,
    NumericAxis,
    SciChartSurface,
    TextAnnotation,
    XAxisDragModifier,
    XyDataSeries,
    YAxisDragModifier,
    ZoomExtentsModifier,
    ZoomPanModifier,
} from "scichart";
import { appTheme } from "./appTheme";
import { RandomWalkGenerator } from "./RandomWalkGenerator";

const titleStyle1 = {
    color: appTheme.VividSkyBlue,
    fontSize: 30,
};
const labelStyle1 = {
    color: appTheme.VividSkyBlue,
};
const titleStyle2 = {
    color: appTheme.VividOrange,
    fontSize: 30,
};
const labelStyle2 = {
    color: appTheme.VividOrange,
    alignment: ELabelAlignment.Right,
};

const ID_X_AXIS_2 = "xAxis2";
const ID_Y_AXIS_2 = "yAxis2";

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

    // Add a primary X,Y Axis pair
    sciChartSurface.xAxes.add(
        new NumericAxis(wasmContext, {
            axisAlignment: EAxisAlignment.Bottom,
            axisTitle: "X Axis Bottom",
            axisTitleStyle: titleStyle1,
            labelStyle: labelStyle1,
            backgroundColor: appTheme.VividSkyBlue + "22",
            axisBorder: {
                borderTop: 1,
                color: appTheme.VividSkyBlue,
            },
        })
    );

    sciChartSurface.yAxes.add(
        new NumericAxis(wasmContext, {
            axisAlignment: EAxisAlignment.Left,
            axisTitle: "Y Axis Left",
            axisTitleStyle: titleStyle1,
            labelStyle: labelStyle1,
            growBy: new NumberRange(0.1, 0.1),
            backgroundColor: appTheme.VividSkyBlue + "22",
            axisBorder: {
                borderRight: 1,
                color: appTheme.VividSkyBlue,
            },
        })
    );

    // Add a secondary X,Y Axis pair
    // Series are tied to the axis via the ID_
    sciChartSurface.xAxes.add(
        new NumericAxis(wasmContext, {
            id: ID_X_AXIS_2,
            axisTitleStyle: titleStyle2,
            labelStyle: labelStyle2,
            axisAlignment: EAxisAlignment.Top,
            axisTitle: "X Axis Top",
            backgroundColor: appTheme.VividOrange + "22",
            axisBorder: {
                borderBottom: 1,
                color: appTheme.VividOrange,
            },
        })
    );

    sciChartSurface.yAxes.add(
        new NumericAxis(wasmContext, {
            id: ID_Y_AXIS_2,
            axisTitleStyle: titleStyle2,
            labelStyle: labelStyle2,
            axisAlignment: EAxisAlignment.Right,
            axisTitle: "Y Axis Right",
            labelFormat: ENumericFormat.Decimal,
            labelPrecision: 2,
            growBy: new NumberRange(0.1, 0.1),
            backgroundColor: appTheme.VividOrange + "22",
            axisBorder: {
                borderLeft: 1,
                color: appTheme.VividOrange,
            },
        })
    );

    // generate some data
    let data = new RandomWalkGenerator().Seed(1337).getRandomWalkSeries(100);

    // Add the first line series on the primary X,Y axis
    // This occurs be default as FastLineRenderableSeries XAxisId and YAxisId are set to a default value
    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            stroke: appTheme.VividSkyBlue,
            strokeThickness: 3,
            dataSeries: new XyDataSeries(wasmContext, { xValues: data.xValues, yValues: data.yValues }),
            pointMarker: new EllipsePointMarker(wasmContext, {
                width: 5,
                height: 5,
                fill: appTheme.VividSkyBlue,
                stroke: appTheme.VividSkyBlue,
            }),
        })
    );

    data = new RandomWalkGenerator().Seed(90210).getRandomWalkSeries(100);

    // The second line series we specify X/Y axis ids to bind this to the correct axis
    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            stroke: appTheme.VividOrange,
            strokeThickness: 3,
            dataSeries: new XyDataSeries(wasmContext, { xValues: data.xValues, yValues: data.yValues }),
            xAxisId: ID_X_AXIS_2,
            yAxisId: ID_Y_AXIS_2,
            pointMarker: new EllipsePointMarker(wasmContext, {
                width: 5,
                height: 5,
                fill: appTheme.VividOrange,
                stroke: appTheme.VividOrange,
            }),
        })
    );

    // Optional: Add some interactivity modifiers to enable zooming and panning
    sciChartSurface.chartModifiers.add(
        new YAxisDragModifier(),
        new XAxisDragModifier(),
        new ZoomPanModifier({ enableZoom: true }),
        new MouseWheelZoomModifier(),
        new ZoomExtentsModifier()
    );

    // Add a title over the chart with information
    sciChartSurface.annotations.add(
        new NativeTextAnnotation({
            x1: 0.01,
            y1: 0.01,
            xCoordinateMode: ECoordinateMode.Relative,
            yCoordinateMode: ECoordinateMode.Relative,
            horizontalAnchorPoint: EHorizontalAnchorPoint.Left,
            verticalAnchorPoint: EVerticalAnchorPoint.Top,
            fontSize: 18,
            opacity: 0.55,
            textColor: appTheme.ForegroundColor,
            text: "SciChart.js supports unlimited X,Y axis.  Drag an axis to see the series scale",
            wrapTo: EWrapTo.ViewRect,
            multiLineAlignment: EMultiLineAlignment.Left,
        })
    );

    sciChartSurface.annotations.add(
        new TextAnnotation({
            x1: 30,
            y1: -3.5,
            fontSize: 18,
            textColor: appTheme.VividSkyBlue,
            text: "Blue series is bound to the Left, Bottom axis",
        })
    );

    // Note annotations need X,Y Axis Id as well in multi-axis scenarios
    sciChartSurface.annotations.add(
        new TextAnnotation({
            x1: 68,
            y1: 3.7,
            fontSize: 18,
            textColor: appTheme.VividOrange,
            horizontalAnchorPoint: EHorizontalAnchorPoint.Right,
            yAxisId: ID_Y_AXIS_2,
            xAxisId: ID_X_AXIS_2,
            text: "Orange series is bound to the Top, Right axis",
        })
    );

    return { sciChartSurface, wasmContext };
};

React Chart with Multiple X Axes

Overview

This example demonstrates how to create a high performance chart using SciChart.js within a React application. It highlights the configuration of multiple X and Y axes, binding data series to specific axes, and enriching the chart with interactive modifiers and annotations.

Technical Implementation

The implementation begins by initializing a SciChartSurface with a custom theme in the drawExample function. Two sets of NumericAxis are added: the primary (blue) set aligned using axis.alignment = EAxisAlignment.Bottom and EAxisAlignment.Left, and the secondary (orange) set aligned to the EAxisAlignment.Top and EAxisAlignment.Right. Each axis is customized with properties such as alignment, title, label style, and borders. Data series are bound to these axes by specifying axis.id and FastLineRenderableSeries.xAxisId, yAxisId, ensuring that each series is rendered within its designated axis pair. Interactive modifiers such as XAxisDragModifier, YAxisDragModifier, ZoomPanModifier, and MouseWheelZoomModifier are added to allow dynamic zooming and panning. This setup is described in the SciChart.js tutorial for multi-axis configuration and interactive behavior.

Features and Capabilities

The chart showcases several advanced features:

  • Multi-axis configuration: The example creates and binds two sets of X,Y axes, enabling the visualization of different data series on separate axes. For more details, refer to the Multi-Axis Tutorial.
  • Interactive Modifiers: With functionality for dragging, zooming, and panning, the chart allows users to interact directly with both the axes and the data. Explore more about these interactions in the Zoom and Pan Demo.
  • Custom Annotations: The use of both native and standard text annotations adds informative overlays to the chart, providing context about the data series. Learn about annotation customization in the Annotations Documentation.
  • Performance Optimization: By utilizing the WebAssembly (wasm) context for rendering and maintaining efficient resource management, the chart ensures optimal performance—a best practice emphasized in the React integration guide.

Integration and Best Practices

The chart is encapsulated within a React component that uses the <SciChartReact/> component to initialize the chart using the drawExample function. This integration highlights essential React practices such as component-based design and proper resource cleanup. Developers are encouraged to review the React integration tutorial to understand how to efficiently implement similar multi-axis charts in their own applications.

This example clearly illustrates how to build an interactive multi-axis chart in React using SciChart.js, combining robust data visualization with modern performance and integration practices.