-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
506 lines (450 loc) · 14.9 KB
/
index.js
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/** Global constants */
var pi = Math.PI,
tau = 2 * pi;
var projection, geoGenerator;
var nDistricts = 3
var oversizeThreshold = 1.05; // 5% over even population
// Name of the overall objects
// in the topojson file
var mainTopology = "Chester"
var datafile = "data/Penn VTD Data/Chester/Chester.topojson"
// Names of fields in the geojson
var populationField = "POPULATION"
var democratsField = "PRES04_DEM"
var republicansField = "PRES04_REP"
// Colors for each user-assigned district
var districtColors = ["green", "orange", "rgb(200, 60, 200)"]
// Number of empty areas to
// examine for possible assignment
// each time the user clicks "Fill"
var fillRate = 30;
var minLightness = 40;
var width = 600,
height = 600;
/** Variables determined when data are first loaded */
var maxBias = 0
var maxDemocratBias = 0
var maxRepublicanBias = 0
var totalPopulation = 0;
// Desired population for each district
var targetPopulation = 0;
var topology = null;
var expander = null;
var features = null;
var geojson = null;
var geometries
var propertiesById = {};
/** Variables that changes from user interactions */
var nAssignedDistricts = 0
var districtPopulation = [0, 0, 0]
var districtDemocrats = [0, 0, 0]
var districtRepublicans = [0, 0, 0]
// To which district are we currently assigning areas
var currentDistrictBrush = 0
// Is the mouse down
var dragging = false;
var svg = d3.select("#mapSvg")
.attr("width", width)
.attr("height", height);
// Main startup
loadData();
function loadData() {
d3.json(datafile, function (error, topo) {
topology = topo;
geojson = topojson.feature(topology, topology.objects[mainTopology]);
geometries = topology.objects[mainTopology].geometries
features = geojson.features;
for (var i=0; i< features.length; i++) {
var id = features[i].properties.NAME10
geometries[i].id = id
propertiesById[id] = features[i].properties
}
expander = new SimpleExpander(topology);
initializePopulation();
initializeVoteCounts()
initMap();
resetDistricts();
});
}
/**
* Set all area assignments
* back to inital state.
*/
function resetDistricts() {
nAssignedDistricts = 0
districtPopulation = [0, 0, 0]
districtDemocrats = [0, 0, 0]
districtRepublicans = [0, 0, 0]
// To which district are we currently assigning areas
currentDistrictBrush = 0
// Is the mouse down
dragging = false;
for (var i = 0; i < features.length; i++) {
var feature = features[i];
feature.properties.district = null;
refreshMap(i);
}
refreshScores();
refreshPalette();
initBorder();
d3.select("#summary").text("")
}
function doExpand() {
expander.expand(fillRate);
}
function maskMouseOver() {
dragging = false;
}
function changeElection() {
var selector = d3.select("#electionSelector")
var selectedElection = selector.property('value')
democratsField = selectedElection + "_DEM"
republicansField = selectedElection + "_REP"
initializeVoteCounts()
resetDistricts()
initMap()
}
function initBorder() {
// Find the outer border of all the areas
// Note that this assumes to
var border = topojson.merge(topology,
topology.objects[mainTopology].geometries);
d3.select("#mapSvg")
.select('#border')
.append('path')
.datum(border)
.style("stroke", "black")
.style("stroke-width", 4)
.style("fill", "none")
.style("opacity", "1.0")
.attr('d', geoGenerator)
}
// Compute total population
// and therefore desired population
// per district
function initializePopulation() {
totalPopulation = 0;
targetPopulation = 0;
for (var i = 0; i < features.length; i++) {
var feature = features[i];
totalPopulation += feature.properties[populationField]
}
targetPopulation = totalPopulation / nDistricts;
}
function initializeVoteCounts() {
var totalDems = 0;
var totalReps = 0;
for (var i = 0; i < features.length; i++) {
var feature = features[i];
var dems = feature.properties[democratsField]
var reps = feature.properties[republicansField]
if ((dems != null) && (reps != null)) {
maxBias = Math.max(maxBias, Math.abs(dems - reps))
maxDemocratBias = Math.max(maxBias, dems - reps)
maxRepublicanBias = Math.max(maxBias, reps - dems)
totalDems += dems;
totalReps += reps;
}
}
console.log("dems:" + totalDems + " reps:"+totalReps + " percent dem:" + Math.round(100* (totalDems/(totalDems+totalReps)) ))
}
// Assign an area to a district
function assignToDistrict(feature, district) {
if (district == feature.properties.district) return;
var pop = feature.properties[populationField]
var dems = feature.properties[democratsField]
var reps = feature.properties[republicansField]
if (feature.properties.district != null) {
districtPopulation[feature.properties.district] -= pop
if ((dems != null) && (reps != null)) {
districtDemocrats[feature.properties.district] -= dems
districtRepublicans[feature.properties.district] -= reps
}
} else {
nAssignedDistricts ++;
}
feature.properties.district = district
districtPopulation[feature.properties.district] += pop
if ((dems != null) && (reps != null)) {
districtDemocrats[feature.properties.district] += dems
districtRepublicans[feature.properties.district] += reps
}
refreshScores();
if (nAssignedDistricts == features.length) {
showSummary();
}
}
function showSummary() {
var message = "Congratulations. You assigned all the precincts to districts."
d3.select("#summary").text(message)
}
function selectColor(feature, i) {
var district = feature.properties.district
if (district != null) return "";
else {
var dems = feature.properties[democratsField]
var reps = feature.properties[republicansField]
if (dems > reps) {
var hue = 240 //blue
var saturation = 70
var lightness = 100 - Math.trunc(minLightness * (dems - reps) / maxBias)
} else {
var hue = 0 //red
var saturation = 70
var lightness = 100 - Math.trunc(minLightness * (reps - dems) / maxBias)
}
var color = "hsl(" + hue + "," + saturation + "%," + lightness + "% )"
return color
}
}
// Refresh D3 binding
// between features and map areas
function initMap() {
projection = d3.geoMercator()
.fitSize([width, height], geojson);
geoGenerator = d3.geoPath()
.projection(projection);
// Build background tiles
var tiles = d3.tile()
.size([width, height])
.scale(projection.scale() * tau)
.translate(projection([0, 0]))
();
d3.select("#mapSvg")
.select("#images").selectAll("image")
.data(tiles)
.enter()
.append("image")
.attr("xlink:href", function (d) { return "http://" + "abc"[d[1] % 3] + ".tile.openstreetmap.org/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; })
.attr("x", function (d) { return (d[0] + tiles.translate[0]) * tiles.scale; })
.attr("y", function (d) { return (d[1] + tiles.translate[1]) * tiles.scale; })
.attr("width", tiles.scale)
.attr("height", tiles.scale);
// Build vectors for each voting areas
d3.select("#mapSvg")
.select('#areas').selectAll("path")
.data(features)
.attr('d', geoGenerator)
.enter()
.append('path')
.attr("id", function (d, i) {
// give each division an id
return "division" + i
})
.attr('d', geoGenerator)
.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 "unassigned"
})
.style("stroke", "rgb(200, 200, 200)")
.style("stroke-width", 1)
.style("fill", selectColor)
.on("mousedown", function (e, i) {
if (features[i].properties.district != null) {
currentDistrictBrush = features[i].properties.district
refreshPalette();
} else {
assignToDistrict(features[i], currentDistrictBrush)
refreshMap(i);
}
dragging = true;
})
.on("mouseup", function (e, i) {
dragging = false;
})
.on("mouseenter", function (e, i) {
if (dragging) {
assignToDistrict(features[i], currentDistrictBrush)
refreshMap(i);
refreshPalette();
}
}).append("svg:title")
.text(function (d, i) {
var feature = features[i]
var name = feature.properties.NAME10
pop = feature.properties.POPULATION
var dems = feature.properties[democratsField]
var reps = feature.properties[republicansField]
if (reps > dems) return name + " R+" + Math.round(reps - dems)
else return name + " D+" + Math.round(dems - reps)
})
}
var selectedCartogramProperty
function changeCartogram() {
var selector = d3.select("#cartogramSelector")
selectedCartogramProperty = selector.property('value')
if (selectedCartogramProperty == "") {
// Revert to natural boundaries
initMap()
} else {
showCartogram()
}
}
/**
* @returns A non-negative value indicating the relative size
* of the precinct on the cartogram
* @param {a geometry or feature of one precinct} d
*/
function cartogramScore(d) {
var properties = d.properties ? d.properties : propertiesById[d.id]
var score
if (selectedCartogramProperty == "POPULATION") {
score = properties[populationField];
} else if (selectedCartogramProperty == "EXCESS_DEMS") {
var dems = properties[democratsField]
var reps = properties[republicansField]
score = dems - reps
} else if (selectedCartogramProperty == "EXCESS_REPS") {
var dems = properties[democratsField]
var reps = properties[republicansField]
score = reps - dems
} else {
score = 1
}
return score
}
function showCartogram() {
d3.select("#summary").text("processing...")
var precincts = d3.select("#mapSvg")
.select('#areas').selectAll("path")
var values = precincts.data()
.map(cartogramScore)
.filter(function(n) {
return !isNaN(n);
})
.sort(d3.ascending)
var lo = values[0],
hi = values[values.length - 1];
var scale = d3.scaleLinear()
.domain([lo, hi])
.range([1, 1000]);
carto = d3.cartogram()
.projection(projection)
.properties(function (d) {
return propertiesById[d.id]
})
.value(function(d) {
return scale(cartogramScore(d));
});
var newFeatures = carto(topology, geometries).features;
precincts
.data(newFeatures)
.transition()
.duration(750)
.ease(d3.easeLinear)
.attr('d', carto.path)
d3.select("#summary").text("")
}
// Refresh D3 binding
// for one division
function refreshMap(division) {
d3.select('#division' + division)
.attr("class", function (d, i) {
var district = d.properties.district
// Use that to determine the area's style
if (district != null) return "district" + district;
else return "unassigned"
})
.style("fill", selectColor)
}
// Refresh D3 binding between
// the districts and the palette
function refreshPalette() {
d3.select("#scoreSvg")
.selectAll('.targetpop')
.data(districtColors)
.style("stroke-width", function (d, i) {
if (i == currentDistrictBrush) return 8;
else return 3;
})
}
// 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 12 + 44 * i })
.attr("y", "29")
.attr("width", "30")
.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()
})
var nOversized = 0;
for (var d=0; d< nDistricts; d++) {
if ( (districtPopulation[d] / targetPopulation ) > oversizeThreshold ) nOversized ++;
}
if (nOversized == 0) {
d3.select("#overflowWarning").style("visibility","hidden")
} else {
d3.select("#overflowWarning").style("visibility","")
}
d3.select("#scoreSvg")
.selectAll('.fraction')
.data(districtPopulation)
.attr("height", function (d, i) { return 100 * districtPopulation[i] / targetPopulation })
.attr("y", function (d, i) { return 25+ 4+ 100 - (100 * districtPopulation[i] / targetPopulation) })
.enter()
.append('rect')
.attr("class", "fraction")
.attr("x", function (d, i) { return 12 + 44 * i })
.attr("width", "30")
.style("fill", function (d, i) {
return districtColors[i];
})
// For each
var predictedWinners = d3.select("#scoreSvg")
.selectAll('.predictedWinner')
.data(districtDemocrats)
.style("fill", function (d, i) {
var dems = districtDemocrats[i];
var reps = districtRepublicans[i];
if (dems) {
if (reps) {
if (dems > reps) return "blue"
else return "red"
} else {
return "blue"
}
} else {
return "gray"
}
})
.text(percentageText)
.enter()
.append('text')
.attr("class", "predictedWinner")
.attr("height", 20)
.attr("y", 154)
.attr("x", function (d, i) { return 10 + 44 * i })
.attr("width", "44")
.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 " --"
}
}