chart = {
const min_node_popularity = 3
const min_link_value = 3
const nodes = data.items.nodes.filter(node => node.popularity > min_node_popularity ).map(d => Object.create(d));
const links = data.items.links.filter(link => link.value > min_link_value ).map(d => Object.create(d));
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(d => -50*Math.sqrt(d.popularity)))
.force("center", d3.forceCenter(width / 2, height / 2));
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const link = svg.append("g")
.attr("stroke", "#999")
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-opacity", d => 0.2)
.attr("stroke-width", d => d.value*0.1);
const node = svg.append("g")
.attr("class", "nodes")
.attr("stroke", "#fff")
.attr("stroke-width", 0.1)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d => Math.sqrt(d.popularity)*2)
.attr("fill", color)
.call(drag(simulation));
node.append("title")
.text(d => d.id+": "+d.popularity+" mentions");
const text = svg.selectAll("nodes")
.data(nodes)
.join("text")
.attr("x", (d, i) => i * 15)
.attr("y", 17)
.attr("dy", "0.2em")
.attr("font-family", "Verdana")
.attr("font-size", d => Math.pow(d.popularity,1/6)*5)
.text(d => d.id);
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("cx", d => d.x)
.attr("cy", d => d.y);
text
.attr("x", d => d.x)
.attr("y", d => d.y);
});
invalidation.then(() => simulation.stop());
return svg.node();
}