Demonstrates how to add Series Selection to a chart using SciChart.js, High Performance JavaScript Charts
import { appTheme } from "./appTheme";
import {
ECoordinateMode,
EHorizontalAnchorPoint,
IRenderableSeries,
NumericAxis,
NumberRange,
SciChartSurface,
SplineLineRenderableSeries,
TextAnnotation,
XyDataSeries,
SeriesSelectionModifier,
AUTO_COLOR,
LegendModifier,
GenericAnimation,
EllipsePointMarker,
} from "scichart";
// Generate some data for the example
const dataSize = 30;
const xValues: number[] = [];
const yValues: number[] = [];
const y1Values: number[] = [];
const y2Values: number[] = [];
const y3Values: number[] = [];
for (let i = 0; i < dataSize; i++) {
xValues.push(i);
y3Values.push(Math.random() + 1);
y2Values.push(Math.random() + 1.8);
y1Values.push(Math.random() + 2.5);
yValues.push(Math.random() + 3.6);
}
// Custom function called when series is hovered
const onHoveredChanged = (sourceSeries: IRenderableSeries, isHovered: boolean) => {
console.log(`Series ${sourceSeries.dataSeries.dataSeriesName} isHovered=${isHovered}`);
const targetSeriesOpacity = 1;
const otherSeriesOpacity = isHovered ? 0.3 : 1;
const sciChartSurface = sourceSeries.parentSurface;
const otherSeries = sciChartSurface.renderableSeries.asArray().filter((rs) => rs !== sourceSeries);
// Animate the hovered series and dim the remaining series.
sciChartSurface.addAnimation(
new GenericAnimation({
from: sourceSeries.opacity,
to: targetSeriesOpacity,
duration: 100,
onAnimate: (from, to, progress) => {
const opacity = (to - from) * progress + from;
sourceSeries.opacity = opacity;
sourceSeries.pointMarker.opacity = opacity;
},
})
);
// Dim opacity on the other non-hovered series
sciChartSurface.addAnimation(
new GenericAnimation({
from: otherSeries[0].opacity,
to: otherSeriesOpacity,
duration: 100,
onAnimate: (from, to, progress) => {
const opacity = (to - from) * progress + from;
otherSeries.forEach((rs) => {
rs.opacity = opacity;
rs.pointMarker.opacity = opacity;
});
},
})
);
};
// Custom function called when a series is selected or deselected
const onSelectedChanged = (sourceSeries: IRenderableSeries, isSelected: boolean) => {
console.log(`Series ${sourceSeries.dataSeries.dataSeriesName} isSelected=${isSelected}`);
// Highlight selection with the theme foreground; deselection restores the series color.
const targetSeriesStroke = isSelected ? appTheme.ForegroundColor : sourceSeries.pointMarker.fill;
sourceSeries.stroke = targetSeriesStroke;
sourceSeries.pointMarker.stroke = targetSeriesStroke;
};
export const drawExample = async (rootElement: string | HTMLDivElement) => {
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: appTheme.SciChartJsTheme,
});
sciChartSurface.xAxes.add(new NumericAxis(wasmContext));
sciChartSurface.yAxes.add(
new NumericAxis(wasmContext, {
growBy: new NumberRange(0.1, 0.1),
})
);
sciChartSurface.chartModifiers.add(
new SeriesSelectionModifier({
enableHover: true,
enableSelection: true,
})
);
for (const { values, dataSeriesName } of [
{ values: yValues, dataSeriesName: "First Series" },
{ values: y1Values, dataSeriesName: "Second Series" },
{ values: y2Values, dataSeriesName: "Third Series" },
{ values: y3Values, dataSeriesName: "Fourth Series" },
]) {
sciChartSurface.renderableSeries.add(
new SplineLineRenderableSeries(wasmContext, {
dataSeries: new XyDataSeries(wasmContext, { xValues, yValues: values, dataSeriesName }),
pointMarker: new EllipsePointMarker(wasmContext, {
fill: AUTO_COLOR,
stroke: AUTO_COLOR,
strokeThickness: 3,
width: 20,
height: 20,
}),
strokeThickness: 3,
onHoveredChanged,
onSelectedChanged,
})
);
}
// Add title annotation
sciChartSurface.annotations.add(
new TextAnnotation({
text: "Hover, Click & Select to animate style!",
fontSize: 20,
textColor: appTheme.ForegroundColor,
x1: 0.5,
y1: 0,
opacity: 0.77,
horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
xCoordinateMode: ECoordinateMode.Relative,
yCoordinateMode: ECoordinateMode.Relative,
})
);
// Add a legend to the chart
sciChartSurface.chartModifiers.add(new LegendModifier({ margin: 25 }));
return { sciChartSurface, wasmContext };
};
This example demonstrates how to implement interactive series selection and hover events in SciChart.js using JavaScript. Multiple renderable series are added to a chart and enhanced with custom event handlers to provide visual feedback when a series is hovered or selected. Developers can learn more about these capabilities by exploring the Series Selection documentation and the [SeriesSelectionModifier] TypeDoc API(https://www.scichart.com/documentation/js/v6/typedoc/classes/seriesselectionmodifier.html) reference.
The chart is initialized by calling SciChartSurface.create() with a WebAssembly context, and NumericAxis components are added to provide dynamic scaling with a customizable NumberRange. A SeriesSelectionModifier is added to the chart and onHoveredChanged and onSelectedChanged event handlers are implemented to adjust the opacity and stroke color of each series. In particular, the example uses GenericAnimation to smoothly transition visual properties when series are hovered, while other series are dimmed to emphasize the active one. Annotations such as TextAnnotation are added to display instructions and, together with a LegendModifier, help provide a legend which adds context to the data.
This example highlights several advanced features of SciChart.js including:
SeriesSelectionModifier, enabling both hover and click-based selection events.GenericAnimation, allowing dynamic changes in opacity and stroke color.LegendModifier for clear data series labeling.SciChartSurface, following best practices detailed in the Getting Started with SciChart JS guide.While this example is implemented using JavaScript, it demonstrates patterns that are easily transferable to frameworks like React, Angular, or Vue. Developers are encouraged to follow efficient performance practices by leveraging the WebAssembly context (wasmContext) for processing large datasets, as showcased in the SciChart.js Performance Demo.
This comprehensive example serves as a practical guide for adding interactivity and advanced visual customizations to SciChart.js charts using plain JavaScript while adhering to best practices for performance optimization and resource management.






