D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
MTClass
Full window
Github gist
class-02 demo // source http://jsbin.com/racetix
<!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(usgs, 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) }); var times = data.features.map(function(d) { return d.properties.time }) var minTime = d3.min(times); var formatTime = d3.timeFormat("%B %d, %Y"); california = features.filter(inCalifornia); console.log(california.length + " in California") function inCalifornia(d) { var lon = d.geometry.coordinates[0]; var lat = d.geometry.coordinates[1]; return (lon > -124.4096) && (lon < -114.1308) && (lat > 32.5343) && (lat < 42.0095); } console.log( formatTime( d3.min(times) ) ) // 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>
https://d3js.org/d3.v4.min.js
https://d3js.org/topojson.v2.min.js