Public
Edited
Jan 3, 2023
Insert cell
Insert cell
Insert cell
Insert cell
d3Data = {
const data = d3.csvParse(await FileAttachment('us-population-state-age.csv').text(), d3.autoType);
const ages = data.columns.slice(1);
for (const d of data) d.total = d3.sum(ages, age => d[age]);
return ages.flatMap(age => data.map(d => ({name: d.name, age, value: d[age] / d.total})));
}
Insert cell
Insert cell
viewof aqData = aq
.fromCSV(await FileAttachment('us-population-state-age.csv').text())
.fold(aq.not('name'), { as: ['age', 'value'] })
.groupby('name')
.derive({ value: d => d.value / op.sum(d.value) })
.ungroup() // groups persist; ungroup to avoid downstream surprises
.view(10)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
xscale = domain => d3.scaleLinear()
.domain(domain)
.range([margin.left + 10, width - margin.right])
Insert cell
yscale = domain => d3.scaleBand()
.domain(domain)
.rangeRound([margin.top, height - margin.bottom])
.padding(0.1)
Insert cell
xaxis = (xscale) => {
return (g) =>
g
.attr("transform", `translate(0,${margin.top})`)
.call(d3.axisTop(xscale).ticks(null, "%"))
.call((g) =>
g
.selectAll(".tick line")
.clone()
.attr("stroke-opacity", 0.1)
.attr("y2", height - margin.bottom - margin.top)
)
.call((g) => g.selectAll(".domain").remove());
}
Insert cell
yaxis = (yscale) => {
return (g) =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(yscale))
.call((g) =>
g
.selectAll(".tick line")
.clone()
.attr("stroke-opacity", 0.1)
.attr("x2", width - margin.right - margin.left)
)
.call((g) => g.selectAll(".domain").remove());
}
Insert cell
Insert cell
chart1a = {
const d3Data = aqData.objects();

const x = xscale(d3.extent(d3Data, d => d.value));
const y = yscale(d3Data.map(d => d.age));

const svg = d3.create('svg').attr('viewBox', [0, 0, width, height]);

svg.append('g').call(xaxis(x));
svg.append('g').call(yaxis(y));

svg.append('g')
.attr('fill', 'steelblue')
.attr('stroke-width', 10)
.attr('pointer-events', 'all')
.selectAll('rect')
.data(d3Data)
.join('rect')
.attr('x', d => x(d.value) - 0.75)
.attr('y', d => y(d.age))
.attr('width', 1.5)
.attr('height', y.bandwidth())
.append('title')
.text(d => `${d.name}\n${(d.value * 100).toFixed(1)}% ${d.age}`);

return svg.node();
}
Insert cell
Insert cell
chart1b = {
const [domains] = aqData // <-- extract first object from table iterator
.rollup({
x: d => [op.min(d.value), op.max(d.value)], // [min, max] domain array
y: d => op.array_agg_distinct(d.age) // array of unique values
});
const x = xscale(domains.x);
const y = yscale(domains.y);

const svg = d3.create('svg').attr('viewBox', [0, 0, width, height]);

svg.append('g').call(xaxis(x));
svg.append('g').call(yaxis(y));

svg.append('g')
.attr('fill', 'steelblue')
.attr('stroke-width', 10)
.attr('pointer-events', 'all')
.selectAll('rect')
.data(aqData) // <-- pass aqData directly as an iterable over row objects
.join('rect')
.attr('x', d => x(d.value) - 0.75)
.attr('y', d => y(d.age))
.attr('width', 1.5)
.attr('height', y.bandwidth())
.append('title')
.text(d => `${d.name}\n${(d.value * 100).toFixed(1)}% ${d.age}`);

return svg.node();
}
Insert cell
Insert cell
domains = aqData.rollup({
x: d => [op.min(d.value), op.max(d.value)], // [min, max] domain array
y: d => op.array_agg_distinct(d.age) // array of unique values
})
.objects()[0] // no groups, so just pull the first result object
Insert cell
Insert cell
indices = d3.range(0, aqData.numRows())
Insert cell
Insert cell
chart2a = {
const x = xscale(domains.x);
const y = yscale(domains.y);

const svg = d3.create('svg').attr('viewBox', [0, 0, width, height]);

svg.append('g').call(xaxis(x));
svg.append('g').call(yaxis(y));

svg.append('g')
.attr('fill', 'steelblue')
.attr('stroke-width', 10)
.attr('pointer-events', 'all')
.selectAll('rect')
.data(indices)
.join('rect')
.attr("x", d => x(aqData.get('value', d)) - 0.75)
.attr("y", d => y(aqData.get('age', d)))
.attr('width', 1.5)
.attr('height', y.bandwidth())
.append('title')
.text(d => `${aqData.get('name', d)}\n${(aqData.get('value', d) * 100).toFixed(1)}% ${aqData.get('age', d)}`);

return svg.node();
}
Insert cell
Insert cell
Insert cell
xField = aqData.getter('value')
Insert cell
yField = aqData.getter('age')
Insert cell
labelField = aqData.getter('name')
Insert cell
Insert cell
chart2b = {
const x = xscale(domains.x);
const y = yscale(domains.y);

const svg = d3.create('svg').attr('viewBox', [0, 0, width, height]);

svg.append('g').call(xaxis(x));
svg.append('g').call(yaxis(y));

svg.append('g')
.attr('fill', 'steelblue')
.attr('stroke-width', 10)
.attr('pointer-events', 'all')
.selectAll('rect')
.data(indices)
.join('rect')
.attr("x", d => x(xField(d)) - 0.75)
.attr("y", d => y(yField(d)))
.attr('width', 1.5)
.attr('height', y.bandwidth())
.append('title')
.text(d => `${labelField(d)}\n${(xField(d) * 100).toFixed(1)}% ${yField(d)}`);

return svg.node();
}
Insert cell
Insert cell
Insert cell
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