Using Theme Manager in React Chart

Demonstrates the light and dark theme in SciChart.js, High Performance JavaScript Charts

src/drawExample.ts
import {
    ECoordinateMode,
    EHorizontalAnchorPoint,
    FastLineRenderableSeries,
    IThemeProvider,
    NumberRange,
    NumericAxis,
    SciChartJSDarkv2Theme,
    SciChartJSLightTheme,
    SciChartJsNavyTheme,
    SciChartSurface,
    TextAnnotation,
    XyDataSeries,
    SweepAnimation,
} from "scichart";
import { ExampleDataProvider } from "./ExampleDataProvider";

export const getChartsInitializationAPI = () => {
    const createLineData = (whichSeries: number) => {
        const data = ExampleDataProvider.getFourierSeriesZoomed(1.0, 0.1, 5.0, 5.15);

        return {
            xValues: data.xValues,
            yValues: data.yValues.map((y) => (whichSeries === 0 ? y : whichSeries === 1 ? y * 1.1 : y * 1.5)),
        };
    };

    const createThemedChart = async (rootElement: string | HTMLDivElement, title: string, theme: IThemeProvider) => {
        // Create a SciChartSurface passing theme into constructor options
        const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
            theme,
        });

        // Create the X,Y Axis
        sciChartSurface.xAxes.add(
            new NumericAxis(wasmContext, {
                labelPrecision: 2,
                maxAutoTicks: 8,
            })
        );
        sciChartSurface.yAxes.add(
            new NumericAxis(wasmContext, {
                labelPrecision: 2,
                maxAutoTicks: 8,
                growBy: new NumberRange(0.05, 0.2),
            })
        );

        // Add title annotation
        sciChartSurface.annotations.add(
            new TextAnnotation({
                text: title,
                fontSize: 16,
                textColor: theme.tickTextBrush,
                x1: 0.5,
                y1: 0,
                opacity: 0.77,
                horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
                xCoordinateMode: ECoordinateMode.Relative,
                yCoordinateMode: ECoordinateMode.Relative,
            })
        );

        let data = createLineData(2);

        // Create and add a line series to the chart
        sciChartSurface.renderableSeries.add(
            new FastLineRenderableSeries(wasmContext, {
                dataSeries: new XyDataSeries(wasmContext, { xValues: data.xValues, yValues: data.yValues }),
                stroke: "auto",
                strokeThickness: 3,
                animation: new SweepAnimation({ duration: 500 }),
            })
        );

        data = createLineData(1);

        // Create and add a line series to the chart
        sciChartSurface.renderableSeries.add(
            new FastLineRenderableSeries(wasmContext, {
                dataSeries: new XyDataSeries(wasmContext, { xValues: data.xValues, yValues: data.yValues }),
                stroke: "auto",
                strokeThickness: 3,
                animation: new SweepAnimation({ duration: 500 }),
            })
        );

        data = createLineData(0);

        // Create and add a line series to the chart
        sciChartSurface.renderableSeries.add(
            new FastLineRenderableSeries(wasmContext, {
                dataSeries: new XyDataSeries(wasmContext, { xValues: data.xValues, yValues: data.yValues }),
                stroke: "auto",
                strokeThickness: 3,
                animation: new SweepAnimation({ duration: 500 }),
            })
        );

        return { sciChartSurface };
    };

    const createNavyThemeChart = (divId: string | HTMLDivElement) =>
        createThemedChart(divId, "Navy Theme", new SciChartJsNavyTheme());
    const createLightThemeChart = (divId: string | HTMLDivElement) =>
        createThemedChart(divId, "Light Theme", new SciChartJSLightTheme());
    const createDarkThemeChart = (divId: string | HTMLDivElement) =>
        createThemedChart(divId, "Dark Theme", new SciChartJSDarkv2Theme());
    const createCustomThemeChart = (divId: string | HTMLDivElement) =>
        createThemedChart(divId, "Custom Theme", customTheme);

    return { createNavyThemeChart, createLightThemeChart, createDarkThemeChart, createCustomThemeChart };
};

// Create a custom theme based on light theme + some modifications.
// Use applyOverrides() on a theme instance rather than spreading the theme into a plain object:
// the spread drops the prototype, and SciChartSurface.create() rejects a plain object that has a
// `type` but no `applyOverrides` as a Builder-API declarative theme definition.
const customTheme = new SciChartJSLightTheme();
customTheme.applyOverrides({
    axisBandsFill: "#83D2F511",
    axisBorder: "#1F3D68",
    gridBackgroundBrush: "white",
    gridBorderBrush: "white",
    loadingAnimationForeground: "#6495ED77",
    loadingAnimationBackground: "#E4F5FC",
    majorGridLineBrush: "#264B9322",
    minorGridLineBrush: "#264B9306",
    sciChartBackground: "#E4F5FC",
    tickTextBrush: "#1F3D68",
    axisTitleColor: "#1F3D68",
    // auto / default colour palette for lines and fills
    strokePalette: ["#264B93", "#A16DAE", "#C52E60"],
    fillPalette: ["#264B9333", "#A16DAE33", "#C52E6033"],
});

Themeing React Charts with SciChart.js

Overview

This example, "Using Theme Manager in React Chart", demonstrates how to implement multiple themed charts within a React application using SciChart.js. The sample illustrates the application of built-in and custom themes to create visually distinct charts with animated line series rendered using WebGL for high performance.

Technical Implementation

The implementation leverages the <SciChartReact/> component to integrate SciChart surfaces within React. Each chart is asynchronously initialized by functions defined in the drawExample function. These functions set up NumericAxis, add a TextAnnotation for titles, and render multiple line series with sweep animations. The example uses React hooks such as useState for managing the chart initialization functions. This showcases best practices for React integration with SciChart.js.

Features and Capabilities

The example supports multiple themes including Navy (SciChartJsNavyTheme), Light (SciChartJSLightTheme), and Dark (SciChartJSDarkv2Theme) alongside a customTheme created by merging properties from a built-in theme (see IThemeProvider) using the spread operator. The customTheme, created by modifying the SciChartJSLightTheme, adjusts properties such as axis colors, grid lines, and stroke palettes. All themes support smooth animations on data series using the sweep animation type that provides a fluid visual experience. This approach aligns with advanced theming techniques outlined in SciChart documentation.

Integration and Best Practices

Key integration points in the example include the use of plain CSS classes for styling and a ChartGroupLoader container for grouping multiple <SciChartReact/> components in a responsive grid layout. The charts are created asynchronously and cleaned up properly, demonstrating efficient asynchronous operations and cleanup in React environments. Developers can refer to the React Charts with SciChart.js tutorial for more insights on performance optimization and custom theming strategies to tailor their implementations. Additionally, the example illustrates proper usage of React hooks and encourages the adoption of these best practices to build highly performant, data-rich web applications.

For further details on SciChart.js theming and its integration with React, visit the SciChart.js ThemeManager Documentation.