d3-stalactite
Stalactite plots for D3: hierarchical data with two
quantities per node — an additive one (e.g. sales; a parent is the sum of
its children) and a proportional one (e.g. profit margin; a parent is the
average of its children, weighted by the additive quantity). Width encodes the
additive quantity, height the proportional one, children hang below their
parent, and an average line mirrors each parent's height onto its children.
Interactive demo
The layout has no dependencies. The renderer draws into a d3 selection you
pass in, so d3 (v6 or later) is only needed by your page.
Behaviour is defined by the language-independent
specification and tested against its fixtures.
Data
Nested JSON, as in portfolio.json:
{ "name": "Portfolio", "children": [
{ "name": "Financials", "children": [
{ "name": "BAC", "add_val": 960.66, "ratio_val": 28.243 },
{ "name": "JPM", "add_val": 1667.75, "ratio_val": 35.06 } ] } ] }
Only leaves carry values; inner nodes are aggregated. Leaves with
add_val = 0 are dropped with a warning. Missing, non-numeric or negative
values are errors naming the node by its path (e.g. Portfolio / Financials / BAC). Other field names can be read with accessors (see below).
Ready-made plot
import { stalactitePlot } from "d3-stalactite";
d3.select("svg").call(stalactitePlot, {
data,
width: 960,
height: 500,
valueNames: ["Sales", "Profit margin"],
});
Calling it again on the same selection updates the plot in place.
| option |
default |
|
data |
— |
the tree (required) |
width, height |
960, 500 |
plot size in px |
gap |
1 |
horizontal gap between siblings in px |
lineWidth |
3 |
thickness of the average lines in px |
sort |
"none" |
"none" (data order) or "descending" (by additive value) |
k |
null |
height scale in px per unit of the proportional value; set the same k to make several plots comparable |
colors |
defaultColors |
one color per depth, repeating when the tree is deeper |
labels |
true |
draw names inside boxes where they fit (never truncated) |
valueNames |
["Additive", "Proportional"] |
names of the two quantities in tooltips |
tooltip |
true |
false, or a function (node, valueNames) => html |
labelFontSize, labelFontFamily, labelPadding |
12, "sans-serif", 4 |
label font and padding |
name, children, n, v |
d.name, d.children, d.add_val, d.ratio_val |
accessors |
Every box shows a tooltip with its name and both values on hover, labelled or
not. Tooltip content from a custom function is inserted as HTML: escape
user-provided text yourself.
Layout only
In the style of d3.treemap():
import { stalactite } from "d3-stalactite";
const layout = stalactite()
.size([960, 500])
.sort("descending")
.n((d) => d.sales)
.v((d) => d.margin);
const { nodes, lines, k } = layout(data);
Methods (each a getter without argument, a chaining setter with one):
width, height, size([width, height]), gap, lineWidth, sort, k,
and the accessors name, children, n, v. The same is available as a
plain function: stalactiteLayout(data, options).
nodes: one object per node, in preorder —
{id, name, depth, parent_id, n, v, x0, x1, y0, y1, data}
(data is your original object).
lines: one average line per inner node, in drawing order (deepest first) —
{node_id, depth, x0, x1, y}.
k: the height scale used.
Draw each node as a rectangle [x0, x1] × [y0, y1] and each line from
(x0, y) to (x1, y) with butt caps, both colored by depth.
Long tables
One row per leaf, e.g. from d3.csv:
import { fromTable } from "d3-stalactite";
const rows = await d3.csv("portfolio.csv", d3.autoType);
const data = fromTable(rows, {
levels: ["sector", "industry", "company"],
n: "sales",
v: "profit_margin",
rootName: "Portfolio",
});
The result is in the nested JSON format above. Empty level values and
duplicate rows are errors.
Development
npm install # dev dependency d3, for the demo and browser tests
npm test # spec fixtures and unit tests (Node's built-in runner)
npm run test:browser # renderer tests in headless Chrome/Edge
npm run demo # demo page at localhost:8080/docs/
npm run render-review # writes ../docs/review/js.png
Set CHROME to the browser executable if Chrome or Edge is not found.
Citation
A. Schlieder, J. Rummel, F. Sadlo, "Proportional Aggregation in Hierarchical
Data Visualization", IEEE TVCG (VIS 2026).
DOI: 10.1109/TVCG.2026.3664464