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 React application using the <SciChartReact/> component. It sets up a chart with X and Y numeric axes, renders multiple line series, and implements crosshair/cursor functionality along with customized tooltips. The example highlights how to leverage high-performance WebAssembly (WASM) rendering within a React environment.
The chart is created asynchronously through the drawExample function, which initializes a SciChartSurface with a chosen theme and adds NumericAxis configured with precise label formats. Three line series are added with distinct styling and point markers. The key feature is the use of the CursorModifier to provide crosshair behavior along with a custom tooltip legend, overridden via a dedicated function getTooltipLegendTemplate to display formatted series data. In addition, zoom and pan modifiers (ZoomPanModifier, ZoomExtentsModifier, and MouseWheelZoomModifier) are incorporated to enhance user interaction. This asynchronous pattern and separation of chart drawing logic from React components follows best practices for React integration and is detailed further in Creating a SciChart React Component from the Ground Up.
The example emphasizes advanced tooltip customization through the CursorModifier, enabling dynamic display of series information as the user moves the mouse over the chart. In addition, it demonstrates advanced zooming and panning controls for detailed data inspection, which can be explored further in Advanced Zoom and Pan with SciChart.js. The use of a custom tooltip legend template allows for a clearly formatted, data-driven tooltip display.
Developers can see how the <SciChartReact/> component is used to seamlessly integrate high performance charts into a React application, maintaining separation of concerns by offloading chart rendering to a dedicated asynchronous function. The example emphasizes performance optimization through the use of a WebAssembly (WASM) context, as outlined in Deploying Wasm and Data Files with your app. Custom tooltips and legends are implemented by leveraging the CursorModifier, with complete API details available in the CursorModifier documentation.






