Public
Edited
Feb 21, 2024
6 forks
22 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
h3 = import("h3-js@4")
Insert cell
hexworld = level => ({
type: "FeatureCollection",
features: h3.getRes0Cells()
.map(i => h3.cellToChildren(i, level))
.flat()
.map(d => ({
type: "Feature",
properties: { id: d, pentagon: h3.isPentagon(d) },
geometry: {
type: "Polygon",
coordinates: [h3.cellToBoundary(d, true).reverse()]
}
}))
})
Insert cell
hexagons = hexworld(resolution)
Insert cell
contours = d3
.geoContour()
.x(d => d3.geoCentroid(d)[0])
.y(d => d3.geoCentroid(d)[1])
.value((d, i) => 10 * Math.log10(areas[i] / median))
.thresholds(100)(hexagons.features)
Insert cell
areas = hexagons.features.map(d => d3.geoArea(d))
Insert cell
median = d3.median(areas)
Insert cell
color = d3.scaleSequential(t => d3.interpolateRdYlGn(1 - t)).domain([-1, 1])
Insert cell
function chart() {
const context = DOM.context2d(width, height),
path = d3.geoPath(projection).context(context);

context.beginPath();
path({ type: "Sphere" });
context.fillStyle = color(0);
context.fill();
context.lineWidth = 3;
context.stroke();
context.clip();
context.lineWidth = 0.5;

for (const c of contours) {
context.beginPath();
path(c);
context.strokeStyle = context.fillStyle = color(c.value);
context.fill();
context.stroke();
}

context.beginPath();
context.fillStyle = "lightblue";
for (const penta of hexagons.features.filter(d => d.properties.pentagon))
path(penta);
context.fill();

context.beginPath();
for (const hepta of hexagons.features.filter(
d => d.geometry.coordinates[0].length == 8
)) {
path({ type: "Point", coordinates: d3.geoCentroid(hepta) });
}
context.fillStyle = "red";
context.fill();

context.beginPath();
context.fillStyle = "black";
for (const octo of hexagons.features.filter(
d => d.geometry.coordinates[0].length == 9
))
path({ type: "Point", coordinates: d3.geoCentroid(octo) });
context.fill();

context.beginPath();
context.fillStyle = "lightblue";
for (const nona of hexagons.features.filter(
d => d.geometry.coordinates[0].length == 10
))
path({ type: "Point", coordinates: d3.geoCentroid(nona) });
context.fill();

context.beginPath();
context.fillStyle = "darkorange";
for (const deca of hexagons.features.filter(
d => d.geometry.coordinates[0].length == 11
))
path({ type: "Point", coordinates: d3.geoCentroid(deca) });
context.fill();

context.strokeStyle = "#fff";
context.beginPath();
path(hexagons);
context.lineWidth = 0.5;
context.stroke();

context.strokeStyle = "black";
context.beginPath();
path(land);
context.lineWidth = 1.5;
context.stroke();

return context.canvas;
}
Insert cell
d3 = require("d3@7", "d3-geo-voronoi@2", "d3-geo-polygon@1.8")
Insert cell
land = {
const world = await fetch("https://cdn.jsdelivr.net/npm/visionscarto-world-atlas@0.1.0/world/110m.json").then(d => d.json());
return topojson.feature(world, world.objects.countries);
}
Insert cell
projection = ({
Airocean: d3
.geoAirocean()
.angle(-150)
.fitExtent([[2, 2], [width - 2, height - 2]], { type: "Sphere" }),
"Gnomonic icosahedral": d3
.geoIcosahedral()
.faceProjection(face => {
const c = face.site.map(d => -d);
c[2] = Math.abs(c[1] - 52.62) < 1 || Math.abs(c[1] + 10.81) < 1 ? 60 : 0;
return d3
.geoProjection(d3.geoGnomonicRaw)
.rotate(c)
.translate([0, 0]);
})
.rotate([-83.65929, 25.44458, -87.45184])
.angle(60)
.fitExtent([[2, 2], [width - 2, height - 2]], { type: "Sphere" }),
"Gray-Fuller icosahedral": d3
.geoIcosahedral()
.faceProjection(face => {
var c = face.site.map(d => -d);
c[2] = Math.abs(c[1] - 52.62) < 1 || Math.abs(c[1] + 10.81) < 1 ? 60 : 0;
return d3
.geoProjection(d3.geoGrayFullerRaw())
.rotate(c)
.translate([0, 0]);
})
.rotate([-83.65929, 25.44458, -87.45184])
.angle(60)
.fitExtent([[2, 2], [width - 2, height - 2]], { type: "Sphere" }),
Orthographic: d3
.geoOrthographic()
.rotate([-110, -35])
.fitExtent([[2, 2], [width - 2, height - 2]], { type: "Sphere" }),
"Azimuthal equal-area": d3
.geoAzimuthalEqualArea()
.rotate([-110, -35])
.clipAngle(54)
.fitExtent([[2, 2], [width - 2, height - 2]], { type: "Sphere" }),
Mercator: d3
.geoMercator()
.fitExtent([[2, 2], [width - 2, height - 2]], { type: "Sphere" })
}[projname])
Insert cell
height = (width * (projname === "Airocean" ? 2.1 : 1)) | 0
Insert cell
import { legend } from "@d3/color-legend"
Insert cell
import { select } from "@jashkenas/inputs"
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