-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelection_stats_2015.html
More file actions
235 lines (221 loc) · 7.25 KB
/
election_stats_2015.html
File metadata and controls
235 lines (221 loc) · 7.25 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>תוצאות בחירות 2015</title>
<style>
.rotate {
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
-ms-transform: rotate(-90deg);
-o-transform: rotate(-90deg);
filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
}
#partiesList {
list-style-type: none;
width: 10em;
}
#partiesList li {
border: 1px solid black;
padding: 0.5em;
margin: 0.25em;
border-radius: 0.25em;
background-color: #eee;
cursor: move;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://rubaxa.github.io/Sortable/Sortable.js"></script>
<script>
// With more than 10000 votes in the 2015 elections
var partiesVoteCount = {
"הליכוד": 985408,
"המחנה הציוני": 786313,
"הרשימה המשותפת": 446583,
"יש עתיד": 371602,
"כולנו": 315360,
"הבית היהודי": 283910,
"ש\"ס": 241613,
"ישראל ביתנו": 214906,
"יהדות התורה": 210143,
"מרצ": 165529,
"יחד": 125158,
"עלה ירוק": 47180
};
function drawAxis(ctx, width, height)
{
ctx.beginPath();
ctx.moveTo(0, height - 20);
ctx.lineTo(width, height - 20);
for (let i = -10; i <= 10; i++) {
if ((Math.abs(i) <= 3) || ((Math.abs(i) % 2) == 1)) {
ctx.moveTo(width/2 + i*80, height - 20);
ctx.lineTo(width/2 + i*80, height - 15);
}
}
ctx.strokeStyle = 'black';
ctx.lineWidth = 0.5;
ctx.stroke();
ctx.fillStyle = 'black';
ctx.textAlign = 'center';
ctx.font = '10px sans';
for (let i = -10; i <= 10; i++) {
if ((Math.abs(i) <= 3) || ((Math.abs(i) % 2) == 1)) {
ctx.fillText(i, width/2 + i*80, height - 5);
}
}
}
function drawCurve(ctx, width, height)
{
ctx.beginPath();
ctx.moveTo(0, height - 20);
for (let i = 1; i < width; i++) {
let x = (i - width / 2) / 80.0;
let y = Math.exp(-x*x / 2) / Math.sqrt(2 * Math.PI);
j = height - 20 - y * (height - 50) * 2.5;
ctx.lineTo(i, j);
}
ctx.strokeStyle = 'blue';
ctx.lineWidth = 2;
ctx.stroke();
}
/* Credit: DeepSpace101's post on Stack Overflow,
http://stackoverflow.com/questions/8816729/javascript-equivalent-for-inverse-normal-function-eg-excels-normsinv-or-nor
*/
function NormSInv(p) {
var a1 = -39.6968302866538, a2 = 220.946098424521, a3 = -275.928510446969;
var a4 = 138.357751867269, a5 = -30.6647980661472, a6 = 2.50662827745924;
var b1 = -54.4760987982241, b2 = 161.585836858041, b3 = -155.698979859887;
var b4 = 66.8013118877197, b5 = -13.2806815528857, c1 = -7.78489400243029E-03;
var c2 = -0.322396458041136, c3 = -2.40075827716184, c4 = -2.54973253934373;
var c5 = 4.37466414146497, c6 = 2.93816398269878, d1 = 7.78469570904146E-03;
var d2 = 0.32246712907004, d3 = 2.445134137143, d4 = 3.75440866190742;
var p_low = 0.02425, p_high = 1 - p_low;
var q, r;
var retVal;
if (p <= 0) {
retVal = -50.0;
}
else if (p >= 1)
{
retVal = 50.0;
}
else if (p < p_low)
{
q = Math.sqrt(-2 * Math.log(p));
retVal = (((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6) / ((((d1 * q + d2) * q + d3) * q + d4) * q + 1);
}
else if (p <= p_high)
{
q = p - 0.5;
r = q * q;
retVal = (((((a1 * r + a2) * r + a3) * r + a4) * r + a5) * r + a6) * q / (((((b1 * r + b2) * r + b3) * r + b4) * r + b5) * r + 1);
}
else
{
q = Math.sqrt(-2 * Math.log(1 - p));
retVal = -(((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6) / ((((d1 * q + d2) * q + d3) * q + d4) * q + 1);
}
return retVal;
}
function drawAreas(ctx, width, height, parties)
{
var totalVotes = 0.0;
for (let i = 0; i < parties.length; i++) {
totalVotes += partiesVoteCount[parties[i]];
}
var start = 0.0, end = 0.0;
var color = '#bbf';
for (let i = 0; i < parties.length; i++) {
let partyName = parties[i];
// figure out start and end
start = end;
end += partiesVoteCount[partyName] / totalVotes;
startX = NormSInv(start);
endX = NormSInv(end);
// fill background color
ctx.beginPath();
ctx.moveTo(width/2 + startX * 80, height - 20);
for (let x = startX; x < endX; x += 0.01) {
let y = Math.exp(-x*x / 2) / Math.sqrt(2 * Math.PI);
ctx.lineTo(width/2 + x * 80, height - 20 - y * (height - 50) * 2.5);
}
ctx.lineTo(width/2 + endX * 80, height - 20);
ctx.fillStyle = color;
ctx.lineWidth = 0;
ctx.fill();
color = (color === '#bbf') ? '#ddf' : '#bbf';
// draw separating line
ctx.beginPath();
ctx.moveTo(width/2 + startX * 80, height - 20);
let startY = Math.exp(-startX*startX / 2) / Math.sqrt(2 * Math.PI);
ctx.lineTo(width/2 + startX * 80, height - 20 - startY * (height - 50) * 2.5);
ctx.strokeStyle = 'blue';
ctx.lineWidth = 1;
ctx.stroke();
// name of the party
ctx.fillStyle = 'black';
ctx.font = '14px sans';
let left = Math.max(startX, Math.min(-2, endX - 1));
let right = Math.min(endX, Math.max(2, startX + 1));
let textX = (left + right) / 2;
let textY = Math.exp(-textX*textX / 2) / Math.sqrt(2 * Math.PI);
if (endX < 0) {
ctx.textAlign = 'right';
ctx.fillText(partyName, width/2 + textX * 80, height - 30 - textY * (height - 50) * 2.5);
} else if (startX > 0) {
ctx.textAlign = 'left';
ctx.fillText(partyName, width/2 + textX * 80, height - 30 - textY * (height - 50) * 2.5);
} else {
ctx.textAlign = 'center';
ctx.fillText(partyName, width/2 + textX * 80, 10);
}
}
}
function updateDisplay()
{
$('#canvas').attr('width', $(window).width() - 30);
$('#canvas').attr('height', Math.min($(window).height() * 0.75 - 30, Math.min($(window).width()*2/3 - 20, 500)));
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
var parties = [];
$('#partiesList li').each(function(elt) {parties.push($(this).text());});
drawAreas(ctx, canvas.width, canvas.height, parties);
drawAxis(ctx, canvas.width, canvas.height);
drawCurve(ctx, canvas.width, canvas.height);
}
$(function() {
$(window).resize(function() {
updateDisplay();
});
updateDisplay();
Sortable.create(document.getElementById('partiesList'),
{onUpdate: updateDisplay});
})
</script>
</head>
<body>
<h1 style="text-align: center">בחירות 2015</h1>
<div style="text-align: center">
<canvas id="canvas"></canvas>
<div>סטיות תקן</div>
</div>
<div style="direction: rtl">
<ul id="partiesList">
<li>הרשימה המשותפת</li>
<li>עלה ירוק</li>
<li>מרצ</li>
<li>המחנה הציוני</li>
<li>יש עתיד</li>
<li>כולנו</li>
<li>יהדות התורה</li>
<li>הליכוד</li>
<li>ש"ס</li>
<li>ישראל ביתנו</li>
<li>הבית היהודי</li>
<li>יחד</li>
</ul>
</div>
</body>
</html>