Skip to content

Instantly share code, notes, and snippets.

@eSlivinski
Last active March 14, 2017 01:49
Show Gist options
  • Save eSlivinski/fdaf7efa230a83ec427966bc3ce31f26 to your computer and use it in GitHub Desktop.
Save eSlivinski/fdaf7efa230a83ec427966bc3ce31f26 to your computer and use it in GitHub Desktop.
Fatal Force Hexbins
!function(){d3.hexbin=function(){function u(n){var r={};return n.forEach(function(n,t){var a=s.call(u,n,t)/o,e=Math.round(a),c=h.call(u,n,t)/i-(1&e?.5:0),f=Math.round(c),l=a-e;if(3*Math.abs(l)>1){var v=c-f,g=f+(f>c?-1:1)/2,m=e+(e>a?-1:1),M=c-g,d=a-m;v*v+l*l>M*M+d*d&&(f=g+(1&e?1:-1)/2,e=m)}var j=f+"-"+e,p=r[j];p?p.push(n):(p=r[j]=[n],p.i=f,p.j=e,p.x=(f+(1&e?.5:0))*i,p.y=e*o)}),d3.values(r)}function a(r){var t=0,u=0;return n.map(function(n){var a=Math.sin(n)*r,e=-Math.cos(n)*r,i=a-t,o=e-u;return t=a,u=e,[i,o]})}var e,i,o,c=1,f=1,h=r,s=t;return u.x=function(n){return arguments.length?(h=n,u):h},u.y=function(n){return arguments.length?(s=n,u):s},u.hexagon=function(n){return arguments.length<1&&(n=e),"m"+a(n).join("l")+"z"},u.centers=function(){for(var n=[],r=0,t=!1,u=0;f+e>r;r+=o,t=!t,++u)for(var a=t?i/2:0,h=0;c+i/2>a;a+=i,++h){var s=[a,r];s.i=h,s.j=u,n.push(s)}return n},u.mesh=function(){var n=a(e).slice(0,4).join("l");return u.centers().map(function(r){return"M"+r+"m"+n}).join("")},u.size=function(n){return arguments.length?(c=+n[0],f=+n[1],u):[c,f]},u.radius=function(n){return arguments.length?(e=+n,i=2*e*Math.sin(Math.PI/3),o=1.5*e,u):e},u.radius(1)};var n=d3.range(0,2*Math.PI,Math.PI/3),r=function(n){return n[0]},t=function(n){return n[1]}}();
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<script src="http://d3js.org/d3.v4.js"></script>
<script src="https://d3js.org/d3-queue.v3.min.js"></script>
<script src="./d3.hexbin.min.js"></script>
<style>
.states path {
fill: #ddd;
stroke: #fff;
stroke-width: 2px;
}
.hex-mesh path{
stroke: black;
stroke-opacity: 0.4;
stroke-width: 1;
fill-opacity: 0;
}
.hexagons path{
stroke-width: 2;
stroke: white;
}
</style>
</head>
<body>
<script>
var width = 960,
height = 500;
var svg = d3.select('body')
.append('svg')
.attr('width', width)
.attr('height', height);
var projection = d3.geoAlbersUsa();
var path = d3.geoPath()
.projection(projection);
var color = d3.scaleLinear()
.range(['#fff ', '#e31a1c'])
.domain([0, 20])
.interpolate(d3.interpolateLab);
var hexbin = d3.hexbin()
.size([width, height])
.radius(20);
var states = svg.append('g')
.attr('class', 'states');
var hexagon = svg.append('g')
.attr('class', 'hexagons');
var mesh = svg.append('g')
.attr('class', 'hex-mesh')
.append('path')
.attr('d', hexbin.mesh());
function render (geometry, shootings) {
states.selectAll('path')
.data(geometry.features)
.enter().append('path')
.attr('d', path);
hexagon.selectAll('path')
.data(hexbin(shootings.map(d => { return d.properties.projected; })))
.enter().append('path')
.attr('transform', d => { return 'translate(' + d.x + ',' + d.y + ')'; })
.attr('fill-opacity', 0)
.attr('d', hexbin.hexagon(1))
.transition()
.attr('fill', d => { return color(d.length); })
.attr('fill-opacity', 1)
.transition()
.attr('d', hexbin.hexagon(hexbin.radius()));
}
d3.queue()
.defer(d3.json, 'us.geojson')
.defer(d3.json, 'police-shootings-2015-2017.geojson') // https://www.washingtonpost.com/graphics/national/police-shootings-2015
.awaitAll((err, results) => {
if (err) { return console.error(err); }
shootings = results[1].features
.map(d => {
d.properties.projected = projection(d.geometry.coordinates);
return d;
})
.filter(d => { return d.properties.projected; });
render(results[0], shootings);
});
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
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