Using d3.js and TopoJson to draw a zoomable map of Tunisia. Done by Mohamed Ali Jamaoui. To know more about Tunisia check this Wiki http://en.wikipedia.org/wiki/Tunisia
Last active
September 30, 2015 02:16
-
-
Save mohamed-ali/0f583c21eca39eb1da5b to your computer and use it in GitHub Desktop.
d3.js and topoJson zoomable map of Tunisia
This file contains 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> | |
<meta charset="utf-8"> | |
<head> | |
<style> | |
.background { | |
fill: none; | |
pointer-events: all; | |
} | |
.feature { | |
fill: #ccc; | |
cursor: pointer; | |
} | |
.feature.active { | |
fill: orange; | |
} | |
path { | |
fill: #ccc; | |
stroke: #fff; | |
stroke-width: .5px; | |
} | |
path:hover { | |
fill: #00AEEC; | |
} | |
.mesh { | |
fill: none; | |
stroke: #fff; | |
stroke-linecap: round; | |
stroke-linejoin: round; | |
stroke-width: 1px; | |
} | |
</style> | |
</head> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://d3js.org/topojson.v1.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500, | |
active = d3.select(null); | |
var path = d3.geo.path(); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
svg.append("rect") | |
.attr("class", "background") | |
.attr("width", width) | |
.attr("height", height) | |
.on("click", reset); | |
var g = svg.append("g") | |
.style("stroke-width", "1.5px"); | |
d3.json("tunisia.json", function(error, topology) { | |
var featureCollection = topojson.feature(topology, topology.objects.governorates); | |
var bounds = d3.geo.bounds(featureCollection); | |
var centerX = d3.sum(bounds, function(d) {return d[0];}) / 2, | |
centerY = d3.sum(bounds, function(d) {return d[1];}) / 2; | |
var projection = d3.geo.mercator() | |
.scale(3000) | |
.center([centerX, centerY]); | |
path.projection(projection); | |
g.selectAll("path") | |
.data(featureCollection.features) | |
.enter().append("path") | |
.attr("d", path) | |
.attr("class", "feature") | |
.on("click", clicked); | |
g.append("path") | |
.datum(topojson.mesh(topology, featureCollection.features, function(a, b) { return a !== b; })) | |
.attr("class", "mesh") | |
.attr("d", path); | |
}); | |
function clicked(d) { | |
if (active.node() === this) return reset(); | |
active.classed("active", false); | |
active = d3.select(this).classed("active", true); | |
var bounds = path.bounds(d), | |
dx = bounds[1][0] - bounds[0][0], | |
dy = bounds[1][1] - bounds[0][1], | |
x = (bounds[0][0] + bounds[1][0]) / 2, | |
y = (bounds[0][1] + bounds[1][1]) / 2, | |
scale = .9 / Math.max(dx / width, dy / height), | |
translate = [width / 2 - scale * x, height / 2 - scale * y]; | |
g.transition() | |
.duration(750) | |
.style("stroke-width", 1.5 / scale + "px") | |
.attr("transform", "translate(" + translate + ")scale(" + scale + ")"); | |
} | |
function reset() { | |
active.classed("active", false); | |
active = d3.select(null); | |
g.transition() | |
.duration(750) | |
.style("stroke-width", "1.5px") | |
.attr("transform", ""); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment