Demonstrates how to create crosshairs on mouseover using SciChart.js, High Performance JavaScript Charts
import { appTheme } from "./appTheme";
import { ExampleDataProvider } from "./ExampleDataProvider";
import {
NumericAxis,
NumberRange,
SciChartSurface,
XyDataSeries,
ENumericFormat,
FastLineRenderableSeries,
EllipsePointMarker,
CursorModifier,
ZoomPanModifier,
ZoomExtentsModifier,
MouseWheelZoomModifier,
SeriesInfo,
CursorTooltipSvgAnnotation,
} from "scichart";
export const drawExample = async (rootElement: string | HTMLDivElement) => {
// Create a SciChartSurface with X,Y Axis
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: appTheme.SciChartJsTheme,
});
sciChartSurface.xAxes.add(
new NumericAxis(wasmContext, {
growBy: new NumberRange(0.05, 0.05),
labelFormat: ENumericFormat.Decimal,
labelPrecision: 4,
})
);
sciChartSurface.yAxes.add(
new NumericAxis(wasmContext, {
growBy: new NumberRange(0.1, 0.1),
labelFormat: ENumericFormat.Decimal,
labelPrecision: 4,
})
);
// Add some data
const data1 = ExampleDataProvider.getFourierSeriesZoomed(0.6, 0.13, 5.0, 5.15);
sciChartSurface.renderableSeries.add(
new FastLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, {
xValues: data1.xValues,
yValues: data1.yValues,
dataSeriesName: "First Line Series",
}),
strokeThickness: 3,
stroke: appTheme.VividSkyBlue,
pointMarker: new EllipsePointMarker(wasmContext, {
width: 7,
height: 7,
strokeThickness: 0,
fill: appTheme.VividSkyBlue,
}),
})
);
const data2 = ExampleDataProvider.getFourierSeriesZoomed(0.5, 0.12, 5.0, 5.15);
sciChartSurface.renderableSeries.add(
new FastLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, {
xValues: data2.xValues,
yValues: data2.yValues,
dataSeriesName: "Second Line Series",
}),
strokeThickness: 3,
stroke: appTheme.VividOrange,
pointMarker: new EllipsePointMarker(wasmContext, {
width: 7,
height: 7,
strokeThickness: 0,
fill: appTheme.VividOrange,
}),
})
);
const data3 = ExampleDataProvider.getFourierSeriesZoomed(0.4, 0.11, 5.0, 5.15);
sciChartSurface.renderableSeries.add(
new FastLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, {
xValues: data3.xValues,
yValues: data3.yValues,
dataSeriesName: "Third Line Series",
}),
strokeThickness: 3,
stroke: appTheme.MutedPink,
pointMarker: new EllipsePointMarker(wasmContext, {
width: 7,
height: 7,
strokeThickness: 0,
fill: appTheme.MutedPink,
}),
})
);
// Here is where we add cursor behaviour
//
sciChartSurface.chartModifiers.add(
// Add the CursorModifier (crosshairs) behaviour
new CursorModifier({
// Defines if crosshair is shown
crosshairStroke: appTheme.VividOrange,
crosshairStrokeThickness: 1,
showXLine: true,
showYLine: true,
// Shows the default tooltip
showTooltip: true,
tooltipContainerBackground: appTheme.VividOrange,
tooltipTextStroke: appTheme.ForegroundColor,
// Defines the axis label colours
axisLabelFill: appTheme.VividOrange,
axisLabelStroke: appTheme.ForegroundColor,
// Shows an additional legend in top left of the screen
tooltipLegendTemplate: getTooltipLegendTemplate,
// Use SVG to render the tooltip & axis labels of the CursorModifier
isSvgOnly: true,
}),
// Add further zooming and panning behaviours
new ZoomPanModifier({ enableZoom: true }),
new ZoomExtentsModifier(),
new MouseWheelZoomModifier()
);
return { sciChartSurface, wasmContext };
};
// Override the standard tooltip displayed by CursorModifier
const getTooltipLegendTemplate = (seriesInfos: SeriesInfo[], svgAnnotation: CursorTooltipSvgAnnotation) => {
let outputSvgString = "";
// Foreach series there will be a seriesInfo supplied by SciChart. This contains info about the series under the house
seriesInfos.forEach((seriesInfo, index) => {
const lineHeight = 30;
const y = 20 + index * lineHeight;
// Use the series stroke for legend text colour
const textColor = seriesInfo.stroke;
// Use the seriesInfo formattedX/YValue for text on the
outputSvgString += `<text x="8" y="${y}" font-size="16" font-family="Verdana" fill="${textColor}">
${seriesInfo.seriesName}: X=${seriesInfo.formattedXValue}, Y=${seriesInfo.formattedYValue}
</text>`;
});
return `<svg width="100%" height="100%">
${outputSvgString}
</svg>`;
};
This example demonstrates how to integrate SciChart.js into a JavaScript application to create high-performance, interactive charts. The implementation utilizes WebAssembly for efficient rendering as explained in the Performance Tips & Tricks documentation. Key features include the configuration of numeric axes, multiple fast line series, and advanced cursor behavior with custom tooltip support.
The chart is asynchronously initialized by creating a SciChartSurface along with X and Y axes configured through NumericAxis and NumberRange, ensuring precise label formatting (see The Numeric Axis for more details). Multiple FastLineRenderableSeries are added with corresponding XyDataSeries and EllipsePointMarker to effectively display data. The example adds several interactive chart modifiers including CursorModifier for crosshair behavior, ZoomPanModifier, ZoomExtentsModifier, and MouseWheelZoomModifier for intuitive zooming and panning. Custom tooltips are implemented by overriding the default behavior using an SVG template, as detailed in Customizing the CursorModifier Tooltip Container Appearance.
This implementation provides dynamic updates with interactive zooming, panning, and detailed tooltips that display a legend for the underlying series. Advanced customization is achieved by dynamically generating SVG annotations for the tooltip, allowing a clear display of series names and formatted data values. Performance optimizations and resource management strategies using WebAssembly are employed to ensure smooth interactions.
The example follows best practices for asynchronous initialization and resource cleanup. A cleanup function is returned after the chart is created, and calling sciChartSurface.delete() ensures that resources are properly disposed of when the chart is no longer needed, thus preventing memory leaks. Developers can refer to the Getting Started with SciChart JS guide for further understanding of initializing and managing chart resources. Additionally, the axis configuration and tooltip customization patterns leveraged in this example align with the recommended approaches discussed in the SciChart documentation.
By using JavaScript, this example clearly illustrates how to build an interactive, high-performance chart with advanced visual customizations and robust user interaction capabilities.






