Tooltip
Hovering a category, or moving along them with the arrow keys once the plot is focused, shows its values in a tooltip beside it. Every chart shares the same tooltip: this page covers what it shows, how its values are written, and how to restyle it or write its content yourself.
What it shows
The tooltip lists the hovered category's values, then its full label:
| Chart | Lists |
|---|---|
| One series | Its value, on its own |
| Several series | A row per series: its swatch, its value and its name, display: "none" series included |
| Stacked bars | The same rows, then their total |
| Pie, doughnut | The slice's value and its share of the series |
| Bubble | Every series' value for the hovered bubble |
A missing value is written "No value", in the chart's locale.
Where it opens
The tooltip opens beside the hovered category, on the side with the most room, so it never runs out of the chart: to the right of a category in the left half, to its left in the right half. On a pie or a doughnut, it points to the hovered slice. It follows the pointer and the arrow keys, and closes once the pointer leaves or the plot loses focus.
Writing the values
Values are written with format, Intl.NumberFormat options applied in
the chart's locale. When options aren't enough, valueFormatter,
set as a property, writes each value, receiving the value and its
series (none for a total). The tooltip, the details modal and the data
table all use it, so they never disagree.
<eun-line-chart
format='{"style":"currency","currency":"EUR","maximumFractionDigits":0}'
></eun-line-chart>
chart.format = { style: "currency", currency: "EUR", maximumFractionDigits: 0 };
chart.valueFormatter = (value, series) =>
series?.key === "target" ? `${euros(value)} (target)` : euros(value);
Hint on clickable charts
While selectable or details is set, the tooltip ends with a hint. It
defaults to the localized "Click to see the details" with details, and
to none with selectable alone; select-hint writes your own.
<eun-line-chart
selectable
select-hint="Click to open the month"
></eun-line-chart>
Hiding it
hide-tooltip shows no tooltip at all, the hovered category still being
marked: fits a sparkline, or a chart whose values are written next to it.
While refreshing, the tooltip is off until the new data comes in.
<eun-line-chart hide-tooltip></eun-line-chart>
Custom appearance
Two custom properties set its colors, and the tooltip part takes any
other rule: its padding, its corners, its shadow, its font.
| Customization | Sets |
|---|---|
--chart-tooltip-background |
The background color |
--chart-tooltip-color |
The text color |
::part(tooltip) |
Any other rule, on the box |
.sessions-chart {
--chart-tooltip-background: #ffffff;
--chart-tooltip-color: #071621;
}
.sessions-chart::part(tooltip) {
padding: 8px 12px;
border: 1px solid #dfe3ea;
border-radius: 12px;
box-shadow: 0 8px 24px rgb(7 22 33 / 0.12);
}
Custom content
The content of the tooltip, in place of the values and the category's
label, can be written in HTML with a template, or in JavaScript with
tooltipRenderer. Either way, the box, its position and the hint of a
clickable chart stay the chart's.
In HTML: a template
A <template slot="tooltip"> placed inside the chart writes the tooltip
in plain HTML, with no script. The chart clones it on every move and
fills it in:
| Attribute | Does |
|---|---|
data-each="values" |
Repeats the element once per series, in series order |
data-each="drawn" |
The same, for the drawn series only (display: "none" left out) |
data-field="category" |
Writes the category's label |
data-field="tick" |
Writes its short tick, else its label |
data-field="key" |
Writes its key |
data-field="total" |
Writes the total of a stacked bar chart; removed elsewhere |
data-field="series" |
Writes the series' name |
data-field="text" |
Writes its value as the chart writes it (format, valueFormatter), "No value" when there's none |
data-field="value" |
Writes its raw value; removed when there's none |
data-field="color" |
Paints the element's background in the series' color |
data-series="<key>" |
Reads a series' field outside a repeated element, by the series' key |
data-bind-<attr> |
Writes the attribute <attr>, {category}, {key}, {tick} and {index} replaced |
The series fields (series, text, value, color) are read from the
repeated element they sit in, or from the series data-series names.
Each repeated copy also gets its series' color as --series-color, for
a border or a text color. An element whose field has nothing to show (no
total, an unknown series, no value) is removed. Everything is written as
text, never parsed as HTML.
<eun-line-chart
label="Revenue per month"
format='{"style":"currency","currency":"EUR"}'
>
<template slot="tooltip">
<strong data-field="category"></strong>
<div data-each="drawn" style="display: flex; align-items: center; gap: 6px">
<span
data-field="color"
style="width: 8px; height: 8px; border-radius: 50%"
></span>
<span data-field="series"></span>
<b data-field="text" style="margin-left: auto"></b>
</div>
<small><span data-field="text" data-series="orders"></span> orders</small>
</template>
</eun-line-chart>
Here the orders, a series set to display: "none", are left out of the
rows (drawn) and written on their own line through data-series.
The template's content is rendered inside the chart's shadow root, so
the page's styles don't reach it: style it inline, as above, or give its
elements a part and style them with ::part(). A total and a
--series-color border, on a stacked bar chart:
<eun-bar-chart
class="sessions-chart"
label="Sessions per month and channel"
stacked
>
<template slot="tooltip">
<strong data-field="tick"></strong>
<div part="tip-row" data-each="values">
<span data-field="series"></span>
<span data-field="text"></span>
</div>
<div part="tip-total">Total <span data-field="total"></span></div>
</template>
</eun-bar-chart>
.sessions-chart::part(tip-row) {
display: flex;
justify-content: space-between;
gap: 12px;
padding-left: 6px;
border-left: 3px solid var(--series-color);
}
.sessions-chart::part(tip-total) {
margin-top: 4px;
font-weight: 700;
}
The template can be changed or removed at any time: the next move reads
it again, and the default content comes back once it's gone. A
tooltipRenderer, when set and returning something, takes over from the
template.
In JavaScript: tooltipRenderer
tooltipRenderer, set as a property, writes the content with a
function, for what a template can't do: a figure computed from the
values, a condition, a component of your own.
It's called on every move with a context:
| Field | Type | Description |
|---|---|---|
category |
EunomiaChartCategory |
The hovered category |
index |
number |
Its index in data.categories |
values |
EunomiaChartTooltipValue[] |
Every series on it, in series order — see below |
total |
number | null |
The total of its values on a stacked bar chart, null otherwise |
totalText |
string | null |
The same, written |
format |
(value, series?) => string |
Writes any value as the chart does, through valueFormatter or format |
Each entry of values holds:
| Field | Type | Description |
|---|---|---|
series |
the series | The series itself, with its key, its label and any field of your own |
value |
number | null |
Its raw value, null when it has none |
text |
string |
The value as the chart writes it, "No value" when there's none |
color |
string |
The color it's drawn in: the series', or the category's on a pie, doughnut or bubble |
drawn |
boolean |
false for a series set to display: "none" |
It returns:
- a DOM node, built with
document.createElement, a<template>'s clone, or any framework's rendered element; - a string, shown as text and never parsed as HTML, so a value coming from your data can't inject markup;
- a Lit template, written with
htmlfromlit; nullorundefined, to keep the default content, such as for some categories only.
If it throws, the error is logged and the default content is shown.
A computed figure
Here the tooltip adds what the default can't: how far revenue is from its target.
chart.tooltipRenderer = ({ category, values }) => {
const [revenue, target] = values;
const box = document.createElement("div");
box.setAttribute("part", "gap-tooltip");
box.append(
Object.assign(document.createElement("strong"), {
textContent: category.label,
}),
);
for (const item of values) {
const row = document.createElement("div");
row.setAttribute("part", "gap-row");
const dot = document.createElement("span");
dot.setAttribute("part", "gap-dot");
dot.style.background = item.color;
row.append(dot, `${item.series.label}: ${item.text}`);
box.append(row);
}
if (revenue.value !== null && target.value !== null) {
const gap = revenue.value - target.value;
const line = document.createElement("div");
line.setAttribute("part", gap >= 0 ? "gap-ahead" : "gap-behind");
line.textContent = `${euros(Math.abs(gap))} ${gap >= 0 ? "ahead of" : "behind"} target`;
box.append(line);
}
return box;
};
Plain text
A string is enough for a single line.
chart.tooltipRenderer = ({ category, values }) =>
`${category.label}: ${values[0].text} of revenue`;
A Lit template
import { html } from "lit";
chart.tooltipRenderer = ({ category, values }) => html`
<strong>${category.label}</strong>
${values
.filter((item) => item.drawn)
.map((item) => html`<div>${item.series.label} · ${item.text}</div>`)}
`;
Styling your content
The content is rendered inside the chart's shadow root, so the page's
styles don't reach it. Style it inline, or give its elements a part
attribute and style them with ::part() from outside, as in the demo
above:
.revenue-chart::part(gap-dot) {
width: 8px;
height: 8px;
border-radius: 50%;
}
Accessibility
- The tooltip is
aria-hidden: what it shows is in the data table read by screen readers, and announced as the arrow keys move from one category to the next. - A
tooltipRendereronly changes what's drawn: a figure that matters and that only the custom tooltip shows (a gap to target, say) should also be a series of its own, set todisplay: "none", so it reaches the data table and the announcements.