React Vertical Charts

Demonstrates how to create a rotated React Chart with vertical X-Axis using SciChart.js, High Performance JavaScript Charts

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

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

    // Setting an XAxis on the Left or Right
    // and YAxis on the Top or Bottom
    // causes the chart and series to be rotated vertically
    sciChartSurface.xAxes.add(
        new NumericAxis(wasmContext, {
            axisTitle: "X Axis with Alignment = Left",
            axisAlignment: EAxisAlignment.Left,
            growBy: new NumberRange(0.15, 0.05),
        })
    );

    sciChartSurface.yAxes.add(
        new NumericAxis(wasmContext, {
            axisTitle: "Y Axis with Alignment = Bottom",
            axisAlignment: EAxisAlignment.Bottom,
            growBy: new NumberRange(0.1, 0.1),
        })
    );

    // Generate some data. xValues/yValues are just arrays of numbers
    const generator = new RandomWalkGenerator().Seed(1337).getRandomWalkSeries(100);
    const xValues = generator.xValues;
    const yValues = generator.yValues;

    // Add a line series to the chart. This will be drawn vertically.
    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            dataSeries: new XyDataSeries(wasmContext, { xValues, yValues }),
            pointMarker: new EllipsePointMarker(wasmContext, {
                width: 9,
                height: 9,
                fill: appTheme.ForegroundColor,
                stroke: appTheme.VividOrange,
            }),
            strokeThickness: 5,
            stroke: appTheme.VividOrange,
        })
    );

    // Add title / instructions
    sciChartSurface.annotations.add(
        new NativeTextAnnotation({
            x1: 0.02,
            y1: 0.02,
            xCoordinateMode: ECoordinateMode.Relative,
            yCoordinateMode: ECoordinateMode.Relative,
            horizontalAnchorPoint: EHorizontalAnchorPoint.Left,
            verticalAnchorPoint: EVerticalAnchorPoint.Top,
            fontSize: 16,
            opacity: 0.77,
            textColor: appTheme.ForegroundColor,
            text: "To rotate a chart in SciChart.js, set XAxis.alignment = Left/Right and YAxis.alignment=Top/Bottom",
            wrapTo: EWrapTo.ViewRect,
            multiLineAlignment: EMultiLineAlignment.Left,
        })
    );

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

    sciChartSurface.zoomExtents();

    return { wasmContext, sciChartSurface };
};

React Vertical Charts

Overview

This example demonstrates how to create a rotated vertical chart using SciChart.js in a React application. By aligning the X axis to the left and the Y axis to the bottom, the chart is effectively rotated 90-degrees, rendered vertically with chart series drawn top to bottom. The example leverages the high performance rendering of SciChart.js along with a functional React component via the <SciChartReact/> component.

Technical Implementation

The chart is initialized in a React component that renders a <SciChartReact/> element with an initChart prop. This prop passes a function responsible for creating a SciChartSurface, setting up NumericAxis with vertical alignment, and rendering a FastLineRenderableSeries with an EllipsePointMarker. The chart is rotated 90-degrees and rendered vertically with series drawn top to bottom by configuring the xAxis.axisAlignment = EAxisAlignment.Left and the yAxis.axisAlignment = EAxisAlignment.Bottom. The implementation also includes a native text annotation to provide user guidance. Advanced interactivity is achieved by adding several modifiers for zooming and panning, as detailed in the Tutorial on Adding Zooming and Panning Behavior.

Features and Capabilities

The example optimizes performance through the use of FastLineRenderableSeries which ensures smooth rendering even with dynamic data. Capabilities such as real-time interactivity are enhanced by integrating user interaction modifiers like ZoomPanModifier, ZoomExtentsModifier, and MouseWheelZoomModifier. Detailed performance optimization guidance is available in the Performance Optimisation of JavaScript Applications & Charts article.

Integration and Best Practices

Integration in React is streamlined by using the <SciChartReact/> component, as explained in React Charts with SciChart.js: Introducing “SciChart React”. Vertical axis alignment is achieved by setting the axis alignment properties, a technique well documented in the Vertical Charts (Rotate, Transpose Axis) - SciChart guide. Additionally, the example demonstrates best practices for initializing and configuring charts in React using the initChart prop, as outlined in the Tutorial on Setting up a project with scichart-react.