This example demonstrates how create a Angular Mountain Chart with animated realtime updates using SciChart.js, our High Performance JavaScript Charts.
import { appTheme } from "./appTheme";
import {
AnimationToken,
CustomAnnotation,
easing,
EHorizontalAnchorPoint,
EVerticalAnchorPoint,
FastMountainRenderableSeries,
GradientParams,
NumericAxis,
NumberRange,
Point,
TEasingFn,
SciChartSurface,
XyDataSeries,
EDataChangeType,
GenericAnimation,
} from "scichart";
import { RandomWalkGenerator } from "./RandomWalkGenerator";
export const drawExample = async (rootElement: string | HTMLDivElement) => {
// Create the SciChartSurface in the div 'scichart-root'
// The SciChartSurface, and webassembly context 'wasmContext' are paired. This wasmContext
// instance must be passed to other types that exist on the same surface.
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: appTheme.SciChartJsTheme,
});
// Create an X,Y Axis and add to the chart
const xAxis = new NumericAxis(wasmContext, { growBy: new NumberRange(0.1, 0.1) });
const yAxis = new NumericAxis(wasmContext, { growBy: new NumberRange(0.1, 0.1) });
sciChartSurface.xAxes.add(xAxis);
sciChartSurface.yAxes.add(yAxis);
// Generate some initial random data for the example
const generator = new RandomWalkGenerator();
const initialValues = generator.getRandomWalkSeries(50);
// Add a mountain series with initial data
const dataSeries = new XyDataSeries(wasmContext, {
xValues: initialValues.xValues,
yValues: initialValues.yValues,
});
sciChartSurface.renderableSeries.add(
new FastMountainRenderableSeries(wasmContext, {
dataSeries,
fillLinearGradient: new GradientParams(new Point(0, 0), new Point(0, 1), [
{ color: appTheme.VividSkyBlue + "77", offset: 0 },
{ color: "Transparent", offset: 1 },
]),
stroke: appTheme.VividSkyBlue,
strokeThickness: 4,
})
);
// The animated pulsing dot at the end of the chart is rendered with this SVG annotation
const svgString = `<svg width="50" height="50" xmlns="http://www.w3.org/2000/svg">
<rect x="0" y="0" width="100%" height="100%" fill="transparent"/>
<circle cx="25" cy="25" fill="${appTheme.VividTeal}" r="5" stroke="${appTheme.VividTeal}">
<animate attributeName="r" from="5" to="25" dur="1s" begin="0s" repeatCount="indefinite"/>
<animate attributeName="opacity" from="1" to="0" dur="1s" begin="0s" repeatCount="indefinite"/>
</circle>
<circle cx="25" cy="25" fill="${appTheme.VividSkyBlue}" r="5"/>
</svg>`;
const pulsingDotAnnotation = new CustomAnnotation({
x1: initialValues.xValues[initialValues.xValues.length - 1],
y1: initialValues.yValues[initialValues.yValues.length - 1],
xCoordShift: 0,
yCoordShift: 0,
horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
verticalAnchorPoint: EVerticalAnchorPoint.Center,
svgString,
});
sciChartSurface.annotations.add(pulsingDotAnnotation);
let timerId: NodeJS.Timeout;
let animationToken: AnimationToken;
// This function performs animation on any XyDataSeries, animating the latest point only
// Be careful of reentrancy, e.g. calling animateXy more than once before previous animation has finished
// might require special handling
const animateXy = (xyDataSeries: XyDataSeries, endX: number, endY: number, duration: number, ease: TEasingFn) => {
const count = xyDataSeries.count();
const startX = xyDataSeries.getNativeXValues().get(count - 1);
const startY = xyDataSeries.getNativeYValues().get(count - 1);
xyDataSeries.append(startX, startY);
const animation = new GenericAnimation<number>({
from: 0,
to: 1,
onAnimate: (from, to, progress) => {
// Using the interpolation factor (ranges from 0..1) compute the X,Y value now
const currentX = (endX - startX) * progress + startX;
const currentY = (endY - startY) * progress + startY;
console.log(currentX, currentY);
// Update X,Y value by direct access to the inner webassembly arrays
xyDataSeries.getNativeXValues().set(count, currentX);
xyDataSeries.getNativeYValues().set(count, currentY);
// Force native redraw
xyDataSeries.notifyDataChanged(EDataChangeType.Update, count - 1, 1);
// update location of pulsing dot
pulsingDotAnnotation.x1 = currentX;
pulsingDotAnnotation.y1 = currentY;
// to just update, but if we want to zoom to fit, we must use zoomExtents
sciChartSurface.zoomExtents();
// update location of pulsing dot
pulsingDotAnnotation.x1 = currentX;
pulsingDotAnnotation.y1 = currentY;
},
ease,
});
sciChartSurface.addAnimation(animation);
};
// This is the loop where we add a new X,Y point and animate every 1 second to demonstrate animations
const runAddDataOnTimeout = () => {
if (sciChartSurface?.isDeleted) {
return;
}
const generated = generator.getRandomWalkSeries(1);
const x = generated.xValues[0];
const y = generated.yValues[0];
animateXy(dataSeries, x, y, 250, easing.outExpo);
timerId = setTimeout(runAddDataOnTimeout, 1000);
};
const startUpdate = () => {
if (timerId) {
stopUpdate();
}
runAddDataOnTimeout();
};
const stopUpdate = () => {
animationToken?.cancelAnimation();
clearTimeout(timerId);
timerId = undefined;
};
return { sciChartSurface, wasmContext, controls: { startUpdate, stopUpdate } };
};
This example demonstrates how to integrate SciChart.js into an Angular standalone component to create a real-time Mountain or Area Chart. The chart continuously updates by appending new data points and animating transitions, making use of a custom SVG annotation that renders a pulsing dot to highlight the latest data point.
The implementation starts with an Angular component that imports the ScichartAngularComponent, as detailed in the Getting started with standalone components - Angular guide. The component binds to the chart using Angular event binding (via onInit and onDelete), which allows it to start and stop real-time updates seamlessly. The core chart logic is encapsulated in the drawExample function where a SciChartSurface is created with a WebAssembly context. NumericAxis are configured, and a FastMountainRenderableSeries is rendered with a gradient fill. New data points are added using a setTimeout loop, and the latest point is animated using the DoubleAnimator with an easing function (easing.outExpo), as explained in the Adding Realtime Updates | JavaScript Chart Documentation - SciChart tutorial.
The chart not only supports real-time data updates but also includes advanced visual customizations. A FastMountainRenderableSeries is used to render the data with a smooth gradient fill, and a custom SVG annotation creates an animated pulsing dot at the most recent data point. These visual elements help in emphasizing data changes dynamically while maintaining high rendering performance. For detailed customization of annotations, refer to the CustomAnnotation Documentation.
Integration into Angular is achieved through effective use of Angular’s event binding and lifecycle management. By initiating updates in the onInit event and stopping them in the onDelete event, the example adheres to best practices for managing component lifecycles, ensuring that WebAssembly resources and real-time update loops are efficiently handled. Developers looking to optimize performance with real-time updates should also consider insights from the DataSeries Realtime Updates | JavaScript Chart Documentation guide. Additionally, integration of the WebAssembly-based SciChartSurface within Angular is in line with modern techniques for deploying high-performance visualizations, as explained in the Deploying Wasm (WebAssembly) and Data Files with your app documentation.








































