function drawChart() {
const links = dlinks.map(d => Object.create(d));
const nodes = dnodes.map(d => Object.create(d));
console.log(nodes);
const radius = 7;
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.name))
.force("charge", d3.forceManyBody().strength(-175))
.force("center", d3.forceCenter(width/2, height/2));
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
const link = svg.append("g")
.attr("stroke", "#aaa")
.attr("stroke-opacity", 0.3)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value) / 2);
const node = svg.append("g")
.attr("stroke", "#000")
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d => d.radius)
.attr("fill", d => statusColor(d.relative))
.call(drag(simulation))
.on('mouseover.fade', fade(0.1))
.on('mouseout.fade', fade(1));
const textElems = svg.append('g')
.selectAll('text')
.data(nodes)
.join('text')
.text(d => d.name)
.attr('font-size',10)
.attr('font-size',10)
.call(drag(simulation))
.on('mouseover.fade', fade(0.1))
.on('mouseout.fade', fade(1));
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", function(d) { return d.x = Math.max((d.radius+1), Math.min(width - (d.radius+1), d.x)); })
.attr("cy", function(d) { return d.y = Math.max((d.radius+1), Math.min(height - (d.radius+1), d.y)); });
textElems
.attr("x", d => d.x + 10)
.attr("y", d => d.y);
});
function fade(opacity) {
return d => {
console.log("d", d)
node.style('opacity', function (o) { return isConnected(d, o) ? 1 : opacity });
textElems.style('visibility', function (o) { return isConnected(d, o) ? "visible" : "hidden" });
link.style('stroke-opacity', o => (o.source === d || o.target === d ? 1 : opacity));
if(opacity === 1){
node.style('opacity', 1)
textElems.style('visibility', 'visible')
link.style('stroke-opacity', 0.3)
}
};
}
const linkedByIndex = {};
links.forEach(d => {
linkedByIndex[`${d.source.index},${d.target.index}`] = 1;
});
function isConnected(a, b) {
return linkedByIndex[`${a.index},${b.index}`] || linkedByIndex[`${b.index},${a.index}`] || a.index === b.index;
}
invalidation.then(() => simulation.stop());
return svg.node()
}