Skip to content

Instantly share code, notes, and snippets.

@zunayed
Last active January 14, 2022 23:20
Show Gist options
  • Save zunayed/9081453 to your computer and use it in GitHub Desktop.
Save zunayed/9081453 to your computer and use it in GitHub Desktop.
D3 world map plus animation
place lat lon
New York City 40.6452217 -73.9493866
London 51.5064316 -0.12719
Montreal 45.5172005 -73.5498962
Toronto 43.6483994 -79.4857025
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<!DOCTYPE html>
<meta charset="utf-8">
<div id="d3_map"></div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
/*global d3: false */
"use strict";
//set up map
var w = 960;
var h = 500;
//map type & center point
var projection = d3.geo.mercator()
.center([-47.109375, 34.664841])
.scale(350);
var path = d3.geo.path().projection(projection);
//initialize map
var svg = d3.select("#d3_map")
.append("svg")
.attr("width", w)
.attr("height", h);
// seperating out the elements lets us organize the draw order
var country_group = svg.append("g");
var arcGroup = svg.append("g");
var point_group = svg.append("g");
//reading geoJSON & CSV files
d3.json("zunayed/9081453/raw//countries.json", function(json){
createMap(json);
});
d3.csv("zunayed/9081453/raw//cities.csv", function(json){
createPoints(json);
});
//draw world map
var createMap = function (json) {
country_group.append("g")
.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("title", function(d) { return d.id; })
.style("fill", "#43a2ca")
.attr("d", path);
};
var createLinks = function (json) {
var links = [];
for(var i=0, len=json.length-1; i<len; i++){
links.push({
type: "LineString",
coordinates: [
[ json[0].lon, json[0].lat ],
[ json[i+1].lon, json[i+1].lat ]
]
});
}
return links;
};
// place dots on screen based on cities
var createPoints = function(json){
point_group.selectAll("circle")
.data(json)
.enter()
.append("circle")
.attr("cx", function(d) { return projection([d.lon, d.lat])[0]; })
.attr("cy", function(d) { return projection([d.lon, d.lat])[1]; })
.attr("place", function(d) { return d.place; })
.attr("r", 6)
.style("fill", "red")
.on("mouseover", mouseover);
//functions for tweening the path
var lineTransition = function lineTransition(path) {
path.transition()
.duration(3000)
.attrTween("stroke-dasharray", tweenDash);
};
var tweenDash = function tweenDash() {
var len = this.getTotalLength(),
interpolate = d3.interpolateString("0," + len, len + "," + len);
return function(t) { return interpolate(t); };
};
//assemble links from office locations
var links = createLinks(json);
//append the arcs
arcGroup.selectAll(".arc")
.data(links)
.enter()
.append("path")
.attr({ "class": "arc" })
.attr({ d: path })
.style({ fill: "none", stroke: "red", "stroke-width": "3px" })
.call(lineTransition);
};
//D3 hoverbox info way
var mouseover = function() {
var place = d3.select(this).attr("place");
d3.select("#info").html("<b>Contact / " + place + "</b>");
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment