Skip to content

Tooltip

Creates HTML tooltips in the body of a webpage.

A tooltip is a small HTML panel positioned next to an element or a point. Give it data (one datum per tooltip), a position (a function returning an element, or [x, y] viewport coordinates), and call render(); render with empty data to hide it. By default the title, body, and footer sections read the fields of the same name from the datum. Here it points at the box in the middle; with coordinates, calling render() again moves the panel, and it keeps itself on screen, flipping below the point when there is no room above. Charts create one of these for you and expose its settings as tooltipConfig.

const config = {
  data: [
    {
      title: "Alpha",
      body: "A tooltip built from the datum's title, body, and footer fields.",
      footer: "Edit the code to change it",
    },
  ],
};
const config = {
  data: [{name: "Alpha", x: 42, y: 17, share: 0.31}],
  title: d => d.name,
  thead: ["Metric", "Value"],
  tbody: [
    ["x", d => d.x],
    ["y", d => d.y],
    ["share", d => `${Math.round(d.share * 100)}%`],
  ],
};

The background color accessor for each tooltip.

padding

The inner padding of each tooltip.

"12px 16px"

The border-radius accessor for each tooltip.

Configuration

import {Tooltip} from "@d3plus/core";

Extends BaseClass.

arrowstring | function

The inner HTML content of the arrow element, empty by default.

arrowStylerecord

CSS styles applied to the arrow element.

backgroundcsscolor | function

The background color accessor for each tooltip.

bodystring | function
bodyStylerecord

CSS styles applied to the body element.

borderstring | function

The border accessor for each tooltip.

borderRadiusstring | function

The border-radius accessor for each tooltip.

classNamestring | array.<string> | undefined

CSS classes added to each tooltip element, next to its fixed d3plus-tooltip class, so page CSS can style these tooltips (a chart passes its tooltipConfig.className). Takes a space-separated string or an array; a new value replaces the classes the previous one added.

colorDefaultscolordefaultsconfig

Overrides the default colors used when assigning fills from data and choosing legible text colors: dark and light (the text colors picked for contrast against a background), missing (null/undefined values), on/off (true/false values), sequential (the anchor hue for magnitude ramps), and scale (the categorical palette, given as a d3 ordinal scale or an array of colors). Keys are merged into the current defaults, and a Viz passes its overrides down to the shapes and components it draws.

configd3plusconfig

Methods that correspond to the key/value pairs and returns this class.

dataarray.<datapoint>Default [ ]

The data array used to create tooltips.

The footer content accessor for each tooltip.

footerStylerecord

CSS styles applied to the footer element.

heightnumber

Overall height of the visualization in pixels.

idaccessorfn

Unique-id accessor per datum (used for keyed enter/update/exit).

localestring | object

The locale used for all text and number formatting. Supports the locales defined in d3plus-format. The locale can be a complex Object, a locale code (like "en-US"), or a 2-digit language code (like "en"). If a 2-digit code is provided, the "findLocale" function is used to identify the most approximate locale.

maxWidthstring | number | function

The max-width accessor for each tooltip.

minWidthstring | number | function

The min-width accessor for each tooltip.

offsetnumber | function

The pixel offset between the tooltip and its anchor point.

onstring

Event listener for the specified event typenames. Mirrors the core d3-selection behavior.

paddingstring | number | function

The inner padding of each tooltip.

parenthtmlelement | null | undefined

Parent element that scopes the tooltip's portal. Default (unset) uses the global <div id="d3plus-portal"> appended to <body>. When set, tooltips mount inside a .d3plus-tooltip-portal child of the given element instead — so multiple charts on a page don't fight over the global portal, and tooltips destroy cleanly when the chart goes away.

Viz auto-sets this when rendering: chart.tooltipClass.parent(chart._select.node().parentNode).

pointerEventsconstoraccessor

SVG pointer-events attribute per datum.

positionstring | htmlelement | array.&lt;number&gt; | virtualelement | function

The position of each tooltip. Can be an HTMLElement to anchor to, a virtual element (any object with a getBoundingClientRect() returning a box in client coordinates), a selection string, or coordinate points in reference to the client viewport (not the overall page). An element or box anchors the tooltip's arrow to the middle of its top edge (or its bottom edge when the tooltip flips below).

shapeConfigd3plusconfig

Configuration object with key/value pairs applied as method calls on each shape.

tableStylerecord

CSS styles applied to the table element.

tbodyfunction | array.&lt;array.&lt;string | function&gt;&gt;Default []

Body rows. A cell function receives (d, i, x); in Pie, Treemap, and stacked Plot charts x.share is the row's fraction of its total, unless the data has its own share field (then x.share is that field).

tbodyStylerecord

CSS styles applied to the table body element.

tdStylerecord

An object with CSS keys and values to be applied to all <td> elements inside of each <tr>. Values may be (d, i) functions, where i is the cell's column index.

thStylerecord

An object with CSS keys and values to be applied to all <th> elements inside of the <thead>. Values may be (d, i) functions, where i is the cell's column index.

theadfunction | array.&lt;array.&lt;string | function&gt;&gt;Default []

Header rows. A cell function receives (d, i, x); in Pie, Treemap, and stacked Plot charts x.share is the row's fraction of its total, unless the data has its own share field (then x.share is that field).

theadStylerecord

CSS styles applied to the table head element.

titlestring | function
titleStylerecord

CSS styles applied to the title element.

titleSwatchbooleanDefault true

Whether a chart's tooltip title leads with a swatch of the hovered shape's color and shape. true by default; set it in legendTooltip to drop the swatch from legend tooltips only.

tooltipStylerecord

Overall CSS styles applied to the tooltip container.

trStylerecord

An object with CSS keys and values to be applied to all <tr> elements inside of each <tbody>.

translatefunction

Defines how informational text strings should be displayed. By default, this function will try to find the string in question (which is the first argument provided to this function) inside of an internally managed translation Object. If you'd like to override to use custom text, simply pass this method your own custom formatting function.

widthnumber

Overall width of the visualization in pixels.