Public
Edited
Aug 1, 2023
4 forks
14 stars
Also listed in…
Gallery
Insert cell
Insert cell
map = {
const clipIn = DOM.uid();
const clipOut = DOM.uid();
return html`<svg width=${width} height=${height} viewBox="0 0 ${width} ${height}" style="display:block;">
<defs>
<clipPath id="${clipIn.id}"><path d="${path(outline)}"></path></clipPath>
<clipPath id="${clipOut.id}"><path d="M0,0V${height}H${width}V0Z${path(outline)}"></path></clipPath>
</defs>
<path d="${path(graticule)}" stroke="#ccc" fill="none"></path>
<path clip-path="${clipIn}" d="${path(land)}"></path>
<path fill-opacity="0.1" clip-path="${clipOut}" d="${path(land)}"></path>
<path d="${path(outline)}" stroke="#000" fill="none"></path>
<g font-size="10" font-family="sans-serif" text-anchor="middle">
${d3.range(-80, 80 + 1, 10).map(y => svg`
<text transform="translate(${offset(projection([longitude + 90, y]), 10) + ""})" dy="0.35em" x="6">${formatLatitude(y)}</text>
<text transform="translate(${offset(projection([longitude - 90, y]), 10) + ""})" dy="0.35em" x="-6">${formatLatitude(y)}</text>`)}
</g>
</svg>`;
}
Insert cell
formatLatitude = y => `${Math.abs(y)}°${y < 0 ? "S" : "N"}`
Insert cell
formatLongitude = x => `${Math.abs(x)}°${x < 0 ? "W" : "E"}`
Insert cell
path = d3.geoPath(projection)
Insert cell
longitude = -100
Insert cell
projection = d3.geoStereographic().rotate([-longitude, 0])
Insert cell
offset = ([x, y], k) => {
const [cx, cy] = projection.translate();
const dx = x - cx, dy = y - cy;
k /= Math.hypot(dx, dy);
return [x + dx * k, y + dy * k];
}
Insert cell
marginTop = 30
Insert cell
marginRight = 40
Insert cell
marginBottom = 30
Insert cell
marginLeft = 40
Insert cell
height = {
const [[x0, y0], [x1, y1]] = d3.geoPath(projection.fitWidth(width, outline)).bounds(outline);
const dx = x1 - x0;
const k = (dx - marginLeft - marginRight) / dx;
const dy = (y1 - y0) * k + marginBottom + marginTop;
projection.scale(projection.scale() * k);
projection.translate([(dx + marginLeft - marginRight) / 2, (dy + marginTop - marginBottom) / 2]);
projection.precision(0.2);
return Math.round(dy);
}
Insert cell
outline = d3.geoCircle().radius(90).center([longitude, 0])()
Insert cell
graticule = d3.geoGraticule10()
Insert cell
land = topojson.feature(world, world.objects.land)
Insert cell
world = FileAttachment("land-50m.json").json()
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