Created
December 3, 2013 20:17
-
-
Save repustate/7776778 to your computer and use it in GitHub Desktop.
Loading a map of Toronto's wards from a GeoJSON file and rendered using d3.js
This file contains 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
// Load in GeoJSON data | |
d3.json("/media/js/toronto.json", function(json) { | |
//Bind data and create one path per GeoJSON feature | |
svg.select('#paths').selectAll("path") | |
.data(json.features) | |
.enter() | |
.append("path") | |
.attr("d", path) | |
.attr("name", function(d) { | |
ward_id = d.properties.NAME.match(ward_id_re)[0] | |
election_data = elections[ward_id] | |
return "<strong>"+d.properties.NAME+"</strong><br/><b>2010</b>: "+election_data.winner+": "+election_data.percent+"%"; | |
}) | |
.attr('ward', function(d) { | |
return d.properties.NAME.match(ward_id_re)[0] | |
}) | |
.style("fill", function(d) { | |
ward_id = d.properties.NAME.match(ward_id_re)[0] | |
election_data = elections[ward_id] | |
if (election_data.winner == 'Ford') { | |
return '#A1DAE9' | |
} else { | |
return '#ebebeb' | |
} | |
}) | |
.style("stroke", "#000") | |
.on("mouseout", function() { | |
d3.select('body').select('.datamaps-hoverover').style("display", "none") | |
d3.select(this).style('stroke-width', '1px'); | |
}) | |
.on("mouseover", function() { | |
d3.select(this).style('stroke-width', '4px'); | |
}) | |
.on("mousemove", function(d) { | |
var position = d3.mouse(this); | |
var node = this; | |
d3.select(this).style('stroke-width', '4px'); | |
d3.select('body').select('.datamaps-hoverover') | |
.style("display", "block") | |
.style('top', ( (position[1] + 30)) + "px") | |
.html(function() { | |
ward_id = node.attributes.ward.nodeValue | |
if (ward_id < 10) { | |
ward_id = '0'+ward_id | |
} | |
key = this_date.getMonth()+1 + '-' + this_date.getDate() + '-' + ward_id; | |
ward_sentiment = wards[key] | |
total = ward_sentiment.pos + ward_sentiment.neg | |
neg_percent = 0 | |
pos_percent = 0 | |
if (total > 0) { | |
neg_percent = Math.round(ward_sentiment.neg/total*100) | |
pos_percent = Math.round(ward_sentiment.pos/total*100) | |
} | |
return node.attributes.name.nodeValue + "<br/><b>2013</b>: Pos: " + pos_percent + "% , Neg: " + neg_percent + "%" | |
}) | |
.style('left', ( position[0]) + "px"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment