Skip to content

Instantly share code, notes, and snippets.

@kpq
Created November 19, 2013 04:54
Show Gist options
  • Save kpq/7540527 to your computer and use it in GitHub Desktop.
Save kpq/7540527 to your computer and use it in GitHub Desktop.
basic state choropleth starter

A basic starter for UCB students who want to make a choropleth map. Nearly identical to Mike Bostock's many examples with counties.

<!DOCTYPE html>
<meta charset="utf-8">
<style type="text/css">
body {
font-family: arial;
width:950px;
margin:20px auto;
}
.states-lines {
stroke: #fff;
stroke-width: 2px;
fill: none;
}
.states-fill {
/*fill:#f0f0f0;*/
}
</style>
<body>
<h4>Choropleth map of states</h4>
<div class="g-map"></div>
</body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 500;
var color = d3.scale.threshold()
.domain([.02, .04, .06, .08, .10])
.range(["#f2f0f7", "#dadaeb", "#bcbddc", "#9e9ac8", "#756bb1", "#54278f"]);
var path = d3.geo.path();
var svg = d3.select(".g-map").append("svg")
.attr("width", width)
.attr("height", height);
queue()
.defer(d3.json, "us.json")
.await(ready)
function ready(err, us) {
window.us = us;
svg.append("g")
.attr("class", "states")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
.attr("d", path)
.style("fill", "#ccc");
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a.id !== b.id; }))
.attr("class", "states-lines")
.attr("d", path);
}
</script>
Display the source blob
Display the rendered blob
Raw
Loading
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