Created
August 8, 2017 17:35
-
-
Save milafrerichs/b2073e7f5a4b33d2af23ede7ab57af02 to your computer and use it in GitHub Desktop.
JS Bin Map with changing detail via buttons // source http://jsbin.com/wicozeq
This file contains hidden or 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> | |
<html> | |
<head> | |
<meta name="description" content="Map with changing detail via buttons"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"></script> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<style id="jsbin-css"> | |
path | |
fill: #ccc | |
</style> | |
</head> | |
<body> | |
<div id="map" style="width: 600px; height: 600px;"> | |
<svg width="600" height="600"> | |
<g> | |
</g> | |
</svg> | |
<button id="show-block">Block</button> | |
<button id="show-neighborhood">Neighborhood</button> | |
</div> | |
<script id="jsbin-javascript"> | |
var projection = d3.geoAlbers(); | |
var geoGenerator = d3.geoPath() | |
.projection(projection); | |
var map = d3.select('#map g') | |
neighborhoods = 'https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/boston.geojson'; | |
blocks = 'https://gist.githubusercontent.com/milafrerichs/12fbfb500be19a41f39a581c8285ecb0/raw/a0734d28b47d48a06ca917529369cb35d232602b/residential_and_non_residential_building_.geojson'; | |
function drawMap(data) { | |
projection | |
.fitExtent([[20, 20], [600, 600]], data) | |
u = map.selectAll('path') | |
.data(data.features); | |
u.enter() | |
.append('path') | |
.merge(u) | |
.attr('d', geoGenerator); | |
u.exit().remove() | |
} | |
var q = d3.queue(); | |
q.defer(d3.json,neighborhoods); | |
q.defer(d3.json,blocks) | |
.awaitAll(function(error, results) { | |
blockData = results[1]; | |
neighborhoodData = results[0]; | |
drawMap(blockData) | |
d3.select('#show-block').on('click', function() { drawMap(blockData);}) | |
d3.select('#show-neighborhood').on('click', function() { drawMap(neighborhoodData);}) | |
}) | |
</script> | |
<script id="jsbin-source-css" type="text/css">path | |
fill: #ccc</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var projection = d3.geoAlbers(); | |
var geoGenerator = d3.geoPath() | |
.projection(projection); | |
var map = d3.select('#map g') | |
neighborhoods = 'https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/boston.geojson'; | |
blocks = 'https://gist.githubusercontent.com/milafrerichs/12fbfb500be19a41f39a581c8285ecb0/raw/a0734d28b47d48a06ca917529369cb35d232602b/residential_and_non_residential_building_.geojson'; | |
function drawMap(data) { | |
projection | |
.fitExtent([[20, 20], [600, 600]], data) | |
u = map.selectAll('path') | |
.data(data.features); | |
u.enter() | |
.append('path') | |
.merge(u) | |
.attr('d', geoGenerator); | |
u.exit().remove() | |
} | |
var q = d3.queue(); | |
q.defer(d3.json,neighborhoods); | |
q.defer(d3.json,blocks) | |
.awaitAll(function(error, results) { | |
blockData = results[1]; | |
neighborhoodData = results[0]; | |
drawMap(blockData) | |
d3.select('#show-block').on('click', function() { drawMap(blockData);}) | |
d3.select('#show-neighborhood').on('click', function() { drawMap(neighborhoodData);}) | |
})</script></body> | |
</html> |
This file contains hidden or 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
path | |
fill: #ccc |
This file contains hidden or 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
var projection = d3.geoAlbers(); | |
var geoGenerator = d3.geoPath() | |
.projection(projection); | |
var map = d3.select('#map g') | |
neighborhoods = 'https://raw.githubusercontent.com/codeforamerica/click_that_hood/master/public/data/boston.geojson'; | |
blocks = 'https://gist.githubusercontent.com/milafrerichs/12fbfb500be19a41f39a581c8285ecb0/raw/a0734d28b47d48a06ca917529369cb35d232602b/residential_and_non_residential_building_.geojson'; | |
function drawMap(data) { | |
projection | |
.fitExtent([[20, 20], [600, 600]], data) | |
u = map.selectAll('path') | |
.data(data.features); | |
u.enter() | |
.append('path') | |
.merge(u) | |
.attr('d', geoGenerator); | |
u.exit().remove() | |
} | |
var q = d3.queue(); | |
q.defer(d3.json,neighborhoods); | |
q.defer(d3.json,blocks) | |
.awaitAll(function(error, results) { | |
blockData = results[1]; | |
neighborhoodData = results[0]; | |
drawMap(blockData) | |
d3.select('#show-block').on('click', function() { drawMap(blockData);}) | |
d3.select('#show-neighborhood').on('click', function() { drawMap(neighborhoodData);}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment