-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess.js
502 lines (491 loc) · 15.7 KB
/
process.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
//alert('data');
//get JSON data
const util = {
format: function name(num) {
return num.toLocaleString("en-In");
},
stripArray: function name(arr, num) {
return arr.filter(function(v, i) {
return i < num;
});
},
SortArrayOfObject: function(AoO, key) {
return AoO.sort(function compare(b, a) {
const genreA = a[key];
const genreB = b[key];
let comparison = 0;
if (genreA > genreB) {
comparison = 1;
} else if (genreA < genreB) {
comparison = -1;
}
return comparison;
});
},
JsonDataToTable: function(data) {
var headers = Object.keys(data[0]);
return `<table class="table"><thead>
<tr>
${headers
.map(function(key) {
return `<th>${key}</th>`;
})
.join("")}
</tr>
</thead>
<tbody>${data
.map(function(rowData) {
return `<tr>${headers
.map(function(key) {
return `<td>${(function(params) {
if (typeof rowData[key] === "number") {
return util.format(rowData[key]);
}
return rowData[key];
})()}</td>`;
})
.join("")}</tr>`;
})
.join("")}
</tbody>
</table>`;
},
getJSONDataFromUrl: function(url, callback) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
callback(JSON.parse(xhttp.responseText));
}
};
xhttp.open("GET", url, true);
xhttp.send();
},
findMaxInArrayOofObj: function(arr, key) {
var maxCount = 0;
var maxIndex = -1;
arr.forEach(function(v, i) {
if (v[key] > maxCount) {
maxCount = v[key];
maxIndex = i;
}
});
return maxIndex;
}
};
var metaData = {
partyWiseData: {}
};
function processData(metaData) {
//This function calculate who won in a particular constituency
metaData.totalVotesCasted = 0;
var allParties = {};
metaData.constituencyData.map(function(cData) {
cData.allCandidateData.map(function(candidateData) {
candidateData["Total Votes"] = parseInt(candidateData["Total Votes"], 10);
allParties[candidateData.Party] = true;
metaData.totalVotesCasted += candidateData["Total Votes"];
if (metaData.partyWiseData[candidateData["Party"]] === undefined) {
metaData.partyWiseData[candidateData["Party"]] = {
votes: 0,
TotalCandidates: 0,
"Winner Count": 0
};
}
metaData.partyWiseData[candidateData["Party"]].votes +=
candidateData["Total Votes"];
metaData.partyWiseData[candidateData["Party"]]["TotalCandidates"] += 1;
});
cData["Total Casted Votes"] = cData.allCandidateData
.map(function(candidateData) {
return candidateData["Total Votes"];
})
.reduce(function(a, b) {
return a + b;
}, 0);
var winnerIndex = util.findMaxInArrayOofObj(
cData.allCandidateData,
"Total Votes"
);
cData.Winner = cData.allCandidateData[winnerIndex];
cData.Margin = cData.allCandidateData
.map(function(cand) {
return cData.Winner["Total Votes"] - cand["Total Votes"];
})
.sort()[1];
metaData.partyWiseData[cData.Winner.Party]["Winner Count"] += 1;
});
allParties = Object.keys(allParties).sort();
metaData.allParties = allParties;
}
function runDataProcessPipeLine(callback) {
util.getJSONDataFromUrl("./Election2019Results.json", function(
constituencyData
) {
metaData.constituencyData = constituencyData;
processData(metaData);
//AllDataProcessing is over, now, we can create sections.
//loader
$("#loader").remove();
//generateChart(metaData);
generateAllSections(metaData, function() {
//now generating charts
generateAllChart();
});
console.log("metaData", metaData);
});
}
function generateAllChart() {
$(".chartPlugin").map(function(index, el) {
//console.log("el", el);
$(el).html(
generateChartByCdata(
JSON.parse(
$(el)
.attr("data-json")
.replace(/ø/g, "'")
)
)
);
});
}
function generateChartByCdata(cData) {
var svgIconStr = `
<div style="border: 1px solid gray;width: 50px;height: 27px;background: lightgray;">
<svg width="700" height="220" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="chart" aria-labelledby="title" role="img">
<g>
${util
.stripArray(
util.SortArrayOfObject(cData.allCandidateData, "Total Votes"),
5
)
.map(function(candidateData, i) {
var p =
(candidateData["Total Votes"] / cData["Total Casted Votes"]) *
100;
return `<g class="bar">
<rect height="${p *
0.3}" x="${5 + 8 * i}" y="${22 - p * 0.3}" width="5"></rect> </g>`;
})
.join("")}
</g>
</svg>
</div>`;
var svgStr = `
<div class="box">
<div>
<div>Constituency : <b>${cData.name}</b></div>
<div>Winner : <b>${cData.Winner.Candidate} (${
cData.Winner.Party
})</b></div>
<div>Margin : <b>${util.format(cData.Margin)}</b> votes</div>
<div>Total Casted Votes : <b>${util.format(
cData["Total Casted Votes"]
)}</b></div>
</div>
<div class="svgchart">
<svg width="700" height="220" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="chart" aria-labelledby="title" role="img">
<g>
${util
.stripArray(
util.SortArrayOfObject(cData.allCandidateData, "Total Votes"),
5
)
.map(function(candidateData, i) {
var p =
(candidateData["Total Votes"] / cData["Total Casted Votes"]) *
100;
return `<g class="bar">
<rect height="${p *
2}" x="${150 * i}" y="${140 - p * 2}" width="100"></rect>
<g>
<text style="font-size: 14px;fill: black;" y="${160}" x="${150 * i}">${util.format(p)} %</text>
<text style="font-size: 14px;fill: black;" y="${180}" x="${150 * i}">${util.format(candidateData["Total Votes"])}</text>
<text style="font-size: 10px;fill: black;" y="${200}" x="${150 * i}">${candidateData.Candidate}</text>
<text style="font-size: 10px;fill: black;" y="${215}" x="${150 * i}">${candidateData.Party}</text>
</g>
</g>`;
})
.join("")}
</g>
</svg>
</div>
</div>`;
return `<div class="thumbChartInTable">
<span>${svgIconStr}</span>
<div class="popOverBox">
${svgStr}
</div>
</div>`;
}
function generateChart(metaData) {
var cData = metaData.constituencyData[0];
console.log("c1Data", cData);
var svgIconStr = `
<div style="border: 1px solid gray;width: 50px;height: 27px;background: lightgray;">
<svg width="700" height="220" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="chart" aria-labelledby="title" role="img">
<g>
${util
.stripArray(
util.SortArrayOfObject(cData.allCandidateData, "Total Votes"),
5
)
.map(function(candidateData, i) {
var p =
(candidateData["Total Votes"] / cData["Total Casted Votes"]) *
100;
return `<g class="bar">
<rect height="${p *
0.3}" x="${5 + 8 * i}" y="${20 - p * 0.3}" width="5"></rect> </g>`;
})
.join("")}
</g>
</svg>
</div>`;
$("#sections").append(`
<div class="section">
<h1>Test</h1>
<div class="ChartData">
${svgIconStr}
</div>
</div>`);
}
runDataProcessPipeLine();
const allSections = [
function TotalVotesPartyWise(metaData) {
var sectionTitle = "Total Votes";
var sectionData = [
{
"Total Votes Casted": metaData.totalVotesCasted
}
];
return {
sectionTitle: sectionTitle,
sectionData: sectionData
};
},
function TotalVotesPartyWise(metaData) {
var sectionTitle = "Top 30 Parties with their Votes";
var sectionData = util.stripArray(
util.SortArrayOfObject(
Object.keys(metaData.partyWiseData).map(function(v) {
var tcw = metaData.partyWiseData[v]["Winner Count"];
if (tcw === undefined) {
tcw = 0;
}
return {
Party: v,
Votes: metaData.partyWiseData[v].votes,
"Total Candidate by Party":
metaData.partyWiseData[v]["TotalCandidates"],
"Total Candidate Won": tcw,
Percentage:
(tcw / metaData.partyWiseData[v]["TotalCandidates"]) * 100
};
}),
"Votes"
),
30
);
return {
sectionTitle: sectionTitle,
sectionData: sectionData
};
},
function TotalVotesPartyWise(metaData) {
var sectionTitle = "Parties wise Data";
var sectionData = util.SortArrayOfObject(
Object.keys(metaData.partyWiseData)
.map(function(v) {
return {
Party: v,
"Total Candidate Won": metaData.partyWiseData[v]["Winner Count"]
};
})
.filter(function(v) {
return v["Total Candidate Won"] > 0;
}),
"Total Candidate Won"
);
return {
sectionTitle: sectionTitle,
sectionData: sectionData
};
},
function AllParties(metaData) {
var sectionTitle = "All Parties";
var sectionData = [
{
"Total Parties Count": metaData.allParties.length - 2,
Parties: metaData.allParties
.map(function(v, i) {
return i + 1 + ". " + v;
})
.join(", ")
}
];
return {
sectionTitle: sectionTitle,
sectionData: sectionData
};
},
function Top10WithHighestMargin(metaData) {
var sectionTitle = "Top 10 Candidate who won with High margin";
var listOfWinners = [];
metaData.constituencyData.map(function(cData) {
listOfWinners.push({
"Winner Candidate": cData.Winner.Candidate,
"Winner Party": cData.Winner.Party,
State: cData.stateName,
Constituency: cData.name,
Margin: cData.Margin
});
});
var sectionData = util.stripArray(
util.SortArrayOfObject(listOfWinners, "Margin"),
10
);
return {
sectionTitle: sectionTitle,
sectionData: sectionData
};
},
function Top10WithLeastMargin(metaData) {
var sectionTitle = "Top 10 Candidate who won with Least margin";
var listOfWinners = [];
metaData.constituencyData.map(function(cData) {
listOfWinners.push({
"Winner Candidate": cData.Winner.Candidate,
"Winner Party": cData.Winner.Party,
State: cData.stateName,
Constituency: cData.name,
Margin: cData.Margin
});
});
var sectionData = util.stripArray(
util.SortArrayOfObject(listOfWinners, "Margin").reverse(),
10
);
return {
sectionTitle: sectionTitle,
sectionData: sectionData
};
},
function Top10CandidateWithHighestVotes(metaData) {
var sectionTitle = "These 10 candidate got maximum votes in";
var fullListOfAllCandidate = [];
metaData.constituencyData.map(function(cData) {
cData.allCandidateData.map(function(candidateData) {
fullListOfAllCandidate.push({
Candidate: candidateData.Candidate,
Party: candidateData.Party,
State: cData.stateName,
Constituency: cData.name,
"Total Votes": candidateData["Total Votes"],
Result: ["Lose", "Won"][
+(cData.Winner.Candidate === candidateData.Candidate)
]
});
});
});
var sectionData = util.stripArray(
util.SortArrayOfObject(fullListOfAllCandidate, "Total Votes"),
10
);
return {
sectionTitle: sectionTitle,
sectionData: sectionData
};
},
function Top10CandidateWithLowestVotes(metaData) {
var sectionTitle = "These 10 candidate got least votes";
var fullListOfAllCandidate = [];
metaData.constituencyData.map(function(cData) {
cData.allCandidateData.map(function(candidateData) {
fullListOfAllCandidate.push({
Candidate: candidateData.Candidate,
Party: candidateData.Party,
State: cData.stateName,
Constituency: cData.name,
"Total Votes": candidateData["Total Votes"],
Result: ["Lose", "Won"][
+(cData.Winner.Candidate === candidateData.Candidate)
]
});
});
});
var sectionData = util.stripArray(
util.SortArrayOfObject(fullListOfAllCandidate, "Total Votes").reverse(),
10
);
return {
sectionTitle: sectionTitle,
sectionData: sectionData
};
},
function ListAllWinners(metaData) {
//This Section list out all winners.
var sectionTitle = "List of All Constituency and Winner Party";
var sectionData = [];
var totalVotersOfWinner = 0;
var totalCastedVotes = 0;
metaData.constituencyData.map(function(cData, i) {
var winnerIndex = util.findMaxInArrayOofObj(
cData.allCandidateData,
"Total Votes"
);
cData.Winner = cData.allCandidateData[winnerIndex];
totalVotersOfWinner += cData.Winner["Total Votes"];
totalCastedVotes += cData["Total Casted Votes"];
var cDataStr = JSON.stringify(cData).replace(/'/g, "ø");
sectionData.push({
Index: i + 1,
State: cData.stateName,
Constituency: cData.name,
Chart: `<div class='chartPlugin' data-json='${cDataStr}'></div>`,
"Total Candidates": cData.allCandidateData.length - 1, //Removed NOTA
"Winner Candidate": cData.Winner.Candidate,
"Winner Party": cData.Winner.Party,
"Total Casted Votes": cData["Total Casted Votes"],
Margin: cData.Margin,
"Winner Votes": cData.Winner["Total Votes"],
"Voter Percentage of Winner":
(cData.Winner["Total Votes"] / cData["Total Casted Votes"]) * 100,
"Margin Percentage": (cData.Margin / cData["Total Casted Votes"]) * 100
});
});
sectionData.push({
Index: "Total",
State: "-",
Constituency: "-",
Chart: "-",
"Total Candidates": "-",
"Winner Candidate": "-",
"Winner Party": "-",
"Margin Percentage": "-",
"Voter Percentage of Winner": "-",
"Winner Votes": totalVotersOfWinner,
Margin: "-",
"Total Casted Votes": totalCastedVotes
});
return {
sectionTitle: sectionTitle,
sectionData: sectionData
};
}
];
function generateAllSections(metaData, callback) {
allSections.forEach(function(sectionDataGenfun) {
var sData = sectionDataGenfun(metaData);
$("#sections").append(`
<div class="section">
<h1>${sData.sectionTitle}</h1>
<div class="tableData">
${util.JsonDataToTable(sData.sectionData)}
</div>
</div>`);
if (typeof callback === "function") {
callback();
}
});
}