Published
Edited
Apr 23, 2022
1 fork
Importers
2 stars
Insert cell
Insert cell
mapfill(d => color(f(d)), d3.geoOrthographic().rotate([0, -35, -20]), {
N: 10000,
graticule: d3.geoGraticule()()
})
Insert cell
function f([lon, lat]) {
return 0.1 + Math.sin(lon * 0.07) * Math.sin(lat * 0.2);
}
Insert cell
color = d3.scaleLinear().range(["orange", "lime"])
Insert cell
Insert cell
Insert cell
mapfillContours(d => f(d), d3.geoOrthographic().rotate([0, -35, -20]), {
// N: 10000,
// thresholds: 10,
color: d3.scaleSequential(d3.interpolateRdYlBu).domain([-1, 1])
// graticule: d3.geoGraticule()()
})
Insert cell
mapfillContours((d, i) => i / 3, d3.geoOrthographic().rotate([0, -35, -20]), {
N: 4, // 4 is the minimum number of points
thresholds: 100,
color: d3.scaleSequential(d3.interpolateSinebow),
graticule: d3.geoGraticule()()
})
Insert cell
function rearrange(coordinates) {
coordinates.flat().forEach(d => d.reverse());
}
Insert cell
function* mapfillContours(
valuePoint,
projection,
{
debug = false,
land = {},
graticule = {},
N = 5000,
thresholds = 40,
color: fill = color
} = {}
) {
const time = performance.now();

const height = width / 2.3;
const context = DOM.context2d(width, height);
const path = d3
.geoPath(
projection.fitExtent(
[
[2, 2],
[width - 2, height - 2]
],
{
type: "Sphere"
}
)
)
.context(context);

// from https://observablehq.com/@mbostock/spherical-fibonacci-lattice
const phi = (1 + Math.sqrt(5)) / 2,
fibonacci = (i, N) => [i / phi, (i + 0.5) / N],
points = Array.from({ length: N }, (_, i) => {
const [x, y] = fibonacci(i, N);
return [(x * 360) % 360, (Math.acos(2 * y - 1) / Math.PI) * 180 - 90];
});

context.beginPath();
path({ type: "Sphere" });
context.fill();
context.stroke();
context.clip();
yield context.canvas;

const v = d3.geoVoronoi(points);

const contours = d3
.tricontour()
.thresholds(thresholds)
.triangulate(() => v.delaunay.delaunay)
.pointInterpolate((i, j, a) => d3.geoInterpolate(points[i], points[j])(a))
.value(valuePoint)
.ringsort((rings) => [rings]);

let first = true;
for (const c of contours.isobands(points)) {
if (first) {
first = false;
context.beginPath();
path({ type: "Sphere" });
context.fillStyle = fill(c.value);
context.fill();
}

rearrange(c.coordinates);
context.beginPath();
path(c);
context.strokeStyle = context.fillStyle = fill((c.value + c.valueMax) / 2);
context.fill();
context.stroke();
}

draw();

function draw() {
context.beginPath();
path(land);
path(graticule);
context.strokeStyle = "#333";
context.stroke();

context.beginPath();
path({ type: "Sphere" });
context.strokeStyle = "#333";
context.lineWidth = 1;
context.stroke();

context.fillStyle = "white";
if (debug) context.fillText((performance.now() - time) | 0, 10, 10);
}
}
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@7", "d3-geo-voronoi@1", "d3-geo-projection@4", "d3-tricontour@1")
Insert cell
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