Built with blockbuilder.org
xxxxxxxxxx
<meta charset="utf-8">
<style>
.background {
fill: none;
pointer-events: all;
}
.mpio {fill : #ddc;}
.mpio-borde {
fill: none;
stroke: #fff;
stroke-linejoin: round;
stroke-width: 0.5;
}
.depto-borde {
fill: none;
stroke: #fff;
stroke-linejoin: round;
stroke-width: 2;}
#feature {
fill: #ccc;
cursor: pointer;
}
#feature.active {
fill: orange;
}
.mesh {
fill: none;
stroke: #fff;
stroke-linecap: round;
stroke-linejoin: round;
}
</style>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 600,
height = 600,
active = d3.select(null);
var projection = d3.geo.mercator()
.scale(1300)
.translate([width / 2, height / 2])
.center([-61,43])
.rotate([2,3,2]);
var path = d3.geo.path()
.projection(projection);
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");
/*
ogr2ogr -f GeoJSON depts.json depto.shp -s_srs EPSG:26986 -t_srs EPSG:4326
topojson --id-property NOMBRE_DPT -p name=NOMBRE_DPT -p name -o colombia-departamentos.json depts.json
*/
d3.json("colombia-municipios.json", function(error, co) {
var subunits = topojson.feature(co, co.objects.mpios);
g.append("path")
.datum(subunits)
.attr("d", path);
/*g.selectAll(".mpio")
.data(topojson.feature(co, co.objects.mpios).features)
.enter().append("path")
.attr("class", function(d) { return "mpio " + "_" + d.id; })
.attr("id", "feature")
.attr("d", path)
.on("click", clicked)
.style("fill", function(d) {
if(d.properties.dpt=="CUNDINAMARCA"){
return "#F01";
}
else{
return "#ccc"
}
});*/
g.selectAll(".mpio")
.data(topojson.feature(co, co.objects.depts).features)
.enter().append("path")
.attr("class", function(d) { return "mpio" + "_" + d.properties.dpt; })
.attr("id", "feature")
.attr("d", path)
.style("fill", "#ccc")
.on("click", clicked);
g.append("path")
.datum(topojson.mesh(co, co.objects.mpios, function(a, b) { return a !== b; }))
.attr("d", path)
.attr("class", "mpio-borde");
g.append("path")
.datum(topojson.mesh(co, co.objects.depts, function(a, b) { return a !== b; }))
.attr("d", path)
.attr("class", "depto-borde");
});
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>
Modified http://d3js.org/d3.v3.min.js to a secure url
Modified http://d3js.org/topojson.v1.min.js to a secure url
https://d3js.org/d3.v3.min.js
https://d3js.org/topojson.v1.min.js