React 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 };
            },
        },
    };
};

React Chart Title Example

Overview

This example demonstrates how to integrate SciChart.js within a React application using the <SciChartReact/> component. It focuses on dynamic chart title management where users can update the title text, alignment, position, and multiline configuration through interactive controls integrated with shared CSS controls.

Technical Implementation

The chart is asynchronously initialized via the SciChartSurface.create() method, establishing the WebGL rendering context and setting up the SciChartSurface with custom title properties. React hooks such as useState and useRef are employed to manage and update the chart title state efficiently within the component lifecycle. For a detailed guide on creating and managing such components, see Creating a SciChart React Component from the Ground Up. The example also integrates event handling for dynamic updates, reflecting changes immediately on the rendered chart.

Features and Capabilities

The example offers several advanced features including immediate propagation of title changes, dynamic customization of visual elements such as text alignment and positioning, and efficient resource management through proper asynchronous initialization and cleanup. These aspects are crucial when working with high-performance WebGL rendering, as outlined in the Performance Tips & Tricks documentation.

Integration and Best Practices

This implementation adheres to modern React integration best practices by leveraging component lifecycle management and state handling for dynamic customization. The use of shared CSS controls for user input demonstrates effective event handling within a React context. Developers can explore further insights on React integration with SciChart.js in React Charts with SciChart.js: Introducing “SciChart React” which provides additional context on managing and optimizing these interactive charting solutions.