Built with blockbuilder.org
forked from nohevog1's block: Final_Project_Version1
xxxxxxxxxx
<meta charset="utf-8">
<title>Class 4</title>
<style>
body {
position: absolute;
margin: 0px;
}
svg {
background-color: #4682b4;
}
.info {
font-family: sans-serif;
color: #000;
position: absolute;
top: 450px;
left: 800px;
}
path {
fill: #555555;
stroke: #aaaaaa;
}
/* NEW: style the earthquakes */
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);
// NEW: Add one layer (i.e., <g> element) each for states & earthquakes
var layer1 = svg.append("g");
var layer2 = svg.append("g");
// Add a <div> element to label the mouse position
var info = d3.select("body").append("div")
.attr("class", "info");
var projection = d3.geoAlbersUsa();
var path = d3.geoPath()
.projection(projection);
// NEW: Read the earthquake data and send it to function "plotQuakes"
var usgs = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson";
// Topojson for US states
var department = "https://umbcvis.github.io/projects/nohevog1/data/Departements.json"
d3.json(department, plotStates);
d3.json(usgs, plotQuakes);
function plotStates(error, json) {
// Convert topojson to an Array of GeoJSON features
// This defines "data", a global variable that can be manipulated in the developer console
data = json.geometry.map(function(d) { return topojson.feature(json, d); })
// Plot the features (states) -- one <path> element for each state
// NEW: Plot the states in a <g> element
layer1.selectAll("path")
.data(data)
.enter()
.append("path")
.attr("d", path);
}
// NEW: Function that plots the earthquake data
function plotQuakes(error, json) {
if (error) return console.log(error);
// Global variable, data, will be visible in the console
data = json;
// Extract an array of feature objects, one for each earthquake
var features = json.features;
// Plot the earthquakes
layer2.selectAll("path.quake")
.data(features)
.enter().append("path")
.attr("class", "quake")
.attr("d", path)
}
// Label the geographic coordinates of the mouse position on the map
function mousemoved() {
info.text(formatLocation(projection.invert(d3.mouse(this)), projection.scale()));
}
// Format the text that will be displayed in the the <div> element on the map
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");
}
</script>
https://d3js.org/d3.v4.min.js
https://d3js.org/topojson.v2.min.js