Published
Edited
Jul 19, 2022
4 forks
Importers
5 stars
Insert cell
Insert cell
map = {
// We're going to draw the map on the following SVG.
let svg = d3.create("svg").attr("width", w).attr("height", h);

// Draw the graticule
svg
.append("path")
.attr("d", path(d3.geoGraticule10()))
.attr("fill", "none")
.attr("stroke", "#333")
.attr("stroke-width", 0.4);

// Draw the countries
let country_group = svg.append("g");
country_group
.selectAll("path")
.data(countries.features)
.join("path")
.attr("d", path)
.attr("data-pop", (d) => console.log(d.properties.POP_EST))
.attr("fill", "#eee")
.attr("stroke", "black")
.attr("stroke-width", 1);

// Draw the bubbles.
let max_pop = d3.max(countries.features.map((o) => o.properties.POP_EST));
let bubble_group = svg.append("g");
bubble_group
.selectAll("circle")
.data(countries.features)
.join("circle")
.attr("cx", (d) => path.centroid(d)[0])
.attr("cy", (d) => path.centroid(d)[1])
.attr("r", (d) => 20 * Math.sqrt(d.properties.POP_EST / max_pop))
.attr("fill", "lightblue")
.attr("stroke", "black");

return svg.node();
}
Insert cell
Insert cell
Insert cell
path = d3.geoPath().projection(projection)
Insert cell
projection = d3.geoNaturalEarth1().fitSize([w + 20, h], countries)
Insert cell
h = 0.625 * w
Insert cell
w = width < 1100 ? width : 1100
Insert cell
// Convert the TopoJSON to GeoJSON
countries = topojson.feature(map_data, map_data.objects.countries)
Insert cell
Insert cell
map_data = FileAttachment("countries.json").json()
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