Tawain vote map.
Created
May 12, 2014 10:03
-
-
Save chilijung/5b50235c43162edb2947 to your computer and use it in GitHub Desktop.
Tawain vote map.
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> | |
/* CSS goes here. */ | |
#content { | |
fill: #FFF; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://d3js.org/topojson.v1.min.js"></script> | |
<div id="content"> | |
</div> | |
<script> | |
var w = 800, | |
h = 600; | |
var projection = d3.geo.mercator() | |
.center([120.979531, 23.978567]) | |
.scale(10000); | |
var color = d3.scale.category10(); | |
var path = d3.geo.path() | |
.projection(projection); | |
var svg = d3.select('#content') | |
.append('svg') | |
.attr('width', w) | |
.attr('height', h) | |
.attr('viewBox', "0 0 800 600") | |
.attr('preserveAspectRatio', 'xMidYMid'); | |
/* JavaScript goes here. */ | |
d3.json('twVote1982.topo.json', function(err, data) { | |
var topo = topojson.feature(data, data.objects["twVote1982.geo"]); | |
var topomesh = topojson.mesh(data, data.objects["twVote1982.geo"], function(a, b){ | |
return a !== b; | |
}); | |
topo.features.forEach(function(d, i) { | |
if(d.properties.name === "新北市第12選區" || d.properties.name === "澎湖縣第1選區" || d.properties.name=== "台東縣第1選區" || d.properties.name === "台中市第1選區") { | |
topo.features.splice(i, 1); | |
} | |
}) | |
console.log(topo.features) | |
svg.selectAll('path.county') | |
.data(topo.features) | |
.enter() | |
.append('path') | |
.attr('class', 'county') | |
.attr('class', function(d) { return d.properties.name; }) | |
.attr('id', function(d) { return d.properties.county; }) | |
.attr('d', path) | |
.style('fill', function(){ | |
return color(Math.random()); | |
}) | |
.style('stroke', 'none') | |
.style('opacity', 0.9); | |
svg.append('path') | |
.attr('class', 'boundary') | |
.datum(topomesh) | |
.attr('d', path) | |
.style('fill', 'none') | |
.style('stroke', "rgba(255,255,255,0.5)") | |
.style('stroke-width', '2px'); | |
}) | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment