Force tree
blast radius
Blast radius from one node: what breaks if this goes.
- Format.d3
- Length85 lines
- Includesnone
The source
85 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.
// Force-directed *tree* — a hierarchy relaxed rather than ranked.
// Feeding `root.links()` to a simulation gives the organic look of a mind
// map while keeping the parent/child structure exact. Distance by depth
// keeps the trunk short and the twigs loose.
const data = {
name: 'postgres', children: [
{ name: 'orders', children: [
{ name: 'checkout' }, { name: 'fulfilment' }, { name: 'returns' },
]},
{ name: 'payments', children: [
{ name: 'ledger' }, { name: 'payouts' }, { name: 'refunds' },
]},
{ name: 'catalogue', children: [
{ name: 'search-index' }, { name: 'pricing' }, { name: 'merchandising' },
]},
{ name: 'reporting', children: [
{ name: 'finance-pack' }, { name: 'ops-dashboard' },
]},
],
};
const root = d3.hierarchy(data);
const nodes = root.descendants().map(d => ({
id: d.data.name,
depth: d.depth,
}));
const byName = new Map(nodes.map(n => [n.id, n]));
const links = root.links().map(l => ({
source: byName.get(l.source.data.name),
target: byName.get(l.target.data.name),
}));
const color = d3.scaleOrdinal([0, 1, 2], [theme.accent, theme.palette[1], theme.palette[4]]);
const radius = d => [12, 8, 5][d.depth] ?? 5;
const simulation = d3.forceSimulation(nodes)
.force('link', d3.forceLink(links).distance(d => 110 - d.target.depth * 25).strength(1))
.force('charge', d3.forceManyBody().strength(-300))
.force('x', d3.forceX(width / 2).strength(0.05))
.force('y', d3.forceY(height / 2).strength(0.05))
.force('collide', d3.forceCollide(24));
// Run the simulation to completion up front, then draw once. A live
// simulation would be serialised at an arbitrary tick and never look the
// same twice; seeding \`randomSource\` pins the initial jitter too.
simulation.randomSource(d3.randomLcg(42)).stop();
for (let i = 0; i < 400; i++) simulation.tick();
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);
svg.append('g')
.attr('fill', 'none')
.attr('stroke', theme.muted)
.attr('stroke-opacity', 0.5)
.selectAll('line')
.data(links)
.join('line')
.attr('stroke-width', d => 3 - d.target.depth * 0.7)
.attr('x1', d => d.source.x).attr('y1', d => d.source.y)
.attr('x2', d => d.target.x).attr('y2', d => d.target.y);
const node = svg.append('g')
.selectAll('g')
.data(nodes)
.join('g')
.attr('transform', d => `translate(${d.x},${d.y})`);
node.append('circle')
.attr('r', radius)
.attr('fill', d => color(d.depth))
.attr('stroke', theme.background)
.attr('stroke-width', 1.5);
node.append('text')
.attr('x', d => radius(d) + 4)
.attr('dy', '0.35em')
.attr('fill', theme.foreground)
.attr('font-weight', d => d.depth === 0 ? 600 : 400)
.text(d => d.id);Render this offline
This template ships in Gnomon and renders on your machine, with no account and nothing sent to a server. The browser editor is free and needs no install.
Others in D3 visualisations
- Sankey — request flowFlow with volume. The best chart here for "where does it all go".
- Chord — service interactionWho talks to whom, when the traffic is bidirectional.
- Arc diagram — dependenciesNodes on one axis, arcs above. Readable where a force graph is not, provided the ordering means something.
- Adjacency matrix — couplingAdjacency matrix. Unfashionable, and better than a force graph for dense dependencies.
- Edge bundling — module importsHierarchical edge bundling. For import graphs big enough that straight edges become a hairball.
- Sunburst — nested spendNested hierarchy, radially. Prettier than an icicle, harder to compare.
- Icicle — nested spend (linear)A sunburst unrolled flat. Harder to love, much easier to compare siblings.
- Circle packing — nested sizeCircle packing. Nested size when the nesting matters more than reading exact areas.
- Treemap — portfolio costNested size. Good for cost, storage, lines of code.
- Tidy tree — structureA tidy tree. The default for anything with one parent per node.
- Radial tree — structure (radial)The same tree bent into a circle. Fits more depth on a slide, costs you easy comparison.
- Dendrogram — clusteringClustering, with join height carrying the distance. Not merely a tree with curves.
- Indented tree — file/spec outlineA file or spec outline. The chart that looks like the thing it describes.
- Force graph — service mapService map. Use for clusters, not for reading individual edges.
- Force graph — disjoint clustersForce graph that keeps unconnected clusters apart rather than flinging them off screen.
- Force graph — radial tiersForce layout pinned to rings, so tier is a position instead of a colour legend.
- Directed graph — call directionDirected edges with arrowheads. For when direction is the question, not just adjacency.
- Calendar heatmap — daily activityDaily activity over a year: deploys, incidents, commits.
- Streamgraph — shifting mixShifting composition over time. Good for the mix, poor for reading any single value.
- Gantt — delivery roadmapDelivery roadmap, rendered from data.
- Radar — capability scoringCapability scoring across axes. Fine for one subject, misleading with four overlaid.
- Bullet chart — target vs actualTarget against actual, in one line. The chart a gauge wishes it were.
- Beeswarm — distribution by groupEvery point, grouped, without the overplotting a strip plot suffers.
- Horizon chart — many series, little spaceMany series in little space. Takes a moment to learn, then very dense.
- Slope chart — before and afterBefore and after, two points, one line each. Devastatingly clear.
- Parallel coordinates — multi-criteriaMulti-criteria comparison: for option analysis.
- Box plot — latency distributionLatency distribution. The chart that shows the tail a mean hides.
- Grouped bar — category comparisonCategory comparison. Unglamorous, and usually the right answer.
- Multi-line — metrics over timeMetrics over time. Keep it under about five series, or switch to horizon.