Public
Edited
Jul 9, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
image = {
// Specify the color scale.
const color = d3.scaleOrdinal(d3.schemeCategory10);

// The force simulation mutates links and nodes, so create a copy
// so that re-evaluating this cell produces the same result.
const nodes = data.map((d) => ({ ...d }));

// Create a simulation with several forces.
const simulation = d3
.forceSimulation(nodes)
.force(
"collide",
d3
.forceCollide()
.radius((d) => d.r)
.iterations(3)
)
.force("x", d3.forceX().strength(0.02))
.force("y", d3.forceY().strength(0.02));

//.force("charge", d3.forceManyBody().strength(strength));

// Create the SVG container.
const svg = d3
.create("svg")
.attr("width", w)
.attr("height", h)
.attr("viewBox", [-w / 2, -h / 2, w, h])
.attr("style", "max-width: 100%; height: auto;");

// Add a circle for each node.
const node = svg
.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("image")
.data(nodes)
.join("image")
.attr("href", (d) => d.url)
.attr("width", (d) => d.r)
.attr("preserveAspectRatio", "xMidYMid meet")
.attr("transform", (d) => `rotate(${d.angle})`);

// Set the position attributes of links and nodes each time the simulation ticks.
simulation.on("tick", () => {
node
.attr("x", (d) => d.x)
.attr("y", (d) => d.y)
.attr("transform", (d) => `rotate(${d.angle}, ${d.x}, ${d.y})`);
});

// When this cell is re-run, stop the previous simulation. (This doesn’t
// really matter since the target alpha is zero and the simulation will
// stop naturally, but it’s a good practice.)
invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
data = d3.range(numPoints).map((i) => ({
url: urls[Math.floor(Math.random() * urls.length)],
r: 50 + (Math.random() - 0.5) * 30,
angle: Math.random() * 360
}))
Insert cell
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