Demonstrates a realtime React static axis chart - where the ticks and gridlines are fixed, but the labels change. With SciChart.js High Performance JavaScript Charts you can achieve this simply by setting isStaticAxis property to true on the X axis.
import {
XyDataSeries,
NumericAxis,
FastLineRenderableSeries,
SciChartSurface,
EAutoRange,
HorizontalLineAnnotation,
NumberRange,
EAxisAlignment,
} from "scichart";
import { appTheme, createChartTheme } from "./appTheme";
export const drawExample = async (rootElement: string | HTMLDivElement) => {
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: createChartTheme({
majorGridLineBrush: "#FFFFFF22",
}),
});
// Create X Axis
const staticXAxis = new NumericAxis(wasmContext, {
labelPrecision: 0,
autoRange: EAutoRange.Always,
axisAlignment: EAxisAlignment.Top,
axisTitle: "Static Axis",
isStaticAxis: true, // when true, gridlines and axis labels keep their initial position on visible range change
// drawMajorBands: false, // avoids flickering - when values change fast and isStaticAxis is true
});
const xAxis = new NumericAxis(wasmContext, {
labelPrecision: 0,
autoRange: EAutoRange.Always,
axisAlignment: EAxisAlignment.Bottom,
axisTitle: "Normal Axis",
id: "Normal",
isStaticAxis: false, // when true, gridlines and axis labels keep their initial position on visible range change
// drawMajorBands: false, // avoids flickering - when values change fast and isStaticAxis is true
});
staticXAxis.visibleRangeChanged.subscribe((data) => (xAxis.visibleRange = data.visibleRange));
sciChartSurface.xAxes.add(staticXAxis, xAxis);
// Create Y Axis
const yAxis = new NumericAxis(wasmContext, {
visibleRange: new NumberRange(-2, 2),
});
sciChartSurface.yAxes.add(yAxis);
// build data series
const xValues = [];
const yValues = [];
const fifoCapacity = 1000;
let i = 0;
const makeY = (x: number) => Math.sin(x * 0.05) - 0.1 * Math.sin(x * 0.1) - Math.cos(x * 0.005);
for (; i < fifoCapacity; i++) {
xValues.push(i);
yValues.push(makeY(i));
}
// Add a line series with initial data
const dataSeries = new XyDataSeries(wasmContext, {
xValues,
yValues,
fifoCapacity,
});
sciChartSurface.renderableSeries.add(
new FastLineRenderableSeries(wasmContext, {
dataSeries,
stroke: appTheme.VividOrange,
strokeThickness: 4,
})
);
const updateCallback = () => {
const xUpdate = [];
const yUpdate = [];
for (let j = 0; j < 2; i++, j++) {
xUpdate.push(i);
yUpdate.push(makeY(i));
}
dataSeries.appendRange(xUpdate, yUpdate);
};
setInterval(updateCallback, 10);
// line annotation at x = 0
sciChartSurface.annotations.add(
new HorizontalLineAnnotation({
stroke: "#FFFFFF44",
strokeThickness: 1,
y1: 0,
})
);
function toggleStaticAxis() {
if (staticXAxis.isPrimaryAxis) {
xAxis.isPrimaryAxis = true;
} else {
staticXAxis.isPrimaryAxis = true;
}
}
return { sciChartSurface, wasmContext, controls: { toggleStaticAxis } };
};
This example demonstrates a real-time chart in a React application using SciChart.js. The chart features a static X axis, where the gridlines and ticks remain fixed while only the labels update dynamically. This provides a smooth visualization experience during real-time data updates.
The chart is initialized using the <SciChartReact/> component from the SciChart.js React integration library. Two NumericAxis are created: a static axis (with the property isStaticAxis set to true) that maintains fixed label and gridline positions, and a normal axis that updates with the new data. The static axis not only preserves its layout but also synchronizes its visible range with the normal axis through an event subscription to visibleRangeChanged (see Axis Ranging for more details). Data is continuously streamed into the chart in real-time via a setInterval callback using dataSeries.appendRange(), aligning with techniques described in the DataSeries Realtime Updates documentation.
The example showcases several advanced features including real-time chart updates and the unique behavior of a static axis with fixed gridlines. A toggle control built with the shared CSS controls button group allows users to switch the primary axis between static and normal modes interactively. The function toggleStaticAxis() in drawExample.ts updates the axis.isPrimaryAxis property, defining which axis is responsible for drawing gridlines. This toggle functionality is managed with React state and the useRef hook, enabling direct control over child component methods.
Integrating SciChart.js within a React application is streamlined with the <SciChartReact/> component, which encapsulates the rendering logic and lifecycle management of the chart. The use of shared CSS controls for styling and interactive controls, such as the toggle for switching axis behaviors, follows best practices for UI development in React. Developers seeking further guidance on React integration can refer to the React Charts with SciChart.js article.
This example serves as a comprehensive guide for implementing a high performance, real-time chart with static axis behavior in React using SciChart.js.










