Public
Edited
Aug 27, 2021
10 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
topojson = require('topojson-client@3')
Insert cell
Insert cell
us = d3.json('https://d3js.org/us-10m.v1.json')
Insert cell
Insert cell
states = topojson.feature(us, us.objects.states).features
Insert cell
Insert cell
Insert cell
Insert cell
path = d3.geoPath();
Insert cell
Insert cell
statesPaths = states.map((feature) => ({
id: feature.id,
d: path(feature),
centroid: path.centroid(feature)
}));
Insert cell
Insert cell
Insert cell
statesPathsLTR = [...statesPaths].sort((a, b) => a.centroid[0] - b.centroid[0]);
Insert cell
Insert cell
statesLTR = {
replayLTR;

return svg`
<svg width=${width} height="600" viewBox="0 0 ${width} 600">
<g>
${statesPathsLTR.map((path, i) => {
const node = d3
.select(
svg`<path d=${path.d} fill="steelblue" fill-opacity="0.1" stroke="steelblue" opacity="0" />`
)
.call(stagger(i))
.node();

return node;
})}
</g>
</svg>
`;
}
Insert cell
Insert cell
Insert cell
statesPathsBTT = [...statesPaths].sort((a, b) => b.centroid[1] - a.centroid[1]);
Insert cell
Insert cell
statesBTT = {
replayBTT;

return svg`
<svg width=${width} height="600" viewBox="0 0 ${width} 600">
<g>
${statesPathsBTT.map((path, i) => {
const node = d3
.select(
svg`<path d=${path.d} fill="steelblue" fill-opacity="0.1" stroke="steelblue" opacity="0" />`
)
.call(stagger(i))
.node();

return node;
})}
</g>
</svg>
`;
}
Insert cell
Insert cell
statesPathsDiagonal = [...statesPaths].sort((a, b) => (a.centroid[0] - b.centroid[0]) + (b.centroid[1] - a.centroid[1]));
Insert cell
Insert cell
statesDiagonal = {
replayDiagonal;

return svg`
<svg width=${width} height="600" viewBox="0 0 ${width} 600">
<g>
${statesPathsDiagonal.map((path, i) => {
const node = d3
.select(
svg`<path d=${path.d} fill="steelblue" fill-opacity="0.1" stroke="steelblue" opacity="0" />`
)
.call(stagger(i))
.node();

return node;
})}
</g>
</svg>
`;
}
Insert cell
Insert cell
Insert cell
stagger = (i) => (path) => {
path.transition()
.duration(500)
.ease(d3.easeLinear)
.delay(i * 50)
.attr("opacity", 1);
}
Insert cell
d3 = require('d3@6');
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