Skip to content

Instantly share code, notes, and snippets.

@mbostock
Created April 20, 2017 15:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbostock/4606548f571791098f2b97689f662d06 to your computer and use it in GitHub Desktop.
Save mbostock/4606548f571791098f2b97689f662d06 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path:hover {
fill: rgb(204, 185, 116);
}
</style>
<svg width="150" height="131.25"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-contour.v1.min.js"></script>
<script>
var thresholds = [0, 4.5, 9, 13.5, 18];
var n = 8, m = 7, values = d3.merge(d3.transpose([
[18, 13, 10, 9, 10, 13, 18],
[13, 8, 5, 4, 5, 8, 13],
[10, 5, 2, 1, 2, 5, 10],
[9, 4, 1, 12, 1, 4, 9],
[10, 5, 2, 1, 2, 5, 10],
[13, 8, 5, 4, 5, 8, 13],
[18, 13, 10, 9, 10, 13, 18],
[18, 13, 10, 9, 10, 13, 18]
]));
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var contours = d3.contours()
.size([n, m])
.thresholds(thresholds.slice(0, -1));
var fill = d3.scaleLinear()
.domain(d3.extent(thresholds))
.range(["rgb(0, 0, 0)", "rgb(200, 200, 200)"]);
svg.selectAll("path")
.data(contours(values))
.enter().append("path")
.attr("d", d3.geoPath(d3.geoIdentity().scale(width / n)))
.attr("fill", function(d, i) { return fill(thresholds[i + 1]); })
.attr("stroke", "black");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment