This example demonstrates how create a JavaScript 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 create an animated real-time mountain chart using SciChart.js in JavaScript. The chart is designed to continuously update by appending new data points and animating the transitions, while leveraging the high performance of a WebAssembly context.
The implementation begins by creating a SciChartSurface using the method documented in the Creating a new SciChartSurface and loading Wasm guide. NumericAxis are configured with the NumericAxis class and a growBy property to ensure proper scaling. A FastMountainRenderableSeries is then used to render the mountain chart with a gradient fill similar to that described in the Mountain (Area) Chart documentation. Real-time data updates are implemented via a setTimeout loop that appends new data points. The newest point is animated using the DoubleAnimator class with an easing function (easing.outExpo) to create smooth transitions. A custom SVG annotation renders an animated pulsing dot at the latest data point, enhancing the visual feedback.
The example showcases several advanced features including real-time data updating, animated transitions, and custom annotations. It manipulates WebAssembly-based native arrays directly for performance optimization and applies gradient styling to the mountain series, providing an engaging real-time visualization that highlights the most recent data updates.
Developers working with JavaScript can integrate this example into their projects by following the Getting Started with SciChart JS guide. The example emphasizes performance optimization by efficiently managing the animation loop and reentrancy, as further explained in the Adding Realtime Updates documentation. Best practices such as proper resource cleanup and efficient DOM interactions are demonstrated, ensuring a robust implementation for high-frequency data updates.








































