JavaScript Chart Title

A Chart Title can be placed above, below, or either side of the chart, and be left, center or right aligned.

src/drawExample.ts
import { appTheme } from "./appTheme";
import { RandomWalkGenerator } from "./RandomWalkGenerator";
import {
    NumericAxis,
    SciChartSurface,
    EMultiLineAlignment,
    ETextAlignment,
    ETitlePosition,
    FastLineRenderableSeries,
    XyDataSeries,
    Thickness,
} from "scichart";

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

    sciChartSurface.title = "Multiline\nChart Title";
    sciChartSurface.titleStyle = {
        color: appTheme.ForegroundColor,
        fontSize: 70,
        padding: Thickness.fromString("4 0 4 0"),
        useNativeText: false,
        placeWithinChart: false,
        multilineAlignment: EMultiLineAlignment.Center,
        alignment: ETextAlignment.Center,
        position: ETitlePosition.Top,
    };

    const xAxis = new NumericAxis(wasmContext, {
        axisTitle: "X Axis Title",
        axisTitleStyle: { fontSize: 16, color: appTheme.ForegroundColor },
    });
    const yAxis = new NumericAxis(wasmContext, {
        axisTitle: "Y Axis",
        axisTitleStyle: { fontSize: 16, color: appTheme.ForegroundColor },
    });
    sciChartSurface.xAxes.add(xAxis);
    sciChartSurface.yAxes.add(yAxis);

    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            strokeThickness: 3,
            stroke: "auto",
            dataSeries: new XyDataSeries(wasmContext, new RandomWalkGenerator().getRandomWalkSeries(30)),
        })
    );

    const updateTitleText = (value: string) => {
        sciChartSurface.title = value;
    };

    const updateTitlePosition = (value: ETitlePosition) => {
        sciChartSurface.titleStyle = { position: value };
    };

    const updateTitleMultilineAlignment = (value: EMultiLineAlignment) => {
        sciChartSurface.titleStyle = { multilineAlignment: value };
    };

    const updateTitleAlignment = (value: ETextAlignment) => {
        sciChartSurface.titleStyle = { alignment: value };
    };

    const updateTitlePlaceWithinChart = (value: boolean) => {
        sciChartSurface.titleStyle = { placeWithinChart: value };
    };

    return {
        sciChartSurface,
        wasmContext,
        controls: {
            updateTitleText,
            updateTitlePosition,
            updateTitleMultilineAlignment,
            updateTitleAlignment,
            updateTitlePlaceWithinChart,
            updateTitleFontSize: (fontSize: number) => {
                sciChartSurface.titleStyle = { fontSize };
            },
            updateTitleColor: (color: string) => {
                sciChartSurface.titleStyle = { color };
            },
        },
    };
};

Chart Title Example (JavaScript)

Overview

This example demonstrates how to configure a multi-line chart title in SciChart.js using JavaScript. The chart is initialized with a custom title, set to "Multiline Chart Title", and styled with properties for font size, color, padding, and text alignment.

Technical Implementation

The chart is created by initializing a SciChartSurface, applying a custom theme, and setting up numeric axes with tailored axis titles via the axis.axisTitleStyle property (see NumericAxis Documentation). A FastLineRenderableSeries is added using an XyDataSeries sourced from randomly generated data, which illustrates efficient handling of dynamic data sets as detailed in the FastLineRenderableSeries documentation. Dynamic update functions are provided to modify the title text, alignment, position, and other style properties in real-time, following the approaches recommended in the Chart Styling - Chart Titles guide.

Features and Capabilities

  • Dynamic Updates: The example includes functions to change the chart title and its alignment dynamically, demonstrating advanced customization techniques.
  • Advanced Styling: Title styling options such as font size, color, and padding are configurable, allowing for multi-line title adjustments as explained in the Label Alignment and Positioning documentation.
  • Efficient Rendering: The use of FastLineRenderableSeries ensures optimized performance for rendering live data.

Integration and Best Practices

Although the provided source code is wrapped in a React component for demonstration purposes, the core implementation leverages JavaScript for initializing and updating the chart. Developers can integrate this approach into any JavaScript application by following practices from the Tutorial 01 - Including SciChart.js in an HTML Page using CDN. For further details on managing data series, refer to the Working with Data section. Overall, this example illustrates how to combine efficient rendering, responsive updates, and advanced styling to create a powerful, high-performance chart using SciChart.js in a JavaScript environment.