{
const svgBackgroundColor = "#152c33",
svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.style("background-color", svgBackgroundColor),
nodes = graph.nodes,
links = graph.links,
clusters = graph.clusters;
const container = svg.append("g");
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(20))
.force('collide', d3.forceCollide(d => 22))
.force('center', d3.forceCenter()
.x(width / 2)
.y(height / 2))
.on("tick", ticked);
const link = container.append("g")
.attr("fill", "none")
.attr("stroke-width", 1.5)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke", d => clusterColors[clusters.indexOf(parseInt(d.cluster, 10))]);
const node = container.append("g")
.attr("fill", "gray")
.selectAll("g")
.data(nodes)
.join("g");
const circles = node.append("circle")
.attr("stroke", d => clusterColors[clusters.indexOf(parseInt(d.cluster, 10))])
.attr("stroke-width", 1.5)
.attr("r", d => d.radius)
.attr("radius", d => d.radius)
.attr('fill', d => clusterColors[clusters.indexOf(parseInt(d.cluster, 10))])
const text = node.append("text")
.text(d => d.name)
.attr("fill", svgBackgroundColor)
.style("opacity", 0)
.attr("text-anchor", "middle")
.attr("dy", 5)
.attr("font-size", d => isNaN(d.name) == true ? "1.1em" : "0.8em")
.attr("font-family", "helvetica")
.attr("font-weight", "bold")
function ticked() {
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);
circles
.attr("cx", d => Math.max(d.radius, Math.min(width - d.radius, d.x)))
.attr("cy", d => Math.max(d.radius, Math.min(height - d.radius, d.y)));
text
.attr("x", d => Math.max(d.radius, Math.min(width - d.radius, d.x)))
.attr("y", d => Math.max(d.radius, Math.min(height - d.radius, d.y)));
}
function hideNumbers(){
text.style("opacity", 0);
let button = d3.select("button#hideNumbers");
button.attr("id", "showNumbers");
button.on("click", showNumbers);
button.html("Show Labels");
}
function showNumbers(){
text.style("opacity", 1);
let button = d3.select("button#showNumbers");
button.attr("id", "hideNumbers");
button.on("click", hideNumbers);
button.html("Hide Labels");
}
d3.select("button#showNumbers").on("click", showNumbers);
yield svg.node();
}