Public
Edited
May 6, 2023
Insert cell
Insert cell
md`by Núria Altimir`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = FileAttachment("pivoted_votes_2023.csv").csv()
Insert cell
pivoted_votes_2023.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
dendrogram = {
const root = cluster(data);
const width= 900;
const height= 400;
// var ymax = d3.max(root.descendants(), function(d) { return d.y; });
// var ymin = d3.min(root.descendants(), function(d) { return d.y; });
// var fx = d3.scaleLinear().domain([ymax, ymin]).range([0, width]);
function elbow(d) { return "M" + d.source.y/2 + "," + d.source.x + "V" + d.target.x + "H" + d.target.y/2; }
let x0 = 50;
let x1 = -x0;
root.each(d => {
if (d.x > x1) x1 = d.x;
if (d.x < x0) x0 = d.x;
});

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, x1 - x0 + root.dx * 2]);
const g = svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("transform", `translate(${root.dy / 3},${root.dx - x0})`);
const link = g.append("g")
.attr("fill", "none")
.attr("stroke", "#555")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1)
.selectAll("path")
.data(root.links())
.join("path")
.attr("d", elbow);
const node = g.append("g")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 3)
.selectAll("g")
.data(root.descendants())
.join("g")
.attr("transform", d => `translate(${d.y/2},${d.x})`);

node.append("circle")
.attr("fill", d => d.children ? "grey " : "black")
.attr("r", d => d.children ? 0 : 1);

node.append("text")
.attr("dy", "0.31em")
.attr("x", d => d.children ? -6 : 6)
.attr("text-anchor", d => d.children ? "end" : "start")
.attr("font-size", d => d.children ? "0.8em" : "0.3em")
.text(d => d.children ? " " : d.data.name)
.clone(true).lower()
.attr("fill", "black")
.attr("outline", "red")

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@6");
Insert cell
height=500;
Insert cell
width= 800;
Insert cell
html`<style>
/* required styles */
* { font-family: Arial, sans-serif;
margin: 0;
}
select { font-size: 16px;
}
</style`
Insert cell
Insert cell
cluster = data => {
const root = d3.hierarchy(data);
root.dx = 1.5;
root.dy = width / (root.height + 1);
return d3.cluster().nodeSize([root.dx, root.dy])(root);
}
Insert cell
Insert cell
path = d3.geoPath(projection)
Insert cell
projection = d3.geoNaturalEarth1()
.center([40,0])
.rotate([0, 0])
.scale(150);//(width / (2 * Math.PI));
Insert cell
Insert cell
x = d3.scaleTime()
.domain(d3.extent(myData, d => d.date))
.range([margin.left, width - margin.right]);
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(myData, d => d.deaths)]).nice()
.range([height - margin.bottom, margin.top]);
Insert cell
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0));
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain"))
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 6)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text("Total deaths per million inhabitants"));
Insert cell
lineMultiCases = d3.line()
.x(d => x(d.date))
.y(d => y(d.deaths));
Insert cell
Insert cell
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