Tooltip
Creates HTML tooltips in the body of a webpage.
Basic example
Section titled “Basic example”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",
},
],
};
Table content
Section titled “Table content”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)}%`],
],
};
Styling
Section titled “Styling”const config = {
data: [
{
title: "Dark theme",
body: "background, border, padding, and borderRadius are all config keys.",
},
],
background: "#212529",
border: "1px solid #212529",
borderRadius: "8px",
padding: "12px 16px",
tooltipStyle: {color: "#f8f9fa", "font-family": "Inter, sans-serif"},
};
Configuration
import {Tooltip} from "@d3plus/core";
Extends BaseClass.
arrowStylerecordCSS styles applied to the arrow element.
bodyStylerecordCSS styles applied to the body element.
classNamestring|array.<string>|undefinedCSS classes added to each tooltip element, next to its fixed
d3plus-tooltipclass, so page CSS can style these tooltips (a chart passes itstooltipConfig.className). Takes a space-separated string or an array; a new value replaces the classes the previous one added.colorDefaultscolordefaultsconfigOverrides the default colors used when assigning fills from data and choosing legible text colors:
darkandlight(the text colors picked for contrast against a background),missing(null/undefined values),on/off(true/falsevalues),sequential(the anchor hue for magnitude ramps), andscale(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.configd3plusconfigMethods that correspond to the key/value pairs and returns this class.
dataarray.<datapoint>Default[ ]The data array used to create tooltips.
CSS styles applied to the footer element.
localestring|objectThe 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.
onstringEvent listener for the specified event typenames. Mirrors the core d3-selection behavior.
parenthtmlelement|null|undefinedParent 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-portalchild 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).
positionstring|array.<number>|htmlelement|virtualelement|functionThe 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).shapeConfigd3plusconfigConfiguration object with key/value pairs applied as method calls on each shape.
tableStylerecordCSS styles applied to the table element.
tbodyStylerecordCSS styles applied to the table body element.
tdStylerecordAn object with CSS keys and values to be applied to all <td> elements inside of each <tr>. Values may be
(d, i)functions, whereiis the cell's column index.thStylerecordAn object with CSS keys and values to be applied to all <th> elements inside of the <thead>. Values may be
(d, i)functions, whereiis the cell's column index.theadStylerecordCSS styles applied to the table head element.
titleStylerecordCSS styles applied to the title element.
tooltipStylerecordOverall CSS styles applied to the tooltip container.
trStylerecordAn object with CSS keys and values to be applied to all <tr> elements inside of each <tbody>.
translatefunctionDefines 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.