Demonstrates how to use tooltips at fixed positions using SciChart.js, High Performance JavaScript Charts
import { appTheme } from "./appTheme";
import { ExampleDataProvider } from "./ExampleDataProvider";
import {
EllipsePointMarker,
ENumericFormat,
FastLineRenderableSeries,
MouseWheelZoomModifier,
NumberRange,
NumericAxis,
RolloverLegendSvgAnnotation,
VerticalSliceModifier,
SciChartSurface,
XyDataSeries,
ZoomExtentsModifier,
ZoomPanModifier,
SeriesInfo,
ECoordinateMode,
NativeTextAnnotation,
EHorizontalAnchorPoint,
} 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);
const lineSeries0 = 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,
}),
});
sciChartSurface.renderableSeries.add(lineSeries0);
const data2 = ExampleDataProvider.getFourierSeriesZoomed(0.5, 0.12, 5.0, 5.15);
const lineSeries1 = 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,
}),
});
sciChartSurface.renderableSeries.add(lineSeries1);
const data3 = ExampleDataProvider.getFourierSeriesZoomed(0.4, 0.11, 5.0, 5.15);
const lineSeries2 = 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,
}),
});
sciChartSurface.renderableSeries.add(lineSeries2);
// Here is where we add rollover tooltip behaviour
//
const vSlice1 = new VerticalSliceModifier({
x1: 5.06,
xCoordinateMode: ECoordinateMode.DataValue,
isDraggable: true,
// Defines if rollover vertical line is shown
showRolloverLine: true,
rolloverLineStrokeThickness: 1,
rolloverLineStroke: appTheme.VividGreen,
lineSelectionColor: appTheme.VividGreen,
// Shows the default tooltip
showTooltip: true,
// Optional: Overrides the legend template to display additional info top-left of the chart
tooltipLegendTemplate: getTooltipLegendTemplate,
// Optional: Overrides the content of the tooltip
tooltipDataTemplate: getTooltipDataTemplate,
});
const vSlice2 = new VerticalSliceModifier({
x1: 0.75,
xCoordinateMode: ECoordinateMode.Relative,
isDraggable: true,
// Defines if rollover vertical line is shown
showRolloverLine: true,
rolloverLineStrokeThickness: 1,
rolloverLineStroke: appTheme.VividOrange,
lineSelectionColor: appTheme.VividOrange,
// Shows the default tooltip
showTooltip: true,
// Optional: Overrides the content of the tooltip
tooltipDataTemplate: getTooltipDataTemplate,
});
sciChartSurface.chartModifiers.add(vSlice1, vSlice2);
// Optional: Additional customisation may be done per-series, e.g.
//
lineSeries0.rolloverModifierProps.tooltipTextColor = appTheme.DarkIndigo;
lineSeries2.rolloverModifierProps.tooltipColor = appTheme.VividPink;
const textAnn1 = new NativeTextAnnotation({
text: "xCoordinateMode: DataValue\nMoves with data\nLinked to Legend\nDraggable",
textColor: appTheme.ForegroundColor,
y1: 0.88,
xCoordinateMode: ECoordinateMode.Pixel,
yCoordinateMode: ECoordinateMode.Relative,
horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
});
// Link the annotation position with the verticalSlice
sciChartSurface.layoutMeasured.subscribe((data) => {
textAnn1.x1 = vSlice1.verticalLine.x1;
});
sciChartSurface.annotations.add(textAnn1);
const textAnn2 = new NativeTextAnnotation({
text: "xCoordinateMode: Relative\nFixed position\nDraggable ",
textColor: appTheme.ForegroundColor,
y1: 0.88,
xCoordinateMode: ECoordinateMode.Pixel,
yCoordinateMode: ECoordinateMode.Relative,
horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
});
sciChartSurface.layoutMeasured.subscribe((data) => {
textAnn2.x1 = vSlice2.verticalLine.x1;
});
sciChartSurface.annotations.add(textAnn2);
// Add further zooming and panning behaviours
sciChartSurface.chartModifiers.add(new ZoomPanModifier({ enableZoom: true }));
sciChartSurface.chartModifiers.add(new ZoomExtentsModifier());
sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier());
sciChartSurface.zoomExtents();
return { sciChartSurface, wasmContext };
};
const getTooltipDataTemplate = (
seriesInfo: SeriesInfo,
tooltipTitle: string,
tooltipLabelX: string,
tooltipLabelY: string
) => {
// Lines here are returned to the tooltip and displayed as text-line per tooltip
const lines: string[] = [];
lines.push(tooltipTitle);
lines.push(`x: ${seriesInfo.formattedXValue}`);
lines.push(`y: ${seriesInfo.formattedYValue}`);
return lines;
};
// Override the standard tooltip displayed by CursorModifier
const getTooltipLegendTemplate = (seriesInfos: SeriesInfo[], svgAnnotation: RolloverLegendSvgAnnotation) => {
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) => {
if (seriesInfo.isWithinDataBounds) {
const lineHeight = 30;
const y = 50 + 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>`;
}
});
// Content here is returned for the custom legend placed in top-left of the chart
return `<svg width="100%" height="100%">
<text x="8" y="20" font-size="17" font-weight="bold" font-family="Verdana" fill="${appTheme.ForegroundColor}">Custom Rollover Legend</text>
${outputSvgString}
</svg>`;
};
This example demonstrates how to integrate SciChart.js into a React application by utilizing the VerticalSliceModifier to implement custom tooltips and annotations at fixed positions. The example creates an interactive chart with multiple line series rendered using high performance WebGL via WebAssembly and illustrates how to add draggable vertical slices for advanced hit testing and dynamic data display.
The chart is integrated into React using the SciChart React package. The initialization function, passed as a prop to the <SciChartReact/> component, sets up the chart surface, configures NumericAxis with specific label formatting and precision, and adds several line series created with the FastLineRenderableSeries to ensure optimal rendering performance. The example leverages the VerticalSliceModifier to display tooltips at specified data and relative coordinates, utilizing the custom callback functions to format tooltip text. Furthermore, event subscriptions on the chart’s preRender event dynamically update the position of annotations linked to the vertical slices, following best practices outlined in the Annotations API Overview.
This example highlights several advanced features including real-time updates of tooltip information and dynamic annotation positioning. The VerticalSliceModifier allows for both data-bound (using DataValue coordinate mode) and fixed (using Relative coordinate mode) positions, with parameters that enable tooltip customization through custom tooltip templates. Additionally, the integration supports interactive features such as zooming and panning via modifiers like the ZoomPanModifier, enhancing the overall user experience with sophisticated data exploration capabilities.
The implementation follows best practices for React integration by encapsulating the SciChart initialization within a dedicated function that is passed to the <SciChartReact/> component. Developers are encouraged to use the provided SciChart React documentation for guidance on constructing reusable chart components. Performance optimizations are achieved with the FastLineRenderableSeries and efficient WebGL rendering underlined in the Performance Tips. Additionally, the use of vertical slices combined with dynamic event handling provides a robust solution for implementing interactive tooltip and annotation features in high performance charts.
For further reading on interactive modifiers, refer to the official SciChart.js VerticalSliceModifier Documentation.






