Published
Edited
Jun 24, 2019
14 stars
Also listed in…
tutorials
random
Insert cell
Insert cell
Insert cell
map = {
const s = svg`<svg id="states-map" viewBox="-2 -2 ${w + 4} ${h + 4}" style="width: 100%; height: auto;">
<g id="main" transform="translate(0,0) scale(1)">
${statesPaths()}
</g>
</svg>`
return s;
}
Insert cell
Insert cell
center = path.centroid(cali)
Insert cell
bounds = path.bounds(cali)
Insert cell
padding = 10
Insert cell
start = [center[0], center[1], bounds[1][1] + padding - bounds[0][1] + padding]
Insert cell
end = [w / 2, h / 2, w / 2]
Insert cell
k = Math.min(w, h) / start[2]
Insert cell
translate = [w / 2 - start[0] * k, h / 2 - start[1] * k]
Insert cell
transformEnd = `translate(${translate}) scale(${k})`
Insert cell
map2 = {
const s = svg`<svg id="states-map" viewBox="-2 -2 ${w + 4} ${h + 4}" style="width: 100%; height: auto;">
<g id="main" transform="${transformEnd}">
${statesPaths()}
</g>
</svg>`
return s;
}
Insert cell
Insert cell
interpolatorTransform = d3.interpolateTransformSvg("translate(0, 0) scale(1)", transformEnd)
Insert cell
interpolatorTransform(t)
Insert cell
Insert cell
map3 = {
const s = svg`<svg id="states-map" viewBox="-2 -2 ${w + 4} ${h + 4}" style="width: 100%; height: auto;">
<g id="main" transform="${interpolatorTransform(t)}">
${statesPaths()}
</g>
</svg>`
return s;
}
Insert cell
Insert cell
Insert cell
Insert cell
map4 = {
replay;
let startTime;
let frame;
let transformStr = interpolatorTransform(0);
const s = svg`<svg id="states-map" viewBox="-2 -2 ${w + 4} ${h + 4}" style="width: 100%; height: auto;">
<g id="main" transform="${transformStr}">
${statesPaths()}
</g>
</svg>`,
g = d3.select(s).select("g");

function tick(timestamp) {
if (!startTime) startTime = timestamp;
const elapsed = timestamp - startTime;
const t2 = elapsed / duration;
if (elapsed < duration) {
transformStr = interpolatorTransform(t2);

g.attr("transform", transformStr);
frame = requestAnimationFrame(tick);
}
};

requestAnimationFrame(tick);
invalidation.then(() => cancelAnimationFrame(frame));

return s;
}
Insert cell
Insert cell
stateId = "06"
Insert cell
w = 960
Insert cell
h = 600
Insert cell
statesPaths = () => states.map(d =>
svg`<path
fill=${d.id === stateId ? "orange" : "#ccc"}
stroke="#333"
stroke-width=${0.5}
d=${path(d)}
/>`)
Insert cell
path = d3.geoPath()
Insert cell
Insert cell
cali = states.find(d => d.id === stateId);
Insert cell
states = topojson.feature(us, us.objects.states).features;
Insert cell
us = await d3.json("https://unpkg.com/us-atlas@2.1.0/us/10m.json")
Insert cell
Insert cell
import {slider} from "@jashkenas/inputs"
Insert cell
topojson = require("topojson-client@3")
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