forked from gerrymandr/ap-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmichael.html
149 lines (131 loc) · 3.85 KB
/
michael.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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
stroke: black;
fill: white;
}
</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","yellow", "rgb(200, 60, 200)"]
// What district are we painting areas
var currentDistrictBrush = 2
var width =600,
height = 600;
var svg = d3.select("#mapSvg")
.attr("width", width)
.attr("height", height);
var geojson = []
function updateDisplay() {
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)
.style("fill",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 districtColors[district];
else return "white"
})
.attr('d', geoGenerator)
.on("click", function(e, i){
geojson.features[i].properties.district = currentDistrictBrush;
updateDisplay();
drawScores();
});
}
d3.json("data/Penn MCDS Data/Chester/Chester.geojson", function(error, x) {
geojson = x;
console.log(x);
updateDisplay();
drawScores();
});
function drawPalette() {
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 20 + i*40})
.attr("cy",30)
.attr("r",18)
.style("fill",function(d,i){
return districtColors[i];
})
.style("stroke-width",4)
.on("click", function(e, i){
currentDistrictBrush = i;
drawPalette()
})
}
drawPalette()
function drawScores() {
var totalPopulation = 0;
var districtPopulation = new Array(nDistricts);
for (var i =0; i<nDistricts; i++) districtPopulation[i] = 0;
for (var i = 0; i < geojson.features.length; i++) {
}
for (var i = 0; i < geojson.features.length; i++) {
var feature = geojson.features[i];
totalPopulation += feature.properties.VAPERSONS
var district = feature.properties.district
if (district != null) {
districtPopulation[district] += feature.properties.VAPERSONS
}
}
var targetPopulation = totalPopulation / nDistricts
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",2)
.style("stroke","grey")
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>