Published
Edited
Mar 14, 2020
2 forks
5 stars
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.style("display", "block")
.attr("viewBox", [0, 0, width, height]);

const defs = svg.append("defs");

defs.append("path")
.attr("id", "outline")
.attr("d", path(outline));

defs.append("clipPath")
.attr("id", "clip")
.append("use")
.attr("xlink:href", new URL("#outline", location));

const g = svg.append("g")
.attr("clip-path", `url(${new URL("#clip", location)})`);

g.append("use")
.attr("xlink:href", new URL("#outline", location))
.attr("fill", "white");

g.append("g")
.selectAll("path")
.data(countries.features)
.join("path")
.attr("fill", d => color(data.get(d.id)))
.attr("d", path)
.append("title")
.text(d => `${d.properties.name}
${data.has(d.id) ? `${data.get(d.id)} per 1,000` : "N/A"}`);

g.append("path")
.datum(topojson.mesh(world, world.objects.countries, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("d", path);

svg.append("use")
.attr("xlink:href", new URL("#outline", location))
.attr("fill", "none")
.attr("stroke", "black");

return svg.node();
}
Insert cell
data = Object.assign(new Map(d3.csvParse(await FileAttachment("beds.csv").text(), ({LOCATION, TIME}) => [remap.get(LOCATION), +TIME])), {title: "Hospital beds per 1,000 people"})
Insert cell
remap = new Map(d3.csvParseRows(await FileAttachment("iso-3166.csv").text()))
Insert cell
color = d3.scaleSequential()
.domain(d3.extent(Array.from(data.values())))
.interpolator(d3.interpolateSpectral)
.unknown("#ddd")
Insert cell
projection = d3.geoEqualEarth()
Insert cell
path = d3.geoPath(projection)
Insert cell
width = 954
Insert cell
height = {
const [[x0, y0], [x1, y1]] = d3.geoPath(projection.fitWidth(width, outline)).bounds(outline);
const dy = Math.ceil(y1 - y0), l = Math.min(Math.ceil(x1 - x0), dy);
projection.scale(projection.scale() * (l - 1) / l).precision(0.2);
return dy;
}
Insert cell
outline = ({type: "Sphere"})
Insert cell
countries = topojson.feature(world, world.objects.countries)
Insert cell
world = FileAttachment("countries-50m.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@5")
Insert cell
import {legend} from "@d3/color-legend"
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