Annotation Hover
Annotations Hover API is an opt-in functionality that allows detecting hover events on chart annotations and adding event handlers.
tip
The JavaScript Hoverable Trade Markers Example can be found in the SciChart.Js Examples Suite on Github, or our live demo at scichart.com/demo.
Above: The JavaScript Annotation Hover Example example from the SciChart.js Demo
Using AnnotationHoverModifier
To enable the hover detection we need to add the AnnotationHoverModifier📘.
- TS
- Builder API (JSON Config)
// Add an annotation with hover behaviour
const boxAnnotation = new BoxAnnotation({
xCoordinateMode: ECoordinateMode.Relative,
yCoordinateMode: ECoordinateMode.Relative,
fill: "#3d34eb",
strokeThickness: 1,
x1: 0.1,
x2: 0.4,
y1: 0.4,
y2: 0.6,
onHover: args => {
const { sender, mouseArgs, isHovered } = args;
if (mouseArgs && isHovered) {
const relativeCoordinates = args.getRelativeCoordinates();
console.log("The annotation is hovered at", relativeCoordinates);
}
}
});
sciChartSurface.annotations.add(boxAnnotation);
// Add AnnotationHoverModifier to enable hover behaviour
const annotationHoverModifier = new AnnotationHoverModifier({
enableHover: true,
targets: [boxAnnotation],
hoverMode: EHoverMode.AbsoluteTopmost,
notifyOutEvent: true,
notifyPositionUpdate: true,
onHover: args => {
const { mouseArgs, includedEntities, hoveredEntities, unhoveredEntities } = args;
const hoveredAnnotations = /** @type {BoxAnnotation[]} */ hoveredEntities;
const unhoveredAnnotations = /** @type {BoxAnnotation[]} */ unhoveredEntities;
hoveredAnnotations.forEach(a => {
const annotation = a as BoxAnnotation;
annotation.fill = "#34eb8c";
annotation.strokeThickness = 3;
});
unhoveredAnnotations.forEach(a => {
const annotation = a as BoxAnnotation;
annotation.fill = "#3d34eb";
annotation.strokeThickness = 1;
});
}
});
sciChartSurface.chartModifiers.add(annotationHoverModifier);
const { wasmContext, sciChartSurface } = await build2DChart(divElementId, {
surface: {
theme: new SciChartJsNavyTheme()
},
// Add an annotation with hover behaviour
annotations: [
{
type: EAnnotationType.RenderContextBoxAnnotation,
options: {
id: "boxAnnotation",
xCoordinateMode: ECoordinateMode.Relative,
yCoordinateMode: ECoordinateMode.Relative,
fill: "#3d34eb",
strokeThickness: 1,
x1: 0.1,
x2: 0.4,
y1: 0.4,
y2: 0.6,
onHover: args => {
const { sender, mouseArgs, isHovered } = args;
if (mouseArgs && isHovered) {
const relativeCoordinates = args.getRelativeCoordinates();
console.log("The annotation is hovered at", relativeCoordinates);
}
}
}
}
],
// Add AnnotationHoverModifier to enable hover behaviour
modifiers: [
{
type: EChart2DModifierType.AnnotationHover,
options: {
enableHover: true,
targets: ["boxAnnotation"],
hoverMode: EHoverMode.AbsoluteTopmost,
notifyOutEvent: true,
notifyPositionUpdate: true,
onHover: args => {
const { mouseArgs, includedEntities, hoveredEntities, unhoveredEntities } = args;
const hoveredAnnotations = hoveredEntities;
const unhoveredAnnotations = unhoveredEntities;
hoveredAnnotations.forEach(annotation => {
annotation.fill = "#34eb8c";
annotation.strokeThickness = 3;
});
unhoveredAnnotations.forEach(annotation => {
annotation.fill = "#3d34eb";
annotation.strokeThickness = 1;
});
}
}
}
]
});
Adding Hover Event Handlers
There are several different options for adding a callback for the hover event.
- By passing onHover