Last active
August 29, 2015 13:58
-
-
Save bperel/9943623 to your computer and use it in GitHub Desktop.
Superimposed maps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> | |
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> | |
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> | |
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
<title></title> | |
<meta name="description" content=""> | |
<meta name="viewport" content="width=device-width"> | |
<link rel="stylesheet" href="map.css"> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
</head> | |
<body> | |
<script src="map.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#map, #externalSvg { | |
position: absolute; | |
left: 200px; | |
} | |
.subunit-label { | |
fill: #777; | |
fill-opacity: .5; | |
font-size: 14px; | |
font-weight: 300; | |
text-anchor: middle; | |
} | |
.subunit-boundary { | |
fill: none; | |
stroke: #777; | |
stroke-dasharray: 2,2; | |
stroke-linejoin: round; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var width = 960; | |
var mapHeight = 440; | |
var projection = d3.geo.equirectangular() | |
.scale((width + 1) / 2 / Math.PI) | |
.precision(.01); | |
var drag = d3.behavior.drag() | |
.origin(function(d) { return d; }) | |
.on("dragstart", dragstarted) | |
.on("drag", dragmove); | |
var zoom = d3.behavior.zoom() | |
.scaleExtent([0.01, 10]) | |
.on("zoom", zoomed); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", mapHeight) | |
.attr("id", "map"); | |
showMap("background", "ne_110m_coastline.json"); | |
d3.xml("German_Empire_1914.svg", "image/svg+xml", function(xml) { | |
svgMap = d3.select( | |
svg | |
.append("g") | |
.call(drag) | |
.call(zoom) | |
.node() | |
.appendChild(document.importNode(xml.documentElement, true))) | |
.attr("name", "German_Empire_1914") | |
.attr("id", "externalSvg") | |
.classed("externalSvg", true) | |
.datum({key: 1}); | |
}); | |
function showMap(id, filePath) { | |
d3.json(filePath, function(error, world) { | |
var path = d3.geo.path() | |
.projection(projection); | |
svg.append("g") | |
.attr("id", id) | |
.selectAll(".subunit") | |
.data(world.features) | |
.enter().append("path") | |
.attr("class", function(d) { | |
return "subunit-boundary subunit " + d.properties.adm0_a3; | |
}) | |
.attr("d", path); | |
}); | |
} | |
function dragstarted(d) { | |
d3.event.sourceEvent.stopPropagation(); | |
} | |
function dragmove(d) { | |
d.x = d3.event.x; | |
d.y = d3.event.y; | |
var x=d.x || 0; | |
var y=d.y || 0; | |
d3.select(this) | |
.attr("transform", "translate("+x+" "+y+")"); | |
} | |
function zoomed() { | |
d3.select(this).attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment