Demonstrates how to run Dataset Animations using SciChart.js, High Performance JavaScript Charts
import {
SciChartSurface,
NumericAxis,
NumberRange,
EllipsePointMarker,
ScatterAnimation,
XyDataSeries,
PaletteFactory,
GradientParams,
Point,
FastLineRenderableSeries,
easing,
} from "scichart";
import { appTheme } from "./appTheme";
export const drawExample = async (rootElement: string | HTMLDivElement) => {
// Create a SciChartSurface
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: appTheme.SciChartJsTheme,
});
const length = 120;
sciChartSurface.xAxes.add(
new NumericAxis(wasmContext, {
visibleRange: new NumberRange(0, length),
growBy: new NumberRange(0.1, 0.1),
})
);
sciChartSurface.yAxes.add(
new NumericAxis(wasmContext, {
visibleRange: new NumberRange(0, length),
growBy: new NumberRange(0.1, 0.1),
})
);
let xValues = Array.from(Array(length).keys());
let yValues = Array.from({ length }, () => Math.random() * length);
// Create a scatter series with some initial data
const scatterSeries = new FastLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, {
xValues,
yValues,
}),
strokeThickness: 2,
pointMarker: new EllipsePointMarker(wasmContext, {
width: 11,
height: 11,
fill: appTheme.VividSkyBlue,
strokeThickness: 0,
}),
paletteProvider: PaletteFactory.createGradient(
wasmContext,
new GradientParams(new Point(0, 0), new Point(1, 1), [
{ offset: 0, color: "#36B8E6" },
{ offset: 0.2, color: "#5D8CC2" },
{ offset: 0.4, color: "#8166A2" },
{ offset: 0.6, color: "#AE418C" },
{ offset: 1.0, color: "#CA5B79" },
]),
{ enableStroke: true, enablePointMarkers: true, strokeOpacity: 0.67 }
),
});
sciChartSurface.renderableSeries.add(scatterSeries);
// create a temp series for passing animation values
const animationSeries = new XyDataSeries(wasmContext);
// register this so it is deleted along with the main surface
sciChartSurface.addDeletable(animationSeries);
// Update data using data animations
let timerId: NodeJS.Timeout;
const animateData = () => {
xValues = xValues.map((x) => x + ((Math.random() - 0.5) * length) / 5);
yValues = yValues.map((y) => y + ((Math.random() - 0.5) * length) / 5);
// Set the values on the temp series
animationSeries.clear();
animationSeries.appendRange(xValues, yValues);
scatterSeries.runAnimation(
new ScatterAnimation({
duration: 1000,
ease: easing.outQuad,
// Do not create a new DataSeries here or it will leak and eventually crash.
dataSeries: animationSeries,
})
);
timerId = setTimeout(animateData, 1200);
};
timerId = setTimeout(animateData, 1000);
const startUpdate = () => {
timerId = setTimeout(animateData, 1000);
};
const stopUpdate = () => {
clearTimeout(timerId);
};
return { wasmContext, sciChartSurface, controls: { startUpdate, stopUpdate } };
};
The Data Animation Example demonstrates how to implement real-time dataset animations with SciChart.js using JavaScript. This example initializes a SciChartSurface asynchronously and dynamically updates chart data with smooth animated transitions, making it ideal for high-performance WebGL visualizations.
The chart is created by asynchronously initializing the SciChartSurface using async/await, a pattern explained in Async and Await in JavaScript. It configures NumericAxis objects with specific visible ranges set by NumberRange to control the chart axes. The data is managed through an XyDataSeries and rendered with a FastLineRenderableSeries, which incorporates a custom EllipsePointMarker for enhanced visual emphasis. Real-time data updates are achieved via a looping setTimeout function that adjusts the data and triggers a ScatterAnimation to animate these changes.
This example highlights several advanced features and customizations: the use of dynamic, randomized data updates that enable smooth animated transitions; advanced styling with a gradient palette applied through PaletteFactory and gradient parameters; and custom point markers providing greater visual clarity. These capabilities are engineered for high-performance, real-time updates while ensuring efficient data handling.
Even though this implementation is in JavaScript, it adheres to best practices applicable across various frameworks. The asynchronous initialization ensures that the chart setup is non-blocking, as illustrated in the asynchronous patterns from Async and Await in JavaScript. The implementation also emphasizes proper resource management and cleanup, following resource management guidelines to prevent memory leaks by disposing of the SciChartSurface appropriately. Finally, developers can consult the Performance Tips & Tricks documentation to further optimize real-time dataset animations in their applications.



