React Chart Title

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

Fullscreen

Edit

 Edit

Docs

index.tsx

RandomWalkGenerator.ts

theme.ts

Copy to clipboard
Minimise
Fullscreen
1import { IThemeProvider, SciChartJsNavyTheme } from "scichart";
2
3const getCssColor = (cssVar: string, fallback: string): string => {
4    if (typeof document === "undefined") {
5        return fallback;
6    }
7    const cssValue = getComputedStyle(document.documentElement).getPropertyValue(cssVar).trim();
8    return cssValue || fallback;
9};
10
11type TRgbColor = { r: number; g: number; b: number };
12
13const parseCssColorToRgb = (color: string): TRgbColor | undefined => {
14    const trimmed = color.trim();
15
16    if (trimmed.startsWith("#")) {
17        let hex = trimmed.slice(1);
18        if (hex.length === 3 || hex.length === 4) {
19            hex = hex
20                .slice(0, 3)
21                .split("")
22                .map((channel) => channel + channel)
23                .join("");
24        } else if (hex.length === 6 || hex.length === 8) {
25            hex = hex.slice(0, 6);
26        } else {
27            return undefined;
28        }
29
30        if (!/^[0-9a-fA-F]{6}$/.test(hex)) {
31            return undefined;
32        }
33
34        return {
35            r: Number.parseInt(hex.slice(0, 2), 16),
36            g: Number.parseInt(hex.slice(2, 4), 16),
37            b: Number.parseInt(hex.slice(4, 6), 16),
38        };
39    }
40
41    const rgbMatch = trimmed.match(/^rgba?\((.+)\)$/i);
42    if (!rgbMatch) {
43        return undefined;
44    }
45
46    const channels = rgbMatch[1]
47        .split(",")
48        .slice(0, 3)
49        .map((channel) => Number.parseFloat(channel.trim()));
50
51    if (channels.length !== 3 || channels.some((channel) => !Number.isFinite(channel))) {
52        return undefined;
53    }
54
55    const clamp = (value: number) => Math.max(0, Math.min(255, value));
56
57    return {
58        r: clamp(channels[0]),
59        g: clamp(channels[1]),
60        b: clamp(channels[2]),
61    };
62};
63
64const getPerceivedBrightness = (color: string): number | undefined => {
65    const rgb = parseCssColorToRgb(color);
66    if (!rgb) return undefined;
67
68    // WCAG-adjacent perceptual weighting for quick dark/light detection.
69    return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000;
70};
71
72export interface AppThemeBase {
73    SciChartJsTheme: IThemeProvider;
74
75    // general colors
76    isDark: boolean;
77    ForegroundColor: string;
78    Background: string;
79
80    // Series colors
81    VividSkyBlue: string;
82    VividPink: string;
83    VividTeal: string;
84    VividOrange: string;
85    VividBlue: string;
86    VividPurple: string;
87    VividGreen: string;
88    VividRed: string;
89
90    MutedSkyBlue: string;
91    MutedPink: string;
92    MutedTeal: string;
93    MutedOrange: string;
94    MutedBlue: string;
95    MutedPurple: string;
96    MutedRed: string;
97
98    PaleSkyBlue: string;
99    PalePink: string;
100    PaleTeal: string;
101    PaleOrange: string;
102    PaleBlue: string;
103    PalePurple: string;
104}
105
106export class SciChart2022AppTheme implements AppThemeBase {
107    SciChartJsTheme = new SciChartJsNavyTheme();
108
109    // Dynamic colors
110    get isDark() {
111        const brightness = getPerceivedBrightness(this.Background);
112        return brightness === undefined || brightness < 128;
113    }
114    get TextColor() { return this.ForegroundColor; }
115    get ForegroundColor() {
116        return getCssColor("--text", "#F5F5F5");
117    }
118    get Background() {
119        return getCssColor("--bg-chart", this.SciChartJsTheme.sciChartBackground);
120    }
121
122    // Series colors
123    VividSkyBlue = "#50C7E0";
124    VividPink = "#EC0F6C";
125    VividTeal = "#30BC9A";
126    VividOrange = "#F48420";
127    VividBlue = "#364BA0";
128    VividPurple = "#882B91";
129    VividGreen = "#67BDAF";
130    VividRed = "#C52E60";
131
132    DarkIndigo = "#14233C";
133    Indigo = "#264B93";
134
135    MutedSkyBlue = "#83D2F5";
136    MutedPink = "#DF69A8";
137    MutedTeal = "#7BCAAB";
138    MutedOrange = "#E7C565";
139    MutedBlue = "#537ABD";
140    MutedPurple = "#A16DAE";
141    MutedRed = "#DC7969";
142
143    PaleSkyBlue = "#E4F5FC";
144    PalePink = "#EEB3D2";
145    PaleTeal = "#B9E0D4";
146    PaleOrange = "#F1CFB5";
147    PaleBlue = "#B5BEDF";
148    PalePurple = "#CFB4D5";
149}
150
151export const appTheme = new SciChart2022AppTheme();
152

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 Material UI.

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 Material UI components 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.

react Chart Examples & Demos

See Also: Styling and Theming (11 Demos)

Chart Background Image with Transparency | SciChart.js

Chart Background Image with Transparency

Demonstrates how to create a React Chart with background image using transparency in SciChart.js

Styling a React Chart in Code | React Charts | SciChart.js

Styling a React Chart in Code

Demonstrates how to style a React Chart entirely in code with SciChart.js themeing API

Using Theme Manager in React Chart | SciChart.js Demo

Using Theme Manager in React Chart

Demonstrates our Light and Dark Themes for React Charts with SciChart.js ThemeManager API

Create a Custom Theme for React Chart | SciChart.js Demo

Create a Custom Theme for React Chart

Demonstrates how to create a Custom Theme for a SciChart.js React Chart using our Theming API

Coloring Series per-point using the PaletteProvider

Coloring Series per-point using the PaletteProvider

Demonstrates per-point coloring in JavaScript chart types with SciChart.js PaletteProvider API

React Point-Markers Chart | React Charts | SciChart.js Demo

React Point-Markers Chart

Demonstrates the different point-marker types for React Scatter charts (Square, Circle, Triangle and Custom image point-marker)

Dashed Line Styling | React Charts | SciChart.js Demo

Dashed Line Styling

Demonstrates dashed line series in React Charts with SciChart.js

Data Labels | React Charts | SciChart.js Demo

Data Labels

Show data labels on React Chart. Get your free demo now.

React Chart with Multi-Style Series | SciChart.js Demo

React Chart with Multi-Style Series

Demonstrates how to apply multiple different styles to a single series using RenderDataTransform

React Chart with lines split by thresholds | SciChart.js

React Chart with lines split by thresholds

Demonstrates how to use a RenderDataTransform to split lines into multiple segments so they can be individually colored according to thresholds

NEW!
React Order of Rendering | React Charts | SciChart.js Demo

React Order of Rendering Example

The React Order of Rendering example gives you full control of the draw order of series and annotations for charts. Try SciChart's advanced customizations.

SciChart Ltd, 16 Beaufort Court, Admirals Way, Docklands, London, E14 9XL.