chart = {
const root = tree(bilink(d3.hierarchy(data)
.sort((a, b) => d3.ascending(a.height, b.height) || d3.ascending(a.data.name, b.data.name))));
const svg = d3.create("svg")
.attr("viewBox", [-400,-300,800,700]);
const g = svg.append("g");
function countExpander(x){
let emptyT = `Totals:
`
let t = `Totals:
`
for(const [key, value] of Object.entries(x)){
t += `${key.split('.').pop()}: ${value}
`
}
if (t !== emptyT){
return t
}else{
return t + "No traffic found..."
}
}
const node = svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 5)
.selectAll("g")
.data(root.leaves())
.join("g")
.attr("transform", d => `rotate(${d.x * 180 / Math.PI - 90}) translate(${d.y},0)`)
.append("text")
.attr("dy", "0.31em")
.attr("x", d => d.x < Math.PI ? 6 : -6)
.attr("text-anchor", d => d.x < Math.PI ? "start" : "end")
.attr("transform", d => d.x >= Math.PI ? "rotate(180)" : null)
.attr("fill", d => d.data.color)
.text(d => d.data.name)
.each(function(d) { d.text = this; })
.on("mouseover", overed)
.on("mouseout", outed)
.on("click",click)
.call(text => text.append("title").text(d => `${id(d)}
${d.outgoing.length} outgoing
${d.incoming.length} incoming
${countExpander(d.data.counts)} `)
);
const link = svg.append("g")
.attr("stroke", colornone)
.attr("fill", "none")
.selectAll("path")
.data(root.leaves().flatMap(leaf => leaf.outgoing))
.join("path")
.style("mix-blend-mode", "multiply")
.attr("d", ([i, o]) => line(i.path(o)))
.each(function(d) { d.path = this; });
function click(d) {
//window.location.href = d.data.url;
window.open(d.data.url, "_blank", "resizable,scrollbars,status");
//d3.select(this).attr("href", d=>d.url);
}
function overed(d) {
link.style("mix-blend-mode", null);
d3.select(this).attr("font-weight", 900).attr("font-size", 15);
d3.selectAll(d.incoming.map(d => d.path)).attr("stroke", colorin).raise();
d3.selectAll(d.incoming.map(([d]) => d.text)).attr("font-weight", 900).attr("font-size", 10);
d3.selectAll(d.outgoing.map(d => d.path)).attr("stroke", colorout).raise();
d3.selectAll(d.outgoing.map(([, d]) => d.text)).attr("font-weight", 900).attr("font-size", 10);
}
function outed(d) {
link.style("mix-blend-mode", "multiply");
d3.select(this).attr("font-weight", null).attr("font-size", 5);
d3.selectAll(d.incoming.map(d => d.path)).attr("stroke", null);
d3.selectAll(d.incoming.map(([d]) => d.text)).attr("font-weight", null).attr("font-size", 5);
d3.selectAll(d.outgoing.map(d => d.path)).attr("stroke", null);
d3.selectAll(d.outgoing.map(([, d]) => d.text)).attr("font-weight", null).attr("font-size", 5);
d3.select(this).attr("fill", d => d.data.color);
}
// svg.call(d3.zoom()
// .extent([[0, 0], [width, height]])
// .scaleExtent([1, 8])
// .on("zoom", zoomed));
// function zoomed() {
// node.attr("transform", d3.event.transform);
// link.attr("transform", d3.event.transform);
// }
return svg.node();
}