chart = {
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id).distance(50))
.force("charge", d3.forceManyBody().strength(-1000))
.force('collision', d3.forceCollide().radius(10))
.force("center", d3.forceCenter(width / 2, height / 2));
const svg = d3.select(DOM.svg(width, height));
const link = svg.append("g")
.attr("stroke-opacity", 0.2)
.selectAll("path")
.data(links)
.join("path")
.attr("stroke-width", 2)
.attr("stroke", color)
.attr("fill", "transparent");
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 0)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 6)
.attr("fill", "#555")
.call(drag(simulation))
.on('click', function(d, i) {
if(d.loc === "_blank") {
var win = window.open(d.url, '_blank');
win.focus()
} else {
window.location.href = d.url;
}
})
.on('mouseover', function(d) {
d3.select(this).style("cursor", "pointer");
})
const textElements = svg.append('g')
.selectAll('text')
.data(nodes)
.enter().append('text')
.text(node => node.id)
.attr('font-size', 10)
.attr("font-family", "Helvetica")
.attr("fill", "#555")
.attr('dx', 10)
.attr('dy', 4)
simulation.on("tick", () => {
link
.attr("d", function(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = Math.sqrt(dx * dx + dy * dy);
return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
});
textElements
.attr("x", node => node.x)
.attr("y", node => node.y)
node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
});
invalidation.then(() => simulation.stop());
return svg.node();
}