Skip to content

Instantly share code, notes, and snippets.

@martgnz
Last active October 28, 2017 15:16
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/1c0fa3985d0a7b51437cdfd326cc2fda to your computer and use it in GitHub Desktop.
Save martgnz/1c0fa3985d0a7b51437cdfd326cc2fda to your computer and use it in GitHub Desktop.
Football coaches per million people
license: mit
border: none
height: 530
<!DOCTYPE html>
<meta charset="utf-8" />
<style>
body {
max-width: 960px;
font-family: "Helvetica Neue", "Helvetica", Arial sans-serif;
}
.tooltip {
position: absolute;
width: 250px;
height: 40px;
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;
}
.legend {
width: 350px;
padding-left: 0;
list-style: none;
margin: 0 auto;
text-align: center;
}
ul {
margin: 0 auto;
}
li {
display: inline-block;
}
li.key {
border-top-width: 10px;
border-top-style: solid;
font-size: 10px;
width: 10%;
height: 10px;
padding-left: 0;
padding-right: 0;
}
li:last-of-type:after {
content: "+";
}
li:first-of-type:after {
content: "<0.1";
}
</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/d3-geo-projection@0.2.16/d3.geo.projection.min.js"></script>
<script src="https://unpkg.com/spamjs@1.1.0/spam.min.js"></script>
<script type='text/javascript'>
var hover = null;
var color = d3.scale
.threshold()
.domain([0.1, 0.5, 1, 2.5])
.range(["#f0f9e8", "#bae4bc", "#7bccc4", "#43a2ca", "#0868ac"]);
var tooltip = d3
.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var legend = d3
.select("body")
.append("ul")
.attr("class", "legend");
var keys = legend.selectAll("li.key").data(color.range());
keys
.enter()
.append("li")
.attr("class", "key")
.style("border-top-color", String)
.text(function(d) {
var r = color.invertExtent(d);
return r[0];
});
document.onmousemove = handleMouseMove;
function handleMouseMove(event) {
mouseX = event.pageX;
mouseY = event.pageY;
tooltip.style("left", mouseX - 100 + "px").style("top", mouseY + 25 + "px");
}
var graticule = d3.geo.graticule();
d3.json("world.json", function(error, d) {
topojson.presimplify(d);
var map = new StaticCanvasMap({
element: "body",
width: 960,
projection: d3.geo.winkel3(),
data: [
{
features: topojson.feature(d, d.objects["countries"]),
static: {
prepaint: function(parameters, d) {
parameters.context.beginPath();
parameters.path(graticule());
parameters.context.lineWidth = 0.5;
parameters.context.strokeStyle = "rgb(170,170,170)";
parameters.context.stroke();
parameters.context.beginPath();
parameters.path(graticule.outline());
parameters.context.lineWidth = 1;
parameters.context.strokeStyle = "rgb(30,30,30)";
parameters.context.stroke();
},
paintfeature: function(parameters, d) {
parameters.context.lineWidth = 0.5 / parameters.scale;
parameters.context.strokeStyle = "rgba(30,30,30,0.8)";
parameters.context.stroke();
if (!d.properties.rate) {
parameters.context.fillStyle = "rgb(255,255,255)";
} else {
parameters.context.fillStyle = color(d.properties.rate);
}
parameters.context.fill();
}
},
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.id +
"</span><br />" +
"</div>" +
"<span>Football coaches per million people</span>" +
"<span class='g-value'>" +
d3.round(hover.properties.rate, 2) +
"</span>"
);
}
},
events: {
hover: function(parameters, d) {
hover = d;
parameters.map.paint();
}
}
}
]
});
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