chrt = {
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).distance(d => linkScale(data.links[d.index].score))
)
.force("charge", d3.forceManyBody().strength(-250))
.force("center", d3.forceCenter(width / 2, height / 2).strength(0.4));
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
const tooltip = d3
.select('body')
.append('div')
.attr('id', 'barchart-tooltip')
.style("font-family", "'Work Sans', sans-serif")
.style('position', 'absolute')
.style('z-index', '1')
.style('visibility', 'hidden')
.style('padding', '10px')
.style('background', 'rgba(0,0,0,0.6)')
.style('font-size', '16px')
.style('width', '300px')
.style('border-radius', '4px')
.style('color', '#fff');
const link = svg
.append("g")
.attr("stroke-opacity", 0.3)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke", (d, i) => linkcolor(data.links[i].score))
.attr("stroke-width", d => Math.sqrt(d.score) * 0.15);
const node = svg
.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 5)
.attr("fill", color)
.attr("fill", (d, i) => colorScale(data.nodes[i].bloodType))
.call(drag(simulation))
// Tooltip interaction
.on("mouseover", function(e, d, i) {
let tooltipWidth = tooltip.node().offsetWidth;
let tooltipHeight = tooltip.node().offsetHeight;
tooltip
.style("left", e.pageX - tooltipWidth / 2 + 'px')
.style("top", e.pageY - tooltipHeight - 10 + 'px')
.style('visibility', 'visible').html(`<table>
<tbody>
<tr>
<td></td>
<td><b> Patient </b></td>
<td><b>Donor </b></td>
</tr>
<tr>
<td><b>🚻 Gender</b></td>
<td>${d.gender === 'M' ? 'Male' : 'Female'}</td>
<td>${d.donor_gender === 'M' ? 'Male' : 'Female'}</td>
</tr>
<tr>
<td><b>🩸 Blood type </b></td>
<td>${d.bloodType}</td>
<td>${d.donor_bloodType}</td>
</tr>
</tbody>
</table>`);
d3.select(this).attr("fill", "steelblue");
})
.on('mousemove', function(e) {
let tooltipWidth = tooltip.node().offsetWidth;
let tooltipHeight = tooltip.node().offsetHeight;
tooltip
.style("left", e.pageX - tooltipWidth / 2 + 'px')
.style("top", e.pageY - tooltipHeight - 10 + 'px');
})
.on("mouseout", function(e, d) {
tooltip.style('visibility', 'hidden');
d3.select(this).attr("fill", "purple");
});
var texts = svg
.selectAll(".texts")
.data(nodes)
.join("text")
.attr("dy", -4)
.attr("font-family", "sans-serif")
.attr('font-size', '10px')
.attr('text-anchor', 'middle')
.text(d => d.index);
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);
texts.attr("x", d => d.x).attr("y", d => d.y);
});
invalidation.then(() => simulation.stop());
return svg.node();
}