Skip to content

Instantly share code, notes, and snippets.

@enjalot
Last active June 21, 2016 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enjalot/badd72a55b963d9fbf87a739f5a87734 to your computer and use it in GitHub Desktop.
Save enjalot/badd72a55b963d9fbf87a739f5a87734 to your computer and use it in GitHub Desktop.
WWSD #8: Data lookups
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>San Francisco Precincts / Prop F</title>
<style>
body {
height: 100%;
margin: 0;
padding: 0;
font-family: 'Lato', sans-serif;
color: #272a2f;
}
#mapcanvas {
width: 960px;
height: 600px;
margin: 10px 10px 10px 10px;
}
.precinct path {
fill: #afafaf;
stroke: #545454;
}
.precinct-boundary {
fill: none;
stroke: #111;
}
</style>
</head>
<body>
<div id="mapcanvas" ></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script>
<script src="https://d3js.org/queue.v1.min.js"></script>
<script>
var width = 960,
height = 600;
// MERCATOR
var projection = d3.geo.mercator()
.center([-122.433701, 37.767683])
.scale(211000)
.translate([width / 2, height/2]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("#mapcanvas").append("svg")
.attr("width", width)
.attr("height", height);
// fancy way of combining AJAX requests
queue()
.defer(d3.json, 'http://enjalot.github.io/wwsd/data/USA/sf-precincts.topojson')
.defer(d3.csv, 'http://enjalot.github.io/wwsd/data/USA/sf-precincts.csv') // REF
.await(ready);
// this function gets called when all the data is loaded from the queue
function ready(error, sfjson, csv) {
if (error) throw error;
// Ignore this topojson thing for now
var precincts = topojson.feature(sfjson, sfjson.objects.precinct);
console.log("precincts", precincts)
console.log("ref", csv)
svg.append("g").attr("class", "precinct")
.selectAll("path")
.data(precincts.features)
.enter()
.append("path")
.attr("d", path)
};
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment