Published
Edited
May 31, 2018
Insert cell
Insert cell
world = d3.json("https://unpkg.com/world-atlas@1/world/110m.json")
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
landTopo = topojson.feature(world, world.objects.land)
Insert cell
countryTopos = topojson.feature(world, world.objects.countries).features
Insert cell
function countryTopoById(id) {
return countryTopos.filter(c => c.id == id)[0];
}
Insert cell
botswanaTopo = countryTopoById(72)
Insert cell
newBotswanaTopo = {
const [cx,cy] = d3.geoCentroid(botswanaTopo);
return d3.geoProject(botswanaTopo, d3.geoTransform({
point: function(lon,lat) {
const m = mouseCoord;
const center = botswanaCenter;
// get this coordinate relative to the center of botswana
const lon0 = lon - center.lon;
const lat0 = lat - center.lat;
// reposition coordinate to the mouse's center
const lon1 = lon0 + m.lon;
const lat1 = lat0 + m.lat;
return this.stream.point(lon1,lat1);
}
}));
}
Insert cell
houstonCountryTopos = popDataJSONWithCodes.map(({countryCode}) =>
countryTopoById(countryCode)
)
Insert cell
Insert cell
countryBordersTopo = topojson.mesh(world, world.objects.countries)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
projection = d3.geoNaturalEarth()
.fitExtent([
[mapPad, mapPad], // top left
[mapWidth - mapPad, mapHeight - mapPad] // bottom right
],
{type: "Sphere"}) // shape to fit
.precision(0.1) // controls the smoothness of curves
Insert cell
graticule = d3.geoGraticule10()
Insert cell
path = d3.geoPath(projection)
Insert cell
Insert cell
Insert cell
Insert cell
mouseDot = d3.geoCircle().radius(5).center(mouseCoord)()
Insert cell
{
const el = svg`
<svg width=${mapWidth} height=${mapHeight}>
<path d="${path({type: "Sphere"})}" fill="none" stroke="rgba(0,0,0,0.5)" stroke-width="0.5"></path>
<path d="${path(graticule)}" fill="none" stroke="rgba(0,0,0,0.2)" stroke-width="0.5"></path>
<path d="${path(landTopo)}" fill="rgba(0,200,50,0.6)"></path>
<path d="${path(countryBordersTopo)}" fill="none" stroke="#fff" stroke-width="0.5px"></path>
<path id="botswana" d="${path(newBotswanaTopo)}" fill="rgba(0,0,0,0.5)"></path>
${houstonCountryTopos.map((topo) => svg`<path d="${path(topo)}" fill="rgba(0,123,230,0.5)"></path>`)}
<!--
<path d="${path(mouseDot)}" fill="#F00"></path>
-->
</svg>
`;
el.addEventListener("mousemove", e => {
const x = e.layerX;
const y = e.layerY;
const [lon,lat] = projection.invert([x,y]);
mutable mouseCoord = {lon,lat};
});
const drag = d3.drag()
.on("start", () => {
})
.on("drag", () => {
const {x,y} = d3.event;
const [lat,lon] = projection.invert([x,y]);
console.log(x,y,lon,lat);
})
.on("end", () => {});
const botswana = el.querySelector("#botswana");
d3.select(botswana).call(drag);
return el;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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