Built with blockbuilder.org
Created
April 28, 2017 00:59
-
-
Save nohevog1/78889b6f2dc5206f6e2d25ac73640f7c to your computer and use it in GitHub Desktop.
Assignment#13_Vrai
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>Class 5</title> | |
<style> | |
body { | |
position: absolute; | |
margin: 0px; | |
font: 16px sans-serif; | |
} | |
svg { | |
background-color: #4682b4; | |
} | |
.info { | |
color: #000; | |
position: absolute; | |
top: 450px; | |
left: 800px; | |
} | |
path { | |
fill: #555555; | |
stroke: #aaaaaa; | |
} | |
path.quake { | |
fill: crimson; | |
} | |
</style> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://d3js.org/topojson.v2.min.js"></script> | |
<body> | |
<script> | |
var width = 960, height = 500; | |
var data; // declare a global variable | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.on("mousemove", mousemoved); | |
var layer1 = svg.append("g"); | |
var layer2 = svg.append("g"); | |
var info = d3.select("body").append("div") | |
.attr("class", "info"); | |
// NEW: Title | |
svg.append('text') | |
.attr('x', width / 2) | |
.attr('y', "1.5em") | |
.style('font-size', '1.5em') | |
.style('text-anchor', 'middle') | |
.text('Earthquakes in the last week') | |
var projection = d3.geoAlbersUsa(); | |
var path = d3.geoPath() | |
.projection(projection); | |
// NEW: Create a color scale for the earthquakes | |
var color = d3.scaleOrdinal(d3.schemeCategory10); | |
// Add a legend | |
var legend = layer2.selectAll('.legend') | |
.data(d3.range(10)) | |
.enter().append('g') | |
.attr('class', 'legend') | |
.attr('transform', 'translate(' + [width - 150, height / 2] +')'); | |
legend.append('circle') | |
.attr('cy', function(d) { return 4 * d * path.pointRadius() ; }) | |
.attr('r', path.pointRadius()) | |
.attr('fill', color) | |
.attr('stroke', '#aaaaaa') | |
legend.append('text') | |
.attr('x', '1em') | |
.attr('y', function(d) { return 4 * d * path.pointRadius(); }) | |
.attr('dy', '.3em') | |
.text(function(d) { return d + ' - ' + (d + 1); }) | |
var usgs = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson"; | |
var url = "https://umbcvis.github.io/classes/class-03/us.json" | |
d3.json(url, plotStates); | |
d3.json(usgs, plotQuakes); | |
function plotStates(error, json) { | |
if (error) throw error; | |
json = json.objects.us.geometries.map(function(d) { return topojson.feature(json, d); }) | |
layer1.selectAll("path") | |
.data(json) | |
.enter() | |
.append("path") | |
.attr("d", path); | |
} | |
// NEW: Various updates for magnitude-dependent labels and colors | |
function plotQuakes(error, json) { | |
if (error) throw error; | |
features = json.features.filter(inRange); // Only those in the viewport | |
var mags = features.map(function(d) { return d.properties.mag }); | |
var largest = d3.max(mags); | |
layer2.selectAll("path.quake") | |
.data(features) | |
.enter().append("path") | |
.attr("class", "quake") | |
.style("fill", magColor) | |
.attr("d", path) | |
// Remove unnecessary legend elements | |
legend.filter(function(d) { return d > largest; }).remove(); | |
// Update the legend labels to indicate # of earthquakes in range | |
legend.select('text') | |
.text(function(value) { return value + ' - ' + (value + 1) + " (" + | |
mags.filter(function(d) { return d >= value && d < value + 1; }).length + ")"; | |
}); | |
} | |
// projection returns null for [lon, lat] that won't appear in the viewport | |
function inRange(d) { | |
return projection(d.geometry.coordinates) !== null; | |
} | |
// NEW: Return magnitude-dependent color | |
function magColor(d) { | |
return color(Math.floor(d.properties.mag)); | |
} | |
function mousemoved() { | |
info.text(formatLocation(projection.invert(d3.mouse(this)), projection.scale())); | |
} | |
function formatLocation(p, k) { | |
var format = d3.format("." + Math.floor(Math.log(k) / 2 - 2) + "f"); | |
return (p[1] < 0 ? format(-p[1]) + "°S" : format(p[1]) + "°N") + " " | |
+ (p[0] < 0 ? format(-p[0]) + "°W" : format(p[0]) + "°E"); | |
} | |
svg.append('circle').attr('cx',760).attr('cy',219).attr('r',50).style("fill", "goldenrod") | |
d3.selectAll("circle").data(d3.range(7)) | |
.call(d3.drag() | |
.on("start", dragstarted) | |
.on("drag", dragged) | |
.on("end", dragended)); | |
function dragstarted(d) { | |
d3.select(this).raise().classed("active", true); | |
} | |
function dragged(d) { | |
d3.select(this).attr("cx", d.x = d3.event.x).attr("cy", d.y = d3.event.y); | |
} | |
function dragended(d) { | |
d3.select(this).classed("active", false); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment