-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathzippy.html
More file actions
246 lines (215 loc) · 8.06 KB
/
zippy.html
File metadata and controls
246 lines (215 loc) · 8.06 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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
stroke: black;
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>
<svg id="paletteSvg" width="500px" height="60px">
<g id="palette"></g>
</svg>
<svg id="scoreSvg" width="500px" height="120px">
<g id="score"></g>
</svg>
<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 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/Penn MCDS Data/Chester/Chester.geojson", function (error, x) {
geojson = x;
initializePopulation();
initMap()
// refreshMap();
refreshScores();
refreshPalette();
});
// Run on init
// 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
}
targetPopulation = totalPopulation / nDistricts
}
// Run when a division is colored
// Assign a division to a district
function assignToDistrict(feature, district) {
var pop = feature.properties.VAPERSONS
if (feature.properties.district != null) {
districtPopulation[feature.properties.district] -= pop
}
feature.properties.district = district
districtPopulation[feature.properties.district] += pop
refreshScores();
}
// 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", function (d, i) {
district = d.properties.district
return districtColors[district];
})
}
// Set up 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')
.style("stroke", "black")
.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 color
if (district != null) return "district" + district;
else return ""
})
.attr("id", function (d,i) {
// give each division an id
return "division"+i
})
.attr('d', geoGenerator)
.on("click", function (e, i) {
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
return "pop "+pop })
}
// 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() {
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", "none")
.style("stroke-width", 3)
.style("stroke", function (d, i) {
return districtColors[i];
})
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];
})
}
</script>