-
Notifications
You must be signed in to change notification settings - Fork 0
/
charts.js
341 lines (269 loc) · 7.99 KB
/
charts.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
var data_caract_h_f;
var data_sport_h_f;
var slider = document.getElementById('slider');
var limites_dates = [1890,2020]
noUiSlider.create(slider, {
start: limites_dates,
// Display colored bars between handles
connect: true,
// Move handle on tap, bars are draggable
behaviour: 'tap-drag',
tooltips: true,
format: {
from: function(value) {
return parseInt(value);
},
to: function(value) {
return parseInt(value);
}
},
range: {
'min': limites_dates[0],
'max': limites_dates[1]
}
});
function init_chart(url, chart_function) {
Papa.parse(url, {
header: true,
download: true,
dynamicTyping: true,
complete: function (results) {
chart_function(results.data);
}
});
}
function load_chart_caract_hf(data) {
data_caract_h_f = data;
let competences_names = ["Endurance", "Strength", "Power", "Speed", "Agility", "Flexibility", "Nerve", "Durability", "Hand-Eye Coordination", "Analytical Aptitude"];
let averages_women = { "Endurance": 0, "Strength": 0, "Power": 0, "Speed": 0, "Agility": 0, "Flexibility": 0, "Nerve": 0, "Durability": 0, "Hand-Eye Coordination": 0, "Analytical Aptitude": 0, "Nombre d'enregistrements": 0 };
let averages_men = { "Endurance": 0, "Strength": 0, "Power": 0, "Speed": 0, "Agility": 0, "Flexibility": 0, "Nerve": 0, "Durability": 0, "Hand-Eye Coordination": 0, "Analytical Aptitude": 0, "Nombre d'enregistrements": 0 };
let dates = [parseInt(slider.noUiSlider.get()[0]), parseInt(slider.noUiSlider.get()[1])];
for (let index = 0; index < data.length; index++) {
const element = data[index];
if(element["Year (classe)"] > dates[1] || element["Year (classe)"] < dates[0]){
console.log("reset");
continue;
}
if (element.Sex === "F") {
averages_women[element["Noms de mesures"]] += element["Valeurs de mesures"];
} else {
averages_men[element["Noms de mesures"]] += element["Valeurs de mesures"];
}
}
let nb_women = averages_women["Nombre d'enregistrements"];
let nb_men = averages_men["Nombre d'enregistrements"];
competences_names = competences_names.filter(item => item !== "Nombre d'enregistrements")
let averages_women_array = [];
let averages_men_array = [];
competences_names.forEach(element => {
averages_women[element] /= nb_women;
averages_men[element] /= nb_men;
averages_women_array.push(averages_women[element]);
averages_men_array.push(averages_men[element]);
});
var trace1 = {
x: competences_names,
y: averages_women_array,
name: 'Women',
type: 'bar',
marker: {
color: ' #B07AA1'
}
};
var trace2 = {
x: competences_names,
y: averages_men_array,
name: 'Men',
type: 'bar',
marker: {
color: ' #4E79A7'
}
};
var data_to_print = [trace1, trace2];
var layout = {
title: 'Comparaison des caractéristiques des sports pratiqués entre les hommes et les femmes aux JO',
barmode: 'group',
yaxis: {
title: {
text: 'Note sur 10'
}
},
xaxis: {
title: {
text: 'Compétence évaluée'
}
}
};
Plotly.newPlot('graphe_hf_caract', data_to_print, layout);
}
function load_chart_sport_hf(data) {
data_sport_h_f = data;
let sports_names = [];
let nb_women = {};
let nb_men = {};
let dates = [parseInt(slider.noUiSlider.get()[0]), parseInt(slider.noUiSlider.get()[1])];
let max_nb_participations = 0;
for (let index = 0; index < data.length; index++) {
const element = data[index];
if(element["Year (classe)"] > dates[1] || element["Year (classe)"] < dates[0]){
continue;
}
if (element.Sex === "F") {
if (element.Sport in nb_women) {
nb_women[element.Sport] += element.Nb;
}
else {
nb_women[element.Sport] = element.Nb;
if (sports_names.indexOf(element.Sport) == -1) {
sports_names.push(element.Sport);
}
}
} else {
if (element.Sport in nb_men) {
nb_men[element.Sport] += element.Nb;
}
else {
nb_men[element.Sport] = element.Nb;
if (sports_names.indexOf(element.Sport) == -1) {
sports_names.push(element.Sport);
}
}
}
}
let nb_women_array = [];
let nb_women_array_reverse = [];
let nb_men_array = [];
sports_names.forEach(element => {
nb_women_array.push(nb_women[element] || 0);
nb_women_array_reverse.push(-nb_women[element] || 0);
nb_men_array.push(nb_men[element] || 0);
if(nb_women[element] > max_nb_participations){
max_nb_participations = nb_women[element];
}
if(nb_men[element] > max_nb_participations){
max_nb_participations = nb_men[element];
}
});
trace1 = {
name: 'Men',
type: 'bar',
x: nb_men_array,
y: sports_names,
marker: { color: '#4E79A7' },
hoverinfo: 'x',
orientation: 'h'
};
trace2 = {
name: 'Women',
type: 'bar',
x: nb_women_array_reverse,
y: sports_names,
marker: { color: '#B07AA1' },
text: nb_women_array,
hoverinfo: 'text',
orientation: 'h'
};
let data_to_print = [trace1, trace2];
let layout = {
xaxis: {
type: 'linear',
title: { text: 'Nombre de personnes' },
},
yaxis: {
title: { text: 'Sports' },
},
bargap: 0.1,
barmode: 'relative',
autosize: true,
height: 1500,
margin: {
l: 200,
r: 50,
b: 100,
t: 100,
pad: 4
},
title: 'Comparaison du nombre de participations des femmes et des hommes aux différents sports'
};
Plotly.newPlot('graphe_hf_sport', data_to_print, layout);
}
function load_chart_caract_age(data) {
let data_to_print = [];
let ages = [];
let caracteristics_by_age = {};
for (let index = 0; index < data.length; index++) {
const element = data[index];
ages.push(element.Age);
Object.keys(element).forEach(key => {
if (key in caracteristics_by_age) {
caracteristics_by_age[key].push(element[key]);
}
else {
caracteristics_by_age[key] = [element[key]];
}
});
}
delete caracteristics_by_age["Age"];
Object.keys(caracteristics_by_age).forEach(key => {
data_to_print.push({
x: ages,
y: caracteristics_by_age[key],
mode: 'lines',
name: key
});
});
var layout = {
title: 'Comparaison des caractéristiques des sports pratiqués selon l\'âge des athlètes aux JO',
yaxis: {
title: {
text: 'Note sur 10'
}
},
xaxis: {
title: {
text: 'Âge de l\'athlète'
}
}
};
Plotly.newPlot('graphe_age_caract', data_to_print, layout);
}
function load_map() {
Plotly.d3.csv('https://julienc85.github.io/dataviz/data/sport_map.csv', function (err, rows) {
function unpack(rows, key) {
return rows.map(function (row) { return row[key]; });
}
let data = [{
type: 'scattergeo',
locationmode: 'ISO-3',
locations: unpack(rows, 'NOC'),
z: unpack(rows, 'Sport Category'),
text: unpack(rows, 'Team'),
hoverinfor: unpack(rows, 'Sport Category'),
text: unpack(rows, 'Sport Category'),
marker: {
size: 12,
color: unpack(rows, 'Id_Sport')
},
}];
let layout = {
title: 'Carte des sports les plus représentés par pays',
geo: {
projection: {
type: 'Equirectangular'
},
showcountries: true
},
};
Plotly.plot("map", data, layout, { showLink: false });
});
}
function updateCharts(values, handle, unencoded, tap, positions) {
load_chart_caract_hf(data_caract_h_f);
load_chart_sport_hf(data_sport_h_f);
}
// Binding signature
slider.noUiSlider.on('change', updateCharts);
init_chart('https://julienc85.github.io/dataviz/data/data_comparaison_caract_h_f.csv', load_chart_caract_hf);
init_chart('https://julienc85.github.io/dataviz/data/data_comparaison_sports_h_f.csv', load_chart_sport_hf);
init_chart('https://julienc85.github.io/dataviz/data/data_comparaison_caract_age.csv', load_chart_caract_age);
load_map();