A demonstration of the d3-hexbin plugin for D3 4.0.
Last active
March 9, 2019 16:58
-
-
Save mbostock/5583afd2a0d03b9c94918659fa151cac to your computer and use it in GitHub Desktop.
Hexagonal Binning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 | |
redirect: https://observablehq.com/@d3/hexbin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<svg width="960" height="500"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://d3js.org/d3-hexbin.v0.2.min.js"></script> | |
<script> | |
var svg = d3.select("svg"), | |
margin = {top: 40, right: 20, bottom: 20, left: 40}, | |
width = +svg.attr("width") - margin.left - margin.right, | |
height = +svg.attr("height") - margin.top - margin.bottom, | |
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
var randomX = d3.randomNormal(width / 2, 80), | |
randomY = d3.randomNormal(height / 2, 80), | |
points = d3.range(10000).map(function() { return [randomX(), randomY()]; }); | |
var hexbin = d3_hexbin.hexbin() | |
.extent([[-margin.left, -margin.top], [width + margin.right, height + margin.bottom]]) | |
.radius(10); | |
var bins = hexbin(points); | |
var color = d3.scaleSequential(d3.interpolateMagma) | |
.domain([d3.max(bins, function(d) { return d.length; }), 0]); | |
g.append("g") | |
.attr("fill", "none") | |
.attr("stroke", "#fff") | |
.attr("stroke-opacity", 0.5) | |
.selectAll("path") | |
.data(bins) | |
.enter().append("path") | |
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }) | |
.attr("fill", function(d) { return color(d.length); }) | |
.attr("d", hexbin.hexagon()); | |
g.append("g") | |
.call(d3.axisTop(d3.scaleIdentity().domain([0, width]))); | |
g.append("g") | |
.call(d3.axisLeft(d3.scaleIdentity().domain([0, height]))); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment