Skip to content

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 ​

FieldHolds
typetype of the chart: xy, pie, scatter…
seriesseries or group of the mark, when the chart has several
stackstack of a bar, on an xy chart with stack on its series
segmentlabel of the segment of a line the mark is on (segments)
labelcategory, name or label of the mark
valueits number, when it has one
indexplace of the mark in its series (or among the categories); -1 when the chart does not say
itemthe item of the spec it comes from: a slice, a point, a category
colorits colour
title, rowswhat the tooltip writes by default
eventthe 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 ​

ChartA mark is
xy lines and areasa category: series and value are those of the series nearest to the pointer
xy barsa category, or the bar itself from the keyboard
pie, treemap, pack, funnel, waterfalla slice, a rectangle, a circle, a step: item is the entry of data
scattera point (also when painted on a canvas): item is the point, series its group
radaran indicator: series is the series nearest to the pointer
heatmapa cell: series is the row, label the column
other chartsthe 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 a keydown event. See accessibility.
  • A tap opens the tooltip and calls onHover, then onClick.
  • 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));