Skip to content
OpenChart

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.

20192020202120222023202402468Inflation (%)US inflation peaked at 8.5% in the thirdquarter of 2022US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)20192020202120222023202402468Inflation (%)US inflation peaked at 8.5% in the thirdquarter of 2022US consumer prices, change from a year earlier, quarterly, 2019 to 2024 (%)
Hover over the chart, or tab to it and use the arrow keys, to read each quarter. Source: US Bureau of Labor Statistics, CPI-U (via FRED CPIAUCSL).
{
  "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

20162017201820192020202120222023010203040Share of generation (%)CoalGasNuclearHydroWindSolarGas made 43.5% of generation from the six mainsources in 2023, more than any otherShare of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)20162017201820192020202120222023010203040Share of generation (%)CoalGasNuclearHydroWindSolarGas made 43.5% of generation from the six mainsources in 2023, more than any otherShare of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)
Click a source in the legend to hide or show its line. Source: Our World in Data (owid/electricity-mix).
{
  "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

1900192019401960198020002020−0.6−0.4−0.2+0.0+0.2+0.4+0.6+0.8+1.0+1.2Difference from 1951–1980 average (°C)1951–1980 average+1.19°CCoolerWarmerEvery reading since 1970 is above the1951–1980 averageGlobal surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)1900192019401960198020002020−0.6−0.4−0.2+0.0+0.2+0.4+0.6+0.8+1.0+1.2Difference from 1951–1980 average (°C)1951–1980 average+1.19°CCoolerWarmerEvery reading since 1970 is above the1951–1980 averageGlobal surface temperature, difference from the 1951–1980 average, 1900 to 2025 (°C)
Click the title, the subtitle, the note or the average line to select it. Source: NASA GISS Surface Temperature Analysis (GISTEMP v4).
{
  "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:

chart.ts
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.
  • 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.