A Chart Title can be placed above, below, or either side of the chart, and be left, center or right aligned.
index.tsx
RandomWalkGenerator.ts
theme.ts
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() {
115 return this.ForegroundColor;
116 }
117 get ForegroundColor() {
118 return getCssColor("--text", "#F5F5F5");
119 }
120 get Background() {
121 return getCssColor("--bg-chart", this.SciChartJsTheme.sciChartBackground);
122 }
123
124 // Series colors
125 VividSkyBlue = "#50C7E0";
126 VividPink = "#EC0F6C";
127 VividTeal = "#30BC9A";
128 VividOrange = "#F48420";
129 VividBlue = "#364BA0";
130 VividPurple = "#882B91";
131 VividGreen = "#67BDAF";
132 VividRed = "#C52E60";
133
134 DarkIndigo = "#14233C";
135 Indigo = "#264B93";
136
137 MutedSkyBlue = "#83D2F5";
138 MutedPink = "#DF69A8";
139 MutedTeal = "#7BCAAB";
140 MutedOrange = "#E7C565";
141 MutedBlue = "#537ABD";
142 MutedPurple = "#A16DAE";
143 MutedRed = "#DC7969";
144
145 PaleSkyBlue = "#E4F5FC";
146 PalePink = "#EEB3D2";
147 PaleTeal = "#B9E0D4";
148 PaleOrange = "#F1CFB5";
149 PaleBlue = "#B5BEDF";
150 PalePurple = "#CFB4D5";
151}
152
153export const appTheme = new SciChart2022AppTheme();
154This example demonstrates the integration of SciChart.js within an Angular application to dynamically manage and update a chart title. The chart title is highly configurable, allowing placement above, below, or to either side of the chart, with flexible alignment options such as left, center, or right. By leveraging Angular’s modular architecture, this example ensures a responsive and maintainable charting solution.
The implementation encapsulates the SciChart.js initialization logic within Angular services, employing dependency injection as outlined in the Introduction to services and dependency injection documentation. Angular lifecycle hooks, such as ngOnInit, are used to initialize the chart and manage real-time updates using JSON-driven configuration, in line with the guidelines provided by Getting Started with SciChart JS.
This example offers several advanced features including dynamic updates that reflect immediate changes in the chart title; advanced customization of title properties such as alignment, position, and multiline configurations; and efficient handling of WebGL rendering for optimal performance. These capabilities ensure that dynamic data binding and event handling are integrated seamlessly into the Angular framework.
The solution follows Angular best practices for third-party library integration. It makes extensive use of dependency injection to encapsulate SciChart.js logic and lifecycle hooks to manage component initialization and updates, as documented in Component Lifecycle - Angular. Performance optimizations are achieved by integrating Angular change detection techniques along with efficient WebGL canvas management, reflecting insights from Angular Performance Optimization - Dave Bush. This structured approach facilitates scalable and maintainable charting applications within the Angular ecosystem.

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

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

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

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

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

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

Demonstrates dashed line series in Angular Charts with SciChart.js

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

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

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

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