Demonstrates how Annotation layering a Angular Chart using SciChart.js, High Performance JavaScript Charts Notice the difference between annotations rendered to SVG and Canvas, as well as annotationLayer property effect.
import { appTheme } from "./appTheme";
import {
SciChartSurface,
NumericAxis,
NumberRange,
ZoomPanModifier,
BoxAnnotation,
TextAnnotation,
EAnnotationLayer,
NativeTextAnnotation,
MouseWheelZoomModifier,
Thickness,
XyDataSeries,
ZoomExtentsModifier,
FastLineRenderableSeries,
EllipsePointMarker,
} from "scichart";
export const drawExample = async (rootElement: string | HTMLDivElement) => {
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: appTheme.SciChartJsTheme,
});
const xAxis = new NumericAxis(wasmContext);
const yAxis = new NumericAxis(wasmContext);
sciChartSurface.xAxes.add(xAxis);
sciChartSurface.yAxes.add(yAxis);
xAxis.majorGridLineStyle.strokeThickness = 3;
xAxis.drawMinorGridLines = false;
xAxis.growBy = new NumberRange(0.2, 0.2);
yAxis.majorGridLineStyle.strokeThickness = 3;
yAxis.drawMinorGridLines = false;
yAxis.growBy = new NumberRange(0.4, 0.4);
const dataSeries = new XyDataSeries(wasmContext, { xValues: [1, 4, 5, 7], yValues: [2, 6, 9, 3] });
const lineSeries = new FastLineRenderableSeries(wasmContext, {
dataSeries,
stroke: "#4682b4",
strokeThickness: 7,
pointMarker: new EllipsePointMarker(wasmContext, { width: 10, height: 10, fill: appTheme.VividOrange }),
dataLabels: {
style: {
fontFamily: "Arial",
fontSize: 20,
},
color: appTheme.VividOrange,
},
});
sciChartSurface.renderableSeries.add(lineSeries);
sciChartSurface.chartModifiers.add(
new ZoomPanModifier({ enableZoom: true }),
new ZoomExtentsModifier(),
new MouseWheelZoomModifier()
);
const backgroundBoxAnnotation = new BoxAnnotation({
annotationLayer: EAnnotationLayer.Background,
stroke: appTheme.VividTeal,
strokeThickness: 1,
fill: appTheme.MutedTeal,
x1: 4,
x2: 5.6,
y1: 10,
y2: 6.2,
isEditable: true,
});
const boxAnnotationBelowSeries = new BoxAnnotation({
annotationLayer: EAnnotationLayer.BelowChart,
stroke: appTheme.VividOrange,
strokeThickness: 3,
fill: appTheme.MutedOrange,
x1: 3.2,
x2: 4.8,
y1: 8,
y2: 4.2,
isEditable: true,
});
const boxAnnotationAboveSeries = new BoxAnnotation({
annotationLayer: EAnnotationLayer.AboveChart, // default
stroke: appTheme.VividSkyBlue,
strokeThickness: 3,
fill: appTheme.MutedSkyBlue,
x1: 2.5,
x2: 4.2,
y1: 5.2,
y2: 1.5,
isEditable: true,
});
const backgroundTextAnnotation = new TextAnnotation({
annotationLayer: EAnnotationLayer.Background,
id: "textAnnotationBackground",
x1: 0.5,
y1: 11,
textColor: "#F1B24A",
fontSize: 32,
text: "Background SVG Annotation",
background: appTheme.VividPurple,
padding: Thickness.fromString("1 5 5 5"),
isEditable: true,
});
const foregroundTextAnnotation = new TextAnnotation({
annotationLayer: EAnnotationLayer.AboveChart,
id: "foregroundTextAnnotation",
x1: 0.5,
y1: 4,
textColor: "#F1B24A",
fontSize: 32,
fontFamily: "Times New Roman",
text: "Foreground SVG Annotation",
background: appTheme.VividRed,
padding: Thickness.fromString("1 5 5 5"),
isEditable: true,
});
const backgroundNativeTextAnnotation = new NativeTextAnnotation({
annotationLayer: EAnnotationLayer.Background,
x1: backgroundBoxAnnotation.x1,
y1: backgroundBoxAnnotation.y1,
textColor: appTheme.DarkIndigo,
fontSize: 20,
fontFamily: "Arial",
text: "Background",
});
const nativeTextAnnotationBelowSeries = new NativeTextAnnotation({
annotationLayer: EAnnotationLayer.BelowChart,
x1: boxAnnotationBelowSeries.x1,
y1: boxAnnotationBelowSeries.y1,
textColor: appTheme.DarkIndigo,
fontSize: 20,
fontFamily: "Arial",
text: "Below Chart",
});
const foregroundNativeTextAnnotation = new NativeTextAnnotation({
annotationLayer: EAnnotationLayer.AboveChart,
x1: boxAnnotationAboveSeries.x1,
y1: boxAnnotationAboveSeries.y1,
textColor: appTheme.DarkIndigo,
fontSize: 20,
fontFamily: "Arial",
text: "Above Chart",
});
sciChartSurface.preRender.subscribe(() => {
backgroundNativeTextAnnotation.x1 = backgroundBoxAnnotation.x1;
backgroundNativeTextAnnotation.y1 = backgroundBoxAnnotation.y1;
nativeTextAnnotationBelowSeries.x1 = boxAnnotationBelowSeries.x1;
nativeTextAnnotationBelowSeries.y1 = boxAnnotationBelowSeries.y1;
foregroundNativeTextAnnotation.x1 = boxAnnotationAboveSeries.x1;
foregroundNativeTextAnnotation.y1 = boxAnnotationAboveSeries.y1;
});
sciChartSurface.annotations.add(
boxAnnotationAboveSeries,
boxAnnotationBelowSeries,
backgroundBoxAnnotation,
backgroundTextAnnotation,
foregroundTextAnnotation,
foregroundNativeTextAnnotation,
nativeTextAnnotationBelowSeries,
backgroundNativeTextAnnotation
);
return { sciChartSurface };
};
This example demonstrates how to integrate SciChart.js into an Angular standalone component, showcasing advanced annotation layering techniques. The chart is designed to display high-performance 2D data visualization featuring multiple annotation layers, by rendering Annotations in the background, below the chart, and above the chart using the EAnnotationLayer enumeration.
The implementation uses the scichart-angular component to embed the chart into an Angular environment. A SciChartSurface is created and configured with NumericAxis, a FastLineRenderableSeries, and custom styling using an Angular-friendly theme. Dynamic annotations, both SVG and native, are added to the chart and linked to update automatically via the sciChartSurface.preRender event. More details on adding such dynamic annotations are available in the Tutorial 06 - Adding Annotations guide.
This example highlights several advanced features including real-time updates of annotations, interactive editable annotations, and multiple layers of annotation rendering (Background, Below Chart, Above Chart). The integration of chart modifiers such as ZoomPanModifier, MouseWheelZoomModifier, and ZoomExtentsModifier enhances the user experience by enabling intuitive zooming and panning functionalities.
The angular integration leverages input property binding to initialize the chart through a dedicated function, ensuring that the charting component remains modular and easily replaceable. Best practices for setting up a SciChart.js project in Angular can be reviewed in the Tutorial 01 - Setting up a npm Project with SciChart.js documentation. Additionally, the example demonstrates performance optimization by employing efficient WebGL rendering techniques and utilizing dynamic event handling to manage annotation positioning, while custom theming enhances visual clarity as described in the Chart Styling - Creating a Custom Theme documentation.







