-
Notifications
You must be signed in to change notification settings - Fork 1
/
medalsbyage.html
334 lines (291 loc) · 10.2 KB
/
medalsbyage.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
<!--
Olivier van Holthe
5927161
Week 4 Javascript
-->
<!DOCTYPE html>
<html>
<head>
<title>Gold and silver medals by age</title>
<link rel="stylesheet" href="style.css">
<textarea id="csv">
X,Y
14,0
15,8
16,35
17,39
18,50
19,87
20,141
21,158
22,227
23,249
24,285
25,261
26,251
27,223
28,238
29,184
30,149
31,106
32,120
33,84
34,58
35,43
36,47
37,15
38,23
39,13
40,10
41,3
42,7
43,6
44,8
45,4
46,3
47,2
48,0
49,2
50,1
51,1
52,1
53,0
54,1
55,0
56,0
57,0
58,0
59,0
</textarea>
<textarea id="json">
{
"values": [
{ "X": 14, "Y": 0 },
{ "X": 15, "Y": 7 },
{ "X": 16, "Y": 29 },
{ "X": 17, "Y": 31},
{ "X": 18, "Y": 56},
{ "X": 19, "Y": 82},
{ "X": 20, "Y": 109},
{ "X": 21, "Y": 167},
{ "X": 22, "Y": 246},
{ "X": 23, "Y": 242},
{ "X": 24, "Y": 256},
{ "X": 25, "Y": 249},
{ "X": 26, "Y": 253},
{ "X": 27, "Y": 255},
{ "X": 28, "Y": 227},
{ "X": 29, "Y": 186},
{ "X": 30, "Y": 155},
{ "X": 31, "Y": 127},
{ "X": 32, "Y": 121},
{ "X": 33, "Y": 70},
{ "X": 34, "Y": 65},
{ "X": 35, "Y": 51},
{ "X": 36, "Y": 40},
{ "X": 37, "Y": 19},
{ "X": 38, "Y": 15},
{ "X": 39, "Y": 14},
{ "X": 40, "Y": 11},
{ "X": 41, "Y": 8},
{ "X": 42, "Y": 7},
{ "X": 43, "Y": 8},
{ "X": 44, "Y": 2},
{ "X": 45, "Y": 4},
{ "X": 46, "Y": 8},
{ "X": 47, "Y": 1},
{ "X": 48, "Y": 0},
{ "X": 49, "Y": 0},
{ "X": 50, "Y": 0},
{ "X": 51, "Y": 2},
{ "X": 52, "Y": 1},
{ "X": 53, "Y": 0},
{ "X": 54, "Y": 0},
{ "X": 55, "Y": 1},
{ "X": 56, "Y": 0},
{ "X": 57, "Y": 0},
{ "X": 58, "Y": 0},
{ "X": 59, "Y": 0}
]
}
</textarea>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>
</head>
<body>
<div id="tooltip">
</div>
<div>This graph shows the number of gold (red line) and silver (green line) olympic medals won per age of the athletes. </div></br>
<canvas id="graph" width="800" height="400">
</canvas>
<canvas id="goldcurve" width="700" height="370"></canvas>
<canvas id="silvercurve" width="700" height="370"></canvas>
<canvas id="crosshair" width="700" height="370"></canvas>
<input type = "checkbox" name = "showgold" onclick="show('goldcurve', 'showgold')" value = "gold medals">Gold medals</br>
<input type = "checkbox" name = "showsilver" onclick="show('silvercurve', 'showsilver')" value = "silver medals">Silver medals</br>
<input type = "checkbox" name = "crosshair" onclick="show('crosshair', 'crosshair')" value = "crosshair">Crosshair
<div id="showvalues"></div>
<script>
var graph;
var xPadding = 100;
var yPadding = 30;
var csvdata = document.getElementById('csv').value;
var jsondata = document.getElementById("json").value;
// convert the csv data into usable object
var csv = csvdata.split('\n');
var result = [];
var headers=csv[0].split(",");
for(var i=1;i<csv.length;i++){
var obj = {};
var currentline=csv[i].split(",");
for(var j=0;j<headers.length;j++){
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
var gold = {}
gold["values"] = result
// convert the json data into usable object
var silver = JSON.parse(jsondata);
console.log(silver)
// Returns the max Y value in the list
function getMaxY() {
var max = 0;
for(var i = 0; i < gold.values.length; i ++) {
if(Number(gold.values[i].Y) > max) {
max = Number(gold.values[i].Y);
}
}
max += 10 - max % 10;
return max;
}
// Return the x coordinate for a graph point
function getXPixel(val) {
return ((graph.width() - xPadding) / gold.values.length) * val + (xPadding * 1.5);
}
// Return the y coordinate for a graph point
function getYPixel(val) {
return graph.height() - (((graph.height() - yPadding) / getMaxY()) * val) - yPadding;
}
// Return values at a specific coordinate
function getXvalue(coor) {
return (coor - (xPadding * 1.5)) / ((graph.width() - xPadding) / gold.values.length);
}
// Return values at a specific coordinate
function getYvalue(coor) {
return (- coor - yPadding + graph.height()) / ((graph.height() - yPadding) / 290);
}
// Show element when checkbox is checked
function show (canvas, name) {
var chboxs = document.getElementsByName(name);
var vis = "none";
for(var i=0;i<chboxs.length;i++) {
if(chboxs[i].checked){
vis = "block";
break;
}
}
document.getElementById(canvas).style.display = vis;
}
function showhidden () {
document.getElementById("tooltip").style.visibility = "visible";
}
graph = $('#graph');
var c = graph[0].getContext('2d');
c.lineWidth = 2;
c.strokeStyle = '#333';
c.font = 'italic 8pt sans-serif';
c.textAlign = "center";
// Draw the axises
c.beginPath();
c.moveTo(xPadding, 0);
c.lineTo(xPadding, graph.height() - yPadding);
c.lineTo(graph.width(), graph.height() - yPadding);
c.stroke();
c.fillText("Age", (graph.width())/2, 390)
// Draw the X value texts
for(var i = 0; i < silver.values.length; i += 2) {
c.fillText(silver.values[i].X, getXPixel(i), graph.height() - 20);
}
// Draw the Y value texts
c.textAlign = "right"
c.textBaseline = "middle";
for(var i = 0; i < getMaxY(); i += 10) {
c.fillText(i, xPadding - 10, getYPixel(i));
}
c.fillText("Medals", 50, 200)
// Draw the line graph for gold medals
var goldcan = document.getElementById("goldcurve");
var gctx = goldcan.getContext("2d");
gctx.strokeStyle = '#f00';
gctx.beginPath();
gctx.moveTo(getXPixel(0), getYPixel(Number(gold.values[0].Y)));
for(var i = 1; i < gold.values.length; i ++) {
gctx.lineTo(getXPixel(i), getYPixel(Number(gold.values[i].Y)));
}
gctx.stroke();
// Draw black filled circles to show datapoints
gctx.fillStyle = '#333';
for(var i = 0; i < gold.values.length; i ++) {
gctx.beginPath();
gctx.arc(getXPixel(i), getYPixel(gold.values[i].Y), 2, 0, Math.PI * 2, true);
gctx.fill();
}
// Draw the line graph for silver medals
var silvercan = document.getElementById("silvercurve");
var sctx = silvercan.getContext("2d");
sctx.strokeStyle = '#0f0';
sctx.beginPath();
sctx.moveTo(getXPixel(0), getYPixel(silver.values[0].Y));
for(var i = 1; i < silver.values.length; i ++) {
sctx.lineTo(getXPixel(i), getYPixel(silver.values[i].Y));
}
sctx.stroke();
// Draw black filled circles to show datapoints
sctx.fillStyle = '#333';
for(var i = 0; i < silver.values.length; i ++) {
sctx.beginPath();
sctx.arc(getXPixel(i), getYPixel(silver.values[i].Y), 2, 0, Math.PI * 2, true);
sctx.fill();
}
// Draw the crosshair
var canvas = document.getElementById("crosshair");
var context = canvas.getContext("2d");
canvas.addEventListener("mousemove", function(event){
var x = event.pageX - canvas.offsetLeft;
var y = event.pageY - canvas.offsetTop;
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
context.moveTo(0,y);
context.lineTo(700,y);
context.moveTo(x,0);
context.lineTo(x,370);
context.strokeStyle = "black";
context.stroke();
context.closePath();
});
// Make the tooltip (only visible when mouse does not move)
var display;
var div = document.getElementById('tooltip');
var timer = setTimeout("showhidden()", 2000);
document.addEventListener('mousemove',function(e) {
div.style.left = e.pageX+10+"px";
div.style.top = e.pageY+10+"px";
display = "Age = " + Math.round((getXvalue(e.pageX-50)+10)) + ", Medals = " + Math.round((getYvalue(e.pageY - 45)));
div.innerHTML = display
// div element displaying values
if (getYvalue(e.pageY - 45) > 0 && getXvalue(e.pageX-50)+10 > 10.7
&& getYvalue(e.pageY - 45) < 285 && getXvalue(e.pageX-50)+10 < 56.4) {
document.getElementById("showvalues").innerHTML = display;
}
// Contents of tooltip
document.getElementById("tooltip").style.visibility = "hidden";
clearTimeout(timer);
timer = setTimeout("showhidden()", 2000);
if (getYvalue(e.pageY - 45) < 0 || getXvalue(e.pageX-50)+10 < 10.7
|| getYvalue(e.pageY - 45) > 285 || getXvalue(e.pageX-50)+10 > 56.4) {
clearTimeout(timer);
}
});
</script>
</body>
</html>