Using the new Sub-Charts API, create a multi-pane stock chart example with indicator panels on a single WebGL chart surface. This allows for higher-performance since the WebGL context is shared. Zooming, panning, cursors are synchronised between the charts.
import { FinChartLegendModifier, IFinanceLegendModifierOptions } from "./FinChartLegendModifier";
import {
BasePaletteProvider,
calcAverageForArray,
build2DChart,
registerAllTypes,
CustomAnnotation,
EAutoRange,
EAxisAlignment,
EAxisType,
EChart2DModifierType,
EDataFilterType,
EFillPaletteMode,
EHorizontalAnchorPoint,
ESeriesType,
EStrokePaletteMode,
EVerticalAnchorPoint,
EXyDirection,
I2DSubSurfaceOptions,
IFillPaletteProvider,
IStrokePaletteProvider,
NumberRange,
parseColorToUIntArgb,
Rect,
SciChartVerticalGroup,
Thickness,
TXywhCoordinates,
} from "scichart";
import { fetchMultiPaneData } from "./ExampleDataProvider";
import { appTheme } from "./appTheme";
// The Builder API is lean by default in v6. The definitions below name their types as
// strings, so register the built-in types before building the chart
registerAllTypes();
export const mainChartWrapper2 = "cc_chart2";
export const subChartWrapper1 = "subChartWrapper1";
export const subChartWrapper2 = "subChartWrapper2";
export const subChartWrapper3 = "subChartWrapper3";
export const dividerId1 = "dividerId1";
export const dividerId2 = "dividerId2";
export const containerId = "containerId";
export const containerId2 = "containerId2";
const getDataForSecondPane = (xValues: number[], closeValues: number[]) => {
const macdArray: number[] = [];
const signalArray: number[] = [];
const divergenceArray: number[] = [];
for (let i = 0; i < xValues.length; i++) {
const maSlow = calcAverageForArray(closeValues, 12, i);
const maFast = calcAverageForArray(closeValues, 25, i);
const macd = maSlow - maFast;
macdArray.push(macd);
const signal = calcAverageForArray(macdArray, 9, i);
signalArray.push(signal);
const divergence = macd - signal;
divergenceArray.push(divergence);
}
return { macdArray, signalArray, divergenceArray };
};
const getDataForThirdPane = (xValues: number[], closeValues: number[]) => {
const RSI_PERIOD = 14;
const rsiArray: number[] = [];
const gainArray: number[] = [];
const lossArray: number[] = [];
if (xValues.length) {
rsiArray.push(NaN);
gainArray.push(NaN);
lossArray.push(NaN);
}
for (let i = 1; i < xValues.length; i++) {
const previousClose = closeValues[i - 1];
const currentClose = closeValues[i];
const gain = currentClose > previousClose ? currentClose - previousClose : 0;
gainArray.push(gain);
const loss = previousClose > currentClose ? previousClose - currentClose : 0;
lossArray.push(loss);
const relativeStrength =
calcAverageForArray(gainArray, RSI_PERIOD) / calcAverageForArray(lossArray, RSI_PERIOD);
const rsi = 100 - 100 / (1 + relativeStrength);
rsiArray.push(rsi);
}
return { rsiArray };
};
export const drawExample = async (rootElement: string | HTMLDivElement) => {
const verticalGroup = new SciChartVerticalGroup();
const {
dateValues: xValues,
openValues,
highValues,
lowValues,
closeValues,
volumeValues,
} = await fetchMultiPaneData();
const { macdArray, signalArray, divergenceArray } = getDataForSecondPane(xValues, closeValues);
const { rsiArray } = getDataForThirdPane(xValues, closeValues);
const axisAlignment = EAxisAlignment.Right;
const commonSubChartSurfaceOptions: I2DSubSurfaceOptions = {
padding: Thickness.fromNumber(10),
isTransparent: false,
theme: appTheme.SciChartJsTheme,
};
const subChartModifiers = [
{ type: EChart2DModifierType.ZoomPan, options: { xyDirection: EXyDirection.XDirection } },
{ type: EChart2DModifierType.PinchZoom, options: { xyDirection: EXyDirection.XDirection } },
{ type: EChart2DModifierType.ZoomExtents, options: { xyDirection: EXyDirection.XDirection } },
{ type: EChart2DModifierType.MouseWheelZoom, options: { xyDirection: EXyDirection.XDirection } },
];
const upCol = appTheme.VividGreen;
const downCol = appTheme.MutedRed;
const opacity = "AA";
const { sciChartSurface: mainSurface, wasmContext } = await build2DChart(rootElement, {
surface: {
id: "mainSurface",
theme: appTheme.SciChartJsTheme,
},
xAxes: {
type: EAxisType.DiscontinuousDateAxis,
options: {
isVisible: false,
},
},
yAxes: {
type: EAxisType.NumericAxis,
options: {
isVisible: false,
},
},
modifiers: {
type: EChart2DModifierType.Custom,
customType: FinChartLegendModifier.customType,
options: {
crosshairStroke: appTheme.ForegroundColor,
} as IFinanceLegendModifierOptions,
},
subCharts: [
{
surface: {
...commonSubChartSurfaceOptions,
position: new Rect(0, 0, 1, 0.5),
id: "subChart1",
subChartContainerId: subChartWrapper1,
},
xAxes: {
type: EAxisType.DiscontinuousDateAxis,
options: {
drawLabels: false,
autoRange: EAutoRange.Once,
//maxAutoTicks: 20,
useNativeText: false,
minorsPerMajor: 3,
// visibleRangeLimit: new NumberRange(0, xValues.length - 1),
},
},
yAxes: [
{
type: EAxisType.NumericAxis,
options: {
maxAutoTicks: 10,
autoRange: EAutoRange.Always,
growBy: new NumberRange(0.3, 0.11),
labelPrecision: 2,
labelPrefix: "$",
axisAlignment,
drawMinorGridLines: false,
},
},
{
type: EAxisType.NumericAxis,
options: {
id: "yAxis2",
isVisible: false,
autoRange: EAutoRange.Always,
growBy: new NumberRange(0, 3),
},
},
],
series: [
{
type: ESeriesType.CandlestickSeries,
ohlcData: {
dataSeriesName: "OHLC Close",
xValues,
openValues,
highValues,
lowValues,
closeValues,
},
options: {
dataPointWidth: 0.7,
brushUp: upCol + opacity,
brushDown: downCol + opacity,
strokeUp: upCol,
strokeDown: downCol,
},
},
{
type: ESeriesType.LineSeries,
xyData: {
xValues,
yValues: closeValues,
filter: {
type: EDataFilterType.XyMovingAverage,
options: {
dataSeriesName: "MA 50 Low",
length: 50,
},
},
},
options: {
stroke: appTheme.VividSkyBlue,
strokeThickness: 2,
},
},
{
type: ESeriesType.LineSeries,
xyData: {
xValues,
yValues: closeValues,
filter: {
type: EDataFilterType.XyMovingAverage,
options: {
dataSeriesName: "MA 200 High",
length: 200,
},
},
},
options: {
stroke: appTheme.VividPink,
strokeThickness: 2,
},
},
{
type: ESeriesType.ColumnSeries,
xyData: {
dataSeriesName: "Volume",
xValues,
yValues: volumeValues,
},
options: {
yAxisId: "yAxis2",
dataPointWidth: 0.9,
strokeThickness: 0,
paletteProvider: new VolumePaletteProvider(
openValues,
closeValues,
upCol + opacity,
downCol + opacity
),
},
},
],
modifiers: subChartModifiers,
sharedData: {},
},
{
surface: {
...commonSubChartSurfaceOptions,
position: new Rect(0, 0.5, 1, 0.3),
subChartContainerId: subChartWrapper2,
id: "subChart2",
},
xAxes: [
{
type: EAxisType.DiscontinuousDateAxis,
options: {
drawLabels: false,
drawMajorTickLines: false,
drawMinorTickLines: false,
useNativeText: false,
minorsPerMajor: 3,
// visibleRangeLimit: new NumberRange(0, xValues.length - 1),
},
},
],
yAxes: [
{
type: EAxisType.NumericAxis,
options: {
maxAutoTicks: 6,
autoRange: EAutoRange.Always,
growBy: new NumberRange(0.1, 0.1),
axisAlignment,
labelPrecision: 2,
drawMinorGridLines: false,
},
},
],
series: [
{
type: ESeriesType.BandSeries,
xyyData: {
dataSeriesName: "MACD",
xValues,
yValues: signalArray,
y1Values: macdArray,
},
options: {
stroke: downCol,
strokeY1: upCol,
fill: upCol + opacity,
fillY1: downCol + opacity,
},
},
{
type: ESeriesType.ColumnSeries,
xyData: {
dataSeriesName: "Divergence",
xValues,
yValues: divergenceArray,
},
options: {
dataPointWidth: 0.5,
paletteProvider: new MacdHistogramPaletteProvider(upCol + opacity, downCol + opacity),
},
},
],
modifiers: subChartModifiers,
},
{
surface: {
...commonSubChartSurfaceOptions,
position: new Rect(0, 0.8, 1, 0.2),
padding: Thickness.fromNumber(10),
id: "subChart3",
isTransparent: false,
subChartContainerId: subChartWrapper3,
},
xAxes: {
type: EAxisType.DiscontinuousDateAxis,
options: {
drawLabels: true,
drawMajorTickLines: true,
drawMinorTickLines: false,
useNativeText: false,
minorsPerMajor: 3,
// visibleRangeLimit: new NumberRange(0, xValues.length - 1),
},
},
yAxes: {
type: EAxisType.NumericAxis,
options: {
labelPrecision: 0,
maxAutoTicks: 6,
autoRange: EAutoRange.Always,
growBy: new NumberRange(0.1, 0.1),
axisAlignment,
drawMinorGridLines: false,
},
},
series: {
type: ESeriesType.LineSeries,
xyData: { dataSeriesName: "RSI", xValues, yValues: rsiArray },
options: {
stroke: appTheme.VividSkyBlue,
strokeThickness: 2,
},
},
modifiers: subChartModifiers,
},
],
});
const [subSurface1, subSurface2, subSurface3] = mainSurface.subCharts;
verticalGroup.addSurfaceToGroup(subSurface1);
verticalGroup.addSurfaceToGroup(subSurface2);
verticalGroup.addSurfaceToGroup(subSurface3);
const chart1XAxis = subSurface1.xAxes.get(0);
const chart2XAxis = subSurface2.xAxes.get(0);
const chart3XAxis = subSurface3.xAxes.get(0);
// Synchronize visible ranges
chart1XAxis.visibleRangeChanged.subscribe((data1) => {
chart2XAxis.visibleRange = data1.visibleRange;
chart3XAxis.visibleRange = data1.visibleRange;
});
chart2XAxis.visibleRangeChanged.subscribe((data1) => {
chart1XAxis.visibleRange = data1.visibleRange;
chart3XAxis.visibleRange = data1.visibleRange;
});
chart3XAxis.visibleRangeChanged.subscribe((data1) => {
chart1XAxis.visibleRange = data1.visibleRange;
chart2XAxis.visibleRange = data1.visibleRange;
});
// Add some trades to the chart using the Annotations API
for (let i = 0; i < subSurface1.renderableSeries.get(0).dataSeries.count(); i++) {
// Every 1000th bar, add a buy annotation
if (i % 1000 === 0) {
subSurface1.annotations.add(buyMarkerAnnotation(i, lowValues[i]));
}
// Every 1000th bar between buys, add a sell annotation
if ((i + 500) % 1000 === 0) {
subSurface1.annotations.add(sellMarkerAnnotation(i, highValues[i]));
}
}
// Resizing Logic
const firstDividerElement = document.getElementById(dividerId1);
const secondDividerElement = document.getElementById(dividerId2);
let isDraggingFirst = false;
let isDraggingSecond = false;
const container = document.getElementById(containerId2);
const baseHeight = container.offsetHeight;
const minPaneSize = 0.2;
const resizePanesFirst = (mouseYOffset: number) => {
let newPosition = mouseYOffset / baseHeight;
if (newPosition < minPaneSize) {
newPosition = minPaneSize;
}
if (newPosition > (subSurface3.subPosition as TXywhCoordinates).y - minPaneSize) {
newPosition = (subSurface3.subPosition as TXywhCoordinates).y - minPaneSize;
}
firstDividerElement.style.top = `${newPosition * 100}%`;
subSurface1.subPosition = new Rect(0, 0, 1, newPosition);
subSurface2.subPosition = new Rect(
0,
newPosition,
1,
(subSurface3.subPosition as TXywhCoordinates).y - newPosition
);
};
const resizePanesSecond = (mouseYOffset: number) => {
let newPosition = mouseYOffset / baseHeight;
if (newPosition > 1 - minPaneSize) {
newPosition = 1 - minPaneSize;
}
if (newPosition < (subSurface1.subPosition as TXywhCoordinates).height + minPaneSize) {
newPosition = (subSurface1.subPosition as TXywhCoordinates).height + minPaneSize;
}
secondDividerElement.style.top = `${newPosition * 100}%`;
subSurface2.subPosition = new Rect(
0,
(subSurface1.subPosition as TXywhCoordinates).height,
1,
newPosition - (subSurface1.subPosition as TXywhCoordinates).height
);
subSurface3.subPosition = new Rect(0, newPosition, 1, 1 - newPosition);
};
const mouseDownHandlerFirst = () => {
isDraggingFirst = true;
};
const mouseDownHandlerSecond = () => {
isDraggingSecond = true;
};
const mouseUpHandler = () => {
if (!isDraggingFirst && !isDraggingSecond) {
return;
}
isDraggingFirst = false;
isDraggingSecond = false;
};
const mouseMoveHandler = (event: MouseEvent) => {
if (!isDraggingFirst && !isDraggingSecond) {
return;
}
const rect = container.getBoundingClientRect();
const mouseYOffset = event.clientY - rect.top;
if (isDraggingFirst) {
resizePanesFirst(mouseYOffset);
} else {
resizePanesSecond(mouseYOffset);
}
};
const paneMouseDownHandler = (event: MouseEvent) => {
event.preventDefault();
};
firstDividerElement.addEventListener("pointerdown", mouseDownHandlerFirst);
secondDividerElement.addEventListener("pointerdown", mouseDownHandlerSecond);
container.addEventListener("pointerdown", paneMouseDownHandler);
container.addEventListener("pointerup", mouseUpHandler);
container.addEventListener("pointermove", mouseMoveHandler);
container.addEventListener("pointerleave", mouseUpHandler);
firstDividerElement.style.top = `${(subSurface1.subPosition as TXywhCoordinates).height * 100}%`;
secondDividerElement.style.top = `${(subSurface3.subPosition as TXywhCoordinates).y * 100}%`;
return { sciChartSurface: mainSurface, wasmContext };
};
/**
* An example PaletteProvider applied to the volume column series. It will return green / red
* fills and strokes when the main price data bar is up or down
*/
// tslint:disable-next-line: max-classes-per-file
class VolumePaletteProvider extends BasePaletteProvider implements IStrokePaletteProvider, IFillPaletteProvider {
public readonly strokePaletteMode: EStrokePaletteMode = EStrokePaletteMode.SOLID;
public readonly fillPaletteMode: EFillPaletteMode = EFillPaletteMode.SOLID;
private openValues: number[];
private closeValues: number[];
private volumeUpArgb: number;
private volumnDownArgb: number;
constructor(openValues: number[], closeValues: number[], volumeUpColor: string, volumeDownColor: string) {
super();
this.openValues = openValues;
this.closeValues = closeValues;
this.volumeUpArgb = parseColorToUIntArgb(volumeUpColor);
this.volumnDownArgb = parseColorToUIntArgb(volumeDownColor);
}
overrideFillArgb(_xValue: number, _yValue: number, index: number): number {
const open = this.openValues[index];
const close = this.closeValues[index];
return close >= open ? this.volumeUpArgb : this.volumnDownArgb;
}
overrideStrokeArgb(xValue: number, yValue: number, index: number): number {
return this.overrideFillArgb(xValue, yValue, index);
}
}
// tslint:disable-next-line: max-classes-per-file
class MacdHistogramPaletteProvider extends BasePaletteProvider implements IStrokePaletteProvider, IFillPaletteProvider {
public readonly strokePaletteMode: EStrokePaletteMode = EStrokePaletteMode.SOLID;
public readonly fillPaletteMode: EFillPaletteMode = EFillPaletteMode.SOLID;
private aboveZeroArgb: number;
private belowZeroArgb: number;
constructor(aboveZeroColor: string, belowZeroColor: string) {
super();
this.aboveZeroArgb = parseColorToUIntArgb(aboveZeroColor);
this.belowZeroArgb = parseColorToUIntArgb(belowZeroColor);
}
overrideFillArgb(_xValue: number, yValue: number, _index: number): number {
return yValue >= 0 ? this.aboveZeroArgb : this.belowZeroArgb;
}
overrideStrokeArgb(xValue: number, yValue: number, index: number): number {
return this.overrideFillArgb(xValue, yValue, index);
}
}
// Returns a CustomAnnotation that represents a buy marker arrow
// The CustomAnnotation supports SVG as content. Using Inkscape or similar you can create SVG content for annotations
const buyMarkerAnnotation = (x1: number, y1: number): CustomAnnotation => {
return new CustomAnnotation({
x1,
y1,
verticalAnchorPoint: EVerticalAnchorPoint.Top,
horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
svgString:
'<svg id="Capa_1" xmlns="http://www.w3.org/2000/svg">' +
'<g transform="translate(-53.867218,-75.091687)">' +
'<path style="fill:#1cb61c;fill-opacity:0.34117647;stroke:#00b400;strokeWidth:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"' +
'd="m 55.47431,83.481251 c 7.158904,-7.408333 7.158904,-7.408333 7.158904,-7.408333 l 7.158906,7.408333 H 66.212668 V 94.593756 H 59.053761 V 83.481251 Z"' +
"/>" +
"</g>" +
"</svg>",
});
};
// Returns a CustomAnnotation that represents a sell marker arrow
// The CustomAnnotation supports SVG as content. Using Inkscape or similar you can create SVG content for annotations
const sellMarkerAnnotation = (x1: number, y1: number): CustomAnnotation => {
return new CustomAnnotation({
x1,
y1,
verticalAnchorPoint: EVerticalAnchorPoint.Bottom,
horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
svgString:
'<svg id="Capa_1" xmlns="http://www.w3.org/2000/svg">' +
'<g transform="translate(-54.616083,-75.548914)">' +
'<path style="fill:#b22020;fill-opacity:0.34117648;stroke:#990000;strokeWidth:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"' +
'd="m 55.47431,87.025547 c 7.158904,7.408333 7.158904,7.408333 7.158904,7.408333 L 69.79212,87.025547 H 66.212668 V 75.913042 h -7.158907 v 11.112505 z"' +
"/>" +
"</g>" +
"</svg>",
});
};
This example demonstrates how to implement a high-performance multi-pane stock chart using JavaScript. It leverages the SciChart.js Sub-Charts API to render several synchronized panes—including a candlestick series, a MACD indicator with divergence and band series, and an RSI chart—on a single shared WebGL chart surface. This approach optimizes rendering performance while keeping interactions such as zooming, panning, and crosshair updates in sync across all panes.
The chart is constructed using the Builder API with JSON configuration via the call to chartBuilder.build2DChart(). This method initializes the primary SciChartSurface along with multiple sub-chart surfaces positioned dynamically using Rect definitions. For enhanced interactivity, custom chart modifiers and SVG annotations, such as the FinChartLegendModifier and FinChartLegendAnnotation, are implemented to provide real-time tooltips and interactive legends. These modifiers update dynamically based on mouse movements and are closely integrated with the axis synchronization logic, which is achieved by subscribing to the axis.visibleRangeChanged event on the x-axes of each sub-chart. For further technical details on creating custom annotations, refer to the Tutorial 06 - Adding Annotations.
The example showcases a wide array of advanced features including:
This implementation is built entirely in JavaScript and follows best practices for high-performance charting with SciChart.js. The use of a shared WebGL context, efficient JSON-based Builder API configuration, and dynamically synchronized axes demonstrates a robust approach to trading chart applications. In addition, the example includes custom event handling for drag-resize logic, which allows users to adjust the sub-chart pane sizes on the fly. To learn more about optimizing performance, developers can review the Performance Tips & Tricks guide.












