Skip to content

Instantly share code, notes, and snippets.

@JMStewart
Created September 9, 2013 20:13
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 JMStewart/6500878 to your computer and use it in GitHub Desktop.
Save JMStewart/6500878 to your computer and use it in GitHub Desktop.
Zip Code Map
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.land {
fill: none;
stroke: #000;
}
.county-boundary {
fill: none;
stroke: #fff;
stroke-width: .5px;
}
.state-boundary {
stroke: #000;
}
.state-boundary:hover{
fill: red !important;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script>
//Test data
//stateData = {1:'green'};
stateData = [[1,'green'],
[2,'steelblue']];
var width = 960,
height = 500;
var projection = d3.geo.albersUsa()
.scale(1000)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("us_zips.json", function(error, us) {
// console.log(topojson.feature(us, us.objects.states).features);
console.log(topojson.feature(us, us.objects.tl_2010_us_zcta510).features);
//keys = _.keys(stateData);
// keys = _.map(stateData, function(entry){return entry[0];});
// data = _.map(topojson.feature(us, us.objects.tl_2010_us_zcta510).features, function(state){
// index = _.indexOf(keys, state.id);
// state.color = (index >= 0) ? stateData[index][1] : "white";
// return state;
// });
var data = topojson.feature(us, us.objects.tl_2010_us_zcta510).features;
/*
svg.append("path")
.datum(topojson.feature(us, us.objects.land))
.attr("d", path)
.attr("class", "land");
*/
/*
svg.insert("path", ".graticule")
.datum(topojson.mesh(us, us.objects.counties, function(a, b) { return a !== b && !(a.id / 1000 ^ b.id / 1000); }))
.attr("class", "county-boundary")
.attr("d", path);
svg.append("path", ".graticule")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "state-boundary")
.attr("d", path);
*/
svg.selectAll("path")
.data(data)
.enter().append("path")
.attr("class", "state-boundary")
.attr("id", function(d){return d.id})
.attr("d", path)
.style("fill", "white");
});
d3.select(self.frameElement).style("height", height + "px");
</script>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment