-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathverify_adjacent.html
More file actions
334 lines (298 loc) · 10 KB
/
verify_adjacent.html
File metadata and controls
334 lines (298 loc) · 10 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
stroke: black;
fill: white;
}
path.blank {
fill: white;
}
path.district0 {
fill: green;
}
path.district1 {
fill: orange;
}
path.district2 {
fill: rgb(200, 60, 200);
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<h2>Redistrict Chester County, PA</h2>
This page allows you to assign the voting areas of Chester county to one of
three districts (green, orange and purple).
Then see whether the Democrats or Republicans will win each district.
<p>
Click here to select which district you want to build. You can also
click in a area that has been assigned and drag your mouse to
assign nearby areas.
</p>
<p>
<svg id="paletteSvg" width="500px" height="60px" align="left">
<g id="palette"></g>
</svg>
<br>
<svg id="scoreSvg" width="500px" height="160px" align="left">
<g id="score"></g>
<text x=10, y=160, style="font-family:monospace">Percent Democrat</text>
</svg>
</p>
<svg id="mapSvg" width="600px" height="600px">
<g class="map"></g>
</svg>
<script>
var nDistricts = 3
// Colors for each user-assigned district
var districtColors = ["green", "orange", "rgb(200, 60, 200)"]
var districtPopulation = [0, 0, 0]
var districtDemocrats = [0, 0, 0]
var districtRepublicans = [0, 0, 0]
var maxBias = 0
var totalPopulation = 0;
// Desired population for each district
var targetPopulation = 0;
// To which district are we currently assigning areas
var currentDistrictBrush = 0
// Is the mouse down
var dragging = false;
var width = 600,
height = 600;
var svg = d3.select("#mapSvg")
.attr("width", width)
.attr("height", height);
var geojson = []
// Main startup
d3.json("data_import/Chester.geojson", function (error, x) {
geojson = x;
initializePopulation();
initMap();
refreshScores();
refreshPalette();
});
function highlightNeighbors(feature) {
return d3.selectAll('#mapSvg path').filter(function (d, i) { return feature.properties.adjacent_features.indexOf(i) > -1 }).attr('class', 'district0');
}
// Compute total population
// and therefore desired population
// per district
function initializePopulation() {
for (var i = 0; i < geojson.features.length; i++) {
var feature = geojson.features[i];
totalPopulation += feature.properties.VAPERSONS
var dems = feature.properties.democrats
var reps = feature.properties.republicans
if ((dems != null) && (reps != null)) {
maxBias = Math.max(maxBias, Math.abs(dems - reps))
}
}
targetPopulation = totalPopulation / nDistricts
}
// Assign an area to a district
function assignToDistrict(feature, district) {
var pop = feature.properties.VAPERSONS
var dems = feature.properties.democrats
var reps = feature.properties.republicans
if (feature.properties.district != null) {
districtPopulation[feature.properties.district] -= pop
districtDemocrats[feature.properties.district] -= dems
districtRepublicans[feature.properties.district] -= reps
}
feature.properties.district = district
districtPopulation[feature.properties.district] += pop
districtDemocrats[feature.properties.district] += dems
districtRepublicans[feature.properties.district] += reps
refreshScores();
}
function selectColor(d, i) {
var district = d.properties.district
if (district != null) return "";
else {
var feature = geojson.features[i]
var dems = feature.properties.democrats
var reps = feature.properties.republicans
if (dems > reps) {
var hue = 240 //blue
var saturation = 60
var lightness = 100 - Math.trunc(25 * (dems - reps) / maxBias)
} else {
var hue = 0 //red
var saturation = 60
var lightness = 100 - Math.trunc(25 * (reps - dems) / maxBias)
}
var color = "hsl(" + hue + "," + saturation + "%," + lightness + "% )"
return color
}
}
// Refresh D3 binding
// between features and map areas
function initMap() {
var projection = d3.geoAlbers()
.fitSize([width, height], geojson);
var geoGenerator = d3.geoPath()
.projection(projection);
d3.select("#mapSvg")
.selectAll('g')
.data(geojson.features)
.enter()
.append('path')
.attr("id", function (d, i) {
// give each division an id
return "division" + i
})
.style("stroke", "rgb(177, 176, 176)")
.style("stroke-width", 1)
.attr("class", function (d, i) {
// Get the district that has been assigned by the user
// Should be a zero-based number
// or missing for unassigned areas
var district = d.properties.district
// Use that to determine the area's style
if (district != null) return "district" + district;
else return ""
})
.attr('d', geoGenerator)
// .style("fill", selectColor)
.on("click", function (e, i) {
d3.selectAll('#mapSvg path').attr('class', 'blank')
highlightNeighbors(e)
// assignToDistrict(geojson.features[i], currentDistrictBrush)
// refreshMap(i);
})
.on("mousedown", function (e, i) {
if (geojson.features[i].properties.district != null) {
currentDistrictBrush = geojson.features[i].properties.district
dragging = true;
refreshPalette();
}
})
.on("mouseup", function (e, i) {
dragging = false;
})
.on("mouseenter", function (e, i) {
if (dragging) {
assignToDistrict(geojson.features[i], currentDistrictBrush)
refreshMap(i);
refreshPalette();
}
}).append("svg:title")
.text(function (d, i) {
var feature = geojson.features[i]
pop = feature.properties.VAPERSONS
var dems = feature.properties.democrats
var reps = feature.properties.republicans
if (reps > dems) return "R +" + (reps - dems)
else return "D +" + (dems - reps)
})
}
// Refresh D3 binding
// between features and map areas
function refreshMap(division) {
d3.select('#division' + division)
.attr("class", function (d, i) {
// Get the district that has been assigned by the user
// Should be a zero-based number
// or missing for unassigned areas
// d.properties.district has already been updated
var district = d.properties.district
// Use that to determine the area's color
if (district != null) return "district" + district;
else return ""
})
// .style("fill", selectColor)
}
// Refresh D3 binding between
// the districts and the palette
function refreshPalette() {
d3.select("#paletteSvg")
.selectAll('circle')
.data(districtColors)
.style("stroke", function (d, i) {
if (i == currentDistrictBrush) return "black";
else return districtColors[i];
})
.enter()
.append("circle")
.attr("cx", function (d, i) { return 30 + i * 40 })
.attr("cy", 30)
.attr("r", 18)
// .style("fill", function (d, i) {
// return districtColors[i];
// })
.style("stroke-width", 4)
// Shouldn't have to set this here
// but it doesn't seem to get initialized properly
.style("stroke", function (d, i) {
if (i == currentDistrictBrush) return "black";
else return districtColors[i];
})
.on("click", function (e, i) {
currentDistrictBrush = i;
dragging = false;
refreshPalette()
})
}
// Refresh D3 binding between districts
// and their scores
function refreshScores() {
// Colored border for each fill tank
d3.select("#scoreSvg")
.selectAll('.targetpop')
.data(districtPopulation)
.enter()
.append('rect')
.attr("class", "targetpop")
.attr("x", function (d, i) { return 10 + 40 * i })
.attr("y", "5")
.attr("width", "36")
.attr("height", "100")
// .style("fill", "white")
.style("stroke-width", 3)
.style("stroke", function (d, i) {
return districtColors[i];
})
.on("click", function (e, i) {
currentDistrictBrush = i;
dragging = false;
refreshPalette()
})
d3.select("#scoreSvg")
.selectAll('.fraction')
.data(districtPopulation)
.attr("height", function (d, i) { return 100 * districtPopulation[i] / targetPopulation })
.attr("y", function (d, i) { return 5 + 100 - (100 * districtPopulation[i] / targetPopulation) })
.enter()
.append('rect')
.attr("class", "fraction")
.attr("x", function (d, i) { return 10 + 40 * i })
.attr("width", "36")
// .style("fill", function (d, i) {
// return districtColors[i];
// })
// for each
var predictedWinners = d3.select("#scoreSvg")
.selectAll('.predictedWinner')
.data(districtDemocrats)
.text(percentageText)
.enter()
.append('text')
.attr("class", "predictedWinner")
.attr("height", 20)
.attr("y", 130)
.attr("x", function (d, i) { return 10 + 40 * i })
.attr("width", "36")
.style("font-family", "monospace")
.text(percentageText)
}
function percentageText(d, i) {
var dems = districtDemocrats[i];
var reps = districtRepublicans[i];
if (dems) {
if (dems > reps) return "" + Math.round(100 * dems / (dems + reps)) + "%"
else return "" + Math.round(100 * dems / (dems + reps)) + "%"
} else {
return " --"
}
}
</script>