-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarviz_27_4.js
1193 lines (973 loc) · 46.9 KB
/
tarviz_27_4.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
// Dataset presets
// preselected DB
const preselectedDB = 1; // of 0/1/2/ <-- when concat array (when object: 1/2/3)
// References of DBs
const dbRefs = ['Bingen et al. (2017)',
'Mills et al. (2016)',
'Carn et al. (2016)'];
// Markup presets
// 1) map background and land mask
const black000 = "rgb(0, 0, 0)"; // #000
const gray333 = "rgb(51, 51, 51)";
const grayccc = "rgb(204, 204, 204)";
const gray111 = "rgb(17, 17, 17)";
const gray69 = "rgb(105, 105, 105)"; // #696969 aka dimgray
const brown01 = "rgb(104, 90, 92)"; // #685a5c
const orange01 = "rgb(217, 137, 97)"; // #d98961
const orange02 = "rgb(231, 189, 114)"; // #e7bd72
// 2) mapping color scales ... largely obsolete map19_2+
const yellowCS = '#ECD078',
redCS = '#C02942',
greenish = ['#ACD855', '#1E8B54'],
reddish = [yellowCS, redCS];
//const actual_colorbar = reddish;
// 3) slider positioning:
const slider_scale_margin_left = 737; // slider axis text position left
const slider_scale_stretch = 720; // slider axis text stretch (center aligned!!)
const slider_label_x_pos = 450,
slider_label_y_pos = 20,
slider_dyn_label_x_pos = 600;
// 4) button state colors (orange shades, compare to css) <----------------------- TODO unification colors in CSS + JS
const btn_defaultColor= gray333; // default: #7777BB
const btn_hoverColor = "#FFDFA5"; // default: #0000ff
const btn_pressedColor= orange02; // default: #000077
// Set the dimensions of canvas for both graphs
const margin = {top: 30, right: 20, bottom: 30, left: 50,
slider: slider_scale_margin_left,
ts_top: 50, ts_left:155},
width = 960 ,
height = 700;
const map_y_top_pos = -70,
ts_y_top_pos = 530;
const map_title_x_pos = 400,
map_title_y_pos = 180;
const map_legend_x_pos = 76,
map_legend_y_pos = 60;
// set time-series graph dimensions
const ts_width = 750;
const ts_height = 100;
const ts_title_x_pos = 150,
ts_title_y_pos = -10;
const ts_legend_x_pos = 23,
ts_legend_y_pos = 560;
//- - Map: initialisation ... moreover, this is general initialisation of svg area
d3.select("div.row.tar-viz-row").append("div").attr('id',"svgTAR");
// d3 global def:
const svg = d3.select("#svgTAR").append("svg").attr("class", "svg-pad"); // set map's BG color here via class (overwrite html body BG color - otherwise not possible to trustworthly change color afterwards)
// Set the size of the SVG element
svg.attr('width', width).attr('height', height);
// ini map & map data d3 groups & layouting
svg.append('g').classed('map' , true).attr("transform", "translate(0,"+map_y_top_pos+")");
svg.append('g').classed('dots', true).attr("transform", "translate(0,"+map_y_top_pos+")");
// ini time-series & layouting
svg.append('g').classed('ts' , true).attr("transform", "translate("+margin.ts_left+","+ts_y_top_pos+")");
/* v3:
svg.append('foreignObject').attr("width", 700).attr("height", 30)
.attr("transform", "translate(5,682)")
.classed('copyright' , true)
.append("xhtml:body")
.style("font", "7px 'Helvetica Neue'").style("font-weight","300").style("letter-spacing","2px")
.style("color", "#e7bd72").style("background-color","transparent")
.style()
//v3: .html("<p>©CC 2018 - HOMMEL & GRAF ENVIRONMENTAL - [email protected]</p>");
*/
//- - Settings map specific:
// specific svg elements:
// Create and configure a geographic projection
//v3: var projection = d3.geo.equirectangular()
var projection = d3.geoEquirectangular()
// optionally scale, set padding:
.scale(width/8)
.translate([width / 2+50, height / 2+20]);
// Create and configure a path generator
//v3: var pathGenerator = d3.geo.path()
var pathGenerator = d3.geoPath()
.projection(projection);
//- - Parse date information from DB's
//v3: var parseDate = d3.time.format("%e %b %Y").parse;
//v3: var parseYear = d3.time.format("%Y");
var parseDate = d3.timeParse("%e %b %Y");
var parseYear = d3.timeFormat("%Y");
//- - defining UI object with initial declaritions, self explaining
var uiObj = new Object(),
str = 'ui_settings',
buttonsDBselection = 'state_DB_selection',
buttonAllYears = 'state_all_years_selection',
slider = 'state_slider';
uiObj.buttonsDBselection = 0; // pre-selected database 0/1/2
uiObj.buttonAllYears = 'on'; // show all years on initialisation
uiObj.slider = 13; // default slider state (indices corresponidng to years on slider axis)
//- - Run viz, incl. importing data: - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
queue()
.defer(d3.csv, "db1_2002-2012.csv") // anual accumulations per volcanoe for mapping
.defer(d3.csv, "db2_1998-2012.csv")
.defer(d3.csv, "db3_1998-2012.csv")
.defer(d3.csv, "db1_1998-2012_ts.csv") // time-series as given in data sets - no accumulation over time - not intended for mapping!!
.defer(d3.csv, "db2_1998-2012_ts.csv")
.defer(d3.csv, "db3_1998-2012_ts.csv")
.defer(d3.json, "land.geojson")
.await(ready);
function ready(error, map_db1, map_db2, map_db3, ts_db1, ts_db2, ts_db3, land) {
if (error) throw error;
console.log('map_db1: ',map_db1);
console.log('map_db2: ',map_db2);
console.log('map_db3: ',map_db3);
console.log('ts_db1: ',ts_db1);
console.log('ts_db2: ',ts_db2);
console.log('ts_db3: ',ts_db3);
console.log('land_1 : ',land);
// for unified scaling of volcano magnitude across the DBs first find max magnitude of all data:
// this mustr be done for mapping and time-series seperately since both apply different data sets
// (map: annually accumulated, time-series: considers each eruption as listed)
// for sake of simplicity, all DBs are merged into a single array
const mergedDBarray_map = map_db1.concat(map_db2,map_db3);
const mergedDBarray_ts = ts_db1.concat(ts_db2,ts_db3);
//console.log('ready: initially merged DB array for setting unified scaling:',mergedDBarray_map);
const allMax_map = find_overall_max(mergedDBarray_map);
const allMax_ts = find_overall_max(mergedDBarray_ts);
console.log('ready: overall max magnitude for unified scaling (map):',allMax_map);
console.log('ready: overall max magnitude for unified scaling (ts) :',allMax_ts);
const db1_map = {'volc':map_db1}; // unified key name necessary
const db2_map = {'volc':map_db2};
const db3_map = {'volc':map_db3};
const db1_ts = {'volc':ts_db1};
const db2_ts = {'volc':ts_db2};
const db3_ts = {'volc':ts_db3};
// storing datasets in single array:
const datasets = [db1_map,db2_map,db3_map];
// defining data object with initial declaritions, self explaining:
var dataObj = new Object(),
str = 'dbName',
db_1_map = 'dataarray1_map',
db_2_map = 'dataarray2_map',
db_3_map = 'dataarray3_map',
db_1_ts = 'dataarray1_ts',
db_2_ts = 'dataarray2_ts',
db_3_ts = 'dataarray3_ts',
selected_db_map = 'selected_dataset_map',
selected_db_ts = 'selected_dataset_ts',
selected_years_map = 'selected_years_map', // selected db but only requested years (default: all years !!)
selected_years_ts = 'selected_years_ts', // same for time-series
name_of_selected_db = 'name_of_selected_db', // just the name of data base for easy debugging or side information
all_years_of_selected_db = 'all_years_of_selected_db', // just the year info of selected (and possibly time-sliced) db
unique_years_of_selected_db = 'unique_years_of_selected_db', // NOT>extracting<NOT unique years (predefine, also consiudering empty years)
selectDB = 'selection_DB_method',
sliceDB = 'timeslice_method'; // try to slice both map and time-series within this method
// datasets embedded:
dataObj.str = 'TAR_DB';
dataObj.db_1_map = db1_map;
dataObj.db_2_map = db2_map;
dataObj.db_3_map = db3_map;
dataObj.db_1_ts = db1_ts;
dataObj.db_2_ts = db2_ts;
dataObj.db_3_ts = db3_ts;
// preselection on initialisation (directly preferred):
dataObj.selected_db_map = dataObj.db_2_map;
dataObj.selected_db_ts = dataObj.db_2_ts;
// in order to initialise slider preselect information about years in the default dataset:
dataObj.all_years_of_selected_db = get_info_all_years_obj(dataObj.selected_db_map);
// ... and build array holding unique values of years in the chosen dataset:
dataObj.unique_years_of_selected_db = [1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012];
// build-in methods to operate on embedded datasets:
dataObj.selectDB = function (item) {
// selecting a DB
if (item > 0 && item < 4) {
map_item = item;
ts_item = item+3;
dataObj.selected_db_map = Object.values(dataObj)[map_item];
dataObj.selected_db_ts = Object.values(dataObj)[ts_item];
dataObj.name_of_selected_db = dbRefs[item-1];
} else {
throw '- Error: Unregistered Data Base - Please select from database index 1,2 or 3 -';
};
};
dataObj.sliceDB = function (item) { // hier bei Deklaration kann man preset angeben
let jsonData_map = {};
let jsonData_ts = {};
this.selected_db_map.volc.forEach(function(d,i) {
if(parseYear(parseDate(d["Time Eruption"])) == item) {
return jsonData_map[i] = d;
};
});
this.selected_db_ts.volc.forEach(function(d,i) {
if(parseYear(parseDate(d["Time Eruption"])) == item) {
return jsonData_ts[i] = d;
};
});
// "destructuring assignments":
return {a:dataObj.selected_years_map = jsonData_map,b:dataObj.selected_years_ts = jsonData_ts} // <--- critical ???
};
console.log('new dataObj.str: ',dataObj.str);
console.log('new dataObj.db_2_map : ',dataObj.db_2_map);
console.log(' alternative db selection via index : ',Object.values(dataObj)[2]);
console.log('new dataObj.selectDB (method): ',dataObj.selectDB(2));
console.log('new dataObj.selected_db : ',dataObj.selected_db);
year = get_year_for_dataOb_slicing(dataObj,uiObj);
console.log('new dataObj.sliceDB (method): ',dataObj.sliceDB(year)); // same in update_viz()
console.log('new dataObj.selected_years_map : ',dataObj.selected_years_map); // <--- !!!!
console.log('new dataObj.selected_years_ts : ',dataObj.selected_years_ts); // <--- !!!!
console.log('new dataObj.all_years_of_selected_db: ',dataObj.all_years_of_selected_db);
console.log('new dataObj.unique_years_of_selected_db: ',dataObj.unique_years_of_selected_db);
// before setting up plot, setup slider (requires dataObj to be known)
//outdated const slider = setup_slider(svg,"#svgTAR",dataObj.unique_years_of_selected_db,uiObj.slider);
//outdated const setup_slider_axis = slider_axis_scale(svg,dataObj.unique_years_of_selected_db);
//receive slider's initial value (which must be 0):
//outdated const ini_current = document.getElementById('slider_input').value;
//outdated const slider_ini_value = [ini_current];
// since 27_4 only set slider title
setup_slider_title(svg,"#svgTAR",dataObj.unique_years_of_selected_db,uiObj.slider);
//-------------------------------------------------------------------------------------------------------------------------------
// tarviz_27+: d3v4 'native' slider:
var new_sliders_year_sel = -99; // initial
var sliderWidth = 500;
// my port applies years only for slider axis/labeling:
var xYearFirst = d3.scaleLinear()
.domain([1998, 2012])
.range([0, sliderWidth])
.clamp(true);
// added index range for simple migrations (alternatively on could set the year tracked
// from slider directly in the dataObj, or so)
var idxYearFirst = d3.scaleLinear()
.domain([0, 14])
.range([0, sliderWidth])
.clamp(true);
var sliderYearFirst = svg.append("g")
.attr("class", "v4_slider")
.attr("transform", "translate(200,400)");
sliderYearFirst.append("line")
.attr("class", ".v4_slider_track")
// .attr("x1", xYearFirst.range()[0])
// .attr("x2", xYearFirst.range()[1])
.attr("x1", idxYearFirst.range()[0])
.attr("x2", idxYearFirst.range()[1])
.select(function() { return this.parentNode.appendChild(this.cloneNode(true)); })
.attr("class", "v4_slider_track-inset")
.select(function() { return this.parentNode.appendChild(this.cloneNode(true)); })
.attr("class", "v4_slider_track-overlay")
.call(d3.drag()
.on("start.interrupt", function() { sliderYearFirst.interrupt(); })
.on("start drag", function() {
// setHandle1(xYearFirst.invert(d3.event.x));
setHandle1(idxYearFirst.invert(d3.event.x));
}));
sliderYearFirst.insert("g", ".track-overlay")
.attr("class", ".v4_slider_ticks")
.attr("transform", "translate(0," + 20 + ")")
.selectAll("text")
.data(xYearFirst.ticks(10))
.enter().append("text")
.attr("x", xYearFirst)
.attr("text-anchor", "middle")
.text(function(d) { return d; });
var handle1 = sliderYearFirst.insert("circle", ".v4_slider_track-overlay")
.attr("class", "v4_slider_handle")
.attr("cx",0)
.attr("r", 8);
// slider functionality
function setHandle1(h) {
// translates x position of slider (relative to overall slider width) handle into the scale of
// xYearFirst within 1998-2012
//orig: var var_pos = handle1.attr("cx", xYearFirst(Math.round(h)));
var var_pos = handle1.attr("cx", idxYearFirst(Math.round(h)));
//svgFilter.style("background-color", d3.hsl(xYearFirst.invert(handle1.attr("cx")), 0.8, 0.8));
updateFilterText();// kann man auch gleich getRangeText() aufrufen!!
//console.log('++++++++++++++++++setHandle1: xYearFirst(Math.round(h)): ',xYearFirst(Math.round(h)));
}
function updateFilterText() {
// var new_sliders_year_sel = getRangeText();
var new_sliders_year_sel = idxYearFirst.invert(handle1.attr("cx")); // gibt das jahr selektiert im slider zurück
console.log('_ _ _ _ _ _ _ _ _ _updateFilterText: uiObj: ',uiObj);
console.log('_ _ _ _ _ _ _ _ _ _updateFilterText: new_sliders_year_sel: ',new_sliders_year_sel);
uiObj.slider = new_sliders_year_sel;
update_viz();
}
//obsolete: function getHandleCx() {
//obsolete: console.log('...................getHandleCx: handle1.attr("cx"): ',handle1.attr("cx")); // ..... nicht benötigt ?????????
//obsolete: return [handle1.attr("cx")];
//obsolete: }
//obsolete: function getRangeText() {
//obsolete: // translates position (function setHandle1) into year of time period
//obsolete: //orig: var valA = xYearFirst.invert(handle1.attr("cx")); // gibt das jahr selektiert im slider zurück
//obsolete: var valA = idxYearFirst.invert(handle1.attr("cx")); // gibt das jahr selektiert im slider zurück
//obsolete: console.log('___________________getRangeText: valA: ',valA);
//obsolete: return valA;
//obsolete: }
//-------------------------------------------------------------------------------------------------------------------------------
// initiate initial data binding
update_viz();
console.log('ready: initial drawing finshed ----------------------------------------------------');
//---------------- DATA BINDING TO EVENTS ----------------------------------------------------------
//var h = xYearFirst.invert(d3.event.x);
// sliderYearFirst.call(d3.drag()
// .on("start drag", function() { setHandle1(xYearFirst.invert(d3.event.x)); }));
//handle1.attr("cx", xYearFirst(Math.round(h)));
//var value_slider = xYearFirst.invert(handle1.attr("cx"));
//console.log(' data binding: value_slider: ',value_slider);
//uiObj.slider = value_slider;
//update_viz();
// newww_slider.on("drag", function(){
console.log('newwwwwwwwwww newww_slider: year set?: ',uiObj.slider);
// });
//outdated slider.on("change", function(){
//outdated // receive current slider value (index) when moved:
//outdated var current_idx = this.value;
//outdated
//outdated
//outdated // + + new method controlling UI object:
//outdated uiObj.slider = current_idx;
//outdated update_viz();
//outdated });
//-- bind data to svg buttons: replaced from 'setup_buttons' function for separating button
// definition from event handling (similarely to slider)
buttonsDB.on("click",function(d,i) {
updateButtonColors(d3.select(this), d3.select(this.parentNode));
// set correct db name as additional button text (see buttonsDB defintion);
let dbSelection= [0,0,0]; // nullify
dbSelection.forEach(function(a, j) {
if(j == i) {
return dbSelection[j]=1;
};
});
update_button_text_reference(dbSelection,"DB");
uiObj.buttonsDBselection = i; // selects from db 0/1/2
update_viz();
});
buttonAllYears.on("click",function(d,i) {
[selection,showAllYears] = updateOnOffButtonColors(d3.select(this));
//console.log('buttonAllYears.on click: übergebe i an update_data_buttonAllYears:',i);
//console.log('buttonAllYears.on click: d3.select(this), d3.select(this.parentNode):', // check what click event returns
// d3.select(this), d3.select(this.parentNode));
//console.log('buttonAllYears.on click: returns: selection,showAllYears:',selection,showAllYears);
uiObj.buttonAllYears = showAllYears;
update_viz();
});
//-----------------------------------------------------------------------------------------------------
function update_viz() {
// 1) select correct db
console.log('%% update_viz: step 1: apply method dataObj.selectDB on uiObj.buttonsDBselection : ',
uiObj.buttonsDBselection,'+1 : ',
dataObj.selectDB( uiObj.buttonsDBselection+1 )); // <-- this line selects (bound to logging)
//dataObj.selectDB( 3 ));
// 2) decide whether show all years or time slice
if (uiObj.buttonAllYears == 'on') {
// 2.1) show all years: repeat method of sliceDB but without chosing a specific year (i.e. just
// conversion into correct data format):
let jsonData_map_allY = {};
let jsonData_ts_allY = {};
dataObj.selected_db_map.volc.forEach(function(d,i) {
return jsonData_map_allY[i] = d;
});
dataObj.selected_db_ts.volc.forEach(function(d,i) {
return jsonData_ts_allY[i] = d;
});
data_map = jsonData_map_allY;
data_ts = jsonData_ts_allY;
// update slider title
let nVolcs = numberVolcanos(data_map);
setup_slider_dyn_text("#svgTAR",nVolcs);
// set dummy year for time-slice drawing in this case
var year_requested = 2011;
} else { // 2.2) if slicing: show year's selected by slider
// DOES THIS TAKE SLECTED DB ???? DOES THIS TAKE SLECTED DB ???? DOES THIS TAKE SLECTED DB ????
// slice dataset:
//year = dataObj.unique_years_of_selected_db[uiObj.slider];
var year_requested = get_year_for_dataOb_slicing(dataObj,uiObj);
// update data object (.selected_years):
console.log('%% update_viz: step 2-2: apply slice method for selected year (slider pos.): ',uiObj.slider,dataObj.sliceDB(year_requested));
data_map = dataObj.selected_years_map;
data_ts = dataObj.selected_years_ts;
// update slider title
let nVolcs = Object.keys(dataObj.selected_years_map).length;
//console.log('%% update_viz: step 2-2: nVolcs (map):',numberVolcanos(dataObj.selected_years_map));
setup_slider_dyn_text("#svgTAR",nVolcs);
// test whether slicing works for time-series too:
//console.log('%% update_viz: step 2-2: nVolcs (ts):',numberVolcanos(dataObj.selected_years_ts));
};
// before plotting graphs, remove circles in map (untouch land) and delete time-series completely
d3.selectAll('g.dots').selectAll('circle').remove();
// delete all time-series elements, but not hte overall container (avoid overdrawing for each UI ineraction)!
d3.selectAll('g#container-ts.ts').select('g.xaxis').remove();
d3.selectAll('g#container-ts.ts').select('g.yaxis').remove();
d3.selectAll('g#container-ts.ts').select('text#ts-title').remove();
d3.selectAll('g#container-ts.ts').selectAll('circle').remove();
// skip empty data (which exist!) on updating plot:
let noData_map = isEmpty(data_map);
let noData_ts = isEmpty(data_ts);
//console.log('update_viz: empty year in time-sliced dataset (map)?:',noData_map);
//console.log('update_viz: empty year in time-sliced dataset (ts) ?:',noData_ts);
if (!noData_map && !noData_ts) {
var spec_colors = draw_map01(data_map, land, svg, allMax_map); // updating map plot
// else if () {
} else {
//
console.log('update_viz: year without data in selected db ',dataObj.name_of_selected_db);
//throw '- Error: update_viz: exception found with empty years in the 2 data sets - compare arrays! -';
};
draw_ts(data_ts, year_requested, spec_colors, allMax_ts); // always updating time-series plot
}; // update_viz
}; // ready function
//-- establish UI Buttons
// buttons are svg elements:
// xTranslate,yTranslate,bSpace,x0,y0
let dims = [0,240,20,20,20];
let offsets= [20,60,100];
let labels= ['DB1','DB2','DB3'];
let btnText = ['Dataset 1','Dataset 2','Dataset 3'];
let btnTextOffset = [170,30]; //x,ypos
// set switch for defining preselected button:
let dbSelection= [0,0,0];
dbSelection.forEach(function(d,i) {
//console.log('buttonDB dbSelection: ',i,d);
if(i == uiObj.buttonsDBselection) {
//console.log('buttonDB dbSelection: found match:',i,uiObj.buttonsDBselection);
return dbSelection[i]=1;
};
});
const buttonsDB = setup_buttons(svg,"btnSet1",dims,offsets,dbSelection,labels,btnText,"DB");
dims = [830,0,20,20,20];
offsets= [20,40];
labels= ['OffOn'];
btnText = ['All Years'];
dbSelection__tmp= [1];
var buttonAllYears = setup_buttons(svg,"btnSet2",dims,offsets,dbSelection__tmp,labels,btnText,['All Years]']);
//-- coding graphs, map 1st
const draw_map01 = (volcano, land, svg, allMax) => {
const thisVolc = Object.values(volcano);
//parse data
thisVolc.forEach(function(d) {
d.coords = [+d.Lon, +d.Lat];
d.magnitude = +d["SO2 kt"];
d.date = parseDate(d["Time Eruption"]);
});
// also for time-slice version include sorting to avoid hidden circles
// that are masked by nearby stronger volcanos drawn w/ bigger circles
thisVolc.sort(function(a, b) {
return a.magnitude > b.magnitude ? -1 : 1;
});
// scale incoming data:
//
// Radius scale
//v3: var rScale = d3.scale.sqrt()
var rScale = d3.scaleSqrt()
//non-unified (i.e. individual db's) scaling of magnitude
.domain(d3.extent(thisVolc, function(d) {
return d.magnitude; }))
// unified scaling:
.domain([0, allMax])
.range([1, 60]);
// Color scale/marker
//v3: var colorScale = d3.scale.ordinal()
var colorScale = d3.scaleOrdinal()
.domain(d3.extent(thisVolc, function(d) {
return d.Volcano; }))
.range(d3.schemeSet3);
// draw land path 1st
//v3: var land = svg.select('g.map').selectAll('path.land').data([land]);
var land = svg.select('g.map').append('path').classed('land', true).data([land]);
//v3: land.enter().append('path').classed('land', true);
//v3: land.enter().classed('land', true);
land.enter();
land.attr('d', pathGenerator);
land.exit().remove();
// bind first data
var theData = thisVolc;
var circles = svg.select('g.dots').selectAll('circle.volcano_map').data(theData);
circles.enter().append('circle')
.classed('volcano_map', true)
.attr('cx', function(d) {
return projection(d.coords)[0];
})
.attr('cy', function(d) {
return projection(d.coords)[1];
})
.attr('r', function(d) { return rScale(d.magnitude); })
.attr('fill', function(d) { return colorScale(d.Volcano); })
.attr('id', function(d) { return d.Volcano;} );
circles.exit().remove();
// add legend if not yet drawn:
var legExists = document.getElementById('unifiedMapLegend')
if(!legExists) map_legend_magnitude_size_circles(svg,"#svgTAR",rScale);
var legExists = document.getElementById('unifiedMapLegend')
// map title
const map_title= d3.select('g.map').append("text")
.attr("id","map-title")
.attr("x",0)
.attr("y",0)
.attr("transform", "translate("+map_title_x_pos+","+map_title_y_pos+")")
.text("Global Distribution of Volcanic Eruptions");
console.log('draw_map01: drawing finshed -----------------------------------');
return colorScale;
}; // const draw_map01
const draw_ts = (volcano, thisYear, colors, allMax) => {
// note, nVolcs is different for same slice in the two types of graphs!
let nVolcs_ts = numberVolcanos(volcano);
// access object records key/vals
let tsVolc = Object.values(volcano);
//v3: const x = d3.time.scale(),
//v3: y = d3.scale.linear(),
const x = d3.scaleTime(),
y = d3.scaleLinear(),
x_label = "X", y_label = "Y";
const series = d3.select('g.ts')
.attr("id","container-ts")
.attr("x",0)
.attr("y",0);
const x_axis = series.append("g")
.attr("class", "xaxis")
.attr("transform", "translate(0," + ts_height + ")")
.attr('stroke', 'none');
const y_axis = series.append("g")
.attr("class", "yaxis")
.attr("transform", "translate(0,0)");
// draw axis, scale first
x.range([0, ts_width]);
// ensure x axis scaling to be fixed for all days of year in case of individual years
// otherwise ticks fidget according actual length of time record (of no data for first
// month do not exist, for instance, these month are not considered on axis)
if (uiObj.buttonAllYears == 'on') {
const mindate = new Date(1998,0,1),
maxdate = new Date(2012,11,31);
x.domain([mindate, maxdate]);
//v3: x_axis.call(d3.svg.axis().scale(x).orient("bottom").ticks(15));
x_axis.call(d3.axisBottom(x).ticks(15));
} else {
const mindate = new Date(thisYear,0,1),
maxdate = new Date(thisYear,11,31);
x.domain([mindate, maxdate]);
//v3: x_axis.call(d3.svg.axis().scale(x).orient("bottom").tickFormat(d3.time.format("%B")));
x_axis.call(d3.axisBottom(x).tickFormat(d3.timeFormat("%B")));
}
y.range([ts_height, 0]);
y.domain([0, 25]);
//v3: y_axis.call(d3.svg.axis().scale(y).orient("left").ticks(6));
y_axis.call(d3.axisLeft(y).ticks(6));
// now rotate text on x axis
// solution based on idea here: https://groups.google.com/forum/?fromgroups#!topic/d3-js/heOBPQF3sAY
// first move the text left so no longer centered on the tick
// then rotate up to get 45 degrees.
x_axis.selectAll("g.xaxis text") // select all the text elements for the xaxis
.attr("transform", function(d) {
// fit labels to 1st Jan tick marks:
//return "translate(" + this.getBBox().height*-1 + "," + this.getBBox().height*1.4 + ")rotate(-45)";
// fit labels to interval:
return "translate(" + this.getBBox().height*1.1 + "," + this.getBBox().height*1.2 + ")rotate(-45)";
});
//parse data
tsVolc.forEach(function(d) {
d.magnitude = +d["SO2 kt"];
});
// re-order by magnitude
tsVolc.sort(function(a, b) {
return a.magnitude > b.magnitude ? -1 : 1;
});
// Radius scale
//v3: var rScale = d3.scale.sqrt()
var rScale = d3.scaleSqrt()
.domain([0, allMax])
.range([1, 10]);
// draw time-series data ("object binding")
let ts_circles = svg.select('g#container-ts.ts').selectAll('circle').data(tsVolc);
ts_circles.enter().append('circle')
.classed('volcano_ts', true)
.attr('cx', function(d) {
return x(get_time(d));
})
.attr('cy', function(d) {
return y(d["Max Plume Height km"]);
})
.attr('r', function(d) {
return rScale(d.magnitude);
})
.attr('fill', function(d) { return colors(d.Volcano); })
.attr('stroke-width', 0.4)
.attr('stroke', gray333)
.attr('id', function(d) { return d.Volcano;} );
ts_circles.exit().remove();
// time series title
const ts_title= d3.select('g.ts').append("text")
.attr("id","ts-title")
.attr("x",0)
.attr("y",0)
.attr("transform", "translate("+ts_title_x_pos+","+ts_title_y_pos+")")
.text("Corresponding Time-Series and Maximum Injection Altitudes");
// axis labels
d3.select('g.ts').append("text")
.attr("id","ts-y-label")
.attr("x",0)
.attr("y",0)
.attr("transform", "translate(-31,87)rotate(-90)")
.text("Altitude [km]");
var legExists = document.getElementById('unifiedTSLegend')
if(!legExists) ts_legend_magnitude_size_circles(svg,"#svgTAR",rScale);
var legExists = document.getElementById('unifiedTSLegend')
}; // const draw_ts
//-- end viz main section - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
function get_time(obj) {
//v3: return d3.time.format.iso.parse(obj["Time Eruption"]);
return d3.isoParse(obj["Time Eruption"]);
}
//-- complementary functions - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
//outdated function setup_slider(thisSVG,mapID,yearArr,iniVal) {
function setup_slider_title(thisSVG,mapID,yearArr,iniVal) {
//outdated const addSlider = d3.select(mapID).append("input")
//outdated .attr("id", "slider_input")
//outdated .attr("class", "slider_input_volc")
//outdated .attr("type", "range")
//outdated .attr("min", 0)
//outdated .attr("max", yearArr.length-1)
//outdated .attr("step", 1)
//outdated .attr("value", iniVal)
//outdated .attr("transform", "translate(0,0)");
const slider_static_title = svg.append("g")
.attr("class", "slider_title")
.attr("transform", "translate("+slider_label_x_pos+","+slider_label_y_pos+")")
.append("text")
.attr("font-size",14)
.attr("fill",orange02)
.text("Number of Volcanos:");
const slider_dyn_title = d3.select('g.slider_title').append("text")
.attr("id","textSliderDynToggle")
.attr("x",140)
.attr("y",0)
.attr("font-size",14)
.attr("fill",orange02)
.text("[numberEruptions]")
//outdated return addSlider;
};
function slider_axis_scale(svg,yearArr) {
//v3: const slider_scale = d3.scale.ordinal()
// const slider_scale = d3.scaleOrdinal()
// .domain(yearArr)
//v3: .rangeRoundBands([0, 600], .1);
const slider_scale = d3.scaleBand()
.domain(yearArr)
.range([0, 600], .1);
//v3: const slider_axis = d3.svg.axis()
//v3: .scale(slider_scale)
//v3: .orient("bottom");
const slider_axis = d3.axisBottom(slider_scale);
svg.append("g")
.attr("class", "slider_axis")
.attr("transform", "translate(" + (width - margin.slider) + ",45)")
.call(slider_axis);
};
function setup_slider_dyn_text(mapID,nEruptions) {
// applying method of 'update_button_text_reference'
d3.select("#textSliderDynToggle").text(nEruptions);
};
// taken from map10 and tidied up:
function setup_buttons(svg,btnID,dims,offsets,dbSelection,labels,btnText,choseText) {
// dimensional settings
const bWidth = 20; //button width
const bHeight= 20; //button height
const x0= 20; //x offset
const y0= 20; //y offset
//fontawesome button labels possible
//container for all buttons
const allButtons= svg.append("g")
.attr("id",btnID)
.attr("transform", "translate("+dims[0]+","+dims[1]+")");
//groups for each button (holding a rect and text)
const buttonGroups= allButtons.selectAll("g.button")
.data(labels)
.enter()
.append("g")
.attr("class",function(d,i) {
console.log('setup_buttons: adding classes: ',d,i);
if(dbSelection[i]==1) {
return "button default";
} else {
return "button";
};
})
.style("cursor","pointer")
.on("mouseover", function() {
if (d3.select(this).select("rect").attr("fill") != btn_pressedColor) {
d3.select(this)
.select("rect")
.attr("fill",btn_hoverColor);
}
})
.on("mouseout", function(d,i) {
console.log('setup_buttons: mouseout: ',d,i);
if (d3.select(this).select("rect").attr("fill") != btn_pressedColor) {
d3.select(this)
.select("rect")
.attr("fill",btn_defaultColor);
}
})
//adding a rect to each toggle button group
//rx and ry give the rect rounded corner
buttonGroups.append("rect")
.attr("class","buttonRect")
.attr("width",bWidth)
.attr("height",bHeight)
.attr("x",x0)
.attr("y",function(d,i) {
return offsets[i];
})
.attr("rx",5)
.attr("ry",5)
.attr("fill",btn_defaultColor)
.attr("stroke",gray69)
.attr("stroke-width","1");
// highlight preselection with correct button shade (btnSet1 only):
const defaultButton = allButtons.selectAll("g.button.default").select("rect");
defaultButton.attr("fill",btn_pressedColor);
// add button description
additional_button_text(buttonGroups,bWidth,dims,offsets,btnText,choseText);
if(choseText == "DB") {
const ypos = 170;
const y0= 30;
const refText1= d3.select('g#btnSet1').append("text")
.attr("id","textRef")
.attr("x",20)
.attr("y",ypos)
.attr("fill",orange02)
.attr("font-size",12)
.text("Reference:");
update_button_text_reference(dbSelection,choseText);
};
return buttonGroups;
};
function updateButtonColors(button, parent) {
parent.selectAll("rect")
.attr("fill",btn_defaultColor)
button.select("rect")
.attr("fill",btn_pressedColor)
};
function updateOnOffButtonColors(button){
var currentColor =button.select('rect').style("fill");
currentColor = currentColor == orange02 ? btn_defaultColor : orange02;
const showAllYears = currentColor == orange02 ? 'on' : 'off';
return [button.select('rect').style("fill", currentColor),showAllYears];
};
function additional_button_text(buttons,bWidth,dims,offsets,btnText,choseText) {
const description1= d3.select('g#btnSet1').append("text")
.attr("id","textButtonTitle")
.attr("x",14)
.attr("y",0)
.attr("fill",orange02)
.attr("font-size",14)
.text("Choose Dataset");
const description2= buttons.select('g#btnSet1')
.data(btnText, function(d) { return d; }).enter()
.append('text')
.attr("id","dbDescription")
.attr("x",60)
// .attr("y",20)
.attr("y", function(d,i) {
return 15+offsets[i];
})
.attr("fill",orange02)
.attr("font-size",12)
.text(function(d,i) {
return d;
});
//pre-define text that the DB radio buttons will toggle
if(choseText == "DB") {
const ypos = 170;
const number = d3.select('g#btnSet1').append("text")
.attr("id","textRefToggle")
.attr("x",20)
.attr("y",ypos+dims[3])
.attr("fill",orange02)
.attr("font-size",12)
.text("[click a button]") // <--- just inital something
}
};
function update_button_text_reference(dbSelection,choseText) {
// does this still has any purpose?
if(choseText == "DB") {
dbRefs.forEach(function(d,i) {
//console.log('update_button_text_reference: dbRefs: d,i: ',d,i);
//console.log('update_button_text_reference: dbRefs: dbSelection[i]: ',dbSelection[i]);
if (dbSelection[i] == 1) addText = d;
});
} else {
throw '- Error update_button_text_reference -';
};
console.log('update_button_text_reference: dbSelection,thisText:',dbSelection,choseText);
console.log('update_button_text_reference: addText:',addText);
// update reference on button toggle (also set default):
const refText2= d3.select("#textRefToggle").text(addText)
};
//-- generalized mapping utilities:
function map_legend_magnitude_size_circles(thisSVG,thisID,thisScale,){
var circleRadii = [5000, 1000, 100]; // representing well VolcDB1 magnitudes
// convert radii array into scaled values:
var dy = [];
circleRadii.forEach(function(d) {
dy.push(thisScale(d));
});
// draw legend circles:
thisSVG.append('g').classed('legend' , true)
.attr("id","unifiedMapLegend")
.attr("transform", "translate("+map_legend_x_pos+","+map_legend_y_pos+")")
var legCircles = thisSVG.selectAll('g.legend').selectAll('g.legendCell')
.data(circleRadii, function(d) { return d; })