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())
.force("center", d3.forceCenter(width / 2, height / 2));
const context = DOM.context2d(width, height);
simulation.on("tick", ticked);
invalidation.then(() => simulation.stop());
return d3.select(context.canvas).call(drag(simulation)).node();
function ticked() {
context.clearRect(0, 0, width, height);
context.beginPath();
links.forEach(drawLink);
context.strokeStyle = "#aaa";
context.stroke();
context.strokeStyle = "#fff";
for (const node of nodes) {
context.beginPath();
drawNode(node)
context.fillStyle = color(node);
context.fill();
context.stroke();
}
}
function drawLink(d) {
context.moveTo(d.source.x, d.source.y);
context.lineTo(d.target.x, d.target.y);
}
function drawNode(d) {
context.moveTo(d.x + 3, d.y);
context.arc(d.x, d.y, 3, 0, 2 * Math.PI);
}
}