Skip to content

Instantly share code, notes, and snippets.

@martgnz
Last active January 22, 2017 19:42
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/c6ef23788c61fac40b44817d1bf19870 to your computer and use it in GitHub Desktop.
Save martgnz/c6ef23788c61fac40b44817d1bf19870 to your computer and use it in GitHub Desktop.
Rent in Barcelona by neighbourhood in 2014
license: mit
border: none
height: 650
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.
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.
<!DOCTYPE html>
<meta charset="utf-8" />
<style>
body {
max-width: 960px;
position: relative;
}
.tooltip {
position: absolute;
z-index: 1;
width: 325px;
height: 60px;
padding: 8px;
background: rgb(255,255,255);
font-family: "Helvetica Neue", "Helvetica", Arial sans-serif;
font-size: 12px;
border: 1px solid rgba(0,0,0,0.2);
box-shadow: 0 3px 5px rgba(0,0,0,0.5),0 0 0 1px rgba(0,0,0,.08);
border-radius: 2px;
pointer-events: none;
}
.g-place {
border-bottom: 1px solid rgb(130,130,130);
padding-bottom: 3px;
margin-bottom: 5px;
}
.g-headline {
font-size: 16px;
}
.g-sub {
color: rgb(130,130,130);
}
.g-value {
float: right;
font-family: "Menlo", monospace;
}
.g-rank {
float: right;
font-size: 14px;
}
.legend {
position: absolute;
right: 0;
top: 0;
font-family: "Helvetica Neue", sans-serif;
font-size: 12px;
height: 90px;
width: 150px;
}
</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'>
var hover = null
// Color scale and legend
var color = d3.scale.quantile()
.domain([30, 60, 90, 150, 250])
.range(["#b2182b", "#ef8a62", "#fddbc7", "#d1e5f0", "#67a9cf", "#2166ac"])
d3.select("body").append("svg")
.attr("class", "legend")
var legend = d3.legend.color()
.shapeHeight(10)
.shapeWidth(30)
.shapePadding(5)
.labelOffset(5)
.orient("vertical")
.labelAlign("start")
.scale(color)
d3.select(".legend")
.call(legend)
// Tooltip stuff
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0)
document.onmousemove = handleMouseMove
function handleMouseMove(event) {
mouseX = event.pageX
mouseY = event.pageY
// yolo
if (mouseX <= 400) {
tooltip.style("left", mouseX - 80 + "px")
} else if (mouseX >= 600) {
tooltip.style("left", mouseX - 300 + "px")
} else {
tooltip.style("left", mouseX - 160 + "px")
}
if (mouseY >= 500) {
tooltip.style("top", mouseY - 100 + "px")
} else {
tooltip.style("top", mouseY + 25 + "px")
}
}
d3.json("districtes.json", function(error, districtes) {
d3.json("bcn.json", function(error, d) {
topojson.presimplify(d)
var map = new StaticCanvasMap({
element: "body",
width: 960,
height: 650,
projection: d3.geo.mercator()
.center([29.6, 30.47])
.scale(250000)
.rotate([0,0,-37.6]),
data: [{
features: topojson.feature(d, d.objects["bcn"]),
static: {
paintfeature: function(parameters, d) {
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()
}
},
dynamic: {
postpaint: function(parameters) {
if (!hover || !hover.properties.rate) {
tooltip.style("opacity", 0)
return
}
parameters.context.beginPath()
parameters.context.lineWidth = 1 / parameters.scale
parameters.path(hover)
parameters.context.stroke()
tooltip.style("opacity", 1)
.html("<div class='g-place'>" +
"<span class='g-headline'>"+ hover.properties.neighbourhood + "</span>" +
"<span class='g-rank'>" + hover.properties.ranking + " of 73</span><br />" +
"<span class='g-sub'>"+ hover.properties.district + "</span><br />" +
"</div>" +
"<div class='g-val'>" +
"<span>Index (Barcelona = 100)</span>" +
"<span class='g-value'>" + hover.properties.rate + "</span>" +
"</div>")
}
},
events: {
hover: function(parameters, d) {
hover = d
parameters.map.paint()
}
}
},
{
features: topojson.feature(districtes, districtes.objects["districtes"]),
static: {
paintfeature: function(parameters, d) {
parameters.context.lineWidth = 1 / parameters.scale
parameters.context.strokeStyle = "rgb(70,70,70)"
parameters.context.stroke()
}
}
}]
})
map.init()
})
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment