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

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