Published
Edited
Jun 23, 2019
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.attr("fill-opacity", 0.8)
.selectAll("path")
.data(series)
.join("path")
.attr("fill", ({key}) => color(regionByState.get(key)))
.attr("d", area)
.append("title")
.text(({key}) => key);

svg.append("g")
.attr("fill", "none")
.attr("stroke-width", 0.75)
.selectAll("path")
.data(series)
.join("path")
.attr("stroke", ({key}) => d3.lab(color(regionByState.get(key))).darker())
.attr("d", area.lineY1());

svg.append("defs")
.selectAll("path")
.data(series)
.join("path")
.attr("id", d => (d.id = DOM.uid("state")).id)
.attr("d", midline);

svg.append("g")
.style("font", "10px sans-serif")
.attr("text-anchor", "middle")
.selectAll("text")
.data(series)
.join("text")
.attr("dy", "0.35em")
.append("textPath")
.attr("href", d => d.id.href)
.attr("startOffset", d => `${Math.max(0.05, Math.min(0.95, d.offset = d3.maxIndex(d, d => d[1] - d[0]) / (d.length - 1))) * 100}%`)
.text(d => d.key);

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

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

return svg.node();
}
Insert cell
data = {
const years = d3.range(1790, 2000, 10);
const states = await d3.tsv("https://gist.githubusercontent.com/mbostock/c48a87f6af8d427526eb24a0217ad686/raw/89b065e3d423364ba3844852393c07768b82ac60/population.tsv", (d, i) => i === 0 ? null : ({name: d[""], values: years.map(y => +d[y].replace(/,/g, "") || 0)}));
states.sort((a, b) => d3.ascending(regionRank.indexOf(regionByState.get(a.name)), regionRank.indexOf(regionByState.get(b.name))) || d3.descending(d3.sum(a.values), d3.sum(b.values)));
return Object.assign(years.map((y, i) => Object.fromEntries([["date", new Date(Date.UTC(y, 0, 1))]].concat(states.map(s => [s.name, s.values[i]])))), {columns: ["date", ...states.map(s => s.name)]});
}
Insert cell
series = d3.stack()
.keys(data.columns.slice(1))
.offset(expand ? d3.stackOffsetExpand : d3.stackOffsetNone)
(data)
Insert cell
regionRank = [
"New England",
"Middle Atlantic",
"South Atlantic",
"East South Central",
"West South Central",
"East North Central",
"West North Central",
"Mountain",
"Pacific"
]
Insert cell
regionByState = {
const regions = await d3.csv("https://raw.githubusercontent.com/cphalpert/census-regions/7bdc6aa1cb0892361e90ce9ad54983041c2ad015/us%20census%20bureau%20regions%20and%20divisions.csv");
return new Map(regions.map(d => [d.State, d.Division]));
}
Insert cell
area = d3.area()
.x(d => x(d.data.date))
.y0(d => y(d[0] + !expand))
.y1(d => y(d[1] + !expand))
Insert cell
midline = d3.line()
.curve(d3.curveBasis)
.x(d => x(d.data.date))
.y(d => y((d[0] + d[1]) / 2))
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y = {
const y = d3.scaleLinear()
.range([height - margin.bottom, margin.top])
return expand
? y
: y.domain([0, d3.max(series, s => d3.max(s, d => d[1]))])
}
Insert cell
color = d3.scaleOrdinal()
.domain(regionRank)
.range(d3.schemeCategory10)
.unknown("gray")
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).ticks(10, expand ? "%" : "s"))
.call(g => g.select(".domain").remove())
Insert cell
height = 720
Insert cell
margin = ({top: 10, right: 10, bottom: 30, left: 40})
Insert cell
d3 = require("d3@5", "d3-array@2")
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