Published unlisted
Edited
Jun 2, 2020
Insert cell
Insert cell
chart = {
const path = d3.geoPath();

const svg = d3
.create("svg")
.attr("viewBox", wide ? [0, 0, width, height] : [0, 0, height, width])
.style("display", "block")
.style("margin", "0 -14px")
.style("width", "calc(100% + 28px)")
.style("height", "auto");

const g = svg
.append("g")
.attr(
"transform",
wide
? null
: `
rotate(90 ${width / 2},${height / 2})
translate(${(width - height) / 2},${(width - height) / 2})
`
)
.attr("stroke", "white")
.attr("stroke-width", 0.03);

for (const threshold of thresholds) {
g.append("path")
.attr("d", path(simplify(contours.contour(data.values, threshold))))
.attr("fill", color(threshold));

yield svg.node();
}
}
Insert cell
contours = d3.contours().size([width, height])
Insert cell
simplify = shape => {
const t = topojson.presimplify(topojson.topology({ foo: shape }));
return Object.assign(
{},
shape,
topojson.feature(topojson.simplify(t), t.objects.foo).geometry
);
}
Insert cell
Insert cell
complete = contours(data.values)
Insert cell
simplified = complete.map(simplify)
Insert cell
{
const [a, b] = [simplified, complete].map(s => s.map(d => d.coordinates).flat(3).length);
return md`**Number of points: ${(-100 + 100 * (a / b)).toFixed(1)}%**
(${a} vs. ${b}).`
}
Insert cell
{
const [a, b] = [simplified, complete].map(s => JSON.stringify(s).length);
return md`**Size in bytes: ${(-100 + 100 * (a / b)).toFixed(1)}%** (${a} vs. ${b}).`
}
Insert cell
width = data.width
Insert cell
height = data.height
Insert cell
wide = Generators.observe(notify => {
let wide;
function resized() {
let w = window.innerWidth > 640;
if (w !== wide) notify(wide = w);
}
resized();
window.addEventListener("resize", resized);
return () => window.removeEventListener("resize", resized);
})
Insert cell
color = d3.scaleSequential(interpolateTerrain).domain(d3.extent(data.values)).nice()
Insert cell
thresholds = color.ticks(20)
Insert cell
data = FileAttachment("volcano.json").json()
Insert cell
interpolateTerrain = {
const i0 = d3.interpolateHsvLong(d3.hsv(120, 1, 0.65), d3.hsv(60, 1, 0.90));
const i1 = d3.interpolateHsvLong(d3.hsv(60, 1, 0.90), d3.hsv(0, 0, 0.95));
return t => t < 0.5 ? i0(t * 2) : i1((t - 0.5) * 2);
}
Insert cell
d3 = require("d3@5", "d3-hsv@0.1")
Insert cell
topojson = require("topojson@3")
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