Styling a React Chart in Code

Demonstrates how to style or theme a React Chart using SciChart.js, High Performance JavaScript Charts

src/drawExample.ts
import {
    SciChartSurface,
    NumericAxis,
    MouseWheelZoomModifier,
    EAxisAlignment,
    ZoomPanModifier,
    TextAnnotation,
    EHorizontalAnchorPoint,
    ECoordinateMode,
} from "scichart";

export const drawExample = async (rootElement: string | HTMLDivElement) => {
    // Demonstrates how to colour chart parts in code
    // This is better done by themes, where you can also style the loader pre-scichart initialisation
    //
    const chartBackgroundColor = "#E4F5FC";
    const axisBandsFill = "#1F3D6805";
    const axisTitleColor = "#1F3D68";
    const majorGridLineColor = "#264B9355";
    const minorGridLineColor = "#264B9322";
    const axisLabelColor = "#1F3D68";
    const axisBackgroundFill = "#00000000";
    const borderColor = "#1F3D68";

    const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement);
    sciChartSurface.background = chartBackgroundColor;

    // Create and style xAxis
    sciChartSurface.xAxes.add(
        new NumericAxis(wasmContext, {
            axisTitle: "X Axis",
            drawMajorBands: true,
            axisBandsFill,
            axisTitleStyle: {
                fontSize: 25,
                fontFamily: "Montserrat",
                fontWeight: "bold",
                fontStyle: "italic",
                color: axisTitleColor,
            },
            majorGridLineStyle: {
                strokeThickness: 1,
                color: majorGridLineColor,
                strokeDashArray: [10, 5],
            },
            minorGridLineStyle: {
                strokeThickness: 1,
                color: minorGridLineColor,
                strokeDashArray: [2, 2],
            },
            majorTickLineStyle: {
                strokeThickness: 1,
                color: majorGridLineColor,
                tickSize: 8,
            },
            minorTickLineStyle: {
                strokeThickness: 1,
                color: minorGridLineColor,
                tickSize: 4,
            },
            useNativeText: false, // this must be false to use labelStyle fontWeight and fontStyle
            labelStyle: {
                fontSize: 16,
                fontWeight: "bold",
                fontStyle: "Italic",
                color: axisLabelColor,
                fontFamily: "Arial",
            },
            backgroundColor: axisBackgroundFill,
            axisBorder: {
                borderTop: 1,
                color: borderColor,
            },
        })
    );

    // Create and style left YAxis
    sciChartSurface.yAxes.add(
        new NumericAxis(wasmContext, {
            axisAlignment: EAxisAlignment.Left,
            axisBandsFill,
            axisTitle: "Left Y Axis",
            axisTitleStyle: {
                fontSize: 25,
                fontFamily: "Montserrat",
                fontWeight: "bold",
                fontStyle: "italic",
                color: axisTitleColor,
            },
            majorGridLineStyle: {
                strokeThickness: 1,
                color: majorGridLineColor,
                strokeDashArray: [10, 5],
            },
            minorGridLineStyle: {
                strokeThickness: 1,
                color: minorGridLineColor,
                strokeDashArray: [2, 2],
            },
            majorTickLineStyle: {
                strokeThickness: 1,
                color: majorGridLineColor,
                tickSize: 8,
            },
            minorTickLineStyle: {
                strokeThickness: 1,
                color: minorGridLineColor,
                tickSize: 4,
            },
            useNativeText: false, // this must be false to use labelStyle fontWeight and fontStyle
            labelStyle: {
                fontSize: 16,
                fontWeight: "bold",
                fontStyle: "Italic",
                color: axisLabelColor,
                fontFamily: "Arial",
            },
            backgroundColor: axisBackgroundFill,
            axisBorder: {
                borderRight: 1,
                color: borderColor,
            },
        })
    );

    // Create and style right YAxis
    sciChartSurface.yAxes.add(
        new NumericAxis(wasmContext, {
            useNativeText: false,
            axisTitle: "Right Y Axis",
            axisTitleStyle: {
                fontSize: 25,
                fontFamily: "Montserrat",
                fontWeight: "bold",
                fontStyle: "italic",
                color: axisTitleColor,
            },
            axisAlignment: EAxisAlignment.Right,
            majorGridLineStyle: {
                strokeThickness: 1,
                color: majorGridLineColor,
                strokeDashArray: [10, 5],
            },
            minorGridLineStyle: {
                strokeThickness: 1,
                color: minorGridLineColor,
                strokeDashArray: [2, 2],
            },
            majorTickLineStyle: {
                strokeThickness: 1,
                color: majorGridLineColor,
                tickSize: 8,
            },
            minorTickLineStyle: {
                strokeThickness: 1,
                color: minorGridLineColor,
                tickSize: 4,
            },
            labelStyle: {
                fontSize: 16,
                fontWeight: "bold",
                fontStyle: "Italic",
                color: axisLabelColor,
                fontFamily: "Arial",
            },
            backgroundColor: axisBackgroundFill,
            axisBorder: {
                borderLeft: 1,
                color: borderColor,
            },
        })
    );

    // Add title annotation
    sciChartSurface.annotations.add(
        new TextAnnotation({
            text: "Chart with custom style applied in code",
            fontSize: 20,
            fontWeight: "Bold",
            textColor: axisLabelColor,
            x1: 0.5,
            y1: 0,
            opacity: 0.77,
            horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
            xCoordinateMode: ECoordinateMode.Relative,
            yCoordinateMode: ECoordinateMode.Relative,
        })
    );

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

    sciChartSurface.zoomExtents();

    return { sciChartSurface, wasmContext };
};

Styling a Chart in Code (React)

Overview

This example demonstrates how to apply custom styling to a SciChart.js chart entirely in code within a React application. The code programmatically customizes various chart elements such as the chart background via sciChartSurface.background, NumericAxis properties (including axisTitleStyle, major/minorGridLinesStyle, major/minorTickLinesStyle, labelStyle, backgroundColor and axisBorder. It serves as a practical reference for developers looking to implement custom theming and styling directly within their application code.

Technical Implementation

The chart is initialized asynchronously using the SciChartSurface.create() method within an async function, leveraging modern JavaScript async/await patterns for smooth and efficient chart creation. Integration into the React application is achieved through the <SciChartReact/> component, ensuring that the chart setup remains clean and modular. Developers can learn more about integrating SciChart with React in the React Charts with SciChart.js: Introducing “SciChart React” article. The example applies custom styles directly to numeric axes, grid lines, tick markers, and annotations, providing precise control over the chart aesthetics.

Features and Capabilities

Key features include advanced chart customizations such as styled axes with tailored title and label styles, customized grid lines with dash arrays, and a text annotation that functions as the chart title. Interactive modifiers like ZoomPanModifier and MouseWheelZoomModifier are integrated to enhance chart interactivity, allowing users to efficiently navigate and explore the data. Further details on these interactive features can be found in Tutorial 03 - Adding Zooming, Panning Behavior.

Integration and Best Practices

The example adheres to best practices for React integration by utilizing the scichart-react component, ensuring a seamless incorporation into the React ecosystem. Asynchronous initialization not only optimizes performance but also guarantees efficient management of the WebAssembly context. For additional insights into performance optimization in React charts, refer to How to Make Charts in React from Scratch? - SciChart. Moreover, the explicit in-code styling promotes a clear separation of concerns, making future maintenance and updates more straightforward. Comprehensive details on performance and resource management are available in the SciChart.js JavaScript Charts User Manual.