Published
Edited
Sep 22, 2022
2 forks
27 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
world = {
const topo = await FileAttachment("ne_110m_admin_0_countries_lakes.json").json();
const geo = topojson.feature(topo, topo.objects.ne_110m_admin_0_countries_lakes);
geo.features.forEach(feature => {
feature.centroid = centroid(feature);
return feature;
});
return geo;
}
Insert cell
Insert cell
legend = legendCircle()
.tickValues([50e6, 200e6, 500e6, 1000e6])
.tickFormat((d, i, e) => {
const val = d >= 1e9 ? `${d / 1e9}B` : `${d / 1e6}M`;
const unit = i === e.length - 1 ? " people" : "";
return `${val}${unit}`;
})
.scale(r);
Insert cell
simulation = d3.forceSimulation(world.features)
.force("x", d3.forceX(d => projection(d.centroid)[0]))
.force("y", d3.forceY(d => projection(d.centroid)[1]))
.force("collide", d3.forceCollide(d => 1 + r(d.properties.POP_EST)))
.stop();
Insert cell
Insert cell
height = width * .49;
Insert cell
Insert cell
path = d3.geoPath(projection);
Insert cell
projection = d3.geoEqualEarth()
.rotate([-10, 0, 0])
.fitSize([width, height], { type: "Sphere" });
Insert cell
Insert cell
import { legendCircle } from "@harrystevens/circle-legend";
Insert cell
import { toc } from "@harrystevens/toc";
Insert cell
Insert cell
r = d3.scaleSqrt()
.domain([0, d3.max(world.features, d => d.properties.POP_EST)])
.range([0, Math.sqrt(width * height) / 10])
Insert cell
Insert cell
// Find the centroid of the largest polygon
centroid = (feature) => {
const geometry = feature.geometry;
if (geometry.type === "Polygon"){
return d3.geoCentroid(feature);
}
else {
let largestPolygon = {}, largestArea = 0;
geometry.coordinates.forEach(coordinates => {
const polygon = { type: "Polygon", coordinates },
area = d3.geoArea(polygon);
if (area > largestArea) {
largestPolygon = polygon;
largestArea = area;
}
});
return d3.geoCentroid(largestPolygon);
}
}
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