Public
Edited
Sep 22, 2023
Insert cell
Insert cell
Insert cell
originalData = FileAttachment("us-population-state-age.csv").csv({typed: true})
Insert cell
data = originalData.columns.slice(1).flatMap((age) => originalData.map((d) => ({state: d.name, age, population: d[age]})))
Insert cell
Insert cell
width = 928
Insert cell
height = 500
Insert cell
{
const margin = {top: 10, right: 10, bottom: 20, left: 40};

const svg_width = width + margin.left + margin.right;
const svg_height = height + margin.top + margin.bottom;

const svg = d3.create("svg")
.attr("width", svg_width)
.attr("height", svg_height)
.attr("viewBox", [0, 0, svg_width, svg_height])
.attr("style", "max-width: 100%; height: auto; font: 10px sans-serif;");

const chart = svg.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

return svg.node();
}
Insert cell
Insert cell
d3.group(data, d => d.state)
Insert cell
d3.rollups(data, D => -d3.sum(D, d => d.population), d => d.state)
Insert cell
d3.groupSort(data, D => -d3.sum(D, d => d.population), d => d.state)
Insert cell
{
const margin = {top: 10, right: 10, bottom: 20, left: 40};

const svg_width = width + margin.left + margin.right;
const svg_height = height + margin.top + margin.bottom;

const svg = d3.create("svg")
.attr("width", svg_width)
.attr("height", svg_height)
.attr("viewBox", [0, 0, svg_width, svg_height])
.attr("style", "max-width: 100%; height: auto; font: 10px sans-serif;");

const chart = svg.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

const x = d3.scaleBand()
.domain(d3.groupSort(data, D => -d3.sum(D, d => d.population), d => d.state))
.range([0, width])
.padding(0.1);

chart.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.call(g => g.selectAll(".domain").remove());

return svg.node();
}
Insert cell
Insert cell
// https://github.com/d3/d3-array
d3.union(data.map(d => d.age))
Insert cell
grouped = d3.index(data, d => d.state, d => d.age)
Insert cell
grouped.get("FL").get("<10")
Insert cell
series = d3.stack().keys(d3.union(data.map(d => d.age)))
.value(([, D], key) => D.get(key).population)
(d3.index(data, d => d.state, d => d.age))
Insert cell
d3.max(series, d => d3.max(d, d => d[1]))
Insert cell
{
const margin = {top: 10, right: 10, bottom: 20, left: 40};

const svg_width = width + margin.left + margin.right;
const svg_height = height + margin.top + margin.bottom;

const svg = d3.create("svg")
.attr("width", svg_width)
.attr("height", svg_height)
.attr("viewBox", [0, 0, svg_width, svg_height])
.attr("style", "max-width: 100%; height: auto; font: 10px sans-serif;");

const chart = svg.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

const x = d3.scaleBand()
.domain(d3.groupSort(data, D => -d3.sum(D, d => d.population), d => d.state))
.range([0, width])
.padding(0.1);

chart.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.call(g => g.selectAll(".domain").remove());

const y = d3.scaleLinear()
.domain([0, d3.max(series, d => d3.max(d, d => d[1]))])
.range([height, 0]);

chart.append("g")
.call(d3.axisLeft(y).ticks(null, "s")) // change display from 5,000,000 to 5M
.call(g => g.selectAll(".domain").remove());

return svg.node();
}
Insert cell
Insert cell
series[0].map(d => (d.key = series[0].key, d))
Insert cell
series[1].map(d => (d.key = series[1].key, d))
Insert cell
{
const margin = {top: 10, right: 10, bottom: 20, left: 40};

const svg_width = width + margin.left + margin.right;
const svg_height = height + margin.top + margin.bottom;

const svg = d3.create("svg")
.attr("width", svg_width)
.attr("height", svg_height)
.attr("viewBox", [0, 0, svg_width, svg_height])
.attr("style", "max-width: 100%; height: auto; font: 10px sans-serif;");

const chart = svg.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

const x = d3.scaleBand()
.domain(d3.groupSort(data, D => -d3.sum(D, d => d.population), d => d.state))
.range([0, width])
.padding(0.1);

chart.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.call(g => g.selectAll(".domain").remove());

const y = d3.scaleLinear()
.domain([0, d3.max(series, d => d3.max(d, d => d[1]))])
.range([height, 0]);

chart.append("g")
.call(d3.axisLeft(y).ticks(null, "s")) // change display from 5,000,000 to 5M
.call(g => g.selectAll(".domain").remove());

chart.append("g")
.selectAll()
.data(series)
.join("g")
.attr("fill", "steelblue")
.selectAll("rect")
.data(D => D.map(d => (d.key = D.key, d)))
.join("rect")
.attr("x", d => x(d.data[0]))
.attr("y", d => y(d[1]))
.attr("height", d => y(d[0]) - y(d[1]))
.attr("width", x.bandwidth());

return svg.node();
}
Insert cell
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
key = legend({color: chart.scales.color, title: "Age (years)"})
Insert cell
chart = {
const margin = {top: 10, right: 10, bottom: 20, left: 40};

const svg_width = width + margin.left + margin.right;
const svg_height = height + margin.top + margin.bottom;

const svg = d3.create("svg")
.attr("width", svg_width)
.attr("height", svg_height)
.attr("viewBox", [0, 0, svg_width, svg_height])
.attr("style", "max-width: 100%; height: auto; font: 10px sans-serif;");

const chart = svg.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);

const x = d3.scaleBand()
.domain(d3.groupSort(data, D => -d3.sum(D, d => d.population), d => d.state))
.range([0, width])
.padding(0.1);

chart.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.call(g => g.selectAll(".domain").remove());

const y = d3.scaleLinear()
.domain([0, d3.max(series, d => d3.max(d, d => d[1]))])
.range([height, 0]);

chart.append("g")
.call(d3.axisLeft(y).ticks(null, "s"))
.call(g => g.selectAll(".domain").remove());

const color = d3.scaleOrdinal()
.domain(series.map(d => d.key))
.range(d3.schemeSpectral[series.length])
.unknown("#ccc");

chart.append("g")
.selectAll()
.data(series)
.join("g")
// .attr("fill", "steelblue")
.attr("fill", d => color(d.key))
.selectAll("rect")
.data(D => D.map(d => (d.key = D.key, d)))
.join("rect")
.attr("x", d => x(d.data[0]))
.attr("y", d => y(d[1]))
.attr("height", d => y(d[0]) - y(d[1]))
.attr("width", x.bandwidth());

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