Public
Edited
Oct 3, 2022
5 stars
Insert cell
Insert cell
Insert cell
map = {
const height = 600;
const projection = d3.geoMercator().fitExtent(
[
[1, 10],
[width - 50, height - 10]
],
japan
);
const path = d3.geoPath(projection);
const [[, top], [, bottom]] = path.bounds(japan);
const f = (d) => bottom + (top - bottom) * J(d);

const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width);

const clip = svg
.append("clipPath")
.attr("id", "clip")
.append("path")
.attr("d", path(japan));

svg
.append("g")
.selectAll()
.data(d3.ticks(0, 1, 50))
.join("line")
.attr("x2", width - 50)
.attr("stroke", "black")
.attr("stroke-width", (d) => ((d * 10) % 1 ? 0.25 : 1))
.attr("transform", (d) => `translate(0,${f(d)})`);

svg
.append("g")
.selectAll()
.data(d3.ticks(0, 1, 10))
.join("text")
.attr("font-family", "sans-serif")
.attr("font-size", "12")
.attr("transform", (d) => `translate(${width - 2},${f(d)})`)
.attr("y", "0.38em")
.attr("text-anchor", "end")
.text((d) => `${(100 * d) | 0}%`);

svg
.append("path")
.attr("stroke", "none")
.attr("fill", "beige")
.attr("fill-opacity", 0.85)
.attr("d", path(japan));

const bg = svg
.append("rect")
.attr("clip-path", "url(#clip)")
.attr("width", width)
.attr("height", height)
.attr("stroke", "none")
.attr("fill", "#BC002D");

svg
.append("path")
.attr("stroke", "black")
.attr("fill", "none")
.attr("d", path(japan));

function update(percentage) {
const r = f(percentage);
bg.attr("y", r);
return r;
}

return Object.assign(svg.node(), { update });
}
Insert cell
map.update(percentage/100)
Insert cell
fill = (shape) => {
const w = 1000; // precision
const context = DOM.context2d(w, w, 1);
// TODO: prefer an area-preserving projection such as geoAzimuthalEqualArea
const projection = d3.geoMercator().fitSize([w, w], shape);
context.beginPath();
const path = d3.geoPath(projection, context);
path(shape);
context.fill();
const [[, top], [, bottom]] = path.bounds(japan);
const d = context.getImageData(
0,
Math.floor(top),
w,
Math.ceil(bottom - top)
).data;
const l = d.length >> 2;
// TODO: we could be more precise by scanning lines instead of pixels
const c = d3.cumsum({ length: l }, (_, i) => d[(i << 2) + 3]);
const H = c[c.length - 1];
return (p) => 1 - d3.bisect(c, (1 - p) * H) / l;
}
Insert cell
J = fill(japan)
Insert cell
Plot.lineY(d3.ticks(0, 1, 500), { x: (d) => d, y: J }).plot({
nice: true,
x: { label: "percentage" },
y: { label: "height" }
})
Insert cell
japan = FileAttachment("japan.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