Public
Edited
Oct 24, 2023
6 forks
32 stars
Insert cell
Insert cell
Insert cell
chart = {
const r = width / height;
const context = DOM.context2d(width, wide ? height : width * r);

if (!wide) {
context.translate(width / 2, height / 2);
context.rotate(Math.PI / 2);
context.translate(-height / 2, -width / 2);
context.scale(r, r);
}

const path = d3.geoPath(null, context);

context.scale(scale, scale);
context.lineWidth = 0.5 / scale;
context.strokeStyle = "#fff";

while (true) {
const t = (performance.now() / duration) % 1;

for (const [a, b] of d3.pairs(thresholds)) {
const d = a * (1 - t) + b * t;
context.beginPath();
path(contours.contour(data.values, d));
context.fillStyle = color(d);
context.fill();
context.stroke();
}

yield context.canvas;
}
}
Insert cell
contours = d3.contours().size([data.width, data.height]).smooth(smooth)
Insert cell
height = Math.round(data.height / data.width * width)
Insert cell
scale = width / data.width
Insert cell
duration = 2000
Insert cell
wide = Generators.observe(notify => {
let wide;
function resized() {
let w = innerWidth > 640;
if (w !== wide) notify(wide = w);
}
resized();
addEventListener("resize", resized);
return () => removeEventListener("resize", resized);
})
Insert cell
color = d3.scaleSequential(d3.interpolateGreys).domain(d3.extent(data.values)).nice()
Insert cell
thresholds = color.ticks(20)
Insert cell
data = FileAttachment("volcano.json").json()
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