chart = {
const k = 10
let clicked = false
let links = [];
const unfilteredNodes = Object.entries(data.entries).map(([key, value]) => ({ id: key, value: value }));
const nodes = unfilteredNodes.filter(node => node.value >= 8000);
const all_subjects = nodes.map(entry => entry.id);
Object.entries(data.links).forEach(([subject, subject_detail]) => {
if(all_subjects.indexOf(subject) == -1) {
return;
}
for(let i = 0; i < subject_detail.categories.length; i++) {
let related_subject = subject_detail.categories[i];
if(all_subjects.indexOf(related_subject) == -1) {
return;
}
let value = subject_detail.series[i];
if(i > 0 && value < 5000) {
continue;
}
var source, target;
if(related_subject < subject) {
source = related_subject;
target = subject;
} else {
source = subject;
target = related_subject;
}
// check if link already exists
let link = links.find(link => link.source === source && link.target === target);
if(!link) {
link = { source, target, value };
links.push(link);
}
}
});
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(-100))
.force("collide", d3.forceCollide(function (d) { return nodeRadiusScale(d.value) + 20 }))
.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", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => 0.001 * d.value);
const nodeG = svg.append("g")
.selectAll("g")
.data(nodes)
.join("g")
.call(drag(simulation))
.on("click", d => (zoom(d), d3.event.stopPropagation()));
nodeG.append('circle')
.attr("r", d => nodeRadiusScale(d.value))
.attr("fill", color)
nodeG.append('text')
.style("fill", "black")
.attr("font-size", "0.8em")
.attr("text-anchor", "middle")
.attr("alignment-baseline", "middle")
.attr("dy", d => -nodeRadiusScale(d.value)-5)
.text(d=>d.id)
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);
nodeG.attr("transform", d => `translate(${d.x}, ${d.y})`)
});
invalidation.then(() => simulation.stop());
function zoom(focus) {
const transition = svg.transition()
.duration(750)
.attr("transform", function(){
clicked = !clicked
if(clicked){
return `translate(${-(focus.x-width/2)*k},${-(focus.y-height/2)*k})scale(${k})`
} else {
return `translate(${0},${0})})scale(1)`
}
});
}
return svg.node();
}