Styling a JavaScript Chart in Code

Demonstrates how to style or theme a JavaScript 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 (JavaScript)

Overview

This example demonstrates how to apply in-code chart styling using SciChart.js in a JavaScript environment. 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, as well as annotations and interactive modifiers. This approach provides a clear and flexible way to style charts without relying on external themes or frameworks.

Technical Implementation

The chart is initialized asynchronously using the SciChartSurface.create() method, which leverages WebAssembly for optimal performance. Developers can refer to the Creating a new SciChartSurface and loading Wasm documentation for more details on this initialization pattern. The implementation focuses on detailed custom axis styling, where NumericAxis properties are configured for both the X and Y axes. Specific styling options like axis bands, custom dashed line styles for grid lines, and bespoke tick settings are applied to enhance the visual presentation. The configuration of these properties is explained in the Axis Styling Documentation. Interactive modifiers such as ZoomPanModifier and MouseWheelZoomModifier are integrated to allow users to pan and zoom within the chart. More on these interactivity features can be found in Tutorial 03 - Adding Zooming, Panning Behavior.

Features and Capabilities

This example highlights several advanced features including:

  • Detailed customization of axis styling with tailored fonts, colors, grid line dash patterns, and axis borders.
  • The use of a text annotation, configured with relative coordinate modes, to serve as a watermarked chart title. For additional information on annotations, visit the TextAnnotation Documentation.
  • Interactive chart modifiers that provide smooth zooming and panning functionality.
  • A proper resource management strategy where the SciChartSurface is disposed of when no longer needed, ensuring optimal memory usage as discussed in Memory Best Practices.

Integration and Best Practices

By integrating SciChart.js directly with JavaScript, this example establishes a modular and maintainable approach without the overhead of additional frameworks. Developers can get started quickly by following guidance from the Tutorial 01 - Including SciChart.js in an HTML Page using CDN. Furthermore, the in-code styling method promotes a clear separation between configuration and presentation, ensuring that chart appearance can be easily updated without altering core functionality. For those interested in expanding the customization further, the Chart Styling - Creating a Custom Theme documentation provides additional insights on applying custom color schemes and theme settings.