Public
Edited
Dec 19, 2022
Insert cell
Insert cell
projection = d3.geoNaturalEarth1()
Insert cell
map = {
const context = DOM.context2d(width, height);
const path = d3.geoPath(projection, context).pointRadius(1);

context.beginPath();
path(outline);
context.fillStyle = "#fff";
context.fillRect(0, 0, width, height);

context.strokeStyle = "white";
geojson.features.forEach((feature, idx) => {
context.beginPath();
context.fillStyle = color(idx);
path(feature);
context.fill();
context.stroke();
});

context.beginPath();
path(outline);
context.strokeStyle = "#ccc";
context.stroke();

yield context.canvas;
}
Insert cell
color = d3
.scaleSequential(d3.interpolateYlOrRd)
.domain([0, geojson.features.length])
Insert cell
geojson = ({
type: "FeatureCollection",
features: coords.map(([lat, lon]) => {
const lon0 = lon - lonRes / 2;
const lon1 = lon + lonRes / 2;
const lat0 = lat - latRes / 2;
const lat1 = lat + latRes / 2;
return {
type: "Feature",
geometry: {
type: "Polygon",
coordinates: [
[
[lon0, lat0],
[lon0, lat1],
[lon1, lat1],
[lon1, lat0],
[lon0, lat0]
]
]
}
};
})
})
Insert cell
height = {
const [[x0, y0], [x1, y1]] = d3.geoPath(projection.fitWidth(width, outline)).bounds(outline);
const dy = Math.ceil(y1 - y0), l = Math.min(Math.ceil(x1 - x0), dy);
projection.scale(projection.scale() * (l - 1) / l).precision(0.2);
return dy;
}
Insert cell
coords = d3.cross(latitudes, longitudes)
Insert cell
longitudes = d3.range(-180 + lonRes / 2, 180, lonRes)
Insert cell
lonRes = 5
Insert cell
latitudes = d3.range(-90 + latRes / 2, 90, latRes)
Insert cell
latRes = 2.5
Insert cell
import { outline, graticule } from "@d3/world-map"
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