Demonstrates how to create a JavaScript Chart with multiple X,Y axis using SciChart.js, High Performance JavaScript Charts
import {
EAxisAlignment,
ECoordinateMode,
EHorizontalAnchorPoint,
ELabelAlignment,
EllipsePointMarker,
EMultiLineAlignment,
ENumericFormat,
EVerticalAnchorPoint,
EWrapTo,
FastLineRenderableSeries,
MouseWheelZoomModifier,
NativeTextAnnotation,
NumberRange,
NumericAxis,
SciChartSurface,
TextAnnotation,
XAxisDragModifier,
XyDataSeries,
YAxisDragModifier,
ZoomExtentsModifier,
ZoomPanModifier,
} from "scichart";
import { appTheme } from "./appTheme";
import { RandomWalkGenerator } from "./RandomWalkGenerator";
const titleStyle1 = {
color: appTheme.VividSkyBlue,
fontSize: 30,
};
const labelStyle1 = {
color: appTheme.VividSkyBlue,
};
const titleStyle2 = {
color: appTheme.VividOrange,
fontSize: 30,
};
const labelStyle2 = {
color: appTheme.VividOrange,
alignment: ELabelAlignment.Right,
};
const ID_X_AXIS_2 = "xAxis2";
const ID_Y_AXIS_2 = "yAxis2";
export const drawExample = async (rootElement: string | HTMLDivElement) => {
// Create the SciChartSurface with theme
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: appTheme.SciChartJsTheme,
});
// Add a primary X,Y Axis pair
sciChartSurface.xAxes.add(
new NumericAxis(wasmContext, {
axisAlignment: EAxisAlignment.Bottom,
axisTitle: "X Axis Bottom",
axisTitleStyle: titleStyle1,
labelStyle: labelStyle1,
backgroundColor: appTheme.VividSkyBlue + "22",
axisBorder: {
borderTop: 1,
color: appTheme.VividSkyBlue,
},
})
);
sciChartSurface.yAxes.add(
new NumericAxis(wasmContext, {
axisAlignment: EAxisAlignment.Left,
axisTitle: "Y Axis Left",
axisTitleStyle: titleStyle1,
labelStyle: labelStyle1,
growBy: new NumberRange(0.1, 0.1),
backgroundColor: appTheme.VividSkyBlue + "22",
axisBorder: {
borderRight: 1,
color: appTheme.VividSkyBlue,
},
})
);
// Add a secondary X,Y Axis pair
// Series are tied to the axis via the ID_
sciChartSurface.xAxes.add(
new NumericAxis(wasmContext, {
id: ID_X_AXIS_2,
axisTitleStyle: titleStyle2,
labelStyle: labelStyle2,
axisAlignment: EAxisAlignment.Top,
axisTitle: "X Axis Top",
backgroundColor: appTheme.VividOrange + "22",
axisBorder: {
borderBottom: 1,
color: appTheme.VividOrange,
},
})
);
sciChartSurface.yAxes.add(
new NumericAxis(wasmContext, {
id: ID_Y_AXIS_2,
axisTitleStyle: titleStyle2,
labelStyle: labelStyle2,
axisAlignment: EAxisAlignment.Right,
axisTitle: "Y Axis Right",
labelFormat: ENumericFormat.Decimal,
labelPrecision: 2,
growBy: new NumberRange(0.1, 0.1),
backgroundColor: appTheme.VividOrange + "22",
axisBorder: {
borderLeft: 1,
color: appTheme.VividOrange,
},
})
);
// generate some data
let data = new RandomWalkGenerator().Seed(1337).getRandomWalkSeries(100);
// Add the first line series on the primary X,Y axis
// This occurs be default as FastLineRenderableSeries XAxisId and YAxisId are set to a default value
sciChartSurface.renderableSeries.add(
new FastLineRenderableSeries(wasmContext, {
stroke: appTheme.VividSkyBlue,
strokeThickness: 3,
dataSeries: new XyDataSeries(wasmContext, { xValues: data.xValues, yValues: data.yValues }),
pointMarker: new EllipsePointMarker(wasmContext, {
width: 5,
height: 5,
fill: appTheme.VividSkyBlue,
stroke: appTheme.VividSkyBlue,
}),
})
);
data = new RandomWalkGenerator().Seed(90210).getRandomWalkSeries(100);
// The second line series we specify X/Y axis ids to bind this to the correct axis
sciChartSurface.renderableSeries.add(
new FastLineRenderableSeries(wasmContext, {
stroke: appTheme.VividOrange,
strokeThickness: 3,
dataSeries: new XyDataSeries(wasmContext, { xValues: data.xValues, yValues: data.yValues }),
xAxisId: ID_X_AXIS_2,
yAxisId: ID_Y_AXIS_2,
pointMarker: new EllipsePointMarker(wasmContext, {
width: 5,
height: 5,
fill: appTheme.VividOrange,
stroke: appTheme.VividOrange,
}),
})
);
// Optional: Add some interactivity modifiers to enable zooming and panning
sciChartSurface.chartModifiers.add(
new YAxisDragModifier(),
new XAxisDragModifier(),
new ZoomPanModifier({ enableZoom: true }),
new MouseWheelZoomModifier(),
new ZoomExtentsModifier()
);
// Add a title over the chart with information
sciChartSurface.annotations.add(
new NativeTextAnnotation({
x1: 0.01,
y1: 0.01,
xCoordinateMode: ECoordinateMode.Relative,
yCoordinateMode: ECoordinateMode.Relative,
horizontalAnchorPoint: EHorizontalAnchorPoint.Left,
verticalAnchorPoint: EVerticalAnchorPoint.Top,
fontSize: 18,
opacity: 0.55,
textColor: appTheme.ForegroundColor,
text: "SciChart.js supports unlimited X,Y axis. Drag an axis to see the series scale",
wrapTo: EWrapTo.ViewRect,
multiLineAlignment: EMultiLineAlignment.Left,
})
);
sciChartSurface.annotations.add(
new TextAnnotation({
x1: 30,
y1: -3.5,
fontSize: 18,
textColor: appTheme.VividSkyBlue,
text: "Blue series is bound to the Left, Bottom axis",
})
);
// Note annotations need X,Y Axis Id as well in multi-axis scenarios
sciChartSurface.annotations.add(
new TextAnnotation({
x1: 68,
y1: 3.7,
fontSize: 18,
textColor: appTheme.VividOrange,
horizontalAnchorPoint: EHorizontalAnchorPoint.Right,
yAxisId: ID_Y_AXIS_2,
xAxisId: ID_X_AXIS_2,
text: "Orange series is bound to the Top, Right axis",
})
);
return { sciChartSurface, wasmContext };
};
This example demonstrates how to create a high-performance chart with multiple X and Y axes using JavaScript and SciChart.js. Its purpose is to showcase the configuration of two independent pairs of numeric axes along with binding data series via unique axis IDs, while integrating interactive modifiers and custom annotations for an enhanced user experience.
The implementation begins by initializing a SciChartSurface with a custom theme. Two sets of NumericAxis are added: the primary (blue) set aligned using axis.alignment = EAxisAlignment.Bottom and EAxisAlignment.Left, and the secondary (orange) set aligned to the EAxisAlignment.Top and EAxisAlignment.Right. Data is generated through a random walk generator, and each data series is bound to its corresponding axis by setting a unique on axis.id. Series can then be registered on a specific axis by setting FastLineRenderableSeries.xAxisId and yAxisId. Interactive modifiers, including YAxisDragModifier, XAxisDragModifier as well as zoom, pan, mouse-wheel zoom and zoom-extents are then added. Developers seeking further details on multi-axis configuration may refer to the Tutorial 08 - Adding Multiple Axis and for interactivity see the Tutorial 03 - Adding Zooming, Panning Behavior.
The chart offers several advanced features:
axis.alignment, axis.axisBorder, and background styling via axis.backgroundColor, providing distinct visual themes. More details can be found in the Axis Styling docs.XAxisDragModifier, YAxisDragModifier, ZoomPanModifier, and MouseWheelZoomModifier enable dynamic interaction, helping users adjust the view in real time.Although this example is implemented using JavaScript, it illustrates best practices for resource cleanup and the management of the SciChartSurface lifecycle, as outlined in the Memory Best Practices guide. Even though a React component wrapper is provided for integration into larger applications, the core functionality relies solely on JavaScript, making it adaptable across various frameworks. This approach ensures that developers can easily integrate and expand upon the chart in different environments without the overhead of additional abstraction layers.










