Public
Edited
Jun 22
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const width = 928;
const height = 680;
const color = d3.scaleOrdinal(d3.schemeCategory10);

const links = data.links.map(d => ({...d}));
const nodes = data.nodes.map(d => ({...d}));

let selected = null;

const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody())
.force("x", d3.forceX())
.force("y", d3.forceY());

const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("style", "max-width: 100%; height: auto; background: #f0f0f0;")
.on("click", (event) => {
if (event.target.tagName === "svg") {
selected = null;
update();
}
});

const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));

const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d => scale(degreeMap.get(d.id)))
.attr("fill", d => color(d.group))
.on("click", (event, d) => {
event.stopPropagation();
selected = d;
update();
});

node.append("title")
.text(d => `${d.id}: ${d.group}`);

function update() {
node
.attr("stroke", d => d === selected ? "#000" : "#fff")
.attr("stroke-width", d => d === selected ? 3 : 1.5)
.attr("r", d => d === selected ? scale(degreeMap.get(d.id)) + 4 : scale(degreeMap.get(d.id)))
.attr("fill", d => d === selected ? "red" : color(d.group));
}

node.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));

simulation.on("tick", () => {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);

node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
});

function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}

function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}

function dragended(event) {
if (!event.active) simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}

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

return Object.assign(svg.node(), {scales: {color}});
}

Insert cell
import {Swatches} from "@d3/color-legend"


Insert cell
scale = d3.scalePow()
.range([2,20])
.domain(d3.extent(degreeMap.values()))
Insert cell
degreeMap = {
const degree = new Map()

data.nodes.forEach(d => {
degree.set(d.id, 0)
})

data.links.forEach(d => {
degree.set(d.target, degree.get(d.target)+1)
})

return degree;
}
Insert cell
data = FileAttachment("imdb_data_100_2025.json").json()
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