-
Notifications
You must be signed in to change notification settings - Fork 0
/
olympic_events.js
5645 lines (4278 loc) · 223 KB
/
olympic_events.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Variáveis dos Idiomas
//Line chart
var line_chart_width = 900;
var line_chart_height = 300;
var svg_line_chart;
var padding = 45;
const lado_do_triangulo = 7;
var country_triangles;
//cores das linhas
var colors = ["rgb(255, 164, 5)","rgb(43, 206, 72)","rgb(0, 117, 220)","rgb(194, 0, 136)",
"rgb(255, 0, 16)","rgb(94, 241, 242)","rgb(116, 10, 255)","rgb(240, 163, 255)","rgb(157, 204, 0)","rgb(0, 153, 143)",
"rgb(153, 0, 0)","rgb(66, 102, 0)"];
var used_colors = [];
function unused_colors() {return colors.filter(d => !used_colors.includes(d)) };
var line_chart_xaxis;
var line_chart_yaxis;
var line_chart_xscale;
var line_chart_xscale2;
var line_chart_xscale_filtered;
var line_chart_yscale;
//Population pyramid
var population_pyramid_width = 300;
var population_pyramid_height = 350;
var svg_pyramid_M;
var svg_pyramid_F;
var svg_pyramid_text;
var population_pyramid_padding = 20;
var population_pyramid_age_interval;
var ydata_pyramid;
var population_pyramid_xaxis;
var population_pyramid_xaxis_invert;
var population_pyramid_xscale_F;
var population_pyramid_xscale_M;
var population_pyramid_yscale;
//Choropleth map
var cmap_width = 850;
var cmap_height = 531;
var map_country_clicked;
var percent_per_country = {};
var map_coutries_names;
//legend
var legend;
var legend_width = 200;
var divisions = 100;
//represent countries in map
var search_countries = [];
//alluvial chart:
var alluvial_chart_width = 400;//347;
var alluvial_chart_height = 400;//350;
//box plots:
var box_plots_width = 300;//347;
var box_plots_height = 300;//350;
//Sports grid
var sports_grid_width = 400;
var sports_grid_height = 320;
//events list
var events_list_width = 400;
var events_list_height = 220;
var events_list_aux_width = 425;
var events_list_aux_height = 30;
var events_list_aux;
var events_list_permanent;
var events_list_scroll_count;
var events_list_yscale;
var tooltip_events_list;
var height_min
var height_1
var height_2
var height_3
var height_max
var height_med
//quantis dos pesos:
var weight_min
var weight_1
var weight_2
var weight_3
var weight_max
var weight_med
var BMI_min
var BMI_1
var BMI_2
var BMI_3
var BMI_max
var BMI_med
var box_plots_xscale_heights;
var box_plots_xscale_weights;
var box_plots_xscale_BMIs;
var box_plots_yscale;
//max_events:
var max_events_width = 350;
var max_events_height = 210;
var max_events_aux;
//Variáveis dos Datasets
var full_dataset; //dataset original
var dataset_line;
var dataset_cmap;
var dataset_cmap_aux_medals;
var dataset_pyramid;
var dataset_alluvial;
var dataset_box_plots;
var dataset_max_events;
var dataset_sports;
var dataset; //dataset filtrado - para ser usado no Boxplot, Sports Grid, Podium e Events
var countries_codes = {}; //Alpha3 e IOC de cada país
var countries_ioc_name = {}; //IOC e nome de cada país
var country_years_dataset; // host countries em cada season (independente dos participantes)
//var medals_won_per_country_Year_dataset; //pre processamento
//var dataset_pyramid;
//Variáveis das categorias de atributos selecionadas medal_types = data.map(d => d.Medal).filter(unique).sort(); //guardar todas as seasons
var countries_2_view;
var selected_country;
var selected_countries;
var unselected_countries_line;
var unselected_countries_cmap;
var countries;
var map_countries;
var countries_in_map;
var available_countries;
var countries_with_participants;
var selected_year;
var selected_years;
var years;
var selected_season;
var selected_seasons;
var seasons;
var selected_bar;
var selected_bars = [];
var bars;
var selected_heights;
var selected_height_min;
var selected_height_max;
var height_min;
var height_max;
var heights;
var selected_weights;
var selected_weight_min;
var selected_weight_max;
var weight_min;
var weight_max;
var weights;
var selected_bmis;
var selected_BMI_min;
var selected_BMI_max;
var BMI_min;
var BMI_max;
var bmis;
var selected_medal_type;
var selected_medal_types;
var medal_types;
var selected_event_type; //individual or team
var selected_event_types;
var event_types;
var selected_event;
var selected_events;
var events;
var selected_sport;
var selected_sports;
var sports;
//Variáveis da função update
var changed_sex_pyramid = false;
var changed_age_pyramid = false;
//===========================================================================================================
//===========================================================================================================
//===========================================================================================================
//===========================================================================================================
//===========================================================================================================
//===========================================================================================================
// DISPATCH
//===========================================================================================================
//Dispatch:
var dispatch = d3.dispatch("select_bar",
"click_sex_button",
"click_line_chart_year",
"click_season_line_chart",
"change_year_slider",
"select_country",
"click_reset_button",
"select_alluvial",
"click_max_events",
"events_list_scroll",
"events_list_click",
"events_list_scroll_bar",
"select_sport" );
dispatch.on("select_bar", function (bar) {
selected_bar = d3.selectAll("rect")
.filter(function(d) {
if (d != undefined) {
return d[0] == bar [0] && d[2]==bar[2]}}).nodes()[0];//{return d[0] == bar[0] && d[2] == bar[2] } );
//este if é "se selecionaste uma barra que nao a do meio:"
if ((selected_bar.id.slice(-1) == "M" || selected_bar.id.slice(-1) == "F") && selected_bar.id.slice(-2,-1) != "M" ) {
if (selected_bars == bars)
{selected_bars = [selected_bar];
}
else if ( selected_bars.includes(selected_bar) )
{selected_bars.splice(selected_bars.indexOf(selected_bar),1);
}
else { selected_bars.push(selected_bar)
}
if (selected_bars.length == 0)
{selected_bars = bars;
}
;}
//agora é "se escolheste uma das barras do meio:"
//é basicamente o mesmo mas se como tivesses selecionado nas duas barras ao mesmo tempo
else {
var selected_bar_aux = d3.selectAll("rect")
.filter(function(d) {
if (d != undefined) {
return d[0] == selected_bar.id.slice(0,-2) && d[2] != "MF"}}).nodes()
//a barra masculina é selected_bars_aux[0] e a feminina selected_bars_aux[1]
if (selected_bars == bars)
{selected_bars = [selected_bar_aux[0], selected_bar_aux[1]];
}
else if ( selected_bars.includes(selected_bar_aux[0]) && selected_bars.includes(selected_bar_aux[1]) )
{selected_bars.splice(selected_bars.indexOf(selected_bar_aux[0]),1);
selected_bars.splice(selected_bars.indexOf(selected_bar_aux[1]),1);
}
else if ( selected_bars.includes(selected_bar_aux[0]) && !selected_bars.includes(selected_bar_aux[1]) )
{ selected_bars.push(selected_bar_aux[1]);
}
else if ( !selected_bars.includes(selected_bar_aux[0]) && selected_bars.includes(selected_bar_aux[1]) )
{ selected_bars.push(selected_bar_aux[0]);
}
else {selected_bars.push(selected_bar_aux[0]);
selected_bars.push(selected_bar_aux[1]);
}
if (selected_bars.length == 0)
{selected_bars = bars
}
;}
;});
dispatch.on("click_sex_button", function(button){
if (JSON.stringify(selected_bars) == JSON.stringify(bars.filter(function(d) { return d.id.slice(-1) == "M"})) && button.path[1].id.slice(-1) == "M")
{selected_bars = bars}
else if (JSON.stringify(selected_bars) == JSON.stringify(bars.filter(function(d) {return d.id.slice(-1) == "F"})) && button.path[1].id.slice(-1) == "F")
{selected_bars = bars}
else {selected_bars = bars.filter( function (d)
{return d.id.slice(-1) == button.path[1].id.slice(-1) && d.id.slice(-2) != "M"})}
;})
dispatch.on("click_line_chart_year",function(button){
selected_year = button.path[0].innerHTML
//só vamos fazer alterações se o ano selecionado foi de facto um ano em que ocorreu um jogo:
if ( years.includes(selected_year) ) {
if (selected_years == years){
selected_years = [selected_year]
;}
else if (selected_years.includes(selected_year)){
selected_years.splice(selected_years.indexOf(selected_year),1)
;}
else {selected_years.push(selected_year)}
if (selected_years.length==0){
selected_years = years
;}
;}
else {svg_line_chart.append("text")
.attr("x",button.offsetX - 175)
.attr("y",button.offsetY - 200)
.attr("dy", "1em")
.attr("class", "label")
.attr("font-family", "verdana")
.attr("font-size","20px")
.attr("font-weight","bold")
.attr("id","temporary_text_click_on_year_line_chart" + String(selected_year))
.attr("fill","red")
.text("There were no Olympics held this year!");
svg_line_chart.select("#temporary_text_click_on_year_line_chart"+ String(selected_year))
.transition()
.duration(500)
.attr("fill","rgba(100,100,100,0)")
.remove()
}
;} )
dispatch.on("click_season_line_chart", function (season){
selected_season = season.path[0].id.slice(6,12)
if (selected_seasons.length == 1 && selected_seasons == selected_season ){
selected_seasons = seasons
;}
else{
selected_seasons = [selected_season];
};
})
dispatch.on("change_year_slider",function(interval){
var change_slider_aux0 = interval[0];
var change_slider_aux1 = interval[1];
if (interval[0] >1906){ change_slider_aux0 = change_slider_aux0 - 2 };
if (interval[0] >1908){ change_slider_aux0 = change_slider_aux0 - 2 };
if (interval[0] >1916){ change_slider_aux0 = change_slider_aux0 + 4 };
if (interval[0] >1936){ change_slider_aux0 = change_slider_aux0 + 8 };
if (change_slider_aux0 >1992){ change_slider_aux0 = change_slider_aux0 - (change_slider_aux0-1992)/2 };
if (interval[1] >1906){ change_slider_aux1 = change_slider_aux1 - 2 };
if (interval[1] >1908){ change_slider_aux1 = change_slider_aux1 - 2 };
if (interval[1] >1916){ change_slider_aux1 = change_slider_aux1 + 4 };
if (interval[1] >1936){ change_slider_aux1 = change_slider_aux1 + 8 };
if (change_slider_aux1 >1992){ change_slider_aux1 = change_slider_aux1 - (change_slider_aux1-1992)/2 };
if (change_slider_aux0 == 1896 && change_slider_aux1 == 2016) {selected_years = years}
else{selected_years = years.filter(function(d){return d >= change_slider_aux0 && d <= change_slider_aux1;})}
;})
dispatch.on("select_country", function(country){
if (selected_countries == countries)
{ selected_countries = [selected_country];
}
else if (selected_countries.includes(selected_country)) //se o pais ja esta selecionado vamos retira-lo da lista de selecionados
{selected_countries.splice(selected_countries.indexOf(selected_country),1);
}
else { //caso contrario, vamos coloca-lo na lista de selecionados
selected_countries.push(selected_country);
}
if (selected_countries.length == 0)
{selected_countries = countries}
});
dispatch.on("click_reset_button", function(){
var new_dataset;
new_dataset = full_dataset.filter(d => d.Medal != "NA");
dataset = full_dataset;
dataset_line = full_dataset;
dataset_cmap = full_dataset;
dataset_pyramid = new_dataset;
dataset_alluvial = new_dataset;
dataset_box_plots = new_dataset;
dataset_max_events = new_dataset;
dataset_sports = new_dataset;
// valores default de todos os selected -> incluir tudo
selected_countries = countries;
selected_years = years;
selected_seasons = seasons;
selected_bars = bars;
selected_heights = heights;
selected_height_max = height_max
selected_height_min = height_min
selected_weights = weights;
selected_weight_max = weight_max
selected_weight_min = weight_min
selected_bmis = bmis;
selected_BMI_max = BMI_max
selected_BMI_min = BMI_min
selected_medal_types = medal_types;
selected_event_types = event_types;
selected_events = events;
selected_sports = sports;
d3.select("#slider_year").remove()
var sliderRange = d3
.sliderBottom()
.min(1896)
.max(2032)
.width(810)
.ticks(0)
.step(2)
.default([1896, 2032])
.fill('#2196f3')
.on('onchange', d => {dispatch.call("change_year_slider", this, d)
update_dataset();
update_pyramid_from_year();
update_cmap_from_others();
update_alluvial_chart();
update_box_plots();
update_max_events();
update_sports_grid();
update_events_list();
//vamos por todos os retangulos nos anos invisiveis
for (i in years ){
svg_line_chart.select("#tick_rect_line_chart"+String( years[i]) )
.attr("visibility", "hidden")}
});
var gRange = svg_line_chart
.append('g')
.attr('transform', 'translate(44,273)')
.attr("id", "slider_year")
.call(sliderRange);
//reset zoom cmap
var zoom_reset = d3.zoom()
.scaleExtent([1, 8])
.on('zoom', function(d) {
svg_cmap.selectAll(".Country")
.attr('transform', d.transform);
});
svg_cmap.call(zoom_reset.transform, d3.zoomIdentity);
update_pyramid_from_pyramid();
update_pyramid_from_line();
update_line_from_line();
update_line_from_pyramid();
update_line_from_year();
update_line_from_season();
update_cmap_from_countries();
update_cmap_from_others();
update_alluvial_chart();
update_max_events();
update_sports_grid();
update_events_list(true);
d3.selectAll(".selection").attr("width",0);
update_box_plots();
});
dispatch.on("select_alluvial", function(rect){
var choice = rect[2]
if (medal_types.includes(choice)){
if(selected_medal_types.length == medal_types.length){selected_medal_types = [choice]}
else if(selected_medal_types.includes(choice)){selected_medal_types.splice(selected_medal_types.indexOf(choice),1)}
else{selected_medal_types.push(choice)}
if(selected_medal_types.length == 0) {selected_medal_types = medal_types}
else if (selected_medal_types.length == medal_types.length){selected_medal_types = medal_types}
}
else if(seasons.includes(choice)){
if (selected_seasons[0] == choice && selected_seasons.length == 1 ){selected_seasons = seasons}
else{selected_seasons = [choice]}
}
//caso em que selecionou um event_type:
else{
if (selected_event_types[0] == choice && selected_event_types.length == 1 ){selected_event_types = event_types}
else{selected_event_types = [choice]}
}
})
dispatch.on("click_max_events",function(number){
if (selected_events[0] == max_events_aux[number][0] && selected_events.length == 1){selected_events = events;
svg_max_events.select("#max_events_"+String(number+1)+"_1")
.transition()
.duration(300)
.style("font-weight", "normal")
svg_max_events.select("#max_events_"+String(number+1)+"_2")
.transition()
.duration(300)
.style("font-weight", "normal")
}
else {selected_events = [max_events_aux[number][0]];
svg_max_events.select("#max_events_"+String(number+1)+"_1")
.transition()
.duration(300)
.style("font-weight", "bold")
svg_max_events.select("#max_events_"+String(number+1)+"_2")
.transition()
.duration(300)
.style("font-weight", "bold")
if (number == 0){
svg_max_events.select("#max_events_"+2+"_1")
.transition()
.duration(300)
.style("font-weight", "normal")
svg_max_events.select("#max_events_"+2+"_2")
.transition()
.duration(300)
.style("font-weight", "normal")
svg_max_events.select("#max_events_"+3+"_1")
.transition()
.duration(300)
.style("font-weight", "normal")
svg_max_events.select("#max_events_"+3+"_2")
.transition()
.duration(300)
.style("font-weight", "normal")
}
else if (number == 1){
svg_max_events.select("#max_events_"+1+"_1")
.transition()
.duration(300)
.style("font-weight", "normal")
svg_max_events.select("#max_events_"+1+"_2")
.transition()
.duration(300)
.style("font-weight", "normal")
svg_max_events.select("#max_events_"+3+"_1")
.transition()
.duration(300)
.style("font-weight", "normal")
svg_max_events.select("#max_events_"+3+"_2")
.transition()
.duration(300)
.style("font-weight", "normal")
}
else{
svg_max_events.select("#max_events_"+1+"_1")
.transition()
.duration(300)
.style("font-weight", "normal")
svg_max_events.select("#max_events_"+1+"_2")
.transition()
.duration(300)
.style("font-weight", "normal")
svg_max_events.select("#max_events_"+2+"_1")
.transition()
.duration(300)
.style("font-weight", "normal")
svg_max_events.select("#max_events_"+2+"_2")
.transition()
.duration(300)
.style("font-weight", "normal")
}
}
})
dispatch.on("events_list_scroll",function(event){
if(event.deltaY > 0){events_list_scroll_count += 1}
else {events_list_scroll_count -= 1}
if(events_list_scroll_count < 0){events_list_scroll_count = 0}
else if (events_list_scroll_count > events_list_aux.length - 11){events_list_scroll_count = events_list_aux.length - 11}
})
dispatch.on("events_list_click",function(){
var ev = events_list_permanent[this.id.slice(17)][0]
if(selected_events.length == events.length){selected_events = [ev]}
else if (selected_events.includes(ev)) {selected_events.splice(selected_events.indexOf(ev),1)}
else {selected_events.push(ev)}
if (selected_events.length == 0) {selected_events = events}
})
dispatch.on("events_list_scroll_bar",function(){
events_list_scroll_count = Math.ceil(events_list_aux.length * (event.offsetY/events_list_height))
if(events_list_scroll_count < 0){events_list_scroll_count = 0}
else if (events_list_scroll_count > events_list_aux.length - 11){events_list_scroll_count = events_list_aux.length - 11}
})
dispatch.on("select_sport",function(){
if (selected_sports == sports)
{ selected_sports = [selected_sport];
}
else if (selected_sports.includes(selected_sport)) //se o pais ja esta selecionado vamos retira-lo da lista de selecionados
{selected_sports.splice(selected_sports.indexOf(selected_sport),1);
}
else { //caso contrario, vamos coloca-lo na lista de selecionados
selected_sports.push(selected_sport);
}
if (selected_sports.length == 0)
{selected_sports = sports}
})
//===========================================================================================================
//===========================================================================================================
//===========================================================================================================
//===========================================================================================================
//===========================================================================================================
//===========================================================================================================
// FUNÇÕES AUXILIARES
//===========================================================================================================
function represent_countries_in_map(c){
//dicionario com IOC como chave e valores: nome do pais (NOC) + (IOC) (se esta disponivel no mapa)
var country_key;
for (i in c){
country_key = countries_ioc_name[c[i]]
if (!countries_in_map.includes(c[i])){
search_countries.push({name: String(country_key) + "*", ioc: c[i]})
countries_ioc_name[c[i]] = countries_ioc_name[c[i]] + "*"
}
else{
search_countries.push({name: String(country_key), ioc: c[i] })
}
};
return search_countries};
function convert_code(code){
if (countries_codes[code] == undefined){return code}
else{return countries_codes[code]}
};
function remove_color(color){
index = used_colors.indexOf(color);
if (!(index == -1)){
used_colors.splice(index,1)
}
};
function unique(value, index, self) {
return self.indexOf(value) === index};
function selected_sex_ages(){
var selected_sex_ages_aux2;
var idadesF = [];
var idadesM = [];
for (i in selected_bars){
if (selected_bars[i].id.slice(-1) == "F") {
//[aux2 aux3[ são os limites do intervalo de idades desta barra
selected_sex_ages_aux2 = selected_bars[i].id.slice(0,-1) * population_pyramid_age_interval
const selected_sex_ages_aux3 = selected_sex_ages_aux2 + population_pyramid_age_interval;
while (selected_sex_ages_aux2 < selected_sex_ages_aux3){
idadesF.push(selected_sex_ages_aux2);
selected_sex_ages_aux2 = selected_sex_ages_aux2 + 1;
}
}
else {
//[aux2 aux3[ são os limites do intervalo de idades desta barra
selected_sex_ages_aux2 = selected_bars[i].id.slice(0,-1) * population_pyramid_age_interval
const selected_sex_ages_aux3 = selected_sex_ages_aux2 + population_pyramid_age_interval;
while (selected_sex_ages_aux2 < selected_sex_ages_aux3){
idadesM.push(selected_sex_ages_aux2);
selected_sex_ages_aux2 = selected_sex_ages_aux2 + 1;
}
}
};
//devolve [idadesF,idadesM]
var selected_sex_ages_aux = [idadesF, idadesM];
return selected_sex_ages_aux
}
//===========================================================================================================
//===========================================================================================================
//===========================================================================================================
//===========================================================================================================
//===========================================================================================================
//===========================================================================================================
// UPDATES
//===========================================================================================================
function update_dataset(){
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
var selected_sex_ages_aux = selected_sex_ages();
var idadesF = selected_sex_ages_aux[0];
var idadesM = selected_sex_ages_aux[1];
var dataset_aux;
var dataset_aux2;
dataset_aux = full_dataset.filter(function(d){
//filtra a informação que todos os idiomas usam
return (selected_events == events || selected_events.includes(d.Event))
&& (selected_sports == sports || selected_sports.includes(d.Sport))
&& ( (selected_height_min == height_min && selected_height_max == height_max) || (selected_height_min <= d.Height && selected_height_max >= d.Height) )
&& ( (selected_weight_min == weight_min && selected_weight_max == weight_max) || (selected_weight_min <= d.Weight && selected_weight_max >= d.Weight) )
&& ( (selected_BMI_min == BMI_min && selected_BMI_max == BMI_max) || (selected_BMI_min <= parseInt(d.BMI) && selected_BMI_max >= parseInt(d.BMI)) )
;});
dataset_aux2 = dataset_aux.filter(function(d){ //filtra também por countries e years
return (selected_countries == countries || selected_countries.includes(d.NOC))
&& (selected_years == years || selected_years.includes(d.Year))
;});
dataset_aux3 = full_dataset.filter(function(d){ //este não é para filtrar por eventos nem desportos
return (selected_countries == countries || selected_countries.includes(d.NOC))
&& (selected_years == years || selected_years.includes(d.Year))
&& ( (d.Sex == "M" && idadesM.includes(parseInt(d.Age))) || (d.Sex == "F" && idadesF.includes(parseInt(d.Age))) )
&& (selected_seasons == seasons || selected_seasons.includes(d.Season))
&& (selected_medal_types == medal_types || selected_medal_types.includes(d.Medal))
&& (d.Medal != "NA")
&& (selected_event_types == event_types || selected_event_types.includes(d.Event_Type))
&& ( (selected_height_min == height_min && selected_height_max == height_max) || (selected_height_min <= d.Height && selected_height_max >= d.Height) )
&& ( (selected_weight_min == weight_min && selected_weight_max == weight_max) || (selected_weight_min <= d.Weight && selected_weight_max >= d.Weight) )
&& ( (selected_BMI_min == BMI_min && selected_BMI_max == BMI_max) || (selected_BMI_min <= parseInt(d.BMI) && selected_BMI_max >= parseInt(d.BMI)) )
;});
dataset_box_plots = full_dataset.filter(function(d){ //este não é para filtrar as idades nem peso nem BMI
return (selected_events == events || selected_events.includes(d.Event))
&& (selected_sports == sports || selected_sports.includes(d.Sport))
&& (selected_countries == countries || selected_countries.includes(d.NOC))
&& (selected_years == years || selected_years.includes(d.Year))
&& ( (d.Sex == "M" && idadesM.includes(parseInt(d.Age))) || (d.Sex == "F" && idadesF.includes(parseInt(d.Age))) )
&& (selected_seasons == seasons || selected_seasons.includes(d.Season))
&& (selected_medal_types == medal_types || selected_medal_types.includes(d.Medal))
&& (d.Medal != "NA")
&& (selected_event_types == event_types || selected_event_types.includes(d.Event_Type))
;});
dataset_max_events = dataset_aux3.filter(function(d){ //este não é para filtrar por eventos
return (selected_sports == sports || selected_sports.includes(d.Sport))
;});
dataset_sports = dataset_aux3.filter(function(d){ //este não é para filtrar por sports
return (selected_events == events || selected_events.includes(d.Event))
;});
dataset_alluvial = dataset_aux2.filter(function(d){ //filtrado por tudo excepto medal_type, season and event_type
return ( (d.Sex == "M" && idadesM.includes(parseInt(d.Age)))
|| (d.Sex == "F" && idadesF.includes(parseInt(d.Age))) )
&& d.Medal != "NA"
});
dataset_pyramid = dataset_aux2.filter(function(d){ //filtrado por tudo excepto age and sex
return (selected_seasons == seasons || selected_seasons.includes(d.Season))
&& (selected_medal_types == medal_types || selected_medal_types.includes(d.Medal))
&& (d.Medal != "NA")
&& (selected_event_types == event_types || selected_event_types.includes(d.Event_Type))
});
dataset_line = dataset_aux.filter(function(d){ //filtrado por tudo excepto countries, medal_type e years
return (selected_seasons == seasons || selected_seasons.includes(d.Season))
&& (selected_event_types == event_types || selected_event_types.includes(d.Event_Type))
&& ( (d.Sex == "M" && idadesM.includes(parseInt(d.Age)))
|| (d.Sex == "F" && idadesF.includes(parseInt(d.Age))))
});
dataset_cmap = dataset_line.filter(function(d){ //filtrado por tudo excepto os countries e medal_type
return (selected_years == years || selected_years.includes(d.Year))
});
dataset = dataset_cmap.filter(function(d){ //filtrado por tudo
return (selected_countries == countries || selected_countries.includes(d.NOC))
&& (selected_medal_types == medal_types || selected_medal_types.includes(d.Medal))
});
}
//funçoes de update:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Atualiza a piramide quando a mesma é clicada
function update_pyramid_from_pyramid(){
for (i in selected_bars) {
d3.selectAll("rect")
.filter(function(d) {
if (d != undefined) {
return d[0]+d[2] == selected_bars[i].id }} )
.transition()
.duration(200)
.attr("fill",
function(d)
{if (d[2] == "M") {return "Cornflowerblue"} else if (d[2] == "F") {return "pink"} } )
}
const unselected_bars = bars.filter(d => !selected_bars.includes(d) )
for (i in unselected_bars ) {
d3.selectAll("rect")
.filter(function(d) {
if (d != undefined) {return d[0]+d[2] == unselected_bars[i].id }} )
.transition()
.duration(300)
.attr("fill", "rgba(100,100,100,0.5)")
}
;}
function update_line_from_pyramid(){
if (dataset_line.length == 0){
svg_line_chart.append("text")
.attr("id", "line_error")
.attr("dy", "1em")
.attr("x", 290)
.attr("y", 40)
.attr("class", "label")
.attr("font-family", "verdana")
.attr("font-size","20px")
.attr("font-weight","bold")
.attr("fill","red")
.text("There is no data to show");
}
else{
d3.select("#line_error").remove();
};
function gerar_data_line_chart(countries){
var r = [];
var dataset_line_aux;
var line_chart_medals = selected_medal_types;
var index_medal = line_chart_medals.indexOf("NA");
if (index_medal != -1) {line_chart_medals.splice(index_medal, 1)};
if (line_chart_medals == []) {line_chart_medals = ["Gold", "Silver", "Bronze"]}
var dataset_line_nest;
dataset_line_aux = dataset_line.filter(function(d){return line_chart_medals.includes(d.Medal)});
dataset_line_nest = d3.rollup(dataset_line_aux, d => d.length, d => d.NOC, d => d.Year);
for (j in countries){
for (i in years){
if ( !dataset_line_nest.has(countries[j]) || !dataset_line_nest.get(countries[j]).has(years[i]) || dataset_line_nest.get(countries[j]).get(years[i]) == undefined) {
r.push([ countries[j], years[i],0])}
else {r.push([ countries[j], years[i],dataset_line_nest.get(countries[j]).get(years[i])])}
}
};
return r}
data_line_chart = gerar_data_line_chart(countries_2_view);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//agora as escalas
line_chart_yscale = d3
.scaleLinear()
.domain([0,d3.max(data_line_chart, function (d) {return d[2];}),])
.range([line_chart_height - padding - 15, padding, ]);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//o eixo:
svg_line_chart.select("#line_chart_yaxis").remove()
line_chart_yaxis = d3
.axisLeft()
.scale(line_chart_yscale)
svg_line_chart
.append("g")
.attr("transform", "translate(" + padding + ",0)")
.attr("class", "yaxis")
.attr("id","line_chart_yaxis")
.call(line_chart_yaxis);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//novas linhas:
var line = d3.line()
.x(function (d) {
return line_chart_xscale2(d[1]);
})
.y(function (d) {
return line_chart_yscale(d[2]);