Simple example of cloropleth map of Colombia by cities showing a diverging variable
Este mapa muestra la diferencia entre los votos del no y los del sí en Colombia. Cada ciudad o municipio está coloreado dependiendo de la diferencia entre los votos del no y los del sí.
Datos del plebiscito tomados del artículo de la Silla vacía
GEO Json from Javier Moreno mapa-colombia-js project on github
forked from mbostock's block: New Jersey State Plane
forked from john-guerra's block: GeoJson map of Colombia Municipios
forked from john-guerra's block: Cloropleth map of Colombian Cities
forked from john-guerra's block: Zoomable Cloropleth map of Colombian Cities
xxxxxxxxxx
<meta charset="utf-8">
<style>
.tract {
stroke: #777;
stroke-width: 0.1px;
}
.tract:hover {
stroke: orange;
}
.tract-border {
fill: none;
/*stroke: #777;*/
stroke-width: 0.1px;
pointer-events: none;
}
.tract-border-state {
fill: none;
stroke: #333;
stroke-width: 0.5px;
pointer-events: none;
}
.legend {
font-family: sans-serif;
font-size: 10pt;
}
.legendTitle {
font-weight: bold;
font-size:11pt;
}
.background {
fill:#c5c5c5;
}
</style>
<svg width="900" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.11.0/d3-legend.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
margin = { top: 20, bottom:20, right: 20, left: 20},
centered,
fmt = d3.format(" >5.0%");
function ready(error, mapData, data) {
if (error) throw error;
var dictCities = {};
data.forEach(function (d) {
//Parse the percentages
d["% No"] = +(d["% No"].slice(0,-1).replace(",", "."))/100;
d["% Si"] = +(d["% Si"].slice(0,-1).replace(",", "."))/100;
d.result = d["% Si"] - d["% No"];
var res = {};
dictCities[d.Municipio.toUpperCase()]=d;
});
var color = d3.scaleSequential(d3.interpolateRdBu)
.domain([-1, 1]);
var land = topojson.feature(mapData, {
type: "GeometryCollection",
geometries: mapData.objects.mpios.geometries.filter(function(d) {
return (d.id / 10000 | 0) % 100 !== 99;
})
});
var landState = topojson.feature(mapData, {
type: "GeometryCollection",
geometries: mapData.objects.depts.geometries.filter(function(d) {
return (d.id / 10000 | 0) % 100 !== 99;
})
});
// Add background
svg.append('rect')
.attr('class', 'background')
.attr('width', width)
.attr('height', height)
.on('click', clicked);
// To allow the zoom back
// svg.on('click', clicked);
var g = svg.append("g");
// EPSG:32111
var path = d3.geoPath()
.projection(d3.geoTransverseMercator()
.rotate([74 + 30 / 60, -38 - 50 / 60])
.fitExtent([[margin.left, margin.top], [width-margin.right, height-margin.bottom]], land));
var pathState = d3.geoPath()
.projection(d3.geoTransverseMercator()
.rotate([74 + 30 / 60, -38 - 50 / 60])
.fitExtent([[margin.left, margin.top], [width-margin.right, height-margin.bottom]], landState));
g.selectAll("path")
.data(land.features)
.enter().append("path")
.attr("class", "tract")
.on('click', clicked)
.style("fill", function (d) {
var city = dictCities[d.properties.name];
if (city)
return color(city.result);
else {
console.log(d.properties.name + "," + d.properties.dpt);
return color(0);
}
})
.attr("d", path)
.append("title")
.text(function(d) {
var city = dictCities[d.properties.name];
var msg = d.properties.name + ", " + d.properties.dpt;
if (city)
msg += " %Si - %No: " + fmt(city.result);
return msg;
});
g.append("path")
.datum(topojson.mesh(mapData, mapData.objects.mpios, function(a, b) { return a !== b; }))
.attr("class", "tract-border")
.attr("d", path);
g.append("path")
.datum(topojson.mesh(mapData, mapData.objects.depts, function(a, b) { return a !== b; }))
.attr("class", "tract-border-state")
.attr("d", pathState);
// The legend
svg.append("g")
.attr("class", "legend")
.attr("transform", "translate(660,20)");
var legendLinear = d3.legendColor()
// .shapeWidth(30)
.cells(7)
.orient('vertical')
.title('%Si - %No')
.labelFormat(fmt)
.labelAlign("start")
.scale(color);
svg.select(".legend")
.call(legendLinear);
// When clicked, zoom in
function clicked(d) {
console.log("Clicked!");
var x, y, k;
// Compute centroid of the selected path
if (d && centered !== d) {
var centroid = path.centroid(d);
x = centroid[0];
y = centroid[1];
k = 6;
centered = d;
} else {
x = width / 2;
y = height / 2;
k = 1;
centered = null;
}
// // Highlight the clicked province
// svg.selectAll('path')
// .style('fill', function(d){return centered && d===centered ? '#D5708B' : fillFn(d);});
// Zoom
g.transition()
.duration(750)
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')scale(' + k + ')translate(' + -x + ',' + -y + ')');
}
}
d3.queue()
.defer(d3.json, "colombia-municipios.json" )
.defer(d3.csv, "plebiscito_Colombia_2016.csv" )
.await(ready);
</script>
https://d3js.org/d3.v4.min.js
https://d3js.org/d3-scale-chromatic.v1.min.js
https://d3js.org/topojson.v1.min.js
https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.11.0/d3-legend.min.js