viewof chart = {
let selectedNode = null;
const nodeStroke = '#fff';
const nodeStrokeWidth = '1.5';
const nodeStrokeOpacity = 1;
const linkOpacity = 0.6;
const nodeDeselectOpacity = 0.4;
const linkDeselectOpacity = 0.2;
const nodeHighlightStroke = "black";
const nodeHighlightStrokeWidth = 2;
const width = 928;
const height = 680;
const color = d3.scaleOrdinal(d3.schemeCategory10);
const links = data.links.map(d => ({...d}));
const nodes = data.nodes.map(d => ({...d}));
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody())
.force("x", d3.forceX())
.force("y", d3.forceY());
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("style", "max-width: 100%; height: auto;");
svg.on('click', deselectNode);
const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", a => degreeScale(degreeMap.get(a.id)))
.attr("fill", d => color(d.group))
.on('click', clicked);
node.append("title")
.text(d => d.id);
node.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
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);
});
function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}
function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}
function dragended(event) {
if (!event.active) simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}
invalidation.then(() => simulation.stop());
function clicked(event, d) {
event.stopPropagation()
const allNodes = svg.selectAll('circle')
.attr('stroke', nodeStroke)
.attr('stroke-width', nodeStrokeWidth)
.attr('stroke-opacity', nodeDeselectOpacity)
.attr('fill-opacity', nodeDeselectOpacity);
const allLinks = svg.selectAll('line')
.attr('stroke-opacity', linkDeselectOpacity);
const currentNode = d3.select(this)
.attr('stroke', nodeHighlightStroke)
.attr('stroke-width', nodeHighlightStrokeWidth)
.attr('stroke-opacity', nodeStrokeOpacity)
.attr('fill-opacity', nodeStrokeOpacity)
selectedNode = d;
svg.property('value', Object.assign(svg.node(),
{ selectedNode: d})).dispatch('input')
}
function deselectNode(event, d) {
const allNodes = svg.selectAll('circle')
.attr('stroke', nodeStroke)
.attr('stroke-width', nodeStrokeWidth)
.attr('stroke-opacity', nodeStrokeOpacity)
.attr('fill-opacity', nodeStrokeOpacity);
const allLinks = svg.selectAll('line')
.attr('stroke-opacity', linkOpacity);
}
return Object.assign(svg.node(), {scales: {color}}, {selectedNode: null});
}