D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
samurainamedmarcus
Full window
Github gist
ebola map
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> #map { position:absolute; width: 100%; height: 100%; } </head> <body> <div id="map"></div> <script> L.mapbox.accessToken = pk.eyJ1IjoiY29sbWFuamIiLCJhIjoiY2l6aWlxc25wMDJjbjMzbWk2aXhzajY5diJ9.KDaT_zLMhnDD8-cLSXSzJw //Setup our Leaflet map using Mapbox.js var map = L.mapbox.map('map', 'mapbox.pencil', {maxZoom: 18, minZoom: 14}) .setView([51.5119112,-0.10000], 15); // Setup our svg layer that we can manipulate with d3 var svg = d3.select(map.getPanes().overlayPane) .append("svg"); var g = svg.append("g").attr("class", "leaflet-zoom-hide"); function project(ll) { // our data came from csv, make it Leaflet friendly var a = [+ll.lat, +ll.lon]; // convert it to pixel coordinates var point = map.latLngToLayerPoint(L.latLng(ll)) return point; } d3.csv("dots.csv", function(err, data) { var dots = g.selectAll("circle.dot") .data(data) dots.enter().append("circle").classed("dot", true) .attr("r", 1) .style({ fill: "#0082a3", "fill-opacity": 0.6, stroke: "#004d60", "stroke-width": 1 }) .transition().duration(1000) .attr("r", 6) function render() { // We need to reposition our SVG and our containing group when the map // repositions via zoom or pan // https://github.com/zetter/voronoi-maps/blob/master/lib/voronoi_map.js var bounds = map.getBounds(); var topLeft = map.latLngToLayerPoint(bounds.getNorthWest()) var bottomRight = map.latLngToLayerPoint(bounds.getSouthEast()) svg.style("width", map.getSize().x + "px") .style("height", map.getSize().y + "px") .style("left", topLeft.x + "px") .style("top", topLeft.y + "px"); g.attr("transform", "translate(" + -topLeft.x + "," + -topLeft.y + ")"); // We reproject our data with the updated projection from leaflet g.selectAll("circle.dot") .attr({ cx: function(d) { return project(d).x}, cy: function(d) { return project(d).y}, }) } // re-render our visualization whenever the view changes map.on("viewreset", function() { render() }) map.on("move", function() { render() }) // render our initial visualization render() }) </script> </body>
https://d3js.org/d3.v4.min.js