This example demonstrates how create a React 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 React Charts example demonstrates a high performance real time Mountain Chart implemented in React using SciChart.js.
The chart is configured by creating NumericAxis X and Y axes and a Mountain Series via the FastMountainRenderableSeries, as detailed in the Mountain (Area) Chart Documentation. Real-time data is injected into the chart using a timer loop, where new points are added and then animated using the DoubleAnimator with easing functions such as easing.outExpo, as described in the Generic Animations guide. A custom SVG annotation renders a pulsing dot at the latest data point, enhancing the dynamic feel of the visualization.
The implementation supports real-time updates by continuously appending new data points and animating transitions. In addition, it includes advanced styling features like gradient fills for the mountain series and animated custom annotations. Developers looking to implement similar real-time behaviors can refer to the Adding Realtime Updates tutorial for further insight.
The example integrates into React through the <SciChartReact/> component, utilizing the onInit and onDelete callbacks to start and stop data updates. This approach ensures smooth lifecycle management within React applications and follows best practices for React integration. Additionally, performance is optimized by leveraging the WebAssembly context provided by SciChart.js, ensuring efficient rendering even with continuous data updates. For more detailed guidance on integrating SciChart.js with React, developers are encouraged to review the resources provided in the SciChart React integration documentation.








































