Published
Edited
Dec 22, 2020
2 forks
2 stars
Insert cell
Insert cell
{
const svg = d3.create("svg").attr("width", width).attr("height", height);
svg.append("path").attr("d", d3.geoPath(projection)(outlineShape)).attr("fill", "none").attr("stroke", "black")
svg.selectAll("path.country").data(allCountries.features)
.join("path")
.attr("class", "country")
.attr("stroke", "black")
.attr("fill", d => d.properties.name === clickedCountry? "steelblue" : "lightgrey")
.attr("d", mapPath)
.on("click", (event, d) => mutable clickedCountry = d.properties.name);
svg.selectAll("circle.name").data(allCountries.features)
.join("circle")
.attr("class", "name")
.attr("cx", d => mapPath.centroid(d)[0]) // get the center of the shape using geoPath.centroid
.attr("cy", d => mapPath.centroid(d)[1])
.attr("r", 3)
.attr("fill", "red");
return svg.node();
}
Insert cell
mutable clickedCountry = null
Insert cell
Insert cell
projection = d3.geoOrthographic()
Insert cell
mapPath = d3.geoPath(projection)
Insert cell
Insert cell
outlineShape = globe
Insert cell
height = {
const [[x0, y0], [x1, y1]] = d3.geoPath(projection.fitWidth(width/2, outlineShape)).bounds(outlineShape);
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
Insert cell
globe = ({type: "Sphere"})
Insert cell
allCountries = topojson.feature(world, world.objects.countries)
Insert cell
germany = allCountries.features.find(d => d.properties.name === "Germany")
Insert cell
Insert cell
Insert cell
germanyStates = FileAttachment("bundeslaender_mittel.geo.json").json()
Insert cell
germanyLandkreise = FileAttachment("4_niedrig.geo.json").json()
Insert cell
Insert cell
world = d3.json("https://cdn.jsdelivr.net/npm/world-atlas@2.0.2/countries-110m.json")
Insert cell
Insert cell
accidents = d3.dsvFormat(";").parse(await FileAttachment("accidents-bundeslaender@1.csv").text())
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@6", "d3-geo@2", "d3-geo-projection@3")
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