Public
Edited
Jun 15, 2023
Insert cell
Insert cell
Insert cell
chart = {
replay;

const nodes = pack().leaves();

const simulation = d3.forceSimulation(nodes)
.force("x", d3.forceX(width / 2).strength(0.01))
.force("y", d3.forceY(height / 2).strength(0.01))
.force("cluster", forceCluster())
.force("collide", forceCollide());

const svg = d3.select(DOM.svg(width, height));
svg.attr("style","background-image:linear-gradient(darkgray, gray);");
var grads = svg.append("defs").selectAll("radialGradient")
.data(nodes)
.enter()
.append("radialGradient")
.attr("gradientUnits", "objectBoundingBox")
.attr("cx", 0)
.attr("cy", 0)
.attr("r", "100%")
.attr("id", function(d, i) { return "grad" + i; });

grads.append("stop")
.attr("offset", "0%")
.style("stop-color", "white");

grads.append("stop")
.attr("offset", "100%")
.style("stop-color", function(d) { return color(d.data.group); });
const node = svg.append("g")
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("style", "drop-shadow(16px 16px 10px black)")
.attr("fill", d => "url(#grad"+ d.index +")")
// .attr("fill", d => color(d.data.group))
const text = svg.select("g").selectAll("text.label")
.data(nodes)
.enter().append("svg:text")
.attr("font-family", "Arial, Helvetica, sans-serif")
.attr("class", "label")
.attr("fill", "white")
.attr("font-size", "20px")
.attr("font-weight", "bold")
.attr("pointer-events", "none")
.attr("user-select", "none")
.attr("text-anchor", "middle")
.attr("x", d => d.x)
.attr("y", d => d.y)
.text(d => d.data.name)

svg.selectAll("circle").attr("style", "filter: drop-shadow(16px 16px 16px rgb(0 0 0 / 0.4));");

node.call(drag(simulation));
node.transition()
.delay((d, i) => Math.random() * 500)
.duration(750)
.attrTween("r", d => {
const i = d3.interpolate(0, d.r);
return t => d.r = i(t);
});

simulation.on("tick", () => {
node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
text
.attr("x", d => d.x)
.attr("y", d => d.y);
});

invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
function forceCluster() {
const strength = 0.2;
let nodes;

function force(alpha) {
const centroids = d3.rollup(nodes, centroid, d => d.data.group);
const l = alpha * strength;
for (const d of nodes) {
const {x: cx, y: cy} = centroids.get(d.data.group);
d.vx -= (d.x - cx) * l;
d.vy -= (d.y - cy) * l;
}
}

force.initialize = _ => nodes = _;

return force;
}
Insert cell
function forceCollide() {
const alpha = 0.4; // fixed for greater rigidity!
const padding1 = 2; // separation between same-color nodes
const padding2 = 6; // separation between different-color nodes
let nodes;
let maxRadius;

function force() {
const quadtree = d3.quadtree(nodes, d => d.x, d => d.y);
for (const d of nodes) {
const r = d.r + maxRadius;
const nx1 = d.x - r, ny1 = d.y - r;
const nx2 = d.x + r, ny2 = d.y + r;
quadtree.visit((q, x1, y1, x2, y2) => {
if (!q.length) do {
if (q.data !== d) {
const r = d.r + q.data.r + (d.data.group === q.data.data.group ? padding1 : padding2);
let x = d.x - q.data.x, y = d.y - q.data.y, l = Math.hypot(x, y);
if (l < r) {
l = (l - r) / l * alpha;
d.x -= x *= l, d.y -= y *= l;
q.data.x += x, q.data.y += y;
}
}
} while (q = q.next);
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
});
}
}

force.initialize = _ => maxRadius = d3.max(nodes = _, d => d.r) + Math.max(padding1, padding2);

return force;
}
Insert cell
pack = () => d3.pack()
.size([width, height])
.padding(1)
(d3.hierarchy(data)
.sum(d => d.value))
Insert cell
brands = [
{ name: 'Adidas', value: 33, group: 'Apparel and Footwear' },
{ name: 'Scuderia Ferrari', value: 12, group: 'Automotive' },
{ name: 'Costco', value: 9, group: 'Retail' },
{ name: 'Ralph Lauren Corporation', value: 7, group: 'Apparel and Fashion' },
{ name: 'Microsoft Corporation', value: 6, group: 'Technology' },
{ name: 'Ferrari', value: 6, group: 'Automotive' },
{ name: 'Google', value: 6, group: 'Technology and Internet Services' },
{ name: 'Nissan', value: 5, group: 'Automotive' },
{ name: 'Walgreens', value: 4, group: 'Pharmacy and Retail' },
{ name: 'Amazon', value: 4, group: 'E-commerce' },
{ name: 'JW Marriott Hotels', value: 4, group: 'Hospitality and Hotels' },
]

Insert cell
data = ({
children: Array.from(
d3.group(
brands,
d => d.group,
),
([, children]) => ({children})
)
})
Insert cell
function centroid(nodes) {
let x = 0;
let y = 0;
let z = 0;
for (const d of nodes) {
let k = d.r ** 2;
x += d.x * k;
y += d.y * k;
z += k;
}
return {x: x / z, y: y / z};
}
Insert cell
drag = simulation => {
function dragstarted(event, d) {
if (!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(event, d) {
d.fx = event.x;
d.fy = event.y;
}
function dragended(event, d) {
if (!event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
n = 200 // number of nodes
Insert cell
m = 10 // number of groups
Insert cell
color = d3.scaleOrdinal(d3.range(brands), d3.schemeTableau10)
Insert cell
d3.range(m)

Insert cell
height = 600
Insert cell
d3 = require("d3@6")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more