chart = {
const context = DOM.context2d(width, height);
const nodes = data.map(Object.create);
const simulation = d3.forceSimulation(nodes)
.alphaTarget(0.3)
.velocityDecay(0.1)
.force("x", d3.forceX().strength(0.01))
.force("y", d3.forceY().strength(0.01))
.force("collide", d3.forceCollide().radius(d => d.r + 3).iterations(3))
.force("center", d3.forceCenter(width / 2, height / 2));
function pointed(event) {
const [x, y] = d3.pointer(event);
for (const d of nodes) {
const xPos = (x);
const yPos = (y);
const magnitude = Math.sqrt(Math.pow((d.x - xPos), 2) + Math.pow((d.y - yPos), 2));
const minSize = 5;
const r = d.startingSize + 1000 / magnitude;
const maxSize = 50;
d.r = r > maxSize ? maxSize : r;
}
simulation.force("collide").initialize(nodes);
}
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
svg.on("touchmove", event => event.preventDefault())
svg.on("pointermove", event => pointed(event));
const node = svg.append("g")
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d => d.r)
.attr("fill", d => color(d.group));
node.append("title")
.text(d => d.id);
simulation.on("tick", () => {
node
.attr("r", d => d.r)
.attr("cx", d => d.x)
.attr("cy", d => d.y);
});
invalidation.then(() => simulation.stop());
return svg.node();
}