Skip to content

Instantly share code, notes, and snippets.

@tomgp
Last active August 29, 2015 13:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Small multiple maps
<html>
<head>
<title>small multiple maps</title>
<style type="text/css">
body{
font-family: sans-serif;
}
.map{
display: inline-block;
text-align: center; }
path{
stroke-width:1; }
.yet-to-vote{
fill:none;
stroke:#666;
stroke-width:0.1; }
.voted{
fill:#666;
stroke:#666;}
.voting{
fill:#FF2600;
stroke:#FF2600;}
.none{
fill:none;
stroke:none; }
</style>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<div id="maps"></div>
</body>
<script type="text/javascript">
var currentDate = '2014-2-10';
var mapWidth = 200;
var mapHeight = 120;
var projection = d3.geo.albersUsa()
.scale(220)
.translate([mapWidth / 2, mapHeight / 2]);
var path = d3.geo.path()
.projection(projection);
var dateFormat = d3.time.format("%B %e");
d3.json("map.geojson", function(error, map) {
var dates_o = {};
map.features.forEach(function(d,i){
if(!dates_o[d.properties.date]){
dates_o[d.properties.date] = true;
}
});
var dates = Object.keys(dates_o);
console.log(dates);
//draw a map for each date
var dateJoin = d3.select('#maps').selectAll('div.map')
.data(dates);
var divs = dateJoin.enter()
.append('div').attr({
'id':function(d){ return 'map_'+d; },
'class':'map'
})
divs.append('p').text(function(d){ return dateFormat(new Date(d)); })
var SVGs = divs.append('svg').attr({
'width':mapWidth,
'height':mapHeight
});
SVGs.each(function(date){
d3.select(this).selectAll('path')
.data(map.features)
.enter().append("path")
.attr({
"d":path,
"id":function(d){
return d.properties.name + date;
}
});
});
function update(){
SVGs.each(function(date){
var now = new Date(date);
d3.select(this).selectAll('path')
.data(map.features)
.attr({
"class":function(d,i){
if(d.properties.date){
var stateDate = new Date(d.properties.date);
if(stateDate > now){
return 'yet-to-vote';
}else if(stateDate < now){
return 'voted';
}
return 'voting';
}
return 'none';
}
});
})
}
update();
});
</script>
</html>
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