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(-600))
.force("x", d3.forceX())
.force("y", d3.forceY());
const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height]);
svg.call(d3.zoom()
.extent([[0, 0], [width, height]])
.on("zoom", zoomed));
const g = svg.append("g");
const link = g.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.2)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", 7)
.on("mouseover", function(d) {
d3.select(this).style("stroke", "red");
})
.on("mouseout", function(d) {
d3.select(this).style("stroke", "#999");
});;
link.append("title")
.text(d => d.sentence);
link.on("click", function(d) {
console.log(d => d.sentence);
});
const node = g.append("g")
.attr("class", "nodes")
.selectAll("g")
.data(nodes)
.enter().append("g")
const circles = node.append("circle")
.attr("stroke", "#fff")
.attr("stroke-width", 0)
.attr("r", 10)
.attr("fill", color)
.call(drag(simulation));
node.append("text")
.text(function(d) {
return d.label;
})
.attr('font-size', 14)
.attr('x', 8)
.attr('y', 0);
node.append("title")
.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("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
})
});
invalidation.then(() => simulation.stop());
function zoomed({transform}) {
g.attr("transform", transform);
}
return svg.node();
}