Last active
March 4, 2016 15:03
-
-
Save nkabrown/0346fa274bd0d3edad85 to your computer and use it in GitHub Desktop.
d3 geojson map of allegheny county pa polling stations
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>D3 GeoJSON</title> | |
<style> | |
html, | |
body { | |
background-color: #000; | |
} | |
</style> | |
<div class="container"> | |
<div class="map"></div> | |
</div> | |
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<script src="https://d3js.org/d3-queue.v2.min.js"></script> | |
<script> | |
const queue = d3_queue.queue(); | |
queue.defer(d3.json, 'watershed.geojson') | |
.defer(d3.json, 'polling_locations.geojson') | |
.await((error, map, points) => createMap(map, points)); | |
function createMap(map, points) { | |
const width = 805; | |
const height = 562; | |
const projection = d3.geo.mercator() | |
.scale(1) | |
.translate([0, 0]) | |
.precision(0); | |
const path = d3.geo.path().projection(projection); | |
const bounds = path.bounds(map); | |
// from Mike Bostock's Project to Bounding Box example http://bl.ocks.org/mbostock/4707858 | |
const scale = .95 / Math.max((bounds[1][0] - bounds[0][0]) / width, (bounds[1][1] - bounds[0][1]) / height); | |
const translate = [(width - scale * (bounds[1][0] + bounds[0][0])) / 2, (height - scale * (bounds[1][1] + bounds[0][1])) / 2]; | |
projection.scale(scale).translate(translate); | |
const svg = d3.select('.map') | |
.append('svg') | |
.attr('width', width) | |
.attr('height', height); | |
svg.selectAll('path') | |
.data(map.features) | |
.enter().append('path') | |
.attr('d', path) | |
.style('fill', 'none') | |
.style('stroke', '#dedede'); | |
svg.selectAll('path') | |
.data(points.features) | |
.enter().append('path') | |
.attr('d', path) | |
.style('fill', 'none') | |
.style('stroke', '#98ff98'); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment