chart = {
const links = communityData.links;
const nodes = communityData.nodes;
const simulation = d3.forceSimulation(nodes)
.force('link', d3.forceLink(links).id(d => d.id))
.force('charge', d3.forceManyBody())
.force("center", d3.forceCenter(dim / 2, dim / 2));
const svg = d3.create('svg')
.attr('viewBox', [0, 0, dim, dim]);
svg.append("defs")
.append("clipPath")
.attr("id", "clip-circle")
.append("circle")
.attr("cx", imgRadius)
.attr("cy", imgRadius)
.attr("r", imgRadius);
const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.1)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));
const node = svg
.append("g")
.attr("stroke", "orange")
.attr("stroke-width", 1.5)
.selectAll("g")
.data(nodes)
.join("g")
.call(drag(simulation));
node.append("circle")
.attr("r", weightRadius)
.attr("fill", "orange")
node.append("defs")
.append("clipPath")
.attr("id", (d) => "clip-" + d.id)
.append("circle")
.attr("cx", weightRadius)
.attr("cy", weightRadius)
.attr("r", weightRadius);
node.append("title")
.text(d => d.name);
node
.append("g")
.attr("clip-path", (d) => `url(#clip-${d.id})`)
.attr("transform", (d) => {
const offset = ((d.weight / 10) + imgRadius) / 2;
return `translate(${-offset}, ${-offset})`;
})
.append("image")
.attr(
"xlink:href",
(d) => d.img
)
.attr("width", (d) => (d.weight / 10) + imgRadius);
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);
node.attr("transform", (d) => `translate(${d.x},${d.y})`);
});
invalidation.then(() => simulation.stop());
return svg.node();
}