Published
Edited
Oct 17, 2021
Insert cell
Insert cell
Insert cell
Insert cell
data = {
const attachment = await FileAttachment("iowa-2016-results.csv").text();
const data = await d3.csvParse(attachment, d3.autoType);
data.forEach(
d =>
(d['parent'] =
d['total_bachelors_or_higher'] / d['universe'] > 0.25 ? 'high' : 'low')
);
data.push({ parent: "state", fips: "high", margin: 0 });
data.push({ parent: "state", fips: "low", margin: 0 });
data.push({ parent: null, fips: "state", margin: 0 });
return data.sort((a, b) => b.margin - a.margin);
}
Insert cell
stratify = d3
.stratify()
.id(d => d.fips)
.parentId(d => d.parent)
Insert cell
stratify(data).sum(d => d.margin)
Insert cell
treemap = data =>
d3
.treemap()
.tile(d3.treemapBinary)
.size([width, height])
.paddingInner(1)
.paddingOuter(10)
.round(true)(
stratify(data)
.sum(d => d.margin)
.sort((a, b) => b.margin - a.margin)
)
Insert cell
// width = width
Insert cell
height = 450
Insert cell
format = d3.format(",d")
Insert cell
color = d3.scaleOrdinal(d3.schemeCategory10)
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