Public
Edited
Mar 6, 2020
2 stars
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 = d3.tsvParse(await FileAttachment("population.tsv").text(), (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(d3.stackOffsetExpand)
(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]))
.y1(d => y(d[1]))
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 = d3.scaleLinear()
.range([height - margin.bottom, margin.top])
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, "%"))
.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

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