Public
Edited
Nov 8, 2022
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

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

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

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

const dot = svg.append("g")
.attr("fill", "currentColor")
.attr("stroke", "white")
.selectAll("circle")
.data(data)
.join("circle")
.attr("r", 3)
.attr("cx", d => x(d.POP_1980))
.attr("cy", d => y(d.R90_10_1980));

const label = svg.append("g")
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 4)
.selectAll("text")
.data(data.filter(d => d.highlight))
.join("text")
.call(text => text.append("tspan")
.attr("dy", "-0.6em")
.attr("x", d => x(d.POP_1980))
.attr("y", d => y(d.R90_10_1980))
.text(d => d.nyt_display)
.clone(true)
.attr("fill", "currentColor")
.attr("stroke", "none"))
.selectAll("tspan");

return Object.assign(svg.node(), {
update(year) {
const t = svg.transition()
.duration(750);

dot.transition(t)
.attr("cx", d => x(d[`POP_${year}`]))
.attr("cy", d => y(d[`R90_10_${year}`]));

label.transition(t)
.attr("x", d => x(d[`POP_${year}`]))
.attr("y", d => y(d[`R90_10_${year}`]));
}
});
}
Insert cell
chart.update(year)
Insert cell
data = Object.assign(d3.csvParse(await FileAttachment("metros.csv").text(), d3.autoType), {
x: "Population →",
y: "↑ Inequality"
})
Insert cell
x = d3.scaleLog()
.domain(padLog(d3.extent(data.flatMap(d => [d.POP_1980, d.POP_2015])), 0.1))
.rangeRound([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain(padLinear(d3.extent(data.flatMap(d => [d.R90_10_1980, d.R90_10_2015])), 0.1))
.rangeRound([height - margin.bottom, margin.top])
Insert cell
function padLinear([x0, x1], k) {
const dx = (x1 - x0) * k / 2;
return [x0 - dx, x1 + dx];
}
Insert cell
function padLog(x, k) {
return padLinear(x.map(Math.log), k).map(Math.exp);
}
Insert cell
grid = g => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom))
.call(g => g.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right));
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80, ".1s"))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", width)
.attr("y", margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text(data.x))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(data.y))
Insert cell
height = 640
Insert cell
margin = ({top: 24, right: 10, bottom: 34, left: 40})
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