-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex.html
326 lines (268 loc) · 7.14 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous">
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="./cod.js"></script>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
#chartdiv {
width: 100%;
height: 500px;
}
</style>
<link rel="stylesheet" id="classic-theme-styles-css" href="https://www.amcharts.com/wp-includes/css/classic-themes.min.css?ver=6.2.2" type="text/css" media="all">
</head>
<body>
<script src="//cdn.amcharts.com/lib/5/index.js"></script>
<script src="//cdn.amcharts.com/lib/5/map.js"></script>
<script src="//cdn.amcharts.com/lib/5/geodata/spainProvinces2High.js"></script>
<script src="//cdn.amcharts.com/lib/5/themes/Animated.js"></script>
<div class="container">
<div class="row">
<h1>Elecciones 23J </h1>
</div>
<div class="demo-theme-dark row">
<div style="border: solid 2px black;" class="col-md-8 demo-block rounded padding demo-background margin-bottom-big" >
<div id="chartdiv"></div>
</div>
<div id="esca" class="row">
</div>
</div>
<script>
let datosJSON;
var numeroEntradas=0;
var colors;
async function fetchJSON(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error('Error:', error);
throw error;
}
}
// Luego puedes llamar a la función fetchJSON de forma síncrona
(async () => {
try {
const jsonData = await fetchJSON('./JSONs/FIN2_CIS_DATOSBRUTOS.json');
datosJSON = Object.values(jsonData);
// Aquí puedes acceder a los datos del objeto JSON
numeroEntradas = Object.keys(datosJSON).length;
cargaMapa();
//Mayor(1);
// Hacer algo con los datos JSON obtenidos
} catch (error) {
// Manejar el error de alguna manera
}
})();
function cargaMapa(){
// Colors
am5.ready(function() {
var colors = {
PP: am5.color(0x244999),
PSOE: am5.color(0xd22532),
PNV: am5.color(0x18a13d),
N: am5.color(0x999999)
}
for(var i = 0; i < datosJSON.length; i++) {
datosJSON[i].polygonSettings = {
fill: colors[datosJSON[i].win]
}
console.log(datosJSON[i].win);
}
var root = am5.Root.new("chartdiv");
// Set themes
root.setThemes([
am5themes_Animated.new(root)
]);
var chart = root.container.children.push(
am5map.MapChart.new(root, {
projection: am5map.geoMercator()
})
);
// Create polygon series
var polygonSeries = chart.series.push(
am5map.MapPolygonSeries.new(root, {
geoJSON: am5geodata_spainProvinces2High
})
);
polygonSeries.mapPolygons.template.setAll({
tooltipText: "{name}",
templateField: "polygonSettings",
interactive: true,
});
polygonSeries.mapPolygons.template.events.on("click", function (event) {
var province = event.target.dataItem.dataContext;
console.log("Province clicked:", province.name);
console.log("Province clicked:", province.id);
buscarData(province.name);
Escanos(province.name);
Cargar("./23j.html", "esca");
// Add your custom logic here for the province click event
}
);
polygonSeries.mapPolygons.template.events.off("click", function (event) {
var province = event.target.dataItem.dataContext;
switch(province.name){
case "Cantabria":
console.log("cerramos: ", province.name);
break;
}
// Add your custom logic here for the province click event
}
);
polygonSeries.mapPolygons.template.states.create("hover", {
fill: am5.color(0x677935)
});
polygonSeries.data.setAll(datosJSON);
console.log(polygonSeries.data);
// Legend
var legend = chart.children.push(am5.Legend.new(root, {
nameField: "name",
fillField: "color",
strokeField: "color",
useDefaultMarker: true,
centerX: am5.p100,
maxWidth: 100,
x: am5.p100,
centerY: am5.p100,
y: am5.p100,
dx: -20,
dy: -20,
background: am5.RoundedRectangle.new(root, {
fill: am5.color(0xffffff),
fillOpacity: 0.3
})
}));
legend.data.setAll([{
name: "PP",
color: colors.PP
}, {
name: "PSOE",
color: colors.PSOE
}, {
name: "EAJ-PNV",
color: colors.PNV
}, {
name: "Neutral",
color: colors.N
}]);
});
}
/**
* https://cdn.amcharts.com/lib/5/geodata/spainProvincesLow.js
* spainProvincesLow
* ---------------------------------------
* This demo was created using amCharts 5.
*
* For more information visit:
* https://www.amcharts.com/
*
* Documentation is available at:
* https://www.amcharts.com/docs/v5/
* ---------------------------------------
*/
// Create root and chart
function buscarData(cod){
for(let x=0;x<numeroEntradas; x++){
var datoAct=datosJSON[x];
if(datoAct.provincia==cod){
setDataPro(datoAct);
}
}
}
function Mayor(pro){
var ma=0;
var pa=null;
console.log("provincia : "+datosJSON[pro].provincia);
for (var partido in datosJSON[pro].votos){
//console.log(partido+":"+datosJSON[pro].votos[partido]);
//console.log("compara : " + datosJSON[pro].votos[partido] + " con "+ ma)
if (datosJSON[pro].votos[partido] > ma)
{
console.log(datosJSON[pro].votos[partido]+" > "+ma);
pa=partido.toString();
ma=datosJSON[pro].votos[partido];
}
}
console.log("Partido mas votado: "+pa);
console.log("==============");
return pa;
}
// end am5.ready()
</script>
<script>
function invokeScript(divid)
{
var scriptObj = divid.getElementsByTagName("SCRIPT");
var len = scriptObj.length;
for(var i=0; i<len; i++)
{
var scriptText = scriptObj[i].text;
var scriptFile = scriptObj[i].src
var scriptTag = document.createElement("SCRIPT");
if ((scriptFile != null) && (scriptFile != "")){
scriptTag.src = scriptFile;
}
scriptTag.text = scriptText;
if (!document.getElementsByTagName("HEAD")[0]) {
document.createElement("HEAD").appendChild(scriptTag)
}
else {
document.getElementsByTagName("HEAD")[0].appendChild(scriptTag);
}
}
}
function nuevaConexion()
{
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
function Cargar(url, capa)
{
var contenido = document.getElementById(capa);
var conexion = nuevaConexion();
conexion.open("GET", url,true);
conexion.onreadystatechange=function()
{
if((conexion.readyState == 4) && (conexion.status == 200))
{
contenido.innerHTML = conexion.responseText;
invokeScript(document.getElementById(capa));
}
}
conexion.send(null);
}
</script>
</body>
</html>