-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscatterPlot.js
804 lines (701 loc) · 26 KB
/
scatterPlot.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
// "use strict"
//global variables
var mergedData;
var xMin,xMax,yMin,yMax;
var durationMin,durationMax,pupilMin,pupilMax,timeMin,timeMax;
var vertices = []; //to draw convex hull
//scales
var xScale, yScale, timeScale;
var rScale = d3.scaleLinear()
.range([3,23]);
var colorScale = d3.scaleLinear()
.range(['#0066ff', '#d0ff00', '#f00000'])
.interpolate(d3.interpolateHcl);
//svg
var svgDiv;
var svg, svgWidth, svgHeight;
var svgRatio = 4/6;
//view option
var ViewOption = {
XY: 1,
TIMEDURATION: 2,
TIMESACCADE: 3
};
var currentViewOption = ViewOption.XY;
//sliders
var timeSlider = d3.select('#timeRange');
function updateTimeLabel(val) {
d3.select('#timeLabel').text(val);
}
var durationSlider = d3.select('#durationSlider');
var pupilSlider = d3.select('#pupilSlider');
var basicOpacity = 0.8;
var highlightOpacity = 0.8;
var mutedOpacity = 0.01;
const delayValue = 0.3;
// Initial document setup
document.addEventListener('DOMContentLoaded', function(){
console.log('DOM content loaded. Initiating all setups.');
//setting global vars and drawing csv
svgDiv = document.getElementById("svgDiv");
svgWidth = +svgDiv.offsetWidth;
svgHeight = +svgDiv.offsetHeight;
svg = d3.select("#svgDiv")
.append("svg")
.attr("width", '100%')
.attr("height", '100%')
.attr("id", "drawnSvg");
svg.append('g').attr('id','plotG');
svg.append('g').attr('id','guideG');
drawLegends();
fetchCsvCallOthers();
});
window.addEventListener('resize', resizeSVG);
function resizeSVG(){
//update the svg size
svgWidth = +svgDiv.offsetWidth;
svgHeight = +svgDiv.offsetHeight;
console.log('Resizing svg w:'+svgWidth+' h:'+svgHeight
+' currentViewOption:'+currentViewOption);
//update the plot locations according to the resized svg
switch(currentViewOption) {
case ViewOption.XY:
viewByXY();
break;
case ViewOption.TIMEDURATION:
viewByTimeAndDuration();
break;
default:
}
}
/**
* Updates the time slider
* Updates time label
*/
function resetTime() {
maxTimeInMs = Math.round(timeMax/1000);
timeSlider.attr('max', maxTimeInMs);
timeSlider.attr('value', maxTimeInMs);
$("#timeRange").val(maxTimeInMs)
updateTimeLabel(formatToMinuteSecond(timeMax));
//timeSlider.attr("visibility", "hidden");
}
// Fetches the csv, calls other functions
function fetchCsvCallOthers()
{
showTimeSlider(false);
clearAllFilters();
console.log('fetching csv data.');
var drawnSvg = document.getElementById("drawnSvg");
//removing previously drawn circles
if(drawnSvg != undefined) {
d3.select('#svgDiv').select('#plotG').selectAll('*').remove();
d3.select('#svgDiv').select('#guideG').selectAll('*').remove();
}
var file = dataSetToLoad();
d3.csv(file)
.then(function(data){
//converting all rows to int
data.forEach(function(d,i) {
d.number = +d.number;
d.time = +d.time;
d.duration = +d.duration;
d.x = +d.x;
d.y = +d.y;
// d.avg_dilation = +d.avg_dilation; //not convert to number in order to detect nan value!
});
mergedData = data;
setScales(mergedData);
render(mergedData);
resetTime();
});
}
// Returns file by checking which data set to load from radio buttons
function dataSetToLoad()
{
d3.select('#dataOption').selectAll('label').classed('active', false);
if(document.getElementById("treeRadio").checked) {
console.log('tree data selected.');
d3.select('#treeBtn').classed('active', true);
return "./data_preprocessed/merged_dilation_fixation_tree.csv";
} else {
console.log('graph data selected.');
d3.select('#graphBtn').classed('active', true);
return "./data_preprocessed/merged_dilation_fixation_graph.csv";
}
}
// Sets the scales for x, y coordinates, duration and avg_dilation
function setScales(data)
{
console.log('setting scales.');
const xValue = d => d.x;
const yValue = d => d.y;
const durationValue = d => d.duration; // plot size
const pupilValue = d => +d.avg_dilation; // plot color
const timeValue = d => d.time;
xMax = d3.max(data, xValue);
xMin = d3.min(data, xValue);
console.log('x '+xMin+' : '+xMax);
yMax = d3.max(data, yValue);
yMin = d3.min(data, yValue);
console.log('y '+yMin+' : '+yMax);
durationMax = d3.max(data, durationValue);
durationMin = d3.min(data, durationValue);
console.log('duration '+durationMin+' : '+durationMax);
pupilMax = d3.max(data, pupilValue);
pupilMin = d3.min(data, pupilValue);
console.log('pupil '+pupilMin+' : '+pupilMax);
timeMax = d3.max(data, timeValue);
timeMin = d3.min(data, timeValue);
console.log('time '+timeMin+' : '+timeMax);
xScale = d3.scaleLinear()
.domain([0, xMax])
.range([0+20, svgWidth-50])
.nice();
yScale = d3.scaleLinear()
.domain([0, yMax])
.range([0+20, svgHeight-50])
.nice();
rScale.domain([100, durationMax]).nice();
colorScale.domain([0, 0.4, 1]); //fixed with exagerated changes
// .domain([0, (pupilMin+pupilMax)/2, pupilMax]) //show the distribution as it is
// .domain([0, pupilMax*0.4, pupilMax]) //bit distorted
timeScale = d3.scaleLinear()
.domain([0, timeMax])
.range([0, 10])
.nice();
timeSlider.attr('max',timeMax/1000); //set time slider range
}
// Draws circle points
function render(dataset)
{
console.log('drawing circles.');
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltipDiv")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.text("");
var plotG = svg.select('#plotG');
var convexhull = plotG.append("polygon")
.attr('id','convexhull')
.attr("class", "hull");
//put scaled d.x and d.y into vertices
vertices = [];
mergedData.forEach(function(d,i){
vertices[i] = [xScale(d.x), yScale(d.y)]; //for convex hull
});
convexhull.datum(d3.polygonHull(vertices))
.attr("points", function(d) { return d.join(" "); });
showConvexhull(false);
showSaccades(true);
// Bind dataset to lines (for saccades)
var saccades = plotG.selectAll("line")
.data(dataset, function(d) {return d;}); //semantic binding
// Add lines(saccades)
saccades.enter().append("line")
.classed('saccade', true)
.attr('x1', function(d,i){
var prev = (i>0) ? dataset[i-1] : d;
return xScale(prev.x);
})
.attr('y1', function(d,i){
var prev = (i>0) ? dataset[i-1] : d;
return yScale(prev.y);
})
.attr('x2', d => xScale(d.x))
.attr('y2', d => yScale(d.y))
.attr('visibility','hidden')
.transition()
.delay(function(d, i){
return timeScale(i*d.time);
})
.attr("visibility", "visible");
// Bind dataset to circles (for fixations)
var fixations = plotG.selectAll("circle")
.data(dataset, function(d) { return d; }); //semantic binding
// Add circles(fixations)
fixations.enter().append("circle")
.classed('fixation', true)
.attr("cx", d => xScale(d.x))
.attr("cy", d => yScale(d.y))
.attr("r", d => rScale(d.duration))
.attr("fill", function(d){
return (d.avg_dilation=="") ? 'darkgray' : colorScale(+d.avg_dilation);
})
.on('mouseover', function(d) {
const msg = "<b>#" + d.number + "</b><br>"
+ "<b>time</b> " + formatToMinuteSecond(d.time) + "<br>"
+ "<b>x</b>:" + d.x+", <b>y</b>:"+d.y + "<br>"
+ "<b>duration</b> " + d.duration + "ms <br>"
+ "<b>dilation</b> "
+ ((d.avg_dilation=="") ? "nan" : ((+d.avg_dilation).toFixed(2)+"mm"));
tooltip.html(msg);
tooltip.style("visibility", "visible");
})
.on("mousemove", function(d, i) {
return tooltip.style("top",
(d3.event.pageY-10)+"px")
.style("left",(d3.event.pageX+10)+"px");
})
.on('mouseout', function(d, i){
tooltip.style("visibility", "hidden");
d3.select('#details').html('');
})
.attr("visibility","hidden")
.transition()
.delay(function(d, i){
return timeScale(i*d.time);
})
.attr("visibility", "visible")
.end()
.then(() =>{
showTimeSlider(true);
});
//initial mode to xy
drawXYMark();
currentViewOption = ViewOption.XY;
d3.select('#viewOptions').selectAll('button').classed('active', false);
d3.select('#viewOption-xy').classed('active', true);
}
// Update the location of each fixation and saccade
function updateXYLocations()
{
var fixations = svg.select('#plotG').selectAll('circle');
fixations
.attr("cx", d => xScale(d.x))
.attr("cy", d => yScale(d.y))
.attr("r", d => rScale(d.duration));
var saccades = svg.select('#plotG').selectAll('line');
saccades
.attr('x1', function(d,i){
var prev = (i>0) ? mergedData[i-1] : d;
return xScale(prev.x);
})
.attr('y1', function(d,i){
var prev = (i>0) ? mergedData[i-1] : d;
return yScale(prev.y);
})
.attr('x2', d => xScale(d.x))
.attr('y2', d => yScale(d.y));
var convexhull = svg.select('#convexhull');
//put scaled d.x and d.y into vertices
vertices = [];
mergedData.forEach(function(d,i){
vertices[i] = [xScale(d.x), yScale(d.y)]; //for convex hull
});
convexhull.datum(d3.polygonHull(vertices))
.attr("points", function(d) { return d.join(" "); });
}
//Show or hide convexhull, or disable the checkbox
function showConvexhull(state)
{
var convexhull = svg.select('#convexhull');
var checkbox = document.getElementById('convexhullCheckbox');
if(state == true) {
console.log('show convexhull');
convexhull.style('visibility', 'visible');
checkbox.disabled = false;
checkbox.checked = true;
} else if(state == false) {
console.log('hide convexhull');
convexhull.style('visibility', 'hidden');
checkbox.disabled = false;
checkbox.checked = false;
} else if(state == "disable") {
convexhull.style('visibility', 'hidden');
checkbox.disabled = true;
checkbox.checked = false;
}
}
// Show or hide saccades, or disable the checkbox
function showSaccades(state)
{
var saccades = svg.select('#plotG').selectAll('line');
var checkbox = document.getElementById('saccadeCheckbox');
if(state == true) {
console.log('show saccades');
saccades.style('visibility', 'visible');
checkbox.disabled = false;
checkbox.checked = true;
} else if(state == false) {
console.log('hide saccades');
saccades.style('visibility', 'hidden');
checkbox.disabled = false;
checkbox.checked = false;
} else if(state == "disable") {
saccades.style('visibility', 'hidden');
checkbox.disabled = true;
checkbox.checked = false;
}
}
// Show or hide timeSlider and lable visible
function showTimeSlider(show){
if(show == true) {
timeSlider.style("visibility", "visible");
d3.select('#timeLabel').style("visibility", "visible");
} else {
timeSlider.style("visibility", "hidden");
d3.select('#timeLabel').style("visibility", "hidden");
}
}
// TODO: Filter with a range of values (double thumbs on the slider)
// Filters fixations by feature
function filterByFeature(feature, val, step)
{
showSaccades("disable");
showConvexhull("disable");
if ( !(feature=='duration' || feature=='avg_dilation') ) {
console.log('not existing feature '+feature);
return;
}
//setting the value of the slider
if(feature == "duration")
d3.select("#durationSlider").attr("value", val);
else if(feature = "avg_dilation")
d3.select("#pupilSlider").attr("value", val);
//actual filter changed
var selected = +val;
var inclusiveVal = step/2;
var start = selected - inclusiveVal;
var end = selected + inclusiveVal;
console.log(`filtering by ${feature} ${start.toFixed(3)} ~ ${end.toFixed(3)}`);
var otherSelected, otherInclusiveVal, otherStart, otherEnd, otherFeature, otherSlider;
//the other filter
if(feature == "duration"){
//getting value of pupilSlider
otherFeature = "avg_dilation";
otherSlider = d3.select("#pupilSlider");
otherSelected = +otherSlider.attr("value");
otherInclusiveVal = otherSlider.attr("step")/2;
otherStart = otherSelected - otherInclusiveVal;
otherEnd = otherSelected + otherInclusiveVal;
console.log(`filtering by ${otherFeature} ${otherStart.toFixed(3)} ~ ${otherEnd.toFixed(3)}`);
}
else if(feature == "avg_dilation"){
//getting value of durationSlider
otherFeature = "duration";
otherSlider = d3.select("#durationSlider");
otherSelected = +otherSlider.attr("value");
otherInclusiveVal = otherSlider.attr("step")/2;
otherStart = otherSelected - otherInclusiveVal;
otherEnd = otherSelected + otherInclusiveVal;
console.log(`filtering by ${otherFeature} ${otherStart.toFixed(3)} ~ ${otherEnd.toFixed(3)}`);
}
// Make selected data stand out
svg.select('#plotG').selectAll('circle')
.style('opacity', mutedOpacity)
.filter(function(d) {
return (((d[feature] >= start) && (d[feature] <= end)) && ((d[otherFeature] >= otherStart) && (d[otherFeature] <= otherEnd)));
})
.style('opacity', highlightOpacity);
//TODO: Mark the active filter on the slider.. give class to it.
var legendSvg;
if (feature =='duration') {
legendSvg = d3.select('#svgDurationSlider');
} else if (feature=='avg_dilation') {
legendSvg = d3.select('#svgPupilSlider');
}
// legendSvg.select('.steps').selectAll('circle')...
}
// Convert milli seconds to M:SS form
function formatToMinuteSecond(milliSeconds) {
var minutes = Math.floor(milliSeconds / 60000);
var seconds = ((milliSeconds % 60000) / 1000).toFixed(0);
return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}
function filterByTime(val) {
showConvexhull('disable');
timeSlider.attr('value', val);
var milliSeconds = val * 1000;
updateTimeLabel(formatToMinuteSecond(milliSeconds));
// svg.select('#plotG').selectAll('circle')
// .style('opacity', mutedOpacity)
// .filter(function(d) {
// return (d.time <= val);
// })
// .style('opacity', highlightOpacity);
svg.select('#plotG').selectAll('circle')
.style('visibility', 'hidden')
.filter(function(d) {
return (d.time <= milliSeconds);
})
.style('visibility', 'visible');
if(document.getElementById('saccadeCheckbox').checked == true)
{
svg.select('#plotG').selectAll('line')
.style('visibility', 'hidden')
.filter(function(d) {
return (d.time <= milliSeconds);
})
.style('visibility', 'visible');
}
}
// Removes filter effect when double clicked on document
function clearAllFilters() {
console.log('clearing all filters.');
//set show options to default
showSaccades(false);
showConvexhull(false);
//changes actual value of sliders
d3.select("#pupilSlider").attr("value", 0);
d3.select("#durationSlider").attr("value", 0);
//changes the view of sliders
$("#pupilSlider").val(0);
$("#durationSlider").val(0);
//show all plots evenly
svg.selectAll('circle')
.style('opacity', basicOpacity);
//TODO: Clear the marks on the legend sliders
}
// Draws legends with circles and scales under sliders
function drawLegends()
{
console.log('drawing svg under legends.');
const sliderLength = 120;
const gOffset = { x:25, y:25 };
const scaleX = d3.scaleLinear().range([0, sliderLength]);
// 1. Fixation Duration Legend
const durationG = d3.select('#svgDurationSlider').append('g')
.attr('transform',`translate(${gOffset.x},${gOffset.y})`);
const durCircles = [0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2];
const durStepDots = [0, 0.5, 1, 1.5, 2];
const durStepTexts = [0, 1, 2];
scaleX.domain([0, d3.max(durCircles)]);
const scaleSize = rScale.domain([0, 2]);
//back circles
durationG.selectAll('circle')
.data(durCircles).enter().append('circle')
.attr('cx', d => scaleX(d))
.attr('r', d => scaleSize(d)) //the size legend
.style('fill', '#CCC');
//lines for the slider
durationG.append('line').attr('x2',sliderLength);
durationG.insert('g').attr('class','steps')
.selectAll('circle')
.data(durStepDots).enter().append('circle')
.attr('cx', d=> scaleX(d));
durationG.select('.steps').selectAll('text')
.data(durStepTexts).enter().append('text')
.attr('x', d=> scaleX(d))
.attr('y', 18) //how far the numbers away from line
.text(d => {return d;});
durationG.select('.steps').append('text')
.attr('x', sliderLength+9).attr('y', 18)
.text('s');
// 2. Pupil Dilation Legend
const pupilG = d3.select('#svgPupilSlider').append('g')
.attr('transform',`translate(${gOffset.x},${gOffset.y})`);
const pupilCircles = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];
const pupilStepDots = [0, 0.25, 0.5, 0.75, 1];
const pupilStepTexts = [0, 1];
scaleX.domain([0, d3.max(pupilCircles)]);
const scaleColor = colorScale.domain([0, 0.3, 1]);
//back circles
pupilG.selectAll('circle')
.data(pupilCircles).enter().append('circle')
.attr('cx', d => scaleX(d))
.attr('r', 14)
.style('fill', d => scaleColor(d)); //the color legend
//lines for the slider
pupilG.append('line').attr('x2',sliderLength);
pupilG.insert('g').attr('class','steps')
.selectAll('circle')
.data(pupilStepDots).enter().append('circle')
.attr('cx', d=> scaleX(d));
pupilG.select('.steps').selectAll('text')
.data(pupilStepTexts).enter().append('text')
.attr('x', d=> scaleX(d))
.attr('y', 25) //how far the numbers away from line
.text(d => {return d;});
pupilG.select('.steps').append('text')
.attr('x', sliderLength+15).attr('y', 25)
.text('mm');
}
//TODO: Implement play
function play() {
console.log('play through time');
var i;
for(i=0; i<Math.round(timeMax/1000); i++) {
// timeSlider.value(i);
setTimeout(function(){
filterByTime(i);
console.log(i);
}, 1000);
//NOTE: not working yet...
}
}
// Locates fixations back to its x,y coordinates
function viewByXY()
{
console.log('locating fixations by x-y.');
//Update the view state and the button view
var isViewChanged = (currentViewOption != ViewOption.XY);
currentViewOption = ViewOption.XY;
d3.select('#viewOptions').selectAll('button').classed('active', false);
d3.select('#viewOption-xy').classed('active', true);
//Update the x,y scale to fit the resized svg
xScale.range([0+20, svgWidth-50]);
yScale.range([0+20, svgHeight-50]);
//Relocate the fixations
var plotG = d3.select('#plotG');
var fixations = plotG.selectAll('circle');
var saccades = plotG.selectAll('line');
var convexhull = plotG.select('#convexhull');
showTimeSlider(true);
showSaccades(true);
showConvexhull(true);
if(isViewChanged) {
//transition effect for changing view
drawXYMark();
fixations.transition()
.delay(function(d,i){ return i * delayValue; })
.ease(d3.easeCubic).duration(1000)
.style('visibility','visible')
// .style('opacity', basicOpacity)
.attr('cx', d => xScale(d.x))
.attr('cy', d => yScale(d.y))
.attr('r', d => rScale(d.duration));
saccades
.style('opacity',0)
.transition().delay(1000)
.style('visibility','visible')
.transition().duration(1000)
.style('opacity',0.3);
convexhull
.style('opacity',0)
.transition().delay(1000)
.style('visibility','visible')
.transition().duration(1000)
.style('opacity',1);
} else {
//just resizing the svg. update the locations!
updateXYLocations();
fixations.style('visibility','visible')
saccades.style('visibility','visible');
}
}
// Draws xy arrow marks on the svg
function drawXYMark()
{
const guideG = svg.select('#guideG');
guideG.selectAll('*').remove(); //remove all previously drawn guides
//NOTE: this can be imported from svg file
guideG.attr('transform','translate(5,5)');
var len = 50;
var xAxis = guideG.append('g').attr('transform',`translate(5, 0)`);
xAxis.append('line').attr('x2',len);
xAxis.append('line').attr('x2',-5).attr('y2',-3)
.attr('transform',`translate(${len}, 0)`);
xAxis.append('text').text('x')
.attr('transform',`translate(${len+8}, 4)`);
var yAxis = guideG.append('g').attr('transform',`translate(0, 5)`);
yAxis.append('line').attr('y2',len);
yAxis.append('line').attr('x2',-3).attr('y2',-5)
.attr('transform',`translate(0, ${len})`);
yAxis.append('text').text('y')
.attr('transform',`translate(0, ${len+12})`);
guideG.selectAll('line').classed('axis-line',true);
guideG.selectAll('text').classed('axis-stepText',true);
//transition
guideG.attr('opacity',0)
.transition().duration(1000)
.attr('opacity',1);
}
function viewByTimeAndDuration()
{
console.log('View the line graph of time and duration.');
//Update the view state and the button view
var isViewChanged = (currentViewOption != ViewOption.TIMEDURATION);
currentViewOption = ViewOption.TIMEDURATION;
d3.select('#viewOptions').selectAll('button').classed('active', false);
d3.select('#viewOption-timeduration').classed('active', true);
const marginX = 50; //left&right margin of graph from the svg border
// const marginY = 200; //top&bottom margin of graph from the svg border
const width = svgWidth - marginX*2;
const height = 200;
const marginY = (svgHeight-height)/2;
const plotG = d3.select('#plotG');
const fixations = plotG.selectAll('circle');
const saccades = plotG.selectAll('line');
saccades.transition().duration(500)
.style('opacity',0).style('visiblity','hidden');
const saccadeCheckbox = document.getElementById('saccadeCheckbox');
saccadeCheckbox.checked = false;
saccadeCheckbox.disabled = true;
var scaleX = d3.scaleLinear()
.domain([0, timeMax/60000])
.range([marginX, svgWidth-marginX]);
var scaleY = d3.scaleLinear()
.domain([0, durationMax/1000])
.range([svgHeight-marginY, marginY]);
var xAxis = d3.axisBottom().scale(scaleX);
var yAxis = d3.axisLeft().scale(scaleY).ticks(5);
var guideG = svg.select('#guideG');
// if(isViewChanged)
// {
showSaccades("disable");
showConvexhull("disable");
//remove preciously drawn guide or axes
guideG.selectAll('*').remove();
//Draw Axes
guideG.classed('axis',true)
.classed('unselectable', true);
//draw x axis
guideG.append('g').attr('id','xAxis')
.attr("transform", `translate(0, ${svgHeight-marginY})`)
.call(xAxis);
//x axis label
guideG.append("text").attr('id','xAxisLabel')
.attr("text-anchor", "end")
.attr("transform", `translate(${svgWidth-marginX},${svgHeight-marginY+35})`)
.text("time (min)");
//draw y axis
guideG.append('g').attr('id','yAxis')
.attr("transform", `translate(${marginX}, 0)`)
.call(yAxis);
//y axis label
guideG.append("text").attr('id','yAxisLabel')
.attr("text-anchor", "end")
.attr("transform", `translate(${marginX-30},${marginY}) rotate(-90)`)
.text("duration (sec)");
//transition
guideG.attr('opacity',0)
.transition().delay(1000).duration(1000)
.attr('opacity',1);
//Move the fixations
scaleX.domain([0, timeMax]);
scaleY.domain([0, durationMax]);
fixations.style('visibility','visible')
.transition()
.delay(function(d,i){ return i * delayValue; })
.ease(d3.easeCubic).duration(1000)
.attr('cx', d => scaleX(d.time))
.attr('cy', d => scaleY(d.duration))
.attr('r', 3);
//Calculate the mean duration
const durationValue = d => d.duration;
var durationMean = d3.mean(mergedData, durationValue);
console.log('Duration Mean: '+durationMean.toFixed(0));
//Show the mean duration
var avgG = svg.select('#guideG')
.append('g').attr('id','avgG')
.attr('transform',`translate(${marginX},${scaleY(durationMean)})`)
.classed('avgG', true);
//mean line
avgG.append('line').attr('x2',0)
.transition().delay(1000).duration(1000)
.attr('x2',width);
//mean label
// avgG.append('text')
// .attr('text-anchor','end')
// .attr('transform',`translate(${width},${scaleY(durationMean)})`)
// .text(`mean ${durationMean.toFixed(2)}`);
// }
// else
// {
// console.log('view by time/duratoin resizing...');
// //TODO: handle resizing
// }
}