Published
Edited
Apr 8, 2020
23 stars
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, 975, 610])
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", 10);

const legend = svg
.append("g")
.attr("fill", "#777")
.attr("transform", "translate(925,608)")
.selectAll("g")
.data([1e6, 5e6, 15e6])
.join("g");

legend
.append("circle")
.attr("fill", "none")
.attr("stroke", "#bbb")
.attr("cy", d => -radius(d))
.attr("r", radius);

legend
.append("text")
.attr("y", d => -2 * radius(d))
.attr("dy", "1.3em")
.text(d3.format("~s"));

let bubble = svg.append("g").selectAll("g");

return Object.assign(svg.node(), {
update(year) {
const t = svg
.transition()
.duration(delay)
.ease(d3.easeLinear);

bubble = bubble
.data(data.filter(d => d.year === year), d => d.id)
.join(
enter =>
enter
.append("g")
.attr("transform", d => `translate(0, 700)`)
.call(g =>
g
.append("path")
.attr("class", "state")
.attr("fill", d => `hsl(${hues.get(d.id)}, 50%, 50%)`)
.attr("opacity", 0)
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("d", d => path(d.feature))
.attr(
"transform",
d => `translate(${path.centroid(d.feature).map(d => -d)})`
)
.transition(t)
.attr("opacity", 0.5)
)
.call(g =>
g
.append("circle")
.attr("fill", d => `hsl(${hues.get(d.id)}, 80%, 90%)`)
.attr("fill-opacity", 0.5)
.attr("stroke", "currentColor")
.attr("r", 0)
)
.call(g =>
g
.append("text")
.attr("dy", "0.35em")
.attr("fill-opacity", 0)
.attr("y", -2)
.text(d => d.feature.properties.name)
.call(text =>
text
.append("tspan")
.style("font-variant-numeric", "tabular-nums")
.attr("x", 0)
.attr("y", 12)
.text(0)
)
.transition(t)
.attr("fill-opacity", 1)
),
update => update,
exit =>
exit.call(bubble =>
bubble
.transition(t)
.call(g => g.select("circle").attr("r", 0))
.call(g => g.select("text").attr("fill-opacity", 0))
.call(g => g.select("tspan").call(textTween, 0))
.remove()
)
);

const sorted = data
.filter(d => d.year === year)
.sort((a, b) => b.value - a.value)
.map(d => d.id);

x.domain(d3.extent(data.filter(d => d.year === year), d => d.value));

bubble
.transition(t)
.each(d => {
console.log(d, x(d.value));
})
.attr(
"transform",
d => `translate(${x(d.value)}, ${y(sorted.indexOf(d.id))})`
);

bubble
.select("circle")
.transition(t)
.attr("r", d => radius(d.value));

bubble
.select("tspan")
.transition(t)
.call(textTween, d => d.value);
}
});
}
Insert cell
function textTween(text, valueof) {
if (typeof valueof !== "function") {
const value = +valueof;
valueof = () => value;
}
return text.textTween(function(d) {
const value = +this.textContent.replace(/,/g, "");
const i = d3.interpolateRound(value, valueof(d));
return t => formatValue(i(t));
});
}
Insert cell
chart.update(year)
Insert cell
delay = 750
Insert cell
data = {
const data = d3.tsvParse(await FileAttachment("population.tsv").text(), (d, i) => i === 0 ? null : d);
const years = data.columns.slice(1, -3).map(x => +x).reverse();
return Object.assign(d3.cross(data, years, (d, year) => ({id: d["FIPS code"], feature: states.get(d["FIPS code"]), year, value: +d[year].replace(/,/g, "")})).filter(d => !isNaN(d.value)).sort((a, b) => d3.descending(a.value, b.value)), {years});
}
Insert cell
states = new Map(us.objects.states.geometries.map(d => [d.id, topojson.feature(us, d)]))
Insert cell
hues = new Map(Array.from(states.keys()).map(id => [id, Math.random() * 360]))
Insert cell
radius = d3.scaleSqrt([0, 2e7], [0, 50])
Insert cell
x = d3.scaleLinear([0, 2e7], [0, width - xPadding])
Insert cell
xPadding = 50
Insert cell
y = d3.scaleLinear([0, 10], [yPadding, 610 - yPadding])
Insert cell
yPadding = 65
Insert cell
path = d3.geoPath()
Insert cell
formatValue = d3.format(",d")
Insert cell
us = FileAttachment("states-albers-10m.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@5")
Insert cell
import {Scrubber} from "@mbostock/scrubber"
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