Published unlisted
Edited
Jan 6, 2021
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
fe_chart = build_net(fe_data)
Insert cell
Insert cell
wa_chart = build_net(wa_data)
Insert cell
Insert cell
ha_chart = build_net(ha_data)
Insert cell
Insert cell
mb_chart = build_net(mb_data)
Insert cell
Insert cell
function build_net(data) {

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).id(d => d.name).distance(150))
.force("charge", d3.forceManyBody().strength(-200))
.force('center', d3.forceCenter(width/2, height/2).strength(0.5))
//.force("x", d3.forceX(width/2).strength(0.01))
//.force("y", d3.forceY(height/2).strength(0.01));
// .force('collision', d3.forceCollide().radius(function(d) {
// return (10)
// }).strength(15));
.force('collision', d3.forceCollide(25));

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("font", "14px sans-serif");

// Per-type markers, as they don't inherit styles.
svg.append("defs").selectAll("marker")
.data(links)
.join("marker")
.attr("id", d => `arrow-${d}`)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 7)
.attr("refY", 0)
.attr("markerWidth", 4)
.attr("markerHeight", 4)
.attr("orient", "auto")
.append("path")
.attr("fill", "black")
.attr("d", "M0,-5L10,0L0,5");

const link = svg.append("g")
.attr("fill", "none")
.selectAll("path")
.data(links)
.join("path")
.attr("stroke", d => "black")
.attr("stroke-width", function(d) { return d.count; })
.attr("stroke-opacity", 0.75)
.attr("marker-end", d => `url(${new URL(`#arrow-${d}`, location)})`);

const node = svg.append("g")
.attr("stroke-linecap", "round")
.selectAll("g")
.data(nodes)
.join("g")
.attr("fill", function(d) { return d.color; })
.call(drag(simulation));

node.append("circle")
.attr("stroke", "white")
.attr("stroke-width", 1.5)
.attr("r", function(d) {
d.radius = Math.sqrt(d.count)*5+10;
return d.radius;});

node.append("text")
.attr("x", 0)
.attr("y", "0.31em")
.attr("text-anchor","middle")
.style('fill', 'black')
.text(d => d.name)
.clone(true).lower()
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-width", 3);

simulation.on("tick", () => {
link.attr("d", linkArc);
node.attr("transform", d => `translate(${d.x},${d.y})`);
node.attr("cx", function(d) { return d.x = Math.max((d.radius + 15), Math.min((width - d.radius -15), d.x)); })
.attr("cy", function(d) { return d.y = Math.max((d.radius + 15), Math.min((height - d.radius -15), d.y)); });
});

invalidation.then(() => simulation.stop());

return svg.node();

}
Insert cell
function linkArc(d) {
const r = Math.hypot(d.target.x - d.source.x, d.target.y - d.source.y);
let t_radius = d.target.radius; // nodeWidth is just a custom attribute I calculate during the creation of the nodes depending on the node width
let dx = d.target.x - d.source.x;
let dy = d.target.y - d.source.y;
let gamma = Math.atan2(dy,dx); // Math.atan2 returns the angle in the correct quadrant as opposed to Math.atan
let tx = d.target.x - (Math.cos(gamma) * (t_radius+d.count)) ; // last term is to back the arrow tip off the circle edge slightly
let ty = d.target.y - (Math.sin(gamma) * (t_radius+d.count)) ;
return `
M${d.source.x},${d.source.y}
A${r*0.8},${r*0.8} 0 0,1 ${tx},${ty}
`;
}
Insert cell
drag = simulation => {
function dragstarted(event, d) {
if (!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(event, d) {
d.fx = event.x;
d.fy = event.y;
}
function dragended(event, d) {
if (!event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
height = 600;
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
ha_data = ({"nodes":ha_nodes, "links": ha_links})

Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@6")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more