# 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.

```json
{
  "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
    },
    {
      "date": "2020-07-01",
      "rate": 1
    },
    {
      "date": "2020-10-01",
      "rate": 1.2
    },
    {
      "date": "2021-01-01",
      "rate": 1.4
    },
    {
      "date": "2021-04-01",
      "rate": 4.2
    },
    {
      "date": "2021-07-01",
      "rate": 5.4
    },
    {
      "date": "2021-10-01",
      "rate": 6.2
    },
    {
      "date": "2022-01-01",
      "rate": 7.5
    },
    {
      "date": "2022-04-01",
      "rate": 8.3
    },
    {
      "date": "2022-07-01",
      "rate": 8.5
    },
    {
      "date": "2022-10-01",
      "rate": 7.7
    },
    {
      "date": "2023-01-01",
      "rate": 6.4
    },
    {
      "date": "2023-04-01",
      "rate": 4.9
    },
    {
      "date": "2023-07-01",
      "rate": 3.2
    },
    {
      "date": "2023-10-01",
      "rate": 3.2
    },
    {
      "date": "2024-01-01",
      "rate": 3.1
    },
    {
      "date": "2024-04-01",
      "rate": 3.4
    },
    {
      "date": "2024-07-01",
      "rate": 2.9
    },
    {
      "date": "2024-10-01",
      "rate": 2.6
    }
  ],
  "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 (%)"
  }
}
```

`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

```json
{
  "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
    },
    {
      "year": "2017",
      "source": "Coal",
      "share": 30.7
    },
    {
      "year": "2017",
      "source": "Gas",
      "share": 33
    },
    {
      "year": "2017",
      "source": "Nuclear",
      "share": 20.5
    },
    {
      "year": "2017",
      "source": "Hydro",
      "share": 7.5
    },
    {
      "year": "2017",
      "source": "Wind",
      "share": 6.5
    },
    {
      "year": "2017",
      "source": "Solar",
      "share": 2
    },
    {
      "year": "2018",
      "source": "Coal",
      "share": 28.2
    },
    {
      "year": "2018",
      "source": "Gas",
      "share": 36
    },
    {
      "year": "2018",
      "source": "Nuclear",
      "share": 19.8
    },
    {
      "year": "2018",
      "source": "Hydro",
      "share": 7
    },
    {
      "year": "2018",
      "source": "Wind",
      "share": 6.7
    },
    {
      "year": "2018",
      "source": "Solar",
      "share": 2.3
    },
    {
      "year": "2019",
      "source": "Coal",
      "share": 23.9
    },
    {
      "year": "2019",
      "source": "Gas",
      "share": 39.2
    },
    {
      "year": "2019",
      "source": "Nuclear",
      "share": 20
    },
    {
      "year": "2019",
      "source": "Hydro",
      "share": 7
    },
    {
      "year": "2019",
      "source": "Wind",
      "share": 7.3
    },
    {
      "year": "2019",
      "source": "Solar",
      "share": 2.6
    },
    {
      "year": "2020",
      "source": "Coal",
      "share": 19.6
    },
    {
      "year": "2020",
      "source": "Gas",
      "share": 41.3
    },
    {
      "year": "2020",
      "source": "Nuclear",
      "share": 20.1
    },
    {
      "year": "2020",
      "source": "Hydro",
      "share": 7.1
    },
    {
      "year": "2020",
      "source": "Wind",
      "share": 8.6
    },
    {
      "year": "2020",
      "source": "Solar",
      "share": 3.3
    },
    {
      "year": "2021",
      "source": "Coal",
      "share": 22.2
    },
    {
      "year": "2021",
      "source": "Gas",
      "share": 39
    },
    {
      "year": "2021",
      "source": "Nuclear",
      "share": 19.3
    },
    {
      "year": "2021",
      "source": "Hydro",
      "share": 6.1
    },
    {
      "year": "2021",
      "source": "Wind",
      "share": 9.3
    },
    {
      "year": "2021",
      "source": "Solar",
      "share": 4.1
    },
    {
      "year": "2022",
      "source": "Coal",
      "share": 19.9
    },
    {
      "year": "2022",
      "source": "Gas",
      "share": 40.4
    },
    {
      "year": "2022",
      "source": "Nuclear",
      "share": 18.5
    },
    {
      "year": "2022",
      "source": "Hydro",
      "share": 6
    },
    {
      "year": "2022",
      "source": "Wind",
      "share": 10.4
    },
    {
      "year": "2022",
      "source": "Solar",
      "share": 4.9
    },
    {
      "year": "2023",
      "source": "Coal",
      "share": 16.2
    },
    {
      "year": "2023",
      "source": "Gas",
      "share": 43.5
    },
    {
      "year": "2023",
      "source": "Nuclear",
      "share": 18.6
    },
    {
      "year": "2023",
      "source": "Hydro",
      "share": 5.8
    },
    {
      "year": "2023",
      "source": "Wind",
      "share": 10.1
    },
    {
      "year": "2023",
      "source": "Solar",
      "share": 5.8
    }
  ],
  "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 (%)"
  }
}
```

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

```json
{
  "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"
    },
    {
      "year": "1960",
      "anomaly": -0.03,
      "trend": "Cooler"
    },
    {
      "year": "1970",
      "anomaly": 0.03,
      "trend": "Warmer"
    },
    {
      "year": "1980",
      "anomaly": 0.26,
      "trend": "Warmer"
    },
    {
      "year": "1990",
      "anomaly": 0.45,
      "trend": "Warmer"
    },
    {
      "year": "2000",
      "anomaly": 0.39,
      "trend": "Warmer"
    },
    {
      "year": "2010",
      "anomaly": 0.72,
      "trend": "Warmer"
    },
    {
      "year": "2020",
      "anomaly": 1.01,
      "trend": "Warmer"
    },
    {
      "year": "2025",
      "anomaly": 1.19,
      "trend": "Warmer"
    }
  ],
  "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)"
  }
}
```

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](/docs/behavior/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:

```ts title="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. |

## Related

- [Events reference](/docs/api/events/) lists every handler, its payload and its name in each framework.
- [Line](/docs/change-over-time/line/) covers line charts and their annotations.
- [Edit mode](/docs/behavior/edit-mode/) turns selection into editing.
