|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
.countries { |
|
fill: none; |
|
stroke: #fff; |
|
stroke-linejoin: round; |
|
} |
|
.legendThreshold { |
|
font-size: 12px; |
|
font-family: sans-serif; |
|
} |
|
.caption { |
|
fill: #000; |
|
text-anchor: start; |
|
font-weight: bold; |
|
} |
|
</style> |
|
<svg width="960" height="600"></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/d3-geo-projection.v2.min.js"></script> |
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.24.0/d3-legend.js"></script> |
|
<script> |
|
// The svg |
|
var svg = d3.select("svg"), |
|
width = +svg.attr("width"), |
|
height = +svg.attr("height"); |
|
|
|
// Map and projection |
|
var path = d3.geoPath(); |
|
var projection = d3.geoNaturalEarth() |
|
.scale(width / 2 / Math.PI) |
|
.translate([width / 2, height / 2]) |
|
var path = d3.geoPath() |
|
.projection(projection); |
|
|
|
// Data and color scale |
|
var data = d3.map(); |
|
var colorScheme = d3.schemeReds[6]; |
|
colorScheme.unshift("#eee") |
|
var colorScale = d3.scaleThreshold() |
|
.domain([1, 6, 11, 26, 101, 1001]) |
|
.range(colorScheme); |
|
|
|
// Legend |
|
var g = svg.append("g") |
|
.attr("class", "legendThreshold") |
|
.attr("transform", "translate(20,20)"); |
|
g.append("text") |
|
.attr("class", "caption") |
|
.attr("x", 0) |
|
.attr("y", -6) |
|
.text("Students"); |
|
var labels = ['0', '1-5', '6-10', '11-25', '26-100', '101-1000', '> 1000']; |
|
var legend = d3.legendColor() |
|
.labels(function (d) { return labels[d.i]; }) |
|
.shapePadding(4) |
|
.scale(colorScale); |
|
svg.select(".legendThreshold") |
|
.call(legend); |
|
|
|
// Load external data and boot |
|
d3.queue() |
|
.defer(d3.json, "http://enjalot.github.io/wwsd/data/world/world-110m.geojson") |
|
.defer(d3.csv, "mooc-countries.csv", function(d) { data.set(d.code, +d.total); }) |
|
.await(ready); |
|
|
|
function ready(error, topo) { |
|
if (error) throw error; |
|
|
|
// Draw the map |
|
svg.append("g") |
|
.attr("class", "countries") |
|
.selectAll("path") |
|
.data(topo.features) |
|
.enter().append("path") |
|
.attr("fill", function (d){ |
|
// Pull data for this country |
|
d.total = data.get(d.id) || 0; |
|
// Set the color |
|
return colorScale(d.total); |
|
}) |
|
.attr("d", path); |
|
} |
|
</script> |