drawChart={
const link_links = link_graph.links.map(d => Object.create(d))
const cos_links = cos_graph.links.map(d => Object.create(d))
const nodes = link_graph.nodes.map(d => Object.create(d))
const radius = 5;
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
const simulation = d3.forceSimulation(nodes)
.force("cos_link", d3.forceLink(cos_links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(-30))
.force("cos_link", d3.forceLink().strength(0))
.force("center", d3.forceCenter(width/2, height/2))
.force("collide", d3.forceCollide().radius(30).strength(.5).iterations(1));
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
const legend = svg.append("g")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
const l_link = svg.append("g")
.attr("stroke-opacity", 0.5)
.selectAll("line")
.data(link_links)
.join("line")
.attr("stroke", 'blue')
.attr("stroke-width", 2)
;
const c_link = svg.append("g")
.attr("stroke-opacity", 0.5)
.selectAll("line")
.data(cos_links)
.join("line")
.attr("stroke", "red")
.attr("stroke-width", 1)
;
const node = svg.append("g")
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", radius)
.attr("fill", d=> node_color(d.doc_type))
.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.id)
.attr('font-size',30)
.attr('font-size',30)
.attr("pointer-events","none");
simulation.on("tick", () => {
c_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);
l_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((radius+1), Math.min(width - (radius+1), d.x)); })
.attr("cy", function(d) { return d.y = Math.max((radius+1), Math.min(height - (radius+1), d.y)); });
textElems
.attr("x", d => d.x + 10)
.attr("y", d => d.y)
.attr("visibility", "hidden")
.attr('visibility', function (d) { return d.degree > 7 ? "visible" : "hidden" });
});
function fade(opacity) {
return d => {
node.style('opacity', function (o) { return isConnected(d, o) ? 1 : opacity });
textElems.style('visibility', function (o) { return isConnected(d, o) ? "visible" : "hidden" });
c_link.style('stroke-opacity', o => (o.source === d || o.target === d ? 1 : opacity));
l_link.style('stroke-opacity', o => (o.source === d || o.target === d ? 1 : opacity));
if(opacity === 1){
node.style('opacity', 1)
textElems.style('visibility', 'hidden')
textElems.style('visibility', function (d) { return d.degree > 7 ? "visible" : "hidden" });
c_link.style('stroke-opacity', 0.3)
l_link.style('stroke-opacity', 0.3)
}
};
}
const linkedByIndex = {};
cos_links.forEach(d => {
linkedByIndex[`${d.source.index},${d.target.index}`] = 1;
});
link_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()
}