Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active March 6, 2019 05:25
Show Gist options
  • Save mbostock/bf2f5f02b62b5b3bb92ae1b59b53da36 to your computer and use it in GitHub Desktop.
Save mbostock/bf2f5f02b62b5b3bb92ae1b59b53da36 to your computer and use it in GitHub Desktop.
Contour Plot III
license: gpl-3.0
<!DOCTYPE html>
<svg width="960" height="500" stroke="#fff" stroke-width="0.5"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-contour.v1.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<script>
// Populate a grid of n×m values where -9.6 ≤ x ≤ 9.6 and -5 ≤ y ≤ 5.
var n = 240, m = 125, values = new Array(n * m);
for (var j = 0.5, k = 0; j < m; ++j) {
for (var i = 0.5; i < n; ++i, ++k) {
values[k] = sinCos(i / n * 19.2 - 9.6, 5 - j / m * 10);
}
}
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var color = d3.scaleSequential(d3.interpolateRdBu)
.domain([-1, 1]);
svg.selectAll("path")
.data(d3.contours()
.size([n, m])
(values))
.enter().append("path")
.attr("d", d3.geoPath(d3.geoIdentity().scale(width / n)))
.attr("fill", function(d) { return color(d.value); });
function sinCos(x, y) {
return Math.sin(x + y) * Math.sin(x - y);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment