chart = {
const nodes = network.nodes,
links = network.links;
const context = DOM.context2d(width, height);
const simulation = d3
.forceSimulation(nodes)
.force("charge", d3.forceManyBody().strength(-2))
.force(
"x",
d3.forceX(width / 2).strength(d => centerStrenghtScale(d.value))
)
.force(
"y",
d3.forceY(height / 2).strength(d => centerStrenghtScale(d.value))
)
.force(
"link",
d3
.forceLink(links)
.id(d => d.id)
.distance(10)
)
.force("boundary", forceBoundary(3, 3, width, height));
function ticked() {
context.clearRect(0, 0, width, height);
context.beginPath();
if (simulation.alpha() < 0.5) {
links.forEach(drawLink);
}
context.strokeStyle = "#aaa";
context.stroke();
context.strokeStyle = "#fff";
for (const node of nodes) {
context.beginPath();
drawNode(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, size(d.value), 0, 2 * Math.PI);
}
simulation.on("tick", ticked);
invalidation.then(() => simulation.stop());
ticked();
return d3
.select(context.canvas)
.call(drag(simulation))
.node();
}