function draw_network_viz() {
let svg = d3.create("svg").attr("width", w).attr("height", h);
let link = svg.append("g").attr("id", "links");
link
.selectAll("line.link")
.data(G.links)
.join("line")
.attr("class", "link")
.attr("id", (d) => d.cy_data.id)
.attr("stroke", "black")
.attr("stroke-width", 1)
.attr("stroke-opacity", 0.1)
.attr("x1", (d) => d.source.x)
.attr("y1", (d) => d.source.y)
.attr("x2", (d) => d.target.x)
.attr("y2", (d) => d.target.y);
let node = svg.append("g").attr("stroke", "#fff").attr("stroke-width", 1.5);
node
.selectAll("circle")
.data(G.nodes)
.join("circle")
.attr("class", (o) => (o.valence == G.max_valence ? "hub" : "port"))
.attr("id", (d) => d.cy_data.id)
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y)
.attr("r", (o) => 6 * (o.valence / G.max_valence) ** 1)
.attr("fill", "black");
node
.selectAll("circle.hub")
.nodes()
.forEach(function (c, i) {
if (i == 0) {
d3.select(c).attr("r", 8).attr("fill", "red").raise();
} else {
d3.select(c).attr("class", "port");
}
});
svg
.on("touchmove", (evt) => evt.preventDefault())
.on("pointerenter pointermove", function (evt) {
node
.selectAll("circle.port")
.attr("r", (o) => 6 * (o.valence / G.max_valence) ** 1)
.attr("fill", "black");
link
.selectAll("line")
.attr("stroke", "black")
.attr("stroke-width", 1)
.attr("stroke-opacity", 0.1);
let [x, y] = d3.pointer(evt);
let closest = qt.find(x, y);
if ((closest.x - x) ** 2 + (closest.y - y) ** 2 < 1000) {
let p = di.pathTo(`#n${closest.index}`);
let shortest_path = {
nodes: p.nodes().map((n) => n.id()),
edges: p.edges().map((e) => e.id())
};
let len = shortest_path.nodes.length;
shortest_path.nodes.forEach((id, i) =>
svg
.select(`#${id}`)
.attr("r", i == 0 ? 8 : 5)
.attr("fill", i == 0 ? "red" : i == len - 1 ? "green" : "blue")
);
shortest_path.edges.forEach((id) =>
svg
.select(`#${id}`)
.attr("stroke-width", 2)
.attr("stroke", "blue")
.attr("stroke-opacity", 1)
);
}
})
.on("pointerleave", function () {
node
.selectAll("circle.port")
.attr("r", (o) => 6 * (o.valence / G.max_valence) ** 1)
.attr("fill", "black");
link
.selectAll("line")
.attr("stroke", "black")
.attr("stroke-width", 1)
.attr("stroke-opacity", 0.1);
});
return svg.node();
}