Skip to content

Instantly share code, notes, and snippets.

@martgnz
Last active January 22, 2017 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martgnz/78f636dd308ad043ba8f7065afaa2e0e to your computer and use it in GitHub Desktop.
Save martgnz/78f636dd308ad043ba8f7065afaa2e0e to your computer and use it in GitHub Desktop.
University graduates in Madrid
height: 650
border: none
license: mit
<!DOCTYPE html>
<meta charset="utf-8" />
<style>
body {
max-width: 960px;
position: relative;
}
.legend {
position: absolute;
right: 0;
top: 0;
font-family: "Helvetica Neue",sans-serif;
font-size: 12px;
display: block;
padding: 10px;
height: 85px;
width: 100px;
}
</style>
<body>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="https://unpkg.com/rbush@1.4.3/rbush.js"></script>
<script src="https://unpkg.com/spamjs@1.1.0/spam.min.js"></script>
<script src="https://unpkg.com/d3-svg-legend@1.13.0/d3-legend.min.js"></script>
<script type='text/javascript'>
// escala
var color = d3.scale.quantile()
.range(["#ca0020", "#f4a582", "#f7f7f7", "#92c5de", "#0571b0"])
// leyenda
d3.select("body").append("svg")
.attr("class", "legend")
var legend = d3.legend.color()
.shapeHeight(10)
.shapeWidth(30)
.shapePadding(10)
.labelOffset(5)
.labelFormat(d3.format("%"))
.orient("vertical")
.labelAlign("start")
// abrir TopoJSON
d3.json("madrid.json", function(error, d) {
topojson.presimplify(d)
// calcular máximo y mínimo
color.domain(d3.extent(d.objects["madrid"].geometries, function(d) { return d.properties.rate }))
// pasar la escala a la leyenda
legend.scale(color)
// llamar a la leyenda
d3.select(".legend")
.call(legend)
// Spam
var map = new StaticCanvasMap({
element: "body",
projection: d3.geo.mercator()
.center([-3.7063242,40.5066516])
.scale(80000),
height: 620,
data: [{
features: topojson.feature(d, d.objects["madrid"]),
static: {
paintfeature: function(parameters, d) {
// pintar el mapa
parameters.context.fillStyle = color(d.properties.rate)
parameters.context.fill()
parameters.context.lineWidth = 0.5
parameters.context.strokeStyle = 'rgba(0,0,0,0.2)'
parameters.context.stroke()
}
}
}]
})
map.init()
})
</script>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment