Skip to content

Instantly share code, notes, and snippets.

@MTClass
Created February 18, 2017 22:37
Show Gist options
  • Save MTClass/131b05eec39f35367bc986dbe5a99bd5 to your computer and use it in GitHub Desktop.
Save MTClass/131b05eec39f35367bc986dbe5a99bd5 to your computer and use it in GitHub Desktop.
class-02 demo // source https://jsbin.com/xejeba
<!DOCTYPE html>
<meta charset="utf-8">
<title>class-02 demo</title>
<style>
.line {
fill: none;
stroke: black;
stroke-width: 1px;
}
rect {
fill: none;
stroke: black;
stroke-width: 1px;
}
path.quake, circle.quake {
fill: crimson;
fill-opacity: 0.4;
}
path.counties, path.county-border {
fill: none;
stroke: #000;
stroke-opacity: .2;
}
path.state {
fill: none;
stroke: #000;
}
</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;
var height = 500;
var smallest = 2.5; // threshold magnitude
var data = null; // global variable
// Set the map projection centered on Oklahoma
var projection = d3.geoAlbersUsa()
.scale(7543)
.translate([702, -186]);
var path = d3.geoPath()
.pointRadius(5)
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var layer1 = svg.append("g");
var layer2 = svg.append("g");
var base = "https://gist.githubusercontent.com/pbogden/935370a5272acff2618b/raw/f4d102d819066bb21a0021b4cbab6502e86bf587/";
var usgs = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson";
// Read and plot the earthquake data
var url = base + "quakes.json";
d3.json(url, plotQuakes);
//d3.json(usgs, plotQuakes);
// Read and plot the state & county boundaries
d3.json(base + "ok.json", plotState);
function plotQuakes(error, json) {
if (error) console.log(error);
// Assign the json to a global so we can manipulate it in the console
data = json;
// Filter the small earthquakes
var features = data.features.filter(function(d) { return (+ d.properties.mag >= smallest) });
// Plot the earthquakes
layer2.selectAll("path.quake")
.data(features)
.enter().append("path")
.attr("class", "quake")
.attr("d", path)
}
function plotState(error, ok) {
if (error) console.log(error);
var objects = ok.objects.ok;
var all = topojson.merge(ok, objects.geometries);
layer1.append("path")
.datum(all)
.attr("class", "state")
.attr("d", path);
layer1.append("path")
.datum(topojson.mesh(ok, objects, function(a, b) { return a !== b; }))
.attr("class", "county-border")
.attr("d", path);
}
</script>
<meta charset="utf-8">
<title>class 3 - states</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;
}
</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 layer = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.on("mousemove", mousemoved);
var info = d3.select("body").append("div")
.attr("class", "info");
var projection = d3.geoAlbersUsa();
var path = d3.geoPath()
.projection(projection);
// If you use this URL, you won't need your own copy of the data.
var url = "https://umbcvis.github.io/classes/class-03/us.json"
d3.json(url, plotStates);
function plotStates(error, json) {
// Create array of GeoJSON features -- this defines the global variable "data"
data = json.objects.us.geometries.map(function(d) { return topojson.feature(json, d); })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment