Published
Edited
Sep 21, 2020
1 star
Insert cell
Insert cell
html`${countries.map((country, i) => {
const [cx, cy] = d3.geoCentroid(country);
const projection = d3.geoAzimuthalEqualArea().rotate([-cx, -cy]).scale(width / 2.5);
const path = d3.geoPath(projection);
const [[x0, y0], [x1, y1]] = path.bounds(country);
return html`<svg class="country" width="${x1 - x0}" height="${y1 - y0}" viewBox="${x0} ${y0} ${x1 - x0} ${y1 - y0}" fill="currentColor"><path d="${path(country)}"></path><title>${country.properties.name}
${(+(country.properties.area * 6371 * 6371).toPrecision(3)).toLocaleString("en")} km²
Rank ${(i + 1)}</title></svg>`;
}).sort((a,b) =>b.width.baseVal.value*b.height.baseVal.value-a.width.baseVal.value*a.height.baseVal.value)}<style>
.country { vertical-align: middle; padding: 4px; border: solid 1px transparent; }
.country:hover { fill: red; border-color: #ccc; }
</style>`
Insert cell
countries = topojson
.feature(await FileAttachment("countries-50m.json").json(), "countries")
.features
.map(d => (d.properties.area = d3.geoArea(d), d))
.sort((a, b) => d3.descending(a.properties.area, b.properties.area))
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@6")
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