-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
351 lines (325 loc) · 14.3 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Eleições presidenciais em São Paulo</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v2.11.0/mapbox-gl.css" rel="stylesheet">
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.2/mapbox-gl-draw.css" type="text/css">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.11.0/mapbox-gl.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.2/mapbox-gl-draw.js"></script>
<script src='https://unpkg.com/@turf/turf@6/turf.min.js'></script>
<script src="https://cdn.plot.ly/plotly-2.16.1.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<script src="data/voting.js"></script>
<script src="./helpers/mapbox_key.js"></script>
<script src="./helpers/addGraph.js"></script>
<script src="./helpers/calculateGraphValues.js"></script>
<link rel="stylesheet" href="style/style.css">
</head>
<body>
<div id="map" class="text-violet-800"></div>
<div id="main" class ="absolute w-1/4 top-0 right-0 my-5 mx-5">
<div id="info" class="relative right-0 overflow-auto rounded shadow-sm px-4 py-4 text-violet-800 font-mono my-2 mx-2">
<div class="my-1 text-sm font-bold">LOCAL DE VOTAÇÃO</div>
<div id="features" class="text-xs"></div>
<div id="graph" class="px-5 py-5 flex justify-center"></div>
</div>
<div id="info" class="relative right-0 overflow-auto rounded shadow-sm px-4 py-4 text-violet-800 font-mono my-2 mx-2">
<div class="my-1 text-sm font-bold">ÁREA RESUMO</div>
<div id="poligono-resumo" class="text-xs"></div>
</div>
<div id="menu" class="relative rounded shadow-sm px-4 py-4 my-2 mx-2 text-violet-800 font-mono">
</div>
</div>
<div id="legend" class="absolute bottom-0 left-0 my-12 mx-4 w-1/6 rounded">
<div class="relative rounded bg-gradient-to-l from-red-600/80 via-yellow-600/80 to-green-600/80 flex justify-evenly place-content-stretch">
<p class="font-mono text-violet-200 text-xs inline-block">+ Bolsonaro</p>
<p class="font-mono text-violet-200 text-xs inline-block">+ Lula</p>
</div>
</div>
<script>
mapboxgl.accessToken = mapbox_key
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/dark-v10',
center: [-46.633066, -23.550823],
zoom: 10,
projection: 'mercator'
});
const draw = new MapboxDraw({
displayControlsDefault: false,
controls: {
polygon: true,
trash: true
},
defaultMode: 'simple_select'
});
var clickedFeature = {'id': null, 'source': null}
var graph = false
map.addControl(draw, 'top-left');
map.on('draw.create', createPolygon);
map.on('draw.delete', createPolygon);
map.on('draw.update', createPolygon);
map.on('load', () => {
const lulaObjs = voting.features.filter(object => object.properties.INDICE_2022 >= 0);
const lulaGeoJson = {
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": lulaObjs
}
const bolsonaroObjs = voting.features.filter(object => object.properties.INDICE_2022 < 0);
const bolsonaroGeoJson = {
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": bolsonaroObjs
}
map.addSource('lulaSource',
{
type: 'geojson',
data: lulaGeoJson,
generateId: true
}
);
map.addSource('bolsonaroSource',
{
type: 'geojson',
data: bolsonaroGeoJson,
generateId: true
}
);
map.addLayer(
{
id: 'lulaCircles',
source: 'lulaSource',
type: 'circle',
layout: {
'visibility': 'visible',
},
paint: {
'circle-color':
[
'interpolate',
['linear'],
['get', 'INDICE_2022'],
0,
'#c6b72c',
20,
'#d6af28',
40,
'#f6a020',
60,
'#f37324',
80,
'#ea4d27',
100,
'#e12729',
],
'circle-opacity': 0.75,
'circle-radius': [
'case',
['boolean', ['feature-state', 'clicked'], false],
10,
5
]
}
},
'road-label'
);
map.addLayer(
{
id: 'bolsonaroCircles',
source: 'bolsonaroSource',
type: 'circle',
layout: {
'visibility': 'visible'
},
paint: {
'circle-color':
[
'interpolate',
['linear'],
['get', 'INDICE_2022'],
-100,
'#007f4e',
-80,
'#399849',
-60,
'#56a446',
-40,
'#72b043',
-20,
'#b5be2f',
0,
'#c6b72c',
],
'circle-opacity': 0.75,
'circle-radius': [
'case',
['boolean', ['feature-state', 'clicked'], false],
10,
5
]
}
},
'road-label'
);
});
map.on('mouseenter', ['bolsonaroCircles', 'lulaCircles'], () => {
map.getCanvas().style.cursor = 'crosshair'
});
map.on('mouseleave', ['bolsonaroCircles', 'lulaCircles'], () => {
map.getCanvas().style.cursor = 'grabbing'
});
map.on('click', (e) => {
if (clickedFeature.id != null) {
map.setFeatureState(
{
source: clickedFeature.source,
id: clickedFeature.id
},
{
clicked: false
}
);
}
const [ features ] = map.queryRenderedFeatures(e.point,{
layers:['bolsonaroCircles', 'lulaCircles']
});
if (features) {
map.flyTo({
center: [e.lngLat.lng, e.lngLat.lat],
zoom: 13
});
clickedFeature['id'] = features.id;
clickedFeature['source'] = features.layer.source;
map.setFeatureState(
{
source: clickedFeature.source,
id: clickedFeature.id
},
{
clicked: true
}
);
document.getElementById('features').innerHTML = `
<div>
<p>TIPO: ${features.properties.LV_TIPO}</p>
<p>NOME: ${features.properties.NOME_LV}</p>
<p>ENDEREÇO: ${features.properties.END_LV}</p>
<p>MUNICÍPIO: ${features.properties.MUN_NOME}</p>
<p>LULA: ${features.properties.VOTOS_LULA_2022} votos</p>
<p>BOLSONARO: ${features.properties.VOTOS_BOLSONARO_2022} votos</p>
<p>ÍNDICE: ${features.properties.INDICE_2022}</p>
<div>
`;
if (graph == false) {
var value = calculateGraphValues(features.properties.VOTOS_LULA_2018_PERC,
features.properties.VOTOS_BOLSONARO_2018_PERC,
features.properties.VOTOS_LULA_2022_PERC,
features.properties.VOTOS_BOLSONARO_2022_PERC)
let {data, layout} = generateGraph(value);
Plotly.react('graph', data, layout);
graph = true
}else{
var value = calculateGraphValues(features.properties.VOTOS_LULA_2018_PERC,
features.properties.VOTOS_BOLSONARO_2018_PERC,
features.properties.VOTOS_LULA_2022_PERC,
features.properties.VOTOS_BOLSONARO_2022_PERC)
let {data, layout} = generateGraph(value)
Plotly.newPlot('graph', data, layout)
}
}else{
document.getElementById('features').innerHTML = ``;
document.getElementById('graph').innerHTML = ``;
graph = true
}
});
map.on('idle', () => {
// If these two layers were not added to the map, abort
if (!map.getLayer('lulaCircles') || !map.getLayer('bolsonaroCircles')) {
return;
}
// Enumerate ids of the layers.
const toggleableLayerIds = ['lulaCircles', 'bolsonaroCircles'];
// Set up the corresponding toggle button for each layer.
for (const id of toggleableLayerIds) {
// Skip layers that already have a button set up.
if (document.getElementById(id)) {
continue;
}
// Create a link.
const link = document.createElement('a');
link.id = id;
link.href = '#';
if (id == 'lulaCircles') {
link.textContent = 'ONDE LULA GANHOU';
}else{
link.textContent = 'ONDE BOLSONARO GANHOU'
}
const active_button = 'rounded my-2 mx-12 py-2 px-2 bg-yellow-200 text-center text-sm block hover:outline outline-2';
const inactive_button = 'rounded my-2 mx-12 py-2 px-2 bg-yellow-50 text-center text-sm block hover:outline outline-2';
link.className = active_button;
// Show or hide layer when the toggle is clicked.
link.onclick = function (e) {
const clickedLayer = this.id;
e.preventDefault();
e.stopPropagation();
const visibility = map.getLayoutProperty(
clickedLayer,
'visibility'
);
// Toggle layer visibility by changing the layout object's visibility property.
if (visibility === 'visible') {
map.setLayoutProperty(clickedLayer, 'visibility', 'none');
this.className = inactive_button;
} else {
this.className = active_button;
map.setLayoutProperty(
clickedLayer,
'visibility',
'visible'
);
}
};
const layers = document.getElementById('menu');
layers.appendChild(link);
}
});
function createPolygon() {
const data = draw.getAll();
if (data.features.length > 0) {
var lv_sum = 0;
var lula_sum = 0;
var bolsonaro_sum = 0;
var features = data['features'];
features.forEach(feature => {
var poly = turf.polygon([feature['geometry']['coordinates'][0]]);
voting.features.forEach(element => {
var point = turf.point(element.geometry.coordinates);
if (turf.inside(point, poly)) {
lv_sum += 1
lula_sum += element.properties.VOTOS_LULA_2022;
bolsonaro_sum += element.properties.VOTOS_BOLSONARO_2022;
}
});
})
var voting_sum = lula_sum + bolsonaro_sum;
var perc_lula = lula_sum / (voting_sum / 100);
var perc_bolsonaro = bolsonaro_sum / (voting_sum / 100);
var indice_sum = perc_lula - perc_bolsonaro;
document.getElementById('poligono-resumo').innerHTML = `
<div>
<p>LOCAIS DE VOTAÇÃO: ${lv_sum}</p>
<p>VOTOS LULA: ${lula_sum}</p>
<p>VOTOS BOLSONARO: ${bolsonaro_sum}</p>
<p>ÍNDICE: ${indice_sum}</p>
<div>
`
}else{
document.getElementById('poligono-resumo').innerHTML = ``
}
};
</script>
</body>
</html>