Published
Edited
May 10, 2021
4 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(-20000))
.force("center", d3.forceCenter(width / 2, height / 2));

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const nodeWidth = 200;
const nodeHeight = 100;

const link = svg.append("g")
.attr("stroke", "#000")
.attr("stroke-width", 1.5)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));
const label = svg.append("g")
.selectAll(".label")
.data(links)
.join("g");
label
.append('rect')
.attr('x', -50)
.attr('y', -15)
.attr('height', 20)
.attr('width', 100)
.attr("fill", '#fff');
label
.append('text')
.attr('text-anchor', 'middle')
.text(d => d.text);

const node = svg.append("g")
.attr("stroke", "#000")
.attr("stroke-width", 1.5)
.selectAll(".node")
.data(nodes)
.join("g")
.call(drag(simulation));
node.append('rect')
.attr("rx", 50)
.attr("width", nodeWidth)
.attr("height", nodeHeight)
.attr("fill", '#fff');

node.append("foreignObject")
.attr('x', '0')
.attr('y', '0')
.attr('width', nodeWidth)
.attr('height', nodeHeight)
.style('pointer-events', 'none')
.append('xhtml:p')
.style('width', '100%')
.style('height', '100%')
.style('padding-left', '1em')
.style('padding-right', '1em')
.style('pointer-events', 'none')
.text(d => d.text);

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);
label.attr("transform", d => "translate(" +
((d.source.x + d.target.x) / 2) + "," +
((d.source.y + d.target.y) / 2) + ")");
node.attr("transform", d => "translate(" + (d.x - (0.5 * nodeWidth)) + "," + (d.y - (0.5 * nodeHeight)) + ")");
});
invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
data = ({
nodes: [
{ id: 'a', text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do' },
{ id: 'b', text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do' },
{ id: 'c', text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do' },
{ id: 'd', text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do' },
{ id: 'e', text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do' },
{ id: 'f', text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do' },
],
links: [
{ source: 'a', target: 'b', text: 'foo' },
{ source: 'a', target: 'c', text: 'bar' },
{ source: 'a', target: 'd', text: 'foobar' },
{ source: 'a', target: 'e', text: 'foo and bar' },
{ source: 'a', target: 'f', text: 'barfoo' },
]
})
Insert cell
height = 600
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