Same chart as before. Only using hexbins.
xxxxxxxxxx
<meta charset="utf-8">
<style>
text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.hexagon {
fill: none;
stroke: #000;
stroke-width: .5px;
}
</style>
<body id="width">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="/mbostock/4248145/example/d3.hexbin.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
windowWidth = document.getElementById("width").offsetWidth
width = windowWidth - margin.left - margin.right,
height = windowWidth - margin.top - margin.bottom;
var randomX = d3.random.normal(width / 2, 160),
randomY = d3.random.normal(height / 2, 160),
points = d3.range(2000).map(function() { return [randomX(), randomY()]; });
var color = d3.scale.linear()
.domain([0, 20])
.range(["white", "steelblue"])
.interpolate(d3.interpolateLab);
var hexbin = d3.hexbin()
.size([width, height])
.radius(windowWidth/85);
var x = d3.time.scale()
.domain([new Date(2001, 01, 01), new Date()])
.range([0, width]);
var y = d3.scale.linear()
.domain([1, 6])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickSize(6, -height);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickSize(6, -width);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var re = new RegExp('Oklahoma');
svg.append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("class", "mesh")
.attr("width", width)
.attr("height", height);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("x", -10)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Magnitude");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("y", -15)
.attr("x", width - 10)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Date of Quake");
svg.append("g")
.attr("class", "title")
.attr("transform", "translate("+ height/2 +", 30)")
.append("text")
.style("text-anchor", "middle")
.style("font-size", "22px")
.text("Oklahoma Earthquakes");
d3.csv('https://earthquake.usgs.gov/fdsnws/event/1/query?format=csv&minmagnitude=1.0&minlongitude=-103&maxlongitude=-94&maxlatitude=37&minlatitude=33&starttime=2001-01-01', type, ready);
function type(d) {
d.place = d.place;
d.mag = +d.mag;
d.time = new Date(d.time);
d.point = [x(d.time), y(d.mag)];
return d;
}
function ready(error, quakes) {
if (error) {
return console.warn(error);
}
quakes = quakes.filter( function(d){ return re.test(d.place); });
quakes = quakes.map( function(d) { return d.point; });
svg.append("g")
.attr("clip-path", "url(#clip)")
.selectAll(".hexagon")
.data(hexbin(quakes))
.enter().append("path")
.attr("class", "hexagon")
.attr("d", hexbin.hexagon())
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.style("fill", function(d) { return color(d.length); })
.append("title")
.text(function(d) {
if (d.length == 1) {
return "1 quake in bin";
} else {
return d.length + " quakes in bin";
}
});
}
</script>
Updated missing url http://bl.ocks.org/mbostock/raw/4248145/d3.hexbin.min.js to /mbostock/4248145/example/d3.hexbin.min.js
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js
https://bl.ocks.org/mbostock/raw/4248145/d3.hexbin.min.js