Published
Edited
Jun 19, 2020
2 forks
Importers
26 stars
Insert cell
Insert cell
Insert cell
Insert cell
polylabelStretched = (rings, ratio) => {
const polygon = [];
for (const ring of rings) { // stretch the input
const newRing = [];
for (const [x, y] of ring) newRing.push([x / ratio, y]);
polygon.push(newRing);
}
const result = polylabel(polygon, 0.5);
result[0] *= ratio; // stretch the result back
result.distance *= ratio;
return result;
}
Insert cell
getLabelPos = (geometry) => {
let pos;
if (geometry.type === 'MultiPolygon') {
let maxDist = 0; // for multipolygons, pick the polygon with most available space
for (const polygon of geometry.coordinates) {
const p = polylabelStretched(polygon, ratio);
if (p.distance > maxDist) {
pos = p;
maxDist = p.distance;
}
}
} else {
pos = polylabelStretched(geometry.coordinates, ratio);
}
return pos;
}
Insert cell
Insert cell
Insert cell
Insert cell
geojson = topojson.feature(states, states.objects.states)
Insert cell
states = (await fetch('https://cdn.jsdelivr.net/npm/us-atlas@3/states-albers-10m.json')).json()
Insert cell
topojson = require("topojson-client")
Insert cell
d3 = require("d3-geo")
Insert cell
polylabel = require('https://bundle.run/polylabel@1.1.0')
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