Published
Edited
Jan 2, 2021
3 stars
Insert cell
Insert cell
chart = {
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));

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

const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height])
.style("font", "12px sans-serif");

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

const node = svg.append("g")
.attr("fill", "currentColor")
.attr("stroke-linecap", "round")
.attr("stroke-linejoin", "round")
.selectAll("g")
.data(nodes)
.join("g")
.attr("r", 5)
.attr("fill", color)
.call(drag(simulation));

node.append("circle")
.attr("stroke", "black")
.attr("stroke-width", 1.5)
.attr("r", 4);

node.append("text")
.attr("x", 8)
.attr("y", "0.31em")
.text(d => d.id)
.clone(true).lower()
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-width", 3);

simulation.on("tick", () => {
node.attr("transform", d => `translate(${d.x},${d.y})`);
link.attr("x1", d => d.source.x)
.attr("x2", d => d.target.x)
.attr("y1", d => d.source.y)
.attr("y2", d => d.target.y)
});
simulation.force("link")
.links(links);

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

return svg.node();
}
Insert cell
data = {
const data = {
"nodes": [
{"id": "Rust", "group": 1},
{"id": "Actix", "group": 1},
{"id": "Rocket", "group": 1},
{"id": "SQL", "group": 2},
{"id": "PostgreSQL", "group": 2},
{"id": "Sequelize", "group": 2},
{"id": "Diesel", "group": 2},
{"id": "JavaScript", "group": 3},
{"id": "React", "group": 3},
{"id": "TypeScript", "group": 3},
{"id": "Node.js", "group": 3},
{"id": "Express.js", "group": 3},
{"id": "Next.js", "group": 3},
{"id": "RedwoodJS", "group": 3},
{"id": "WebAssembly", "group": 4},
{"id": "GraphQL", "group": 5},
{"id": "Prisma", "group": 5},
{"id": "Git", "group": 6},
{"id": "Github", "group": 6},
{"id": "Python", "group": 7},
{"id": "Django", "group": 7},
{"id": "Flask", "group": 7}
],
"links": [
{"source": "Rust", "target": "Actix", "value": 1},
{"source": "Rust", "target": "Rocket", "value": 1},
{"source": "Rust", "target": "PostgreSQL", "value": 1},
{"source": "Rust", "target": "Diesel", "value": 1},
{"source": "Rust", "target": "WebAssembly", "value": 1},
{"source": "SQL", "target": "PostgreSQL", "value": 1},
{"source": "SQL", "target": "Sequelize", "value": 1},
{"source": "SQL", "target": "Diesel", "value": 1},
{"source": "SQL", "target": "JavaScript", "value": 1},
{"source": "SQL", "target": "GraphQL", "value": 1},
{"source": "SQL", "target": "Prisma", "value": 1},
{"source": "JavaScript", "target": "React", "value": 1},
{"source": "JavaScript", "target": "TypeScript", "value": 1},
{"source": "JavaScript", "target": "Node.js", "value": 1},
{"source": "JavaScript", "target": "Express.js", "value": 1},
{"source": "JavaScript", "target": "Next.js", "value": 1},
{"source": "JavaScript", "target": "RedwoodJS", "value": 1},
{"source": "Git", "target": "Github", "value": 1},
{"source": "Python", "target": "Django", "value": 1},
{"source": "Python", "target": "Flask", "value": 1}

]
}
return data
};
Insert cell
height = 600
Insert cell
color = {
const scale = d3.scaleOrdinal(d3.schemeCategory10);
return d => scale(d.group);
}
Insert cell
drag = simulation => {
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;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
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