-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
2802 lines (2291 loc) · 109 KB
/
index.html
File metadata and controls
2802 lines (2291 loc) · 109 KB
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
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<!-- <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
:root {
--accent-color: #E8336D;
}
html *
{
/* font-size: 1em !important;*/
/*color: #000 !important;*/
font-family: 'Lato' !important;
}
/* body{
background-color: whitesmoke;
}*/
svg {
background-color: white;
font-family: 'Lato';
}
.overlay {
fill: none;
pointer-events: all;
}
.focus circle {
fill: white;
stroke: var(--accent-color);
stroke-width: 2px;
}
.hover-line {
stroke: var(--accent-color);
stroke-width: 2px;
stroke-dasharray: 1,1;
}
.hover-text {
fill: var(--accent-color);
font-weight: bold;
/*font-size: 0.9em;*/
}
.annotation path {
stroke: var(--accent-color);
fill: none;
}
/* .annotation:not(.above):not(.anomaly) path {
stroke-dasharray: 1,3;
}*/
.handle {
visibility: hidden;
}
.annotation path.connector-arrow{
fill: var(--accent-color);
}
.annotation text {
fill: var(--accent-color);
}
.annotation-note-title {
font-weight: bold;
}
.annotation.xythreshold {
cursor: move;
}
.annotation.xythreshold .annotation-subject{
stroke-width: 3px;
}
.annotation.badge path.subject-pointer, .annotation.badge path.subject {
fill: var(--accent-color);
stroke-width: 3px;
stroke-linecap: round;
}
.annotation.badge path.subject-ring {
fill: white;
stroke-width: 3px;
}
.annotation.badge .badge-text {
fill: white;
font-size: .7em;
}
rect.annotation-note-bg {
fill: rgba(255, 255, 255, 0);
/*fill: rgba(0, 0, 0, 0);*/
}
text.hover {
font-size: .7em;
}
text.title {
font-size: 1.2em;
font-weight: bold;
}
my_dataviz1_text {
fill: var(--accent-color);
}
my_dataviz2_text {
fill: var(--accent-color);
}
my_dataviz3_text {
fill: var(--accent-color);
}
my_dataviz4_text {
fill: var(--accent-color);
}
my_dataviz5_text {
fill: var(--accent-color);
}
</style>
<title>Narrative Visualization</title>
</head>
<!-- Body starts here -->
<body onload="myFunction1();">
<div class="container">
<!-- Headline -->
<br>
<h3>Covid-19 Timeline in US</h3>
<br>
<div class="row border border-secondary">
<!-- Buttons -->
<div class="container mt-3">
<div class="btn-group" role="group" aria-label="Basic example">
<button id="but1" type="button" class="btn btn-info" onclick="myFunction1()">1</button>
<button id="but2" type="button" class="btn btn-info" onclick="myFunction2()">2</button>
<button id="but3" type="button" class="btn btn-info" onclick="myFunction3()">3</button>
<button id="but4" type="button" class="btn btn-info" onclick="myFunction4()">4</button>
<button id="but5" type="button" class="btn btn-info" onclick="myFunction5()">5</button>
</div>
</div>
<!-- The chart -->
<div class="container mt-3">
<!-- <p class="text-left">The chart will sit here</p> -->
<div id="main" class="container mt-3">
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!-- D3 JavaScript -->
<script src="https://d3js.org/d3.v4.js"></script>
<!-- Color Scale -->
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<!-- Load d3-annotation -->
<script src="https://rawgit.com/susielu/d3-annotation/master/d3-annotation.min.js"></script>
<script>
//Function to remove Divs
function removeDivs() {
d3.select("#my_dataviz1_outer").remove();
d3.select("#my_dataviz2_outer").remove();
d3.select("#my_dataviz3_outer").remove();
d3.select("#my_dataviz4_outer").remove();
d3.select("#my_dataviz5_outer").remove();
}
//1st chart
function myFunction1() {
removeDivs();
document.getElementById("but1").style.color = "white";
document.getElementById("but2").style.color = "black";
document.getElementById("but3").style.color = "black";
document.getElementById("but4").style.color = "black";
document.getElementById("but5").style.color = "black";
document.getElementById("but2").style.background = "#f1f1f1";
document.getElementById("but3").style.background = "#f1f1f1";
document.getElementById("but4").style.background = "#f1f1f1";
document.getElementById("but5").style.background = "#f1f1f1";
document.getElementById("but1").style.background = "#666";
var div_outer = document.createElement("div");
div_outer.id = "my_dataviz1_outer";
document.getElementById("main").appendChild(div_outer);
var div_text = document.createElement("div");
div_text.id = "my_dataviz1_text";
document.getElementById("my_dataviz1_outer").appendChild(div_text);
var text0 = document.createTextNode("Covid-19 impact in US:");
bold = document.createElement('strong');
bold.appendChild(text0);
document.getElementById("my_dataviz1_text").appendChild(bold);
var brk0 = document.createElement("br");
document.getElementById("my_dataviz1_text").appendChild(brk0);
var text = document.createTextNode("Like many other countries in the world, US is also heavily impacted by Covid-19 (novel coronavirus caused by SARS-CoV2 virus). US is the most impacted country in the world by number of cases and deaths. Till date US has recorded more than 35.5 million cases and more than 600,000 deaths.");
document.getElementById("my_dataviz1_text").appendChild(text);
var brk = document.createElement("br");
document.getElementById("my_dataviz1_text").appendChild(brk);
var brk2 = document.createElement("br");
document.getElementById("my_dataviz1_text").appendChild(brk2);
// var cases_text = document.createElement("p")
// cases_text.id = "cases_text";
// document.getElementById("my_dataviz1_text").appendChild(cases_text);
// var brk3 = document.createElement("br");
// document.getElementById("my_dataviz1_text").appendChild(brk3);
var div = document.createElement("div");
div.id = "my_dataviz1";
document.getElementById("my_dataviz1_outer").appendChild(div);
// set the dimensions and margins of the graph
var margin = {top: 30, right: 60, bottom: 30, left: 60},
width = 1000 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#my_dataviz1")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var div = document.createElement("div");
div.id = "tooltip";
// div.style.position = "absolute";
div.style.padding = "5px";
div.style.backgroundColor = "white";
document.getElementById("my_dataviz1").appendChild(div);
const tooltip = d3.select('#tooltip');
const tooltipLine = svg.append('line');
var bisectDate = d3.bisector(function(d) { return d.date; }).left;
//Read the data
d3.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv",
// When reading the csv, I must format variables:
function(d){
return { date : d3.timeParse("%Y-%m-%d")(d.date), cases : d.cases, deaths : d.deaths }
},
// Now I can use this dataset:
function(data) {
// Add X axis --> it is a date format
var x = d3.scaleTime()
.domain(d3.extent(data, function(d) { return d.date; }))
.range([ 0, width ]);
svg.append("g")
.transition()
// .ease(d3.easeLinear)
.duration(500)
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
setTimeout(function(){
// Add Y axis
var y0 = d3.scaleLinear()
.domain([0, d3.max(data, function(d) { return +d.cases; })])
.range([ height, 0 ]);
svg.append("g")
.call(d3.axisLeft(y0))
.append("text")
.attr("y", -2)
.attr("fill", "darkseagreen")
.attr("text-anchor", "start")
.attr("font-size",11)
.text("↑ Cases");
// Add 2nd Y axis
var y1 = d3.scaleLinear()
.domain([0, d3.max(data, function(d) { return +d.deaths; })])
.range([ height, 0 ]);
svg.append("g")
.attr("transform", "translate(" + width + " ,0)")
.call(d3.axisRight(y1))
.append("text")
.attr("y", -2)
.attr("fill", "indianred")
.attr("text-anchor", "start")
.attr("font-size",11)
.text("↑ Deaths");
// Add the line
svg.append("path")
.datum(data)
.classed("line", true)
.attr("fill", "none")
.attr("stroke", "darkseagreen")
.attr("stroke-width", 2)
.attr("d", d3.line()
.x(function(d) { return x(d.date) })
.y(function(d) { return y0(d.cases) })
)
.attr("stroke-dasharray", function(d) {
return this.getTotalLength()
})
.attr("stroke-dashoffset", function(d) {
return this.getTotalLength()
});
// Add the 2nd line
svg.append("path")
.datum(data)
.classed("line", true)
.attr("fill", "none")
.attr("stroke", "indianred")
.attr("stroke-width", 2)
.attr("d", d3.line()
.x(function(d) { return x(d.date) })
.y(function(d) { return y1(d.deaths) })
)
.attr("stroke-dasharray", function(d) {
return this.getTotalLength()
})
.attr("stroke-dashoffset", function(d) {
return this.getTotalLength()
});
svg.selectAll(".line")
.transition()
.duration(2000)
.ease(d3.easeLinear)
.attr("stroke-dashoffset", 0);
tipBox = svg.append('rect')
.attr('width', width)
.attr('height', height)
.attr('opacity', 0)
.attr("class", "overlay")
.on('mousemove', drawTooltip)
.on('mouseout', removeTooltip);
function removeTooltip() {
if (tooltip) tooltip.style('display', 'none');
if (tooltipLine) tooltipLine.attr('stroke', 'none');
}
function drawTooltip() {
const x0 = x.invert(d3.mouse(tipBox.node())[0]);
var i = bisectDate(data, x0, 1),
d0 = data[i - 1],
d1 = data[i],
d = x0 - d0.date > d1.date - x0 ? d1 : d0;
tooltipLine.attr('stroke', 'black')
.attr('x1', x(x0))
.attr('x2', x(x0))
.attr('y1', 0)
.attr('y2', height);
var format_date = d3.timeFormat("%m/%d/%y");
tooltip.html(format_date(d.date) + " - Total Cases: " + d.cases + " & Total Deaths: " + d.deaths)
.style('display', 'block')
.style('left', d3.event.pageX + 20)
.style('top', d3.event.pageY - 20)
}
const type = d3.annotationLabel;
const annotations = [
{
note: {
label: "First Covid-19 case detected in US",
title: "01/21/2020",
},
data: { date: "2020-01-21", cases: 1 },
connector: {type: "elbow"},
subject: { radius: 5 },
dy: -250,
dx: 75
}
]
const annotations_02 = [
{
note: {
label: "First recorded death in US",
title: "02/29/2020",
},
data: { date: "2020-02-29", deaths: 1 },
connector: {type: "elbow"},
subject: { radius: 5 },
dy: -275,
dx: 125
}
]
const annotations_03 = [
{
note: {
label: "Death rate growing rapidly",
title: "May, 2020 - November, 2020",
},
data: { date: "2020-08-01", deaths: 154917 },
connector: {type: "elbow"},
dy: -150,
dx: 60,
subject: { radius: 5 }
}
]
const annotations_04 = [
{
note: {
label: "Mass vaccination started in US",
title: "12/14/2020",
},
data: { date: "2020-12-14", cases: 0 },
connector: {type: "elbow"},
dy: -100,
dx: 40,
subject: { radius: 5 }
}
]
const annotations_05 = [
{
note: {
label: "50.7% US population received atleast one dose, infection rate going down",
title: "05/31/2021",
},
data: { date: "2021-05-31", cases: 33277912 },
connector: {type: "elbow"},
dy: 45,
dx: -1,
subject: { radius: 5 }
}
]
// Add annotation to the chart
const makeAnnotations = d3.annotation()
.editMode(true)
.type(d3.annotationCalloutCircle)
.accessors({
x: d => x(d3.timeParse("%Y-%m-%d")(d.date)),
y: d => y0(d.cases)
})
.accessorsInverse({
date: d => timeFormat(x.invert(d.x)),
close: d => y0.invert(d.y)
})
.annotations(annotations)
// Add annotation to the chart
const makeAnnotations_02 = d3.annotation()
.editMode(true)
.type(d3.annotationCalloutCircle)
.accessors({
x: d => x(d3.timeParse("%Y-%m-%d")(d.date)),
y: d => y1(d.deaths)
})
.accessorsInverse({
date: d => timeFormat(x.invert(d.x)),
close: d => y1.invert(d.y)
})
.annotations(annotations_02)
// Add annotation to the chart
const makeAnnotations_03 = d3.annotation()
.editMode(true)
.type(d3.annotationCalloutCircle)
.accessors({
x: d => x(d3.timeParse("%Y-%m-%d")(d.date)),
y: d => y1(d.deaths)
})
.accessorsInverse({
date: d => timeFormat(x.invert(d.x)),
close: d => y1.invert(d.y)
})
.annotations(annotations_03)
// Add annotation to the chart
const makeAnnotations_04 = d3.annotation()
.editMode(true)
.type(d3.annotationCalloutCircle)
.accessors({
x: d => x(d3.timeParse("%Y-%m-%d")(d.date)),
y: d => y0(d.cases)
})
.accessorsInverse({
date: d => timeFormat(x.invert(d.x)),
close: d => y0.invert(d.y)
})
.annotations(annotations_04)
// Add annotation to the chart
const makeAnnotations_05 = d3.annotation()
.editMode(true)
.type(d3.annotationCalloutCircle)
.accessors({
x: d => x(d3.timeParse("%Y-%m-%d")(d.date)),
y: d => y0(d.cases)
})
.accessorsInverse({
date: d => timeFormat(x.invert(d.x)),
close: d => y0.invert(d.y)
})
.annotations(annotations_05)
setTimeout(function(){
svg
.append("g")
.attr("class", "annotation-group")
.call(makeAnnotations)
}, 3000);
setTimeout(function(){
svg
.append("g")
.attr("class", "annotation-group")
.call(makeAnnotations_02)
}, 4500);
setTimeout(function(){
svg
.append("g")
.attr("class", "annotation-group")
.call(makeAnnotations_03)
}, 6000);
setTimeout(function(){
svg
.append("g")
.attr("class", "annotation-group")
.call(makeAnnotations_04)
}, 7500);
setTimeout(function(){
svg
.append("g")
.attr("class", "annotation-group")
.call(makeAnnotations_05)
}, 9000);
}, 750);
}) //CSV parse ends
} //Function1 ends
//2nd Chart
function myFunction2() {
removeDivs();
document.getElementById("but2").style.color = "white";
document.getElementById("but3").style.color = "black";
document.getElementById("but1").style.color = "black";
document.getElementById("but4").style.color = "black";
document.getElementById("but5").style.color = "black";
document.getElementById("but1").style.background = "#f1f1f1";
document.getElementById("but3").style.background = "#f1f1f1";
document.getElementById("but4").style.background = "#f1f1f1";
document.getElementById("but5").style.background = "#f1f1f1";
document.getElementById("but2").style.background = "#666";
var outer_div = document.createElement("div");
outer_div.id = "my_dataviz2_outer";
document.getElementById("main").appendChild(outer_div);
var div_text = document.createElement("div");
div_text.id = "my_dataviz2_text";
document.getElementById("my_dataviz2_outer").appendChild(div_text);
var text0 = document.createTextNode("The Cases:");
bold = document.createElement('strong');
bold.appendChild(text0);
document.getElementById("my_dataviz2_text").appendChild(bold);
var brk0 = document.createElement("br");
document.getElementById("my_dataviz2_text").appendChild(brk0);
var text = document.createTextNode("US has witnessed huge spike in infection rate from May, 2020 to February 2021. On 01/08/2021 US recorded 300,777 cases in a single day. The infection rate has gone down slowly once more and more US residents got vaccinated. We are again seeing a spike in infections starting mid-July, 2021, after vaccination rate got plateaued.");
document.getElementById("my_dataviz2_text").appendChild(text);
var brk = document.createElement("br");
document.getElementById("my_dataviz2_text").appendChild(brk);
var brk2 = document.createElement("br");
document.getElementById("my_dataviz2_text").appendChild(brk2);
var button1 = document.createElement("button");
button1.innerHTML = "Daily Cases";
button1.id = "daily_cases";
var button2 = document.createElement("button");
button2.innerHTML = "Seven-day Moving Average";
button2.id = "daily_avg_cases";
document.getElementById("my_dataviz2_outer").appendChild(button1);
document.getElementById("my_dataviz2_outer").appendChild( document.createTextNode( '\u00A0' ) );
document.getElementById("my_dataviz2_outer").appendChild(button2);
document.getElementById("daily_cases").setAttribute("class", "btn btn-info");
document.getElementById("daily_avg_cases").setAttribute("class", "btn btn-info");
document.getElementById("daily_cases").style.color = "white";
document.getElementById("daily_avg_cases").style.color = "black";
document.getElementById("daily_cases").style.background = "#666";
document.getElementById("daily_avg_cases").style.background = "#f1f1f1";
document.getElementById("daily_cases").addEventListener("click", update1);
document.getElementById("daily_avg_cases").addEventListener("click", update2);
document.getElementById("my_dataviz2_outer").appendChild( document.createTextNode( '\u000A' ) );
document.getElementById("my_dataviz2_outer").appendChild( document.createTextNode( '\u000A' ) );
var div = document.createElement("div");
div.id = "my_dataviz2";
document.getElementById("my_dataviz2_outer").appendChild(div);
// set the dimensions and margins of the graph
var margin = {top: 30, right: 60, bottom: 30, left: 60},
width = 1000 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#my_dataviz2")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var div = document.createElement("div");
div.id = "tooltip";
// div.style.position = "absolute";
div.style.padding = "5px";
div.style.backgroundColor = "white";
document.getElementById("my_dataviz2").appendChild(div);
const tooltip = d3.select('#tooltip');
const tooltipLine = svg.append('line');
var bisectDate = d3.bisector(function(d) { return d.date; }).left;
// Add X axis --> it is a date format
var x = d3.scaleTime()
.range([ 0, width ]);
var xAxis = d3.axisBottom().scale(x);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.attr("class","myXaxis")
// Initialize an Y axis
var y = d3.scaleLinear().range([height, 0]);
var yAxis = d3.axisLeft().scale(y);
svg.append("g")
.attr("class","myYaxis")
//1st button function
function update1() {
document.getElementById("daily_cases").style.color = "white";
document.getElementById("daily_avg_cases").style.color = "black";
// document.getElementById("daily_avg_cases_100").style.color = "black";
document.getElementById("daily_cases").style.background = "#666";
document.getElementById("daily_avg_cases").style.background = "#f1f1f1";
// document.getElementById("daily_avg_cases_100").style.background = "#f1f1f1";
// var bisectDate = d3.bisector(function(d) { return d.date; }).left;
//Read the data
d3.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/rolling-averages/us.csv",
// When reading the csv, I must format variables:
function(d){
return { date : d3.timeParse("%Y-%m-%d")(d.date), cases : d.cases}
},
// Now I can use this dataset:
function(data) {
// Create the X axis:
x.domain(d3.extent(data, function(d) { return d.date; }));
svg.selectAll(".myXaxis")
// .transition()
// .duration(3000)
.call(xAxis);
// create the Y axis
y.domain([0, 320000 ]);
svg.selectAll(".myYaxis")
.transition()
.duration(3000)
.call(yAxis);
// Create a update selection: bind to the new data
var u = svg.selectAll(".lineTest")
.data([data], function(d){ return d.date });
// Updata the line
u
.enter()
.append("path")
.attr("class","lineTest")
.merge(u)
.transition()
.duration(3000)
.attr("d", d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.cases); }))
.attr("fill", "none")
.attr("stroke", "darkseagreen")
.attr("stroke-width", 2)
// function plotToolTip (data) {
// var focus = svg.append("g")
// .attr("class", "focus")
// .style("display", "none");
// focus.append("line")
// .attr("class", "x-hover-line hover-line")
// .attr("y1", 0)
// .attr("y2", height);
// focus.append("line")
// .attr("class", "y-hover-line hover-line")
// .attr("x1", width)
// .attr("x2", width);
// focus.append("circle")
// .attr("r", 5);
// focus.append("text")
// .attr("class", "hover-text")
// .attr("x", -60)
// .attr("dy", "-1em");
// svg.append("rect")
// .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
// .attr("class", "overlay")
// .attr("width", width)
// .attr("height", height)
// .on("mouseover", function() { focus.style("display", null); })
// .on("mouseout", function() { focus.style("display", "none"); })
// .on("mousemove", mousemove);
// var format_date = d3.timeFormat("%m/%d/%y");
// function mousemove() {
// var x0 = x.invert(d3.mouse(this)[0]),
// i = bisectDate(data, x0, 1),
// d0 = data[i - 1],
// d1 = data[i],
// d = x0 - d0.date > d1.date - x0 ? d1 : d0;
// focus.attr("transform", "translate(" + x(d.date) + "," + y(d.cases) + ")");
// focus.select("text").text(function() {
// var str = format_date(d.date) + ": " + d.cases;
// return str;
// });
// focus.select(".x-hover-line").attr("y2", height - y(d.cases));
// focus.select(".y-hover-line").attr("x2", width + width);
// }
// }
// plotToolTip(data)
tipBox = svg.append('rect')
.attr('width', width)
.attr('height', height)
.attr('opacity', 0)
.attr("class", "overlay")
.on('mousemove', drawTooltip)
.on('mouseout', removeTooltip);
function removeTooltip() {
if (tooltip) tooltip.style('display', 'none');
if (tooltipLine) tooltipLine.attr('stroke', 'none');
}
function drawTooltip() {
const x0 = x.invert(d3.mouse(tipBox.node())[0]);
var i = bisectDate(data, x0, 1),
d0 = data[i - 1],
d1 = data[i],
d = x0 - d0.date > d1.date - x0 ? d1 : d0;
tooltipLine.attr('stroke', 'black')
.attr('x1', x(x0))
.attr('x2', x(x0))
.attr('y1', 0)
.attr('y2', height);
var format_date = d3.timeFormat("%m/%d/%y");
tooltip.html(format_date(d.date) + " - Daily Cases: " + d.cases)
.style('display', 'block')
.style('left', d3.event.pageX + 20)
.style('top', d3.event.pageY - 20)
}
const annotations = [
{
note: {
label: "US Covid-19 cases reach 2 million",
title: "06/10/2020",
},
data: { date: "2020-06-10", cases: 0 },
connector: {type: "elbow"},
subject: { radius: 5 },
dy: -250,
dx: -75
}
]
const annotations_02 = [
{
note: {
label: "First known Covid-19 reinfection reported in US",
title: "08/28/2020",
},
data: { date: "2020-08-28", cases: 0 },
connector: {type: "elbow"},
subject: { radius: 5 },
dy: -200,
dx: -75
}
]
const annotations_03 = [
{
note: {
label: "US President and First Lady tested positive",
title: "10/02/2020",
},
data: { date: "2020-10-02", cases: 0 },
connector: {type: "elbow"},
subject: { radius: 5 },
dy: -250,
dx: -5
}
]
const annotations_04 = [
{
note: {
label: "CDC eases mask guidance for fully vaccinated individuals",
title: "05/13/2021",
},
data: { date: "2021-05-13", cases: 0 },
connector: {type: "elbow"},
subject: { radius: 5 },
dy: -200,
dx: 20
}
]
// Add annotation to the chart
const makeAnnotations = d3.annotation()
.editMode(true)
.type(d3.annotationCalloutCircle)
.accessors({
x: d => x(d3.timeParse("%Y-%m-%d")(d.date)),
y: d => y(d.cases)
})
.accessorsInverse({
date: d => timeFormat(x.invert(d.x)),
close: d => y.invert(d.y)
})
.annotations(annotations)
// Add annotation to the chart
const makeAnnotations_02 = d3.annotation()
.editMode(true)
.type(d3.annotationCalloutCircle)
.accessors({
x: d => x(d3.timeParse("%Y-%m-%d")(d.date)),
y: d => y(d.cases)
})
.accessorsInverse({
date: d => timeFormat(x.invert(d.x)),
close: d => y.invert(d.y)
})
.annotations(annotations_02)
// Add annotation to the chart
const makeAnnotations_03 = d3.annotation()
.editMode(true)
.type(d3.annotationCalloutCircle)
.accessors({
x: d => x(d3.timeParse("%Y-%m-%d")(d.date)),
y: d => y(d.cases)
})
.accessorsInverse({
date: d => timeFormat(x.invert(d.x)),
close: d => y.invert(d.y)
})
.annotations(annotations_03)
// Add annotation to the chart
const makeAnnotations_04 = d3.annotation()
.editMode(true)
.type(d3.annotationCalloutCircle)
.accessors({
x: d => x(d3.timeParse("%Y-%m-%d")(d.date)),
y: d => y(d.cases)
})
.accessorsInverse({
date: d => timeFormat(x.invert(d.x)),
close: d => y.invert(d.y)
})
.annotations(annotations_04)
setTimeout(function(){
svg
.append("g")
.attr("class", "annotation-group")
.call(makeAnnotations)
}, 3000);
setTimeout(function(){
svg
.append("g")
.attr("class", "annotation-group")
.call(makeAnnotations_02)
}, 4500);
setTimeout(function(){
svg
.append("g")
.attr("class", "annotation-group")
.call(makeAnnotations_03)
}, 6000);
setTimeout(function(){
svg
.append("g")
.attr("class", "annotation-group")
.call(makeAnnotations_04)
}, 7500);
}) //CSV parse ends
} //1st button function ends
//2nd button function
function update2() {
document.getElementById("daily_cases").style.color = "black";
document.getElementById("daily_avg_cases").style.color = "white";
document.getElementById("daily_cases").style.background = "#f1f1f1";
document.getElementById("daily_avg_cases").style.background = "#666";
// var bisectDate = d3.bisector(function(d) { return d.date; }).left;
//Read the data
d3.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/rolling-averages/us.csv",
// When reading the csv, I must format variables:
function(d){
return { date : d3.timeParse("%Y-%m-%d")(d.date), cases_avg : d.cases_avg}
},
// Now I can use this dataset:
function(data) {
// Create the X axis:
x.domain(d3.extent(data, function(d) { return d.date; }));
svg.selectAll(".myXaxis").transition()
.duration(3000)
.call(xAxis);
// create the Y axis
y.domain([0, 280000 ]);
svg.selectAll(".myYaxis")
.transition()