Create a Custom Theme for JavaScript Chart

Demonstrates how to create a Custom Theme for SciChart.js, High Performance JavaScript Charts

src/drawExample.ts
import { closeValues, dateValues, highValues, lowValues, openValues } from "./data";

import {
    FastCandlestickRenderableSeries,
    FastColumnRenderableSeries,
    FastLineRenderableSeries,
    NumericAxis,
    NumberRange,
    OhlcDataSeries,
    RolloverModifier,
    SciChartSurface,
    XyDataSeries,
} from "scichart";

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

    // Create and apply your custom theme
    sciChartSurface.applyTheme({
        annotationsGripsBackgroundBrush: "white",
        annotationsGripsBorderBrush: "white",
        axis3DBandsFill: "#1F3D6833",
        axisBandsFill: "#1F3D6833",
        axisBorder: "#1F3D68",
        axisPlaneBackgroundFill: "Transparent",
        columnFillBrush: "white",
        columnLineColor: "white",
        cursorLineBrush: "#6495ED99",
        defaultColorMapBrush: [
            { offset: 0, color: "DarkBlue" },
            { offset: 0.5, color: "CornflowerBlue" },
            { offset: 1, color: "#FF22AA" },
        ],
        downBandSeriesFillColor: "#52CC5490",
        downBandSeriesLineColor: "#E26565FF",
        downBodyBrush: "white",
        downWickColor: "white",
        gridBackgroundBrush: "white",
        gridBorderBrush: "white",
        labelBackgroundBrush: "#6495EDAA",
        labelBorderBrush: "#6495ED",
        labelForegroundBrush: "#EEEEEE",
        legendBackgroundBrush: "#1D2C35",
        lineSeriesColor: "white",
        loadingAnimationForeground: "#6495ED",
        loadingAnimationBackground: "#0D213A",
        majorGridLineBrush: "#1F3D68",
        minorGridLineBrush: "#102A47",
        mountainAreaBrush: "white",
        mountainLineColor: "white",
        overviewFillBrush: "white",
        planeBorderColor: "white",
        rolloverLineBrush: "#FD9F2533",
        rubberBandFillBrush: "#99999933",
        rubberBandStrokeBrush: "#99999977",
        sciChartBackground: "#0D213A",
        scrollbarBackgroundBrush: "white",
        scrollbarBorderBrush: "white",
        scrollbarGripsBackgroundBrush: "white",
        scrollbarViewportBackgroundBrush: "white",
        scrollbarViewportBorderBrush: "white",
        shadowEffectColor: "#000000FF",
        textAnnotationBackground: "#6495EDAA",
        textAnnotationForeground: "#333333",
        tickTextBrush: "#6495ED",
        upBandSeriesFillColor: "white",
        upBandSeriesLineColor: "white",
        upBodyBrush: "#6495EDA0",
        upWickColor: "#6495ED",
        axisTitleColor: "#EEEEEE",
        chartTitleColor: "#EEEEEE",
    });

    // Create the XAxis, YAxis
    const xAxis = new NumericAxis(wasmContext);
    xAxis.visibleRange = new NumberRange(0, 31);
    xAxis.axisTitle = "X Axis";
    sciChartSurface.xAxes.add(xAxis);
    const yAxis = new NumericAxis(wasmContext);
    yAxis.visibleRange = new NumberRange(1, 1.2);
    yAxis.labelProvider.formatLabel = (dataValue: number) => dataValue.toFixed(3);
    yAxis.labelProvider.formatCursorLabel = (dataValue: number) => dataValue.toFixed(3);
    sciChartSurface.yAxes.add(yAxis);

    // Create some series with data
    const series1 = new FastLineRenderableSeries(wasmContext);
    series1.strokeThickness = 3;
    series1.stroke = "#16a085";
    sciChartSurface.renderableSeries.add(series1);

    series1.dataSeries = new XyDataSeries(wasmContext, {
        xValues: [1, 5, 10, 15, 20, 25, 30],
        yValues: [1.12, 1.121, 1.119, 1.11, 1.115, 1.112, 1.1],
    });

    const series2 = new FastCandlestickRenderableSeries(wasmContext, {
        strokeThickness: 2,
        dataSeries: new OhlcDataSeries(wasmContext, {
            xValues: dateValues,
            openValues,
            highValues,
            lowValues,
            closeValues,
        }),
        dataPointWidth: 0.5,
    });
    series2.rolloverModifierProps.tooltipColor = "#d0d3d4";
    series2.rolloverModifierProps.markerColor = "#d0d3d4";
    sciChartSurface.renderableSeries.add(series2);

    const series3 = new FastColumnRenderableSeries(wasmContext, {
        fill: "rgba(176, 196, 222, 0.7)",
        stroke: "#4682b4",
        strokeThickness: 2,
        dataPointWidth: 0.5,
    });
    sciChartSurface.renderableSeries.add(series3);
    const dataSeries = new XyDataSeries(wasmContext);
    for (let i = 1; i <= 30; i++) {
        dataSeries.append(i, 1 + Math.sin(i * 0.1) * 0.1);
    }
    series3.dataSeries = dataSeries;

    // Create tootip behaviour
    sciChartSurface.chartModifiers.add(new RolloverModifier());
    return { sciChartSurface, wasmContext };
};

Create A Custom Theme for JavaScript Chart

Overview

This example demonstrates how to create and apply a custom theme to a SciChart.js chart using JavaScript. The implementation focuses on leveraging SciChart.js's extensive theming API to modify the appearance of various chart components—including axes, grid lines, series, and interactive modifiers—while ensuring high-performance rendering through WebGL.

Technical Implementation

The chart is initialized asynchronously via the SciChartSurface.create method, which sets up the required WebGL context for optimal performance. Once the surface is created, a custom theme is applied using the sciChartSurface.applyTheme() method, where properties like axis borders, series colors, and annotation styles (conforming to the IThemeProvider type) are defined. The example instantiates high-performance renderable series such as FastLineRenderableSeries, FastCandlestickRenderableSeries, and FastColumnRenderableSeries to efficiently visualize complex datasets.

Features and Capabilities

Key features of this example include asynchronous chart initialization and dynamic data binding using data series like XyDataSeries and OhlcDataSeries. An interactive RolloverModifier enhances the chart experience by providing tooltips on mouse hover, which is particularly useful for real-time data updates. The custom theme not only personalizes the visual style but also contributes to performance optimizations by maintaining consistent styling across different chart elements.

Integration and Best Practices

Although this implementation is built with JavaScript, it follows best practices that are applicable to any modern web application. Asynchronous initialization using promises ensures that the WebGL context is loaded efficiently, while a dedicated cleanup function properly disposes of the SciChartSurface to avoid memory leaks. Developers are encouraged to explore further details in the SciChart.js Custom Theme Documentation and consult the SciChart.js initialization guidelines to ensure robust implementation and optimal performance.