Public
Edited
Dec 13
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const arrowId = DOM.uid("arrow");
const gradientIds = data.map(() => DOM.uid("gradient"));

svg.append("defs")
.append("marker")
.attr("id", arrowId.id)
.attr("markerHeight", 10)
.attr("markerWidth", 10)
.attr("refX", 5)
.attr("refY", 2.5)
.attr("orient", "auto")
.append("path")
.attr("fill", endColor)
.attr("d", "M0,0v5l7,-2.5Z"); // arrow head

svg.append("defs")
.selectAll("linearGradient")
.data(data)
.join("linearGradient")
.attr("id", (d, i) => gradientIds[i].id)
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", d => x(d.x1))
.attr("x2", d => x(d.x2))
.attr("y1", d => y(d.y1))
.attr("y2", d => y(d.y2))
.call(g => g.append("stop").attr("stop-color", startColor).attr("stop-opacity", 0.5))
.call(g => g.append("stop").attr("offset", "100%").attr("stop-color", endColor));

svg.append("g")
.call(grid);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);

svg.append("g")
.attr("fill", "none")
.selectAll("path")
.data(data)
.join("path")
.attr("stroke", (d, i) => gradientIds[i])
.attr("marker-end", arrowId)
.attr("d", d => arc(x(d.x1), y(d.y1), x(d.x2), y(d.y2)));

svg.append("g")
.attr("fill", "currentColor")
.selectAll("circle")
.data(data)
.join("circle")
.attr("r", 1.75)
.attr("cx", d => x(d.x1))
.attr("cy", d => y(d.y1));

svg.append("g")
.attr("fill", "currentColor")
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 4)
.attr("paint-order", "stroke fill")
.selectAll("text")
//.data(data.filter(d => d.highlight))
.data(data.filter(d => true))
.join("text")
.attr("dy", "0.1em")
.attr("x", d => x(d.x1))
.attr("y", d => y(d.y1))
.text(d => d.cc);

return svg.node();
}
Insert cell
data = Object.assign( cc_list_data, {'x': "prefix count", 'y': "Distance to RIS (AS Hops)"} )
Insert cell
risDistanceCompare20222023 = FileAttachment("ris-distance-compare@3.2022.2023.json").json()
Insert cell
risDistanceCompare1202301202307 = FileAttachment("ris-distance-compare@1.2023-01.2023-07.json").json()
Insert cell
risDistanceCompare = FileAttachment("ris-distance-compare@2.2023.09.01.2024.12.11.json").json()
Insert cell
d1 = risDistanceCompare['meta']['dates'][0].replaceAll('.','-')
Insert cell
d2 = risDistanceCompare['meta']['dates'][1].replaceAll('.','-')
Insert cell
cc_list_data = {
const data_src = risDistanceCompare
if ( cc_list == '' ) {
return data_src.results
}
var countries = cc_list.split(',')
var d = []
const cc_set = new Set( countries )
for (var cc_obj_idx in data_src.results ) {
const cc_obj = data_src.results[ cc_obj_idx ]
if ( cc_set.has( cc_obj['cc'] ) ) {
d.push( cc_obj )
}
}
return d
}
Insert cell
x = d3.scaleLog()
.domain(padLog(d3.extent(data.flatMap(d => [d.x1, d.x2])), 0.1))
.rangeRound([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain(padLinear(d3.extent(data.flatMap(d => [d.y1, d.y2])), 0.1))
.rangeRound([height - margin.bottom, margin.top])
Insert cell
function padLinear([x0, x1], k) {
const dx = (x1 - x0) * k / 2;
return [x0 - dx, x1 + dx];
}
Insert cell
function padLog(x, k) {
return padLinear(x.map(Math.log), k).map(Math.exp);
}
Insert cell
endColor = d3.schemeCategory10[3]
Insert cell
startColor = d3.schemeCategory10[1]
Insert cell
function arc(x1, y1, x2, y2) {
const r = Math.hypot(x1 - x2, y1 - y2) * 2;
return `
M${x1},${y1}
A${r},${r} 0,0,1 ${x2},${y2}
`;
}
Insert cell
grid = g => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom))
.call(g => g.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right));
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80, ".1s"))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", width)
.attr("y", margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text(data.x))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(data.y))
Insert cell
height = 640
Insert cell
margin = ({top: 24, right: 10, bottom: 34, left: 40})
Insert cell
d3 = require("d3@6")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more