Built with blockbuilder.org
Last active
March 30, 2017 21:44
-
-
Save is64377/7c8b54c4855c285a1d07678bdd398070 to your computer and use it in GitHub Desktop.
Class7a
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: mit |
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> | |
<meta charset="utf-8"> | |
<title>earthquakes</title> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
.dot { | |
stroke: #000; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v4.min.js"></script> | |
<script src="//d3js.org/d3-scale-chromatic.v1.min.js"></script> | |
<script> | |
var margin = {top: 20, right: 20, bottom: 30, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var x = d3.scaleLinear() | |
.range([0, width]); | |
var y = d3.scaleLinear() | |
.range([height, 0]); | |
//var color = d3.scaleOrdinal(d3.schemeSpectral); | |
var color = d3.scaleSequential(d3.interpolateRainbow); | |
var xAxis = d3.axisBottom() | |
.scale(x); | |
var yAxis = d3.axisLeft() | |
.scale(y); | |
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 url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson"; | |
d3.json(url, function(error, json) { | |
if (error) throw error; | |
var data = json.features.map(function(d) { | |
return [ d.geometry.coordinates[0], d.geometry.coordinates[1], d.properties.mag ]; | |
}); | |
x.domain(d3.extent(data, function(d) { return d[0]; })).nice(); | |
y.domain(d3.extent(data, function(d) { return d[2]; })).nice(); | |
color.domain(d3.extent(data, function(d) { return d[1]; })); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis) | |
.append("text") | |
.attr("class", "label") | |
.attr("x", width) | |
.attr("y", -6) | |
.style("text-anchor", "end") | |
.text("Longitude"); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
.append("text") | |
.attr("class", "label") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.text("Magnitude"); | |
svg.selectAll(".dot") | |
.data(data) | |
.enter().append("circle") | |
.attr("class", "dot") | |
.attr("r", 3.5) | |
.attr("cx", function(d) { return x(d[0]); }) | |
.attr("cy", function(d) { return y(d[2]); }) | |
.style("fill", function(d) { return color(d[1]); }); | |
var legend = svg.selectAll(".legend") | |
.data(d3.range(10 * Math.round(color.domain()[1] / 10), 10 * Math.round(color.domain()[0] / 10), -10)) | |
.enter().append("g") | |
.attr("class", "legend") | |
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); | |
legend.append("rect") | |
.attr("x", width - 18) | |
.attr("width", 18) | |
.attr("height", 18) | |
.style("fill", color); | |
legend.append("text") | |
.attr("x", width - 24) | |
.attr("y", 9) | |
.attr("dy", ".35em") | |
.style("text-anchor", "end") | |
.text(function(d) { return d; }); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment