Bubble chart
A bubble chart compares items on three measures at once: each category becomes a bubble, placed along the horizontal axis by the first series, along the vertical axis by the second, its area following the third. Products by price, units sold and revenue; teams by response time, satisfaction and ticket volume. With two series only, every bubble has the same size, as on a scatter plot. Hovering a bubble, or moving along them with the arrow keys, marks it with guides to both axes and shows its values in a tooltip.
Dependencies
When to use
Reach for a bubble chart to compare a handful to a few dozen items on
three measures at once, where how they spread and cluster matters more
than each value: products by price, sales and revenue, customers by
age, basket and orders. Prefer a
The data follows the contract shared by every chart, its categories
being the bubbles: see
Install & usage
npm install @eunomia/elements @eunomia/charts
import "@eunomia/charts/bubble-chart.js";
<eun-bubble-chart
label="Products by price, units sold and revenue"
x-format='{"style":"currency","currency":"EUR"}'
></eun-bubble-chart>
<script type="module">
const chart = document.querySelector("eun-bubble-chart");
chart.data = {
categories: [
{ key: "chair", label: "Office chair" },
{ key: "desk", label: "Standing desk" },
{ key: "lamp", label: "Desk lamp" },
],
series: [
{ key: "price", label: "Price", values: [240, 620, 45] },
{ key: "sold", label: "Units sold", values: [184, 96, 142] },
{ key: "revenue", label: "Revenue", values: [44160, 59520, 6390] },
],
};
</script>
Importing the file registers <eun-bubble-chart> as a custom element,
with no further setup needed. It draws at its own width, so give it room
the way you would an image: in a card, a grid cell, a flex item with
min-width: 0. Import @eunomia/elements/skeleton.js too to show a
placeholder while loading, and @eunomia/elements/modal.js to use
details.
npm install @eunomia/elements @eunomia/charts
<script type="module">
import "@eunomia/charts/bubble-chart.js";
const chart = document.querySelector("eun-bubble-chart");
chart.data = {
categories: products.map((product) => ({
key: product.id,
label: product.name,
})),
series: [
{
key: "price",
label: "Price",
values: products.map((product) => product.price),
},
{
key: "sold",
label: "Units sold",
values: products.map((product) => product.sold),
},
{
key: "revenue",
label: "Revenue",
values: products.map((product) => product.revenue),
},
],
};
</script>
<eun-bubble-chart
label="Products by price, units sold and revenue"
></eun-bubble-chart>
npm install @eunomia/elements @eunomia/charts
import { useMemo } from "react";
import "@eunomia/charts/bubble-chart.js";
const EUROS = { style: "currency", currency: "EUR" };
function ProductsChart({ products, loading }) {
const data = useMemo(
() => ({
categories: products.map((product) => ({
key: product.id,
label: product.name,
})),
series: [
{
key: "price",
label: "Price",
values: products.map((product) => product.price),
},
{
key: "sold",
label: "Units sold",
values: products.map((product) => product.sold),
},
{
key: "revenue",
label: "Revenue",
values: products.map((product) => product.revenue),
},
],
}),
[products],
);
return (
<eun-bubble-chart
label="Products by price, units sold and revenue"
data={data}
xFormat={EUROS}
sizeFormat={EUROS}
loading={loading}
/>
);
}
React 19 sets data, xFormat and sizeFormat as properties. On
React 18, set them through a ref, or with eunProps from
@eunomia/elements/react.
npm install @eunomia/elements @eunomia/charts
"use client";
import "@eunomia/charts/bubble-chart.js";
export function ProductsChart({ data }) {
return (
<eun-bubble-chart
label="Products by price, units sold and revenue"
data={data}
/>
);
}
Build the data on the server, and import chart-ssr.css to reserve the
chart's height before it upgrades (see Server rendering below).
npm install @eunomia/elements @eunomia/charts
<script setup>
import "@eunomia/charts/bubble-chart.js";
defineProps({ data: Object });
const euros = { style: "currency", currency: "EUR" };
</script>
<template>
<eun-bubble-chart
label="Products by price, units sold and revenue"
:data="data"
:x-format="euros"
/>
</template>
npm install @eunomia/elements @eunomia/charts
import { CUSTOM_ELEMENTS_SCHEMA, Component, Input } from "@angular/core";
import type { EunomiaBubbleChartData } from "@eunomia/charts";
import "@eunomia/charts/bubble-chart.js";
@Component({
selector: "app-products-chart",
template: `
<eun-bubble-chart
label="Products by price, units sold and revenue"
[data]="data"
[xFormat]="euros"
></eun-bubble-chart>
`,
schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class ProductsChartComponent {
@Input() data!: EunomiaBubbleChartData;
euros = { style: "currency", currency: "EUR" };
}
Server rendering
Before it upgrades, a chart is an empty tag. Import chart-ssr.css with
the page's other styles to reserve its room from the first paint:
--chart-height, plus a row for the legend once legend="visible", or
100% of its parent's height with fill.
@import "@eunomia/charts/chart-ssr.css";
Without a bundler, link node_modules/@eunomia/charts/dist/ssr/chart-ssr.css
in the page's <head>. Two cases still shift the page on upgrade: a
legend left to legend="auto", which only shows with the data, and a
legend that wraps onto more than one row. See
Alternatives
| You want… | Prefers |
|---|---|
| Items compared on a single measure | eun-bar-chart |
| Two related measures along the same categories, as bars and a line | eun-combo-chart |
| To list exact values, to be read, sorted or compared one by one | eun-table |
Guidance
- Give every chart a
label: it names the plot for assistive technology and captions its data table - Name each series after its measure and unit: its label titles its axis, and heads its row in the tooltip
- Size the bubbles after an amount (revenue, volume, population): their area follows it, so a bubble twice as large holds twice as much
- Keep to a few dozen bubbles at most, and set
legend="hidden"past a dozen, naming the bubbles in the tooltip
- Sizing bubbles after a value that can be negative or a rate: only positive amounts get a bubble
- Reading exact values off the chart: the tooltip and the data table hold them, a table lists them better
- Mutating
datain place: replace the object, the chart only redraws when it changes
Live testing
Properties
data and label are the two you'll always set. The data's own fields,
shared with the other charts, are listed on
Bubble chart <eun-bubble-chart>
Attributes
| Name | Type | Default | Description |
|---|---|---|---|
| data | EunomiaChartData<S> | EMPTY_DATA | What the chart draws, as JSON: its categories and its series |
| legend-position | EunomiaChartLegendPosition | 'start' | How the legend's entries line up under the plot |
| hide-tooltip | boolean | false | Shows no tooltip on hover or keyboard moves |
| hide-grid | boolean | false | Draws no gridlines |
| hide-value-axis | boolean | false | Hides the labels and the titles of both axes, the plot taking their room |
| hide-axis-titles | boolean | false | Hides the titles of the axes, the series' labels, keeping their values |
| from-zero | boolean | false | Starts both axes at zero, rather than fitting them to the values |
| x-min | number | — | The exact left end of the horizontal axis, whatever the values: a bubble placed before it isn't drawn, one across it is cut off |
| x-max | number | — | The exact right end of the horizontal axis, whatever the values: a bubble placed past it isn't drawn, one across it is cut off |
| y-min | number | — | The exact lower end of the vertical axis, whatever the values: a bubble placed below it isn't drawn, one across it is cut off |
| y-max | number | — | The exact upper end of the vertical axis, whatever the values: a bubble placed above it isn't drawn, one across it is cut off |
| empty-label | string | undefined | — | The text shown in place of the bubbles while there are none to draw |
| hide-empty-label | boolean | false | Shows no text while there are no bubbles to draw |
| hide-empty-grid | boolean | false | Draws no gridlines and no axis labels while there are no bubbles to draw |
| format | Intl.NumberFormatOptions | undefined | — | The Intl.NumberFormat options the values are written with, as JSON |
| x-format | Intl.NumberFormatOptions | — | The Intl.NumberFormat options of the series giving the horizontal position, and of that axis' labels in compact notation. Defaults to format. As an attribute, JSON |
| size-format | Intl.NumberFormatOptions | — | The Intl.NumberFormat options of the series giving the size. Defaults to format. As an attribute, JSON |
| select-hint | string | undefined | — | The hint at the bottom of the tooltip while bubbles are clickable. Defaults to the localized string when details is set (English: "Click to see the details"), to none otherwise |
| non-interactive | boolean | false | Draws the chart as a plain picture: no focus stop, no hover, no tooltip, no selection |
| locale | string | undefined | — | The locale used to resolve dictionary and `Intl` formatting. Defaults to the shared default set via `setDefaultLocale()`, itself defaulting to `"en"`. |
| label | string | undefined | — | The chart's accessible name, also the caption of its data table. |
| legend | EunomiaChartLegend | "auto" | When the legend is shown — see EunomiaChartLegend. |
| fill | boolean | false | Takes the whole height of the chart, rather than `--chart-height`: the plot stretches to what's left once the legend is laid out, and redraws whenever that changes. The chart takes 100% of its parent's height by default; size it like any block otherwise (`height`, `flex: 1`, a grid cell). |
| loading | boolean | false | Shows a placeholder in place of the plot while the data loads. |
| refreshing | boolean | false | Keeps the plot shown as a placeholder while new data loads, such as after a change of period: the marks faded, their colors kept, under a shimmer, the axes kept, the tooltip off. |
| animated | boolean | false | Draws the marks in once the chart is first seen. |
| selectable | boolean | false | Makes every category clickable, firing `eunchartselect`. |
| details | boolean | false | Opens a modal listing a category's values once it's clicked. Implies selectable. |
Import the exact TypeScript type behind any property above, see
Properties
JS-only — no matching HTML attribute, set these from a script or a template binding.
| Name | Type | Default | Description |
|---|---|---|---|
| tickFormatter | EunomiaBubbleChartTickFormatter | — | Writes a value of an axis, in place of the formats. Receives the value and the axis, x or y. Property only |
| valueFormatter | EunomiaChartValueFormatter | undefined | — | Writes a value in the tooltip, the details and the data table, in place of the formats. Property only |
| tooltipRenderer | EunomiaChartTooltipRenderer<S> | undefined | — | Writes the content of the tooltip in place of the values and the category's label. Receives the category, the values of every series written and colored, and the total; returns a DOM node, a string shown as text or a Lit template, null keeping the default content. Property only |
| resolvedLocale | string | — | This instance's own `locale` when set, the shared default locale otherwise. |
| dictionary | ChartDictionary | — | This instance's locale-resolved chart strings. |
Methods
| Name | Returns | Description |
|---|---|---|
| replayAnimation() | Promise<void> | Plays the entrance animation again, such as once new data is shown. Does nothing while the person asked to reduce motion. |
Slots
| Name | Description |
|---|---|
| details | Extra content shown in the details modal, below the category's values, such as the records behind them. Update it from eunchartselect. A <template slot="details"> is filled for the clicked category instead, as the tooltip's: data-field, data-each and data-bind-* attributes such as data-bind-href="/orders?day={key}" |
| tooltip | A <template> writing the content of the tooltip in plain HTML, in place of the values and the category's label: data-each="values" (or "drawn") repeats an element per series, data-field writes category, key, tick, total, and per series series, text, value or color. tooltipRenderer takes over when set. data-bind-<attr> writes an attribute, {category}, {key}, {tick} and {index} replaced |
Events
| Name | Type | Description |
|---|---|---|
| eunchartselect | ChartSelectEvent | Fired when a bubble is clicked, or a category activated with Enter or Space, while selectable or details is set. Cancelable to keep the details modal from opening |
Every event above follows the same naming convention, covered in
CSS custom properties
| Name | Description |
|---|---|
| --chart-height | Sets the height of the plot, axis labels included, ignored with fill. 220px by default |
| --chart-min-height | Sets the least height of the plot with fill. 80px by default |
| --chart-color-1 | Sets the color of the first category, and of every eighth one after it. The primary color by default |
| --chart-color-2 | Sets the color of the second category. The secondary color by default |
| --chart-color-3 | Sets the color of the third category |
| --chart-color-4 | Sets the color of the fourth category |
| --chart-color-5 | Sets the color of the fifth category |
| --chart-color-6 | Sets the color of the sixth category |
| --chart-color-7 | Sets the color of the seventh category |
| --chart-color-8 | Sets the color of the eighth category |
| --chart-bubble-opacity | Sets the opacity of the bubbles' fill, so overlapping ones show through. 0.7 by default |
| --chart-surface-color | Sets the color of the surface the chart sits on, ringing the hovered bubble |
| --chart-grid-color | Sets the color of the gridlines |
| --chart-axis-color | Sets the color of the axis labels and titles, of the hovered bubble's guides and of the legend |
| --chart-axis-font-size | Sets the font size of the axis labels |
| --chart-tooltip-background | Sets the background color of the tooltip |
| --chart-tooltip-color | Sets the text color of the tooltip |
| --chart-focus-outline-color | Sets the color of the plot's focus ring |
| --chart-animation-duration | Sets the duration of the entrance animation |
| --chart-refresh-opacity | Sets the opacity the bubbles take while refreshing, their colors kept. 0.4 by default |
| --chart-refresh-highlight-color | Sets the color of the shimmer sweeping the plot while refreshing |
CSS shadow parts
| Name | Description |
|---|---|
| plot | The svg the chart is drawn in |
| tooltip | The tooltip |
| legend | The legend |
| legend-item | An entry of the legend, its swatch and its name |
| legend-swatch | The swatch of an entry of the legend |
| details | The details modal |
Types
import type {
EunomiaBubbleChartData,
EunomiaBubbleChartSeries,
EunomiaBubbleChartAxis,
EunomiaBubbleChartTickFormatter,
EunomiaChartCategory,
EunomiaChartLegend,
EunomiaChartLegendPosition,
EunomiaChartValueFormatter,
} from "@eunomia/charts";
import { ChartSelectEvent } from "@eunomia/charts";
ChartSelectEvent carries the selected category, its index in
data.categories, and values: every series with its value on that
category, null for none.
Series
Three series: position and size
The categories are the bubbles. The first series places them along the horizontal axis, the second along the vertical one, and the third sizes them, their area following its value: here each product's price, its units sold and the revenue they brought. Each axis is titled after its series, and the largest bubbles are drawn first, so the smaller ones stay on top.
chart.data = {
categories: [
{ key: "chair", label: "Office chair" },
{ key: "desk", label: "Standing desk" },
{ key: "lamp", label: "Desk lamp" },
],
series: [
{ key: "price", label: "Price", values: [240, 620, 45] },
{ key: "sold", label: "Units sold", values: [184, 96, 142] },
{ key: "revenue", label: "Revenue", values: [44160, 59520, 6390] },
],
};
Two series: a scatter plot
With only two series, every bubble gets the same size: a scatter plot, for how two measures relate.
More than three series
Past the third, series aren't drawn: they show in the tooltip, the details and the data table, the bubble's own record. Here five measures per support team, the open tickets and the agents next to the three that place and size the bubbles.
series: [
{ key: "response", label: "First response, in hours", values: response },
{ key: "satisfaction", label: "Satisfaction, out of 10", values: satisfaction },
{ key: "tickets", label: "Tickets", values: tickets },
{ key: "backlog", label: "Open tickets", values: backlog },
{ key: "agents", label: "Agents", values: agents },
],
A series set to display: "none" is left out of the roles wherever it
stands, its values only in the tooltip, here the margin listed first.
series: [
{ key: "margin", label: "Margin", values: margin, display: "none" },
{ key: "price", label: "Price", values: price },
{ key: "sold", label: "Units sold", values: sold },
{ key: "revenue", label: "Revenue", values: revenue },
],
A bubble without a position (a null on either axis), or of a missing,
zero or negative size, isn't drawn; its values stay in the data table.
Axes
Fitted axes and zero
Each axis fits its values on round steps, without reaching for zero, so
bubbles crowded at one end still spread out. from-zero starts both
axes at zero, for amounts best read from it. Negative values are drawn
as they come, the axes extending below zero.
Fixed bounds
x-min, x-max, y-min and y-max hold an axis between fixed bounds:
it ends exactly on them, whatever the values, the end left unset still
fitting them (or zero with from-zero). A bubble placed past a bound
isn't drawn, one across it is cut off at the edge of the plot. Here a
satisfaction out of 10, read on its whole scale.
<eun-bubble-chart y-min="0" y-max="10"></eun-bubble-chart>
Hiding the grid and the axes
hide-grid draws no gridlines, hide-axis-titles drops the series'
labels from the axes, and hide-value-axis drops the labels and the
titles of both, the plot taking their room.
Values
format takes Intl.NumberFormat options, applied in the chart's
locale, for the vertical axis' series; x-format and size-format
take over for the series placing the bubbles horizontally and sizing
them, defaulting to format. Values are written in full in the tooltip,
the details and the data table, and in compact notation along the axes.
When a format isn't enough, valueFormatter writes the values and
tickFormatter the axes, both set as properties.
<eun-bubble-chart
x-format='{"style":"currency","currency":"EUR"}'
format='{"style":"percent"}'
size-format='{"notation":"compact"}'
></eun-bubble-chart>
chart.tickFormatter = (value, axis) =>
axis === "x" ? `${value} h` : `${Math.round(value * 100)}%`;
Legend and colors
The legend lists the bubbles, from two of them on (legend="auto"),
always (visible) or never (hidden); past a dozen bubbles, hide it and
let the tooltip name them. Bubbles take the palette's colors in category
order, and a category's own color takes over: give the same color to
bubbles of a same family, such as products of a same range. The fill is
semi-transparent, so overlapping bubbles show through,
--chart-bubble-opacity setting it.
categories: [
{ key: "chair", label: "Office chair", color: "var(--eun-color-primary-500)" },
{ key: "desk", label: "Standing desk", color: "var(--eun-color-primary-500)" },
{ key: "lamp", label: "Desk lamp", color: "var(--eun-color-secondary-500)" },
],
The legend and the tooltip work the same on every chart: see
States
Loading and refreshing
loading shows a placeholder the height of the chart while its first
data loads. refreshing turns the current bubbles into a placeholder
while new data loads: they're faded, their colors kept, under a shimmer, the axes stay,
and the tooltip is off until the new data comes in.
Empty
A chart without bubbles to draw keeps its grid and writes "No data".
empty-label writes your own message, hide-empty-label none, and
hide-empty-grid drops the gridlines and the axis labels.
Entrance animation
By default, a chart is drawn at once, with no motion: the right choice for a dashboard refreshed often, or for one chart among many on a page.
animated grows the bubbles one after the other once the chart is first
seen. replayAnimation() plays it again. It's skipped when the person
asked their system to reduce motion.
<!-- No animation, the default -->
<eun-bubble-chart
label="Products by price, units sold and revenue"
></eun-bubble-chart>
<!-- Animated when first seen -->
<eun-bubble-chart
label="Products by price, units sold and revenue"
animated
></eun-bubble-chart>
Selection
Selectable bubbles
selectable makes every bubble clickable, and every category
activatable with Enter or Space once the plot is focused, firing
eunchartselect with the category and its values. Where bubbles
overlap, the pointer picks the smallest one under it.
Click a bubble.
chart.addEventListener("eunchartselect", (event) => {
showProduct(event.category.key);
});
Details modal
details opens a modal on click, headed by the category's label and
listing the value of every series. It needs eun-modal to be
registered. The details slot adds content below the values, filled
from eunchartselect; canceling it keeps the modal closed.
The modal works the same on every chart: see
Non-interactive
non-interactive draws the chart as a plain picture: no focus stop, no
hover, no tooltip and no selection, even with selectable or details.
It fits a small chart in a table cell or a card, where the row or the
card already takes the click. The plot keeps its accessible name and its
data table. See
<eun-bubble-chart
label="Products by price, units sold and revenue"
non-interactive
></eun-bubble-chart>
Customization
Custom appearance
The custom properties below restyle the chart, and its parts (plot,
tooltip, legend, details) take any other rule.
.products-chart {
--chart-height: 300px;
--chart-bubble-opacity: 0.45;
--chart-grid-color: #99f6e4;
--chart-axis-color: #115e59;
--chart-tooltip-background: #134e4a;
--chart-tooltip-color: #f0fdfa;
}
| Variable | Sets |
|---|---|
--chart-height |
Sets the height of the plot, axis labels included. 220px by default |
--chart-color-1 … -8 |
Sets the palette the bubbles take their color from, in category order |
--chart-bubble-opacity |
Sets the opacity of the bubbles' fill. 0.7 by default |
--chart-surface-color |
Sets the color of the surface the chart sits on, ringing the hovered bubble |
--chart-grid-color |
Sets the color of the gridlines |
--chart-axis-color |
Sets the color of the axis labels and titles, of the hovered bubble's guides and the legend |
--chart-axis-font-size |
Sets the font size of the axis labels |
--chart-tooltip-background |
Sets the background color of the tooltip |
--chart-tooltip-color |
Sets the text color of the tooltip |
--chart-animation-duration |
Sets the duration of the entrance animation |
--chart-refresh-opacity |
Sets the opacity the bubbles take while refreshing, their colors kept. 0.4 by default |
Keyboard interactions
The plot is a single focus stop. Once focused, it shows the last category's bubble, and the arrow keys move along the others, in category order.
| Key | Action |
|---|---|
Tab / Shift+Tab |
Moves focus to or away from the plot |
ArrowLeft |
Shows the previous category's bubble |
ArrowRight |
Shows the next category's bubble |
Home / End |
Shows the first or the last category's bubble |
Enter / Space |
Selects the shown category, while selectable or details is set |
Aria attributes and rules
- The plot is an
svgwithrole="img", named bylabel. Give every chart one. - Every value stays reachable in a visually hidden table, captioned by
label, a row per bubble and a column per series, those that aren't drawn included. Screen reader users read the data there rather than through the drawing. - Moving along the categories with the keyboard announces each one through a polite live region, its label followed by its values.
- The tooltip and the legend are
aria-hidden: they repeat what the table already holds. - While
refreshing, the plot is markedaria-busy. - A missing value is written "No value", never left blank, so it can't be mistaken for a zero.
- The details modal is an
eun-modal, with its own focus trap, Escape to close and return of focus to the plot. animatedis skipped when the person asked their system to reduce motion.- With
non-interactive, the plot is no focus stop and reacts to neither the pointer nor the keyboard; its accessible name and its data table are still read by screen readers.
Color and contrast
Bubbles are told apart by their place first, and by color only through
the legend, so keep their labels in the tooltip and the data table, and
never let color alone carry a meaning. Their fill is semi-transparent:
keep its edge at a 3:1 contrast with the surface around it (WCAG
1.4.11), raising --chart-bubble-opacity on a busy background.
Reference links
WAI Web Accessibility Tutorials: Complex images Understanding WCAG 2.2: Non-text contrast