Published
Edited
Nov 15, 2021
Fork of D3 + Leaflet
1 fork
2 stars
Insert cell
Insert cell
map = {
const container = html`<div style="height:600px;">`;
yield container;
const map = L.map(container, {zoomSnap: 0}).setView([37.8, -96.9], 4);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: "&copy; <a href=https://www.openstreetmap.org/copyright>OpenStreetMap</a> contributors"
}).addTo(map);

const svg = d3.select(map.getPanes().overlayPane).append("svg").style("position", "relative"),
g = svg.append("g").attr("class", "leaflet-zoom-hide");

function projectPoint(x, y) {
var point = map.latLngToLayerPoint(new L.LatLng(y, x));
this.stream.point(point.x, point.y);
}

const transform = d3.geoTransform({point: projectPoint}),
path = d3.geoPath().projection(transform);

var tip;
var feature = g.selectAll("path")
.data(states.features)
.enter().append("path")
.attr("d", path)
.style("fill", "#000")
.style("fill-opacity", .2)
.style("stroke", "#fff")
.style("stroke-width", "1.5px")
.style("pointer-events", "all")
.on("mouseover mousemove", function (event) {
d3.select(this).style("fill", "brown").style("fill-opacity", .7);
const pointer = d3.pointer(event);
const data = d3.select(this).data()[0];
tip.call(hover, pointer, data.properties.STATE.split("\n"))
})
.on("mouseout", function () {
d3.select(this).style("fill", "#000").style("fill-opacity", .2);
tip.call(hover, null);
});
tip = g.append("g")
.attr("transform", "translate(0, -10)")
.selectAll(".hover")
.data([""])
.join("g") // This gets repositioned in hover()
.attr("class", "hover")
.style("pointer-events", "none")
.style("text-anchor", "middle");

var bounds = path.bounds(states),
topLeft = bounds[0], // [x, y]
bottomRight = bounds[1];
svg.attr("width", bottomRight[0] - topLeft[0])
.attr("height", bottomRight[1] - topLeft[1])
.style("left", topLeft[0] + "px")
.style("top", topLeft[1] + "px");

g.attr("transform", "translate(" + -topLeft[0] + "," + -topLeft[1] + ")");

map.on("zoom", reset);
reset();

// Reposition the SVG to cover the features.
function reset() {
const bounds = path.bounds(states),
topLeft = bounds[0],
bottomRight = bounds[1];

svg .attr("width", bottomRight[0] - topLeft[0])
.attr("height", bottomRight[1] - topLeft[1])
.style("left", topLeft[0] + "px")
.style("top", topLeft[1] + "px");

g .attr("transform", "translate(" + -topLeft[0] + "," + -topLeft[1] + ")");

feature.attr("d", path);
}
}
Insert cell
L = {
const L = await require("leaflet@1/dist/leaflet.js");
if (!L._style) {
const href = await require.resolve("leaflet@1/dist/leaflet.css");
document.head.appendChild(L._style = html`<link href=${href} rel=stylesheet>`);
}
return L;
}
Insert cell
d3.geoBounds(states)
Insert cell
states = topojson.feature(us, us.objects.states)
Insert cell
import {us, topojson} from "@pbogden/my-u-s-map-svg"
Insert cell
import {hover} from "@mkfreeman/plot-tooltip"
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