-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgerrymandering.html
153 lines (122 loc) · 3.63 KB
/
gerrymandering.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
background: #565656;
}
svg {
position: absolute;
top: 0;
left: 0;
}
.boundary {
fill: none;
stroke: #fff;
stroke-width: 0px;
}
.mesh {
fill: none;
stroke: #fff;
stroke-linecap: round;
stroke-linejoin: round;
}
#option {
position: absolute;
top: 20px;
left: 20px;
z-index: 2;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/d3.geo.projection.v0.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<div id="option">
<button onclick="ppCols()"/>Polsby Popper</button>
<button onclick="chCols()"/>Convex Hull</button>
<button onclick="reCols()"/>Reock</button>
<button onclick="shCols()"/>Schwartzberg</button>
</div>
<script>
var width = 900,
height = 650;
var Rcolor = d3.scale.linear()
.domain([0,100])
.range(["#fff","#990012"]);
var Dcolor = d3.scale.linear()
.domain([0,100])
.range(["#fff","#000073"]);
var projection = d3.geo.albersUsa()
.scale(1200)
.translate([width / 2, height / 2]);
var zoom = d3.behavior.zoom()
.translate([0, 0])
.scale(1)
.scaleExtent([1, 8])
.on("zoom", zoomed);
var path = d3.geo.path()
.projection(projection);
var graticule = d3.geo.graticule();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var g = svg.append("g");
svg
.call(zoom)
.call(zoom.event);
d3.json("topo_uscd.json", function(error, us) {
g.selectAll("path")
.data(topojson.feature(us, us.objects.uscd).features)
.enter().append("path")
.attr("class", "district")
.attr("d", path)
.attr("fill", "#adadad")
g.append("path")
.datum(topojson.mesh(us, us.objects.uscd, function(a, b) { return a !== b; }))
.attr("class", "boundary")
.attr("d", path);
});
d3.select(self.frameElement).style("height", height + "px");
function ppCols() {
g.selectAll("path")
.attr("fill", function(d){
if (d.properties.party == "R") {return Rcolor(d.properties.Polsby_Popp)}
else if (d.properties.party == "D") {return Dcolor(d.properties.Polsby_Popp)}
else {return "#fff"}
});
}
function chCols() {
g.selectAll("path")
.attr("fill", function(d){
if (d.properties.party == "R") {return Rcolor(d.properties.Convex_Hull)}
else if (d.properties.party == "D") {return Dcolor(d.properties.Convex_Hull)}
else {return "#fff"}
});
}
function reCols() {
g.selectAll("path")
.attr("fill", function(d){
if (d.properties.party == "R") {return Rcolor(d.properties.Reock)}
else if (d.properties.party == "D") {return Dcolor(d.properties.Reock)}
else {return "#fff"}
});
}
function shCols() {
g.selectAll("path")
.attr("fill", function(d){
if (d.properties.party == "R") {return Rcolor(d.properties.Schwartzber)}
else if (d.properties.party == "D") {return Dcolor(d.properties.Schwartzber)}
else {return "#fff"}
});
}
function zoomed() {
g.style("stroke-width", 1.5 / d3.event.scale + "px");
g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
</script>
<!--
ogr2ogr -f GeoJSON /Users/LaughingMan/Desktop/projects/gerrymandering/uscd.json /Users/LaughingMan/Desktop/projects/gerrymandering/cb_2013_us_cd113_20m/cb_2013_us_cd113_20m.shp
topojson -o topo_uscd.json --id-property ID -q 1e5 -p party,STATE,Polsby_Popp,Schwartzber,Convex_Hull,Reock,first_name,last_name uscd.json
Thanks to Mike Bostock for being a living legend!
Also credit to Scott Murray and his excellent D3 book.
-->