Public
Edited
Aug 22, 2023
Paused
17 forks
49 stars
Insert cell
Insert cell
Insert cell
chart = {

// Declare the chart dimensions and margins.
const width = 928;
const height = 720;
const marginTop = 10;
const marginRight = 10;
const marginBottom = 30;
const marginLeft = 40;
// Declare the scales.
const x = d3.scaleUtc()
.domain(d3.extent(data, d => d.date))
.range([marginLeft, width - marginRight]);

const y = d3.scaleLinear()
.range([height - marginBottom, marginTop]);
const color = d3.scaleOrdinal()
.domain(regionRank)
.range(d3.schemeCategory10)
.unknown("gray");

// Create the SVG container.
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;");

// Create the areas
const area = d3.area()
.x(d => x(d.data.date))
.y0(d => y(d[0]))
.y1(d => y(d[1]));
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);

// Create the lines.
const midline = d3.line()
.curve(d3.curveBasis)
.x(d => x(d.data.date))
.y(d => y((d[0] + d[1]) / 2));

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());

// Append a path for text labels
svg.append("defs")
.selectAll("path")
.data(series)
.join("path")
.attr("id", d => (d.id = DOM.uid("state")).id)
.attr("d", midline);

// Append the labels.
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);

// Append the axes.
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0));

svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y).ticks(10, "%"))
.call(g => g.select(".domain").remove());

return Object.assign(svg.node(), {scales: {color}});
}
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

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