Events
onClick and onHover tell you which mark the reader points at: a bar, a slice, a point, a cell, a category of a line.
ts
import { createChart } from 'nervure';
createChart(host, spec, {
onClick: (p) => openDetails(p.series, p.label, p.value),
onHover: (p) => (status.textContent = p ? `${p.label}: ${p.value}` : ''),
});What you receive
| Field | Holds |
|---|---|
type | type of the chart: xy, pie, scatter… |
series | series or group of the mark, when the chart has several |
stack | stack of a bar, on an xy chart with stack on its series |
segment | label of the segment of a line the mark is on (segments) |
label | category, name or label of the mark |
value | its number, when it has one |
index | place of the mark in its series (or among the categories); -1 when the chart does not say |
item | the item of the spec it comes from: a slice, a point, a category |
color | its colour |
title, rows | what the tooltip writes by default |
event | the DOM event behind it: click, pointermove, keydown |
onHover is called once each time the pointer reaches another mark, and with null when it leaves the marks.
By chart
| Chart | A mark is |
|---|---|
xy lines and areas | a category: series and value are those of the series nearest to the pointer |
xy bars | a category, or the bar itself from the keyboard |
pie, treemap, pack, funnel, waterfall | a slice, a rectangle, a circle, a step: item is the entry of data |
scatter | a point (also when painted on a canvas): item is the point, series its group |
radar | an indicator: series is the series nearest to the pointer |
heatmap | a cell: series is the row, label the column |
| other charts | the shape under the pointer, with its tooltip title as label |
Keyboard and touch
- Enter (or Space) on a mark reached with the arrow keys calls
onClick, with akeydownevent. See accessibility. - A tap opens the tooltip and calls
onHover, thenonClick. - A click on the legend or on the zoom buttons is not a click on a mark.
The events add to what the specs already have (onItemClick, onPointClick): both are called.
In frameworks
tsx
// React: the latest functions are called, no need to memoise them
<Chart spec={spec} onClick={(p) => setSelected(p.label)} onHover={(p) => setHovered(p?.label ?? null)} />vue
<!-- Vue -->
<NervureChart :spec="spec" @click="(p) => (selected = p.label)" @hover="(p) => (hovered = p?.label)" />svelte
<!-- Svelte -->
<div use:chart={{ spec, onClick: (p) => (selected = p.label) }} style="height: 300px"></div>js
// the element: point-click and point-hover, the point in event.detail
document.querySelector('nervure-chart').addEventListener('point-click', (e) => console.log(e.detail.label));