liveBook · Manning

D3.pointer()

As explained in D3.js in Action, Third Edition

d3.pointer() is a D3-selection function that returns pointer coordinates relative to a specified target for a DOM event.

d3.pointer() returns an event’s horizontal and vertical coordinates relative to a target. It belongs to D3’s d3-selection module and is useful for positioning visualization elements from mouse interaction. section 7.3.2 section 7.5

Use the returned coordinate array to connect pointer movement with a chart:

  • Attach a mousemove listener to the relevant SVG paths. section 7.3.2
  • Read the horizontal coordinate with d3.pointer(e)[0]. section 7.3.2

Following the pointer with a tooltip

d3.selectAll(".areas-container path").on("mousemove", e => { const xPosition = d3.pointer(e)[0]; d3.select(".tooltip").attr("transform", `translate(${xPosition}, 0)`); });

How D3.js in Action, Third Edition covers it

How deep it goes
Defines the returned coordinate array and demonstrates horizontal extraction, but does not discuss internal coordinate calculations.
Examples
Demonstrates a mouse-following tooltip on a streamgraph of music sales.
What it assumes
Assumes familiarity with DOM events, D3 selections, SVG elements, and continuous scales.
What you can do after
Apply d3.pointer() in D3 event handlers to move tooltips and derive data positions from pointer movement.
How it connects
Connects DOM events, D3 selections, tooltips, SVG positioning, and continuous scales.

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.