Interaction and events
Tooltips, crosshairs, legend toggles and selection, and the events a chart emits as a reader uses it.
Tooltips, keyboard navigation and legend toggles work with no code. Event handlers tell your page what the reader did, and the interaction mount option decides whether chart elements can be selected or edited.
{
"mark": {
"type": "line",
"strokeWidth": 2
},
"data": [
{
"date": "2019-01-01",
"rate": 1.6
},
{
"date": "2019-04-01",
"rate": 2
},
{
"date": "2019-07-01",
"rate": 1.8
},
{
"date": "2019-10-01",
"rate": 1.8
},
{
"date": "2020-01-01",
"rate": 2.5
},
{
"date": "2020-04-01",
"rate": 0.3
},
// … 18 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"axis": {
"title": "Inflation (%)",
"format": ".0f",
"grid": true
}
}
},
"crosshair": true,
"chrome": {
"title": "US inflation peaked at 8.5% in the third quarter of 2022",
"subtitle": "US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"axis": {
"title": "Inflation (%)",
"format": ".0f",
"grid": true
}
}
},
"crosshair": true,
"chrome": {
"title": "US inflation peaked at 8.5% in the third quarter of 2022",
"subtitle": "US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"axis": {
"title": "Inflation (%)",
"format": ".0f",
"grid": true
}
}
},
"crosshair": true,
"chrome": {
"title": "US inflation peaked at 8.5% in the third quarter of 2022",
"subtitle": "US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"axis": {
"title": "Inflation (%)",
"format": ".0f",
"grid": true
}
}
},
"crosshair": true,
"chrome": {
"title": "US inflation peaked at 8.5% in the third quarter of 2022",
"subtitle": "US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"axis": {
"title": "Inflation (%)",
"format": ".0f",
"grid": true
}
}
},
"crosshair": true,
"chrome": {
"title": "US inflation peaked at 8.5% in the third quarter of 2022",
"subtitle": "US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
crosshair: true draws a vertical rule at the nearest quarter as the pointer moves along a line or area chart, and the tooltip gives that quarter’s values. Keyboard readers can tab to the chart and step from quarter to quarter with the left and right arrow keys. encoding.tooltip picks which fields the tooltip lists, and mark.tooltip: null turns tooltips off.
Legend toggles
{
"mark": {
"type": "line",
"strokeWidth": 2
},
"data": [
{
"year": "2016",
"source": "Coal",
"share": 31.2
},
{
"year": "2016",
"source": "Gas",
"share": 34.8
},
{
"year": "2016",
"source": "Nuclear",
"share": 20.3
},
{
"year": "2016",
"source": "Hydro",
"share": 6.6
},
{
"year": "2016",
"source": "Wind",
"share": 5.7
},
{
"year": "2016",
"source": "Solar",
"share": 1.4
},
// … 42 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of generation (%)",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal"
}
},
"endpointLabels": false,
"legend": {
"position": "top"
},
"chrome": {
"title": "Gas made 43.5% of generation from the six main sources in 2023, more than any other",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 48 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of generation (%)",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal"
}
},
"endpointLabels": false,
"legend": {
"position": "top"
},
"chrome": {
"title": "Gas made 43.5% of generation from the six main sources in 2023, more than any other",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 48 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of generation (%)",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal"
}
},
"endpointLabels": false,
"legend": {
"position": "top"
},
"chrome": {
"title": "Gas made 43.5% of generation from the six main sources in 2023, more than any other",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 48 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of generation (%)",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal"
}
},
"endpointLabels": false,
"legend": {
"position": "top"
},
"chrome": {
"title": "Gas made 43.5% of generation from the six main sources in 2023, more than any other",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 48 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of generation (%)",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal"
}
},
"endpointLabels": false,
"legend": {
"position": "top"
},
"chrome": {
"title": "Gas made 43.5% of generation from the six main sources in 2023, more than any other",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Clicking a legend entry hides that series and redraws the chart. The y axis rescales to the series still showing, every series keeps its color, and the last visible series can’t be hidden. onLegendToggle receives the series name and whether it is now visible, and the event log under the chart lists each call. A toggle never produces an edit transaction; the spec stays as it was.
Selection
{
"mark": "bar",
"data": [
{
"year": "1900",
"anomaly": -0.08,
"trend": "Cooler"
},
{
"year": "1910",
"anomaly": -0.43,
"trend": "Cooler"
},
{
"year": "1920",
"anomaly": -0.27,
"trend": "Cooler"
},
{
"year": "1930",
"anomaly": -0.16,
"trend": "Cooler"
},
{
"year": "1940",
"anomaly": 0.12,
"trend": "Warmer"
},
{
"year": "1950",
"anomaly": -0.17,
"trend": "Cooler"
},
// … 8 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from 1951–1980 average (°C)",
"format": "+.1f"
}
},
"color": {
"field": "trend",
"type": "nominal",
"scale": {
"domain": [
"Cooler",
"Warmer"
],
"range": [
"#3b82c4",
"#d1495b"
]
}
}
},
"labels": {
"density": "none"
},
"annotations": [
{
"type": "refline",
"y": 0,
"label": "1951–1980 average",
"style": "solid",
"strokeWidth": 1
},
{
"type": "text",
"x": "2025",
"y": 1.19,
"text": "+1.19°C",
"anchor": "top",
"offset": {
"dx": 0,
"dy": -8
}
}
],
"chrome": {
"title": "Every reading since 1970 is above the 1951–1980 average",
"subtitle": "Global surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 14 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from 1951–1980 average (°C)",
"format": "+.1f"
}
},
"color": {
"field": "trend",
"type": "nominal",
"scale": {
"domain": [
"Cooler",
"Warmer"
],
"range": [
"#3b82c4",
"#d1495b"
]
}
}
},
"labels": {
"density": "none"
},
"annotations": [
{
"type": "refline",
"y": 0,
"label": "1951–1980 average",
"style": "solid",
"strokeWidth": 1
},
{
"type": "text",
"x": "2025",
"y": 1.19,
"text": "+1.19°C",
"anchor": "top",
"offset": {
"dx": 0,
"dy": -8
}
}
],
"chrome": {
"title": "Every reading since 1970 is above the 1951–1980 average",
"subtitle": "Global surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)"
}
};
export function Example() {
return <Chart spec={spec} interaction="select" />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 14 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from 1951–1980 average (°C)",
"format": "+.1f"
}
},
"color": {
"field": "trend",
"type": "nominal",
"scale": {
"domain": [
"Cooler",
"Warmer"
],
"range": [
"#3b82c4",
"#d1495b"
]
}
}
},
"labels": {
"density": "none"
},
"annotations": [
{
"type": "refline",
"y": 0,
"label": "1951–1980 average",
"style": "solid",
"strokeWidth": 1
},
{
"type": "text",
"x": "2025",
"y": 1.19,
"text": "+1.19°C",
"anchor": "top",
"offset": {
"dx": 0,
"dy": -8
}
}
],
"chrome": {
"title": "Every reading since 1970 is above the 1951–1980 average",
"subtitle": "Global surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)"
}
};
</script>
<template>
<Chart :spec="spec" interaction="select" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 14 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from 1951–1980 average (°C)",
"format": "+.1f"
}
},
"color": {
"field": "trend",
"type": "nominal",
"scale": {
"domain": [
"Cooler",
"Warmer"
],
"range": [
"#3b82c4",
"#d1495b"
]
}
}
},
"labels": {
"density": "none"
},
"annotations": [
{
"type": "refline",
"y": 0,
"label": "1951–1980 average",
"style": "solid",
"strokeWidth": 1
},
{
"type": "text",
"x": "2025",
"y": 1.19,
"text": "+1.19°C",
"anchor": "top",
"offset": {
"dx": 0,
"dy": -8
}
}
],
"chrome": {
"title": "Every reading since 1970 is above the 1951–1980 average",
"subtitle": "Global surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)"
}
};
</script>
<Chart {spec} interaction="select" />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 14 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": "bar",
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from 1951–1980 average (°C)",
"format": "+.1f"
}
},
"color": {
"field": "trend",
"type": "nominal",
"scale": {
"domain": [
"Cooler",
"Warmer"
],
"range": [
"#3b82c4",
"#d1495b"
]
}
}
},
"labels": {
"density": "none"
},
"annotations": [
{
"type": "refline",
"y": 0,
"label": "1951–1980 average",
"style": "solid",
"strokeWidth": 1
},
{
"type": "text",
"x": "2025",
"y": 1.19,
"text": "+1.19°C",
"anchor": "top",
"offset": {
"dx": 0,
"dy": -8
}
}
],
"chrome": {
"title": "Every reading since 1970 is above the 1951–1980 average",
"subtitle": "Global surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec, { interaction: "select" });
With interaction: "select", a click or the Tab key selects the title, the subtitle, an annotation or the legend. onSelect receives a reference to the element whose path is a JSON Pointer into the spec, such as /annotations/1 for the note on the last column. onDeselect fires when Escape, a click on empty space or another selection clears it. Select mode keeps the arrow keys for reading the data; edit mode adds dragging and text editing.
Handlers and the interaction mode
Pass handlers as mount options in plain JavaScript, or as props on the React, Vue and Svelte components:
import { createChart } from "@opendata-ai/openchart-vanilla";
createChart(container, spec, {
interaction: "select",
onMarkClick: ({ datum }) => console.log(datum),
onLegendToggle: (series, visible) => console.log(series, visible),
onSelect: (element) => console.log(element.path),
});Leave interaction out and the library infers it once, at mount: an onEdit handler means edit mode, onSelect or onDeselect means select mode, and anything else means read mode. Set it explicitly when the handlers you attach should not decide it. This page’s event log attaches onSelect to every chart it logs, so the page mounts the legend demo with interaction: "read" to keep selection off.
Fields that matter
| Field | What it does |
|---|---|
crosshair |
Draws a vertical rule at the nearest point on line and area charts. Off by default. |
encoding.tooltip |
One field or a list of fields for the tooltip, independent of the other channels. |
mark.tooltip |
null turns tooltips off. |
legend |
show: false hides the legend; position moves it. |
interaction |
Mount option: "read", "select" or "edit". |
onMarkClick, onMarkHover, onMarkFocus |
A data mark was clicked, hovered or reached with the keyboard, with its data row. |
onLegendToggle |
A series was hidden or shown. |
onSelect, onDeselect |
An element was selected or cleared, with its path in the spec. |
Related
- Events reference lists every handler, its payload and its name in each framework.
- Line covers line charts and their annotations.
- Edit mode turns selection into editing.