Skip to content

Instantly share code, notes, and snippets.

@Digital-Banana
Last active January 22, 2020 00:43
Show Gist options
  • Save Digital-Banana/46610709d4423b90ade4e21ce78a33d0 to your computer and use it in GitHub Desktop.
Save Digital-Banana/46610709d4423b90ade4e21ce78a33d0 to your computer and use it in GitHub Desktop.
Maastricht Choropleth Map
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Maastricht</title>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Roboto:400,700" rel="stylesheet">
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<svg id="map"></svg>
<script src="index.js"></script>
</body>
</html>
var margin = { top: 50, right: 50, bottom: -20, left: 50 },
width = 300 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var svg = d3.select("#map")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top +")");
var projection = d3.geoMercator()
.center([5.690973, 50.851368])
.scale(110000)
.translate([width / 2, height / 2]);
var path = d3.geoPath()
.projection(projection);
var totaalInw = [0, 500, 1000, 2000, 3000, 4000, 5000, 6000, 7000];
var palette = ['#fff7f3','#fde0dd','#fcc5c0','#fa9fb5','#f768a1','#dd3497','#ae017e','#7a0177','#49006a'];
var color = d3.scaleLinear()
.domain(totaalInw)
.range(palette);
var tip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
d3.json("maastricht.json", function(err, json) {
if (err) throw error;
function drawMap() {
svg = svg.append("g")
.attr("class", "maastricht")
.selectAll("#map")
.data(json.features);
svg
.enter().append("g")
.append("path")
.attr("d", path)
.style("fill", function(d) {
return color(d.properties.AANT_INW);
})
.on("mouseover", function(d) {
tip.transition()
.duration(300)
.style("opacity", .9)
tip.html(
"<h4>" + "Plaats " + "</h4>" + "<p>" + d.properties.BU_NAAM + "</p>" +
"<br>" + "<hr> " +
"<h4>" + "Inwoners " + "</h4>" + "<p>" + d.properties.AANT_INW) + "</p>"
})
.on("mousemove", function(d) {
tip
.style("left", (d3.event.pageX + 15) + "px")
.style("top", (d3.event.pageY + 5) + "px");
})
.on("mouseout", function(d) {
tip.transition()
.duration(300)
.style("opacity", 0);
});
};
drawMap();
});
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.
html {
font-family: 'Roboto', sans-serif;
}
body {
background: whitesmoke;
}
g.maastricht path {
stroke: indigo;
stroke-width: 1px;
stroke-linejoin: round;
stroke-linecap: round;
}
g.maastricht path:hover {
opacity: .85;
transition: .2s;
}
div.tooltip {
position: absolute;
width: 160px;
height: 110px;
padding: 15px;
background: #eee;
border: 1px solid #444;
border-radius: 3px;
pointer-events: none;
}
h1, h2, h3, h4, p, a, span, div {
margin: 0;
}
.tooltip p {
margin: 0;
display: inline;
width: 200px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment