A basic starter for UCB students who want to make a choropleth map. Nearly identical to Mike Bostock's many examples with counties.
Created
November 19, 2013 04:54
-
-
Save kpq/7540527 to your computer and use it in GitHub Desktop.
basic state choropleth starter
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> | |
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment