Public
Edited
Mar 10, 2023
1 fork
2 stars
Insert cell
Insert cell
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]);
// .attr("viewBox", [-width / 2, -width / 2, width, width]);
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();
}
Insert cell
data = hierarchy(await FileAttachment("my_newflared_w_color-1.json").json())
Insert cell
function hierarchy(data, delimiter = ".") {
let root;
const map = new Map;
data.forEach(function find(data) {
const {name} = data;
if (map.has(name)) return map.get(name);
const i = name.lastIndexOf(delimiter);
map.set(name, data);
if (i >= 0) {
find({name: name.substring(0, i), children: []}).children.push(data);
data.name = name.substring(i + 1);
} else {
root = data;
}
return data;
});
return root;
}
Insert cell
Insert cell
function id(node) {
return `${node.parent ? id(node.parent) + "." : ""}${node.data.name}`;
}
Insert cell
colorin = "#00f"
Insert cell
colorout = "#f00"
Insert cell
colornone = "#ccc"
Insert cell
width = 800

Insert cell
height = 800
Insert cell
radius = width / 2
Insert cell
line = d3.lineRadial()
.curve(d3.curveBundle.beta(0.85))
.radius(d => d.y)
.angle(d => d.x)
Insert cell
tree = d3.cluster()
.size([2 * Math.PI, radius - 220])
Insert cell
d3 = require("d3@5")
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