Skip to main content
100%

Tooltip demo

✓ Published0🌍 Public
Aalex-r-bigelow
Last edited May 20, 2019
Created on May 20, 2019

This demo showcases a reusable tooltip function for D3 v5 that positions an HTML tooltip relative to a target element without occluding it. The tooltip accepts either static HTML strings or JavaScript functions to populate its content, with customizable anchoring and automatic clamping to keep it within the viewport. Two hoverable divs demonstrate the tooltip in action, while the underlying `showTooltip` and `hideTooltip` functions handle positioning, click-outside dismissal, and timed hiding via `window.setTimeout`. The tooltip div is styled with CSS and positioned absolutely using the `getBoundingClientRect` API.

AI-generated description

A semi-smart function for displaying tooltips with D3.js

Functions

<dl> <dt><a href="#showTooltip">showTooltip([parameters])</a></dt> <dd><p>Shows a tooltip; assumes the existence of a div with <code>id=&quot;tooltip&quot;</code>, that is positioned absolutely</p> </dd> <dt><a href="#hideTooltip">hideTooltip()</a></dt> <dd><p>Shorthand for hiding the tooltip</p> </dd> </dl>

<a name="showTooltip"></a>

showTooltip([parameters])

Shows a tooltip; assumes the existence of a div with id="tooltip", that is positioned absolutely

Kind: global function

Param Type Default Description
[parameters] <code>Object</code> <code>{}</code> Parameter object
[parameters.content] <code>String</code> <code>''</code> The message that will be displayed. Can be an empty string (hides the tooltip), a non-empty HTML string, or a function (which is called with the d3-selected tooltip div as the first argument)
[parameters.targetBounds] <code>Object</code> <code></code> Specifies a bounding box that the tooltip should NOT occlude (typically, the results of a getBoundingClientRect() call)
[parameters.anchor] <code>Object</code> <code></code> Specifies -1 to 1 positioning of the tooltip relative to targetBounds; for example, { x: -1 } right-aligns the tooltip to the left edge of targetBounds, { x: 0 } centers the tooltip horizontally, and { x: 1 } left-aligns the tooltip to the right edge of targetBounds
[parameters.hideAfterMs] <code>Number</code> <code>1000</code> Hides the tooltip after a certain delay; set this to zero to disable hiding

<a name="hideTooltip"></a>

hideTooltip()

Shorthand for hiding the tooltip

Kind: global function

mit Licensed

Similar vizzes