Dendrogram

clustering

Clustering, with join height carrying the distance. Not merely a tree with curves.

Dendrogram — clustering: rendered example
Rendered by Gnomon from the source below. No edits.

The source

71 lines of D3 JavaScript, and uses only the bundled D3 build. Copy it, or open the template inside Gnomon and render it as it is.

// Dendrogram (`d3.cluster`) — every leaf on the same line, so the picture
// reads as "how these group up" rather than "how deep each branch is".
// That is exactly the difference from `d3.tree`.

const data = {
  name: 'estate',
  children: [
    { name: 'keep', children: [
      { name: 'Quote Engine' }, { name: 'Risk Scoring' },
      { name: 'Underwriting' }, { name: 'Data Lakehouse' },
    ]},
    { name: 'migrate', children: [
      { name: 'Policy Admin Core' }, { name: 'Contact Centre' },
      { name: 'Cloud Platform' },
    ]},
    { name: 'retire', children: [
      { name: 'Legacy Ledger' }, { name: 'Campaign Manager' },
    ]},
    { name: 'tolerate', children: [
      { name: 'BI & Reporting' }, { name: 'Network Core' },
    ]},
  ],
};

const root = d3.hierarchy(data);
const margin = { top: 20, right: 170, bottom: 20, left: 20 };
d3.cluster().size([
  height - margin.top - margin.bottom,
  width - margin.left - margin.right,
])(root);

const color = d3.scaleOrdinal(data.children.map(c => c.name), theme.palette);
const topOf = d => d.ancestors().find(a => a.depth === 1)?.data.name ?? '';

const svg = d3.select(container).append('svg')
    .attr('viewBox', [0, 0, width, height])
    .attr('width', width)
    .attr('height', height)
    .attr('font-family', 'system-ui, sans-serif')
    .attr('font-size', 11);

const g = svg.append('g').attr('transform', `translate(${margin.left},${margin.top})`);

// Elbow connectors rather than curves — a dendrogram's job is to show
// discrete membership, and right angles say "grouped" more clearly.
g.append('g')
    .attr('fill', 'none')
    .attr('stroke-width', 1.5)
    .attr('stroke-opacity', 0.6)
  .selectAll('path')
  .data(root.links())
  .join('path')
    .attr('stroke', d => d.target.depth === 1 ? theme.muted : color(topOf(d.target)))
    .attr('d', d => `M${d.source.y},${d.source.x}V${d.target.x}H${d.target.y}`);

const node = g.append('g')
  .selectAll('g')
  .data(root.descendants())
  .join('g')
    .attr('transform', d => `translate(${d.y},${d.x})`);

node.append('circle')
    .attr('r', 4)
    .attr('fill', d => d.depth === 0 ? theme.foreground : color(topOf(d)));

node.append('text')
    .attr('dy', '0.32em')
    .attr('x', d => d.children ? -8 : 8)
    .attr('text-anchor', d => d.children ? 'end' : 'start')
    .attr('fill', theme.foreground)
    .text(d => d.data.name);

Render this offline

This template ships in Gnomon and renders on your machine, with no account and no diagram uploads. The browser editor is free and needs no install.

Get GnomonOpen the browser editor

Others in D3 visualisations

Analytics cookies (optional)

May we use Google Analytics to understand visits, product links and feature use? If you accept, Google receives usage and device information, including your IP address, and sets analytics cookies. We do not send your documents, filenames or account details. Advertising features are disabled.

Analytics is off until you accept. Your choice is saved on this browser for 180 days using necessary local storage. You can change it at any time in Analytics preferences. Closing this message rejects analytics. Read our privacy policy.