graphic = {
const width = 500;
const height = 500;
const text = await FileAttachment("Dummy Graph@2.svg").text();
const document = new DOMParser().parseFromString(text, "image/svg+xml");
const svg = d3.select(document.documentElement).remove();
const root = svg.selectAll("g").filter(function () {
return this.id.includes("Dummy");
})[0];
const nodeG = svg.selectAll("g").filter(function () {
return (
!this.id.includes("Dummy") &&
d3.select(this).selectAll("ellipse").size() > 0
);
});
const edgeG = svg.selectAll("g").filter(function () {
return (
!this.id.includes("Dummy") &&
d3.select(this).selectAll("ellipse").size() == 0
);
});
console.log("Nodes:",nodeG);
console.log("Edges:",edgeG);
nodeG.selectAll("path").remove();
nodeG.selectAll("ellipse").each(function () {
const g = d3.select(this);
const gp = d3.select(this.parentNode);
const x = g.attr("cx");
const y = g.attr("cy");
gp.append("text")
.text(gp.attr("id"))
.attr("x", x)
.attr("y", y)
.attr("text-anchor", "middle")
.attr("dominant-baseline", "middle")
.style("fill", "black");
});
edgeG.selectAll("path").each(function () {
const g = d3.select(this);
const gp = d3.select(this.parentNode);
const x = g.attr("cx");
const y = g.attr("cy");
g.attr("fill",function(){
return (gp.attr("id")==="A-C")?"red":g.attr("fill")
})
});
nodeG.selectAll("ellipse").on("click", clicked);
svg.call(
d3
.zoom()
.extent([
[0, 0],
[width, height]
])
.scaleExtent([-17, 18])
.on("zoom", zoomed)
);
function clicked(event, d) {
if (event.defaultPrevented) return;
d3.select(this).transition().attr("rx", 76).attr("ry", 76);
}
function dragstarted() {
d3.select(this).raise();
}
function dragged(event, d) {
d3.select(this)
.attr("cx", (d.x = event.x))
.attr("cy", (d.y = event.y));
}
function dragended() {}
function zoomed({ transform }) {
d3.selectAll("g").attr("transform", transform);
}
return svg.node();
}