liveBook · Manning

Tooltip

As explained in D3.js in Action, Third Edition

A tooltip is a transient annotation that appears in response to interaction to reveal information associated with a visual element without permanently occupying chart space.

A tooltip reveals information when someone interacts with a chart, without leaving that annotation on screen all the time. Use one when a mark’s exact value or identity matters but permanent labels would overcrowd the visualization. A tooltip can sit above a mark, follow the mouse, or appear in a panel beside the chart. section 7.3 section 11.2.3

For a simple SVG tooltip on a line chart:

  • Append a group to the inner chart. Put a semitransparent rectangle and a text element inside it, and start with the group’s opacity at 0. section 7.3.1
  • Listen for mouseenter on each data-point circle. Format its attached temperature value, place the group above the circle using its cx and cy attributes, then transition its opacity to 1. section 7.3.1
  • On mouseleave, set opacity back to 0 and move the group away. If data updates add new circles, attach listeners to those new elements too. section 7.3.1 section 7.4.3
Figure 7.9 To create a tooltip, we first append a group element to the inner chart. In this group, we add a rectangle for the tooltip’s background and a text element. We set the opacity of the tooltip to 0. Then we attach two mouse events to the chart’s circles: "mouseenter" and "mouseleave". When the mouse enters a circle, we set the tooltip’s text, translate it above the circle, and set its opacity to 100%. When the mouse leaves, we set the opacity to 0 and move the tooltip away from the chart.
figure

The diagram traces the simple tooltip’s lifecycle: create its SVG elements, keep them hidden, then respond to mouse entry and exit. section 7.3.1

A tooltip that follows a streamgraph

A streamgraph’s stacked areas make precise sales values difficult to read. Build a group containing a dashed vertical guide, a year label, and one sales line per music format. On mousemove over an area, use d3.pointer() to obtain the horizontal position, translate the group, invert the x-scale and round to find the year, then update each sales line from that year’s data. Keeping the tooltip’s format order aligned with the areas’ stacking order helps readers connect labels to shapes. section 7.3.2

Not every tooltip belongs over its chart. A circle pack can put an HTML information panel to the side when a hovered language node needs several fields: language, branch, family, and speaker count. A choropleth can instead use an absolutely positioned HTML tooltip to give a country name and exact count that its color alone cannot convey. Choose the contents by asking what the marks leave unanswered. section 11.2.3 section 13.2.3 section 14.6

How D3.js in Action, Third Edition covers it

How deep it goes
Works through SVG construction, mouse listeners, positioning, transitions, and data-driven updates; also demonstrates HTML placements.
Examples
Uses a weather line chart, music-sales streamgraph, language circle pack, and Nobel-laureate choropleth.
What it assumes
Assumes existing charts and D3 selections, while teaching tooltip construction and event handling step by step.
What you can do after
Build it from scratch with SVG marks and D3 events, or operate an HTML information panel beside a chart.
How it connects
Connects tooltips to D3 selections, mouse events, transitions, SVG, streamgraphs, and choropleth maps.

Also explained in

Compare how 2 books explain tooltip →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.