chart = {
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));
const fcolor = "#4e79a7";
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id).distance(50))
.force("charge", d3.forceManyBody().strength(-100))
.force("x", d3.forceX())
.force("y", d3.forceY());
const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height]);
const link = svg.append("g")
.attr("stroke", fcolor)
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", 1);
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 12)
.attr("fill", d => Number.isInteger(d.id) ? fcolor : "#c7bbbb")
.call(drag(simulation));
node.on('click', d => {
console.log('findind id: ' + d.id);
const connectedNodes = findConnectedNodes(d.id, []);
console.log('result: ', connectedNodes);
svg.selectAll("circle")
.classed("selected", (c) => Number.isInteger(c.id) && connectedNodes.indexOf(c.id) > -1);
svg.selectAll("line")
.classed("selected",
(c) => {
const lk = data.links[c.index];
return connectedNodes.indexOf(lk.source) > -1 || connectedNodes.indexOf(lk.target) > -1;
}, {passive: true});
});
const label = svg.append("g")
.selectAll("text")
.data(nodes)
.join("text")
.attr("font-size", 10)
.attr("fill", "white")
.attr("pointer-events", "none")
.text(d => d.id)
.call(drag(simulation));
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);
label
.attr("dx", d => d.x - (d.id < 10 ? 3 : 5))
.attr("dy", d => d.y + 3);
});
invalidation.then(() => simulation.stop());
return svg.node();
}