chart = {
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(flinks).id(d => d.id)
.distance(230))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const g = svg.append("g")
const link = g.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(flinks)
.join("line")
.attr("stroke-width", d => Math.sqrt(40/d.MR))
.style("stroke", color_link);
const globalNode = g.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5);
const tooltip = d3tip()
.style('color', 'white')
.style('background-color', 'rgba(0,0,0,0.5)')
.style('border-radius', '4px')
.style("padding", "6px")
.style('float', 'left')
.style('font-family', 'Nunito')
.style("font-size", "5px")
.offset([-10,50])
.html(d => `
<div style='float: right'>
Gene Name: ${d.alias} <br/>
AGI Locus: ${d.id} <br/>
</div>`)
svg.call(tooltip)
const node = globalNode
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 8)
.attr("fill", color)
.call(drag(simulation))
.on('mouseover.fade', fade(0.1))
.on('mouseover', tooltip.show)
.on('mouseout.fade', fade(1))
.on('mouseout', tooltip.hide);
function fade(opacity) {
return d => {
node.style('opacity', function (o) { return isConnected(d, o) ? 1 : opacity });
link.style('stroke-opacity', o => (o.source === d || o.target === d ? 1 : opacity));
if(opacity === 1){
node.style('opacity', 1)
link.style('stroke-opacity', 1)
}
};
}
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;
}
const textElements = g.append('g')
.selectAll('text')
.data(nodes)
.enter().append('text')
.text(node => node.alias)
.attr('font-size', 10)
.attr("font-family", "Nunito")
.attr("fill", "#555")
.attr('dx', 10)
.attr('dy', 4)
let zoomLvl = 1;
let lastK = 0;
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);
textElements
.attr("x", d => d.x)
.attr("y", d => d.y);
node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
});
invalidation.then(() => simulation.stop());
svg.call(d3.zoom()
.extent([[0, 0], [width, height]])
.scaleExtent([1, 80])
.on("zoom", zoomed));
function zoomed() {
let e = d3.event
if(e.transform.k > 2 && lastK != e.transform.k){
lastK = e.transform.k;
console.log("zoomed");
zoomLvl =Math.log2(e.transform.k);
globalNode.attr("stroke-width", 1.5/zoomLvl );
link.attr("stroke-width", d => Math.sqrt(d.value)/(zoomLvl));
}
g.attr("transform", e.transform);
}
return svg.node();
}