-
Notifications
You must be signed in to change notification settings - Fork 15
/
clinical-vis.html
893 lines (893 loc) · 31.6 KB
/
clinical-vis.html
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
<!--
@license
Copyright 2018 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../polymer/polymer.html">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,500,500i,700,700i" rel="stylesheet">
<dom-module id="clinical-vis">
<template>
<style>
:host{
font-family: 'Roboto', sans-serif;
font-size: 13sp;
line-height: 1.2em;
}
body {
margin: 0px;
background: #fcfcfc;
}
.basic-data {
min-width: 220px;
width: 220px;
float: left;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
margin-right: 24px;
border-radius: 3px;
}
.basic-data-heading {
background: #E3EEEF;
font-weight: 400;
color: #26272A;
padding: 6px;
font-size: 10pt;
border-bottom: solid 3px #c0dadc;
}
.basic-data-pair {
display: block;
margin: 4px;
padding: 4px;
}
.basic-data-key {
font-size: 10pt;
line-height: 1.2em;
font-weight: 300;
padding-left: 4px;
color: #356266;
}
.basic-data-value {
font-size: 11pt;
line-height: 1.4em;
padding: 4px;
color: #232321;
font-weight: 400;
hyphens: auto;
}
.cat-holder {
font-size: 16sp;
font-weight: 300;
color: #0063A3;
}
.key-and-chart-holder {
display: flex;
font-size: 14sp;
padding: 8px 0px;
border-bottom: solid 2px #f2f3f5;
}
.bar-key-holder {
padding: 20px;
width: 120px;
min-width: 120px;
text-align: right;
font-size: 16sp;
font-weight: 400;
color: #db5825;
}
.vitals-top-holder {
display: flex;
}
.cat-holder-inner {
padding: 20px;
width: 120px;
min-width: 120px;
text-align: right;
font-size: 16sp;
font-weight: 400;
color: #db5825;
}
.key-holder {
width: 120px;
min-width: 120px;
font-weight: 400;
color: #561f06;
font-size: 10pt;
text-align: right;
padding: 10px;
}
.chart-holder {
width: 500px;
flex-grow: 1;
padding-bottom: 5px 0px;
}
.chart-holder-inner {
width: 100%;
}
.xaxis-holder {
margin-left: 190px;
font-size: 8pt;
color: #525251;
border-bottom: solid 3px #D3DADB;
}
.vitals-axis-holder {
margin-left: 30px;
padding-top: 18px;
font-size: 8pt;
color: #525251;
border-bottom: solid 3px #D3DADB;
overflow: visible;
}
.heading-label {
font-weight: 400;
color: #F0F2F0;
padding: 4px;
font-size: 11pt;
}
.note-card {
border: solid 1px #D3DADB;
background: #F7FFF7;
border-radius: 3px;
margin: 0px 24px 24px 0px;
min-width: 600px;
width: 600px;
box-shadow: 0 1px 3px rgba(4,28,2,0.12), 0 1px 2px rgba(0,2,0,0.24);
}
.note-heading {
font-weight: 500;
font-size: 16sp;
background: #448e3d;
color: #F0F2F0;
padding: 6px;
}
.note-text {
min-height: 300px;
height: 400px;
overflow-y: auto;
overflow-x: hidden;
resize: vertical;
white-space: pre-line;
font-size: 11pt;
line-height: 1.4em;
padding: 12px;
color: rgba(0,0,0,0.84);
border-top: solid 3px #1f541a;
}
.lab-card {
border: solid 1px #D3DADB;
background: #f7fdff;
border-radius: 3px;
margin: 0px 24px 24px 0px;
min-width: 600px;
width: 600px;
box-shadow: 0 1px 3px rgba(2,22,25,0.12), 0 1px 2px rgba(0,6,7,0.24);
}
.lab-heading {
font-weight: 500;
font-size: 16sp;
color: #F0F2F0;
background: #1ca4bc;
padding: 6px;
}
.selected-labs {
padding-bottom: 24px;
border-top: solid 3px #296671;
min-height: 400px;
height: 400px;
overflow-y: scroll;
resize: vertical;
}
.lab-key-and-value {
display: flex;
padding: 2px;
font-size: 14px;
border-bottom: solid 0.5px rgb(218, 229, 232);
margin-bottom: 1px;
height: 40px;
line-height: 40px;
}
.lab-key {
font-weight: 500;
text-align: right;
padding-right: 18px;
min-width: 200px;
width: 200px;
color: #023a44;
border-right: solid 1px rgba(218, 229, 232, 0.75);
}
.lab-value {
min-width: 40px;
width: 40px;
text-align: right;
color: #03697b;
}
.lab-units {
min-width: 40px;
width: 40px;
font-size: 9pt;
color: #75a2aa;
padding-left: 3px;
line-height: 44px;
}
.chart-units {
font-size: 12px;
color: #8C615A;
padding-left: 3px;
font-weight: 400;
line-height: 1em;
}
.lab-sparkline {
width: 120px;
min-width:120px;
margin: 6px 36px 6px auto;
}
.charts-and-labs {
overflow: hidden;
}
.chart-card {
border: 1px solid #D3DADB;
border-radius: 3px;
background: rgba(255, 251, 249,0.12);
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
width: 55%;
min-width: 35%;
display: inline-block;
vertical-align: top;
position: relative;
}
.chart-card-heading {
font-weight: 400;
font-size: 16sp;
color: #561f06;
background: #ffdfd1;
padding: 6px;
}
#chartstop {
padding: 20px;
overflow: auto;
border-top: solid 3px #dc987a;
}
#textarea {
font-family: 'Roboto', sans-serif;
display: inline-block;
vertical-align: top;
position: relative;
}
.c3-ygrid-line {
stroke: #D3DADB;
}
.c3-axis-y{
font-family: 'Roboto', sans-serif;
color: #26272A;
}
.c3 line {
stroke: #D3DADB;
}
.c3 .domain {
stroke: #D3DADB;
}
.limit-line line {
stroke: rgba(204, 217, 172, 0.50);
stroke-width: 3;
}
.lab-limit-line line {
stroke: rgba(176, 193, 133, 0.50);
stroke-width: 1.5;
}
.chart-holder-inner-bar {
margin-left: 30px;
}
.xaxis-holder line {
stroke: #525251;
}
.xaxis-holder .domain {
display: none;
}
.vitals-axis-holder line {
stroke: #525251;
}
.vitals-axis-holder .domain {
display: none;
}
.c3-text {
font-family: 'Roboto', sans-serif;
font-weight: 400;
}
</style>
<div class="basic-data">
<div class="basic-data-heading">Patient Information</div>
<div class="basic-data-pair">
<div class="basic-data-key">Name</div>
<div class="basic-data-value">[[getFakeName(json.gender)]]</div>
</div>
<div class="basic-data-pair">
<div class="basic-data-key">Age</div>
<div class="basic-data-value">[[age]]</div>
</div>
<div class="basic-data-pair">
<div class="basic-data-key">Gender</div>
<div class="basic-data-value">[[json.gender]]</div>
</div>
<div class="basic-data-pair">
<div class="basic-data-key">Ethnicity</div>
<div class="basic-data-value">[[json.ethnicity]]</div>
</div>
<div class="basic-data-pair">
<div class="basic-data-key">Admitting Diagnosis</div>
<div class="basic-data-value">[[json.diagnosis]]</div>
</div>
<div class="basic-data-pair">
<div class="basic-data-key">ICU type</div>
<div class="basic-data-value">[[getIcuName(json.icu_type)]]</div>
</div>
</div>
<div class="charts-and-labs">
<div id="textarea">
<div class="note-card">
<div class="note-heading"><span class="heading-label">Selected Note</span> [[noteTime]]</div>
<div class="note-text">[[noteText]]</div>
</div>
<div class="lab-card">
<div class="lab-heading"><span class="heading-label">Selected Labs</span> [[labsTime]]</div>
<div class="selected-labs">
<template is="dom-repeat" items="[[labsList]]">
<div class="lab-key-and-value">
<div class="lab-key">[[item]]</div>
<div class="lab-value">[[getLabValue(item, labsTimestamp)]]</div>
<div class="lab-units">[[getKeyUnits(item)]]</div>
<div class="lab-sparkline" id$=[[getLabId(item)]]></div>
</div>
</template>
</div>
</div>
</div>
<div class="chart-card">
<div class="chart-card-heading">Patient Timeline</div>
<div id="chartstop"></div>
</div>
</div>
</template>
<script>
const unitData = {
'Albumin': {'units': 'g/dL', 'low': 3.5, 'high': 6},
'Anion Gap': {'units': 'mEq/L'},
'Bicarbonate': {'units': 'mEq/L', 'low': 22, 'high': 32},
'Bilirubin': {'units': 'mg/dL', 'low': 0.1, 'high': 1.2},
'Blood urea nitrogen': {'units': 'mEq/L', 'low': 6, 'high': 20},
'Calcium': {'units': 'mg/dL', 'low': 8.4, 'high': 10.3},
'Chloride': {'units': 'mEq/L', 'low': 96, 'high': 108},
'Cholesterol': {'units': 'mg/dL', 'low': 40, 'high': 240},
'Creatinine': {'units': 'mEq/L', 'low': 0.4, 'high': 1.1},
'Diastolic blood pressure': {'units': 'mmHg', 'low': 60, 'high': 90},
'Fraction inspired oxygen': {'units': '%'},
'Glucose': {'units': 'mg/dL', 'low': 70, 'high': 105},
'Heart Rate': {'units': 'bpm', 'low': 50, 'high': 100},
'Hematocrit': {'units': '%', 'low': 37, 'high': 52},
'Hemoglobin': {'units': 'g/dL', 'low': 11.2, 'high': 15.7},
'Lactate': {'units': 'mmol/dL', 'low': .5, 'high': 2},
'Lymphocytes': {'units': 'm/uL', 'low': 1, 'high': 3},
'Magnesium': {'units': 'mg/dL', 'low': 1.6, 'high': 2.6},
'Mean blood pressure': {'units': 'mmHg'},
'Monocytes': {'units': 'M/uL', 'low': .2, 'high': 1},
'Neutrophilis': {'units': 'M/uL', 'low': 2, 'high': 7},
'Oxygen saturation': {'units': '%', 'low': 94, 'high': 100},
'Partial pressure of CO2': {'units': 'mmHg', 'low': 38, 'high': 42},
'Partial pressure of O2': {'units': 'mmHg', 'low': 75, 'high': 100},
'pH': {'low': 7.35, 'high': 7.45},
'Phosphate': {'units': 'mg/dL', 'low': 2.7, 'high': 4.5},
'Platelets': {'units': 'K/uL', 'low': 150, 'high': 400},
'Potassium': {'units': 'mEq/L', 'low': 3.3, 'high': 5.1},
'Prothrombin time': {'units': 'secL', 'low': 11, 'high': 14},
'Partial thromboplastin time': {'units': 'sec', 'low': 25, 'high': 35},
'Red blood cell count': {'units': 'M/uL', 'low': 42, 'high': 6.1},
'Respiratory rate': {'units': 'bpm', 'low': 12, 'high': 25},
'Sodium': {'units': 'mEq/L', 'low': 133, 'high': 145},
'Systolic blood pressure': {'units': 'mmHg', 'low': 95, 'high': 140},
'Temperature': {'units': 'C', 'low': 36.6, 'high': 37},
'Troponin-I': {'units': 'ng/mL'},
'Troponin-T': {'units': 'ng/mL', 'low': 0, 'high': 0.01},
'Urine output': {},
'Weight': {'units': 'kg', 'low': 40, 'high': 240},
'White blood cell count': {'units': 'K/uL', 'low': 5, 'high': 10},
};
Polymer({
is: "clinical-vis",
properties: {
json: {tye: String, observer: 'jsonChanged'},
noteTime: String,
noteText: String,
age: Number,
selectedNoteIndex: Number,
notesChart: Object,
},
getFakeName: function(gender) {
return gender == 'M' ? 'John Doe' : 'Jane Doe';
},
getIcuName: function(icu) {
if (icu == 'TSICU') {
return 'Trauma ICU';
}
if (icu == 'CSRU') {
return 'Cardiac Surgical Recovery Unit';
}
return icu;
},
getItemText: function(event) {
const isNum = !isNaN(event.value) && event.value != '';
if (isNum) {
let num = parseFloat(event.value)
return +num.toFixed(3);
}
return event.value;
},
getLabValue: function(key, timestamp) {
for (const lab of this.labResultsByTimestamp[timestamp]) {
if (lab.key == key) {
return +(parseFloat(lab.value).toFixed(3));
}
}
return '-';
},
getKeyUnits: function(key) {
return unitData[key] && unitData[key].units ? unitData[key].units : null;
},
getLabId: function(key) {
requestAnimationFrame(() => this.drawSparkline(key));
return key.replace(/\s/g,'');
},
drawSparkline: function(key) {
const times = [];
const data = [];
let selectedIndex = -1;
for (let time of Object.keys(this.labResultsByTimestamp).sort()) {
for (const lab of this.labResultsByTimestamp[time]) {
if (lab.key == key && lab.value != null) {
data.push(lab.value);
times.push(time * 1000);
if (time == this.labsTimestamp) {
selectedIndex = data.length - 1;
}
break;
}
}
}
const printableKey = key.length < 10 ? key : key.substring(0, 8) + '...';
const isFirstLabInList = this.labsList.indexOf(key) == 0;
const tooltipTop = isFirstLabInList ? 30 : -60;
const chartRange = this.getKeyRange(key);
const gridData = {y: {show: false}};
if (chartRange != null) {
gridData.y['lines'] = [
{value: +chartRange[0], class: 'lab-limit-line'},
{value: +chartRange[1], class: 'lab-limit-line'},
];
}
const chart = c3.generate({
bindto: '#' + key.replace(/\s/g,''),
data: {
x: 'time',
columns: [
['time'].concat(times),
[printableKey].concat(data)
],
color: function (color, d) {
return d.index == selectedIndex ? "#007696" : "#95c1c9";
}
},
legend: {
show: false
},
tooltip:{
show: true,
position: function (data, width, height, element) {
return {top: tooltipTop, left: 0};
},
},
axis: {
x: {show: false, type: 'timeseries', localtime: false, min: this.minX * 1000, max: this.maxX * 1000},
y: {show: false}
},
size: {
height: 40,
width: 120
},
point: {
show: true
},
grid: gridData,
});
},
getKeyRange: function(key) {
return unitData[key] && unitData[key].low ? [unitData[key].low, unitData[key].high] : null;
},
jsonChanged: function(json) {
this.$.chartstop.innerHTML = '';
const dob = new Date(json.dob)
const firstEntryDate = new Date(+json.events[0].timestamp * 1000);
const diff = firstEntryDate.getTime() - dob.getTime();
this.age = Math.floor(diff / (1000*60*60*24*365.25));
json.events.forEach((item) => {
if (!item['category']) {
item['category'] = 'Other';
}
});
timestamps = json.events.map(event => event['timestamp']).sort();
timestamps = timestamps.filter((item, pos) => pos == 0 || item != timestamps[pos - 1]);
categories = json.events.map(event => event['category']).sort();
categories = categories.filter((item, pos) => pos == 0 || item != categories[pos - 1]);
eventsByCat = {}
categories.forEach((cat) => eventsByCat[cat] = {});
for (var cat in eventsByCat) {
if (cat == 'Other') {
continue;
}
keys = json.events.filter((item) => item['category'] == cat).map((item) => item['key']).sort();
keys = keys.filter((item, pos) => pos == 0 || item != keys[pos - 1]);
for (key of keys) {
const timeToEvents = {}
events = json.events.filter((item) => item['category'] == cat).filter((item) => item['key'] == key);
for (event of events) {
timeToEvents[event['timestamp']] = event;
}
eventsByCat[cat][key] = timeToEvents;
}
}
this.generateCharts(eventsByCat, json.notes.sort((a, b) => a.timestamp - b.timestamp));
},
generateCharts: function(eventsByCat, notes) {
const allEvents = []
minX = Number.MAX_VALUE;
maxX = 0;
const vitalsOrdering = {
'Temperature': 0,
'Heart Rate': 1,
'Systolic blood pressure': 2,
'Diastolic blood pressure': 3,
'Mean blood pressure': 4,
'Respiratory rate': 5,
'Oxygen saturation': 6,
'Urine output': 7,
'Weight': 8,
'Glascow coma scale': 9,
};
const labOrdering = {
'Sodium': 0,
'Potassium': 1,
'Chloride': 2,
'Bicarbonate': 3,
'Blood urea nitrogen': 4,
'Creatinine': 5,
'Glucose': 6,
};
for (cat in eventsByCat) {
let keys = Object.keys(eventsByCat[cat]);
if (cat == 'Vitals') {
keys = keys.sort((a, b) => {
if (a in vitalsOrdering && b in vitalsOrdering) {
return vitalsOrdering[a] - vitalsOrdering[b];
}
if (a in vitalsOrdering) {
return -1;
}
if (b in vitalsOrdering) {
return 1;
}
else {
return a - b;
}});
}
for (key of keys) {
if (cat == 'Vitals') {
allEvents.push({key: key, category: cat, events: eventsByCat[cat][key]});
}
const times = Object.keys(eventsByCat[cat][key]).map((time) => eventsByCat[cat][key][time].timestamp)
minX = Math.min(minX, ...times);
maxX = Math.max(maxX, ...times);
}
}
for (note of notes) {
minX = Math.min(minX, +note.timestamp);
maxX = Math.max(maxX, +note.timestamp);
}
// Pad one extra hour to ensure enough space for edge data points and their labels.
minX -= 60 * 60;
maxX += 60 * 60;
this.minX = minX;
this.maxX = maxX;
this.labResultsByTimestamp = {}
const labKeys = [];
for (key in eventsByCat['Labs']) {
labKeys.push(key);
for (timestamp in eventsByCat['Labs'][key]) {
lab_bar_timestamp = timestamp - (timestamp % (60 * 60));
if (!(lab_bar_timestamp in this.labResultsByTimestamp)) {
this.labResultsByTimestamp[lab_bar_timestamp] = [];
}
this.labResultsByTimestamp[lab_bar_timestamp].push({
key: key,
value: 'value_num' in eventsByCat['Labs'][key][timestamp] ?
eventsByCat['Labs'][key][timestamp].value_num :
eventsByCat['Labs'][key][timestamp].value_str});
}
}
this.labsList = labKeys;
const holder = d3.select(this.$.chartstop)
let currentCat = '';
let currentCatSelection = null;
const xAxisWidth = holder.node().getBoundingClientRect().width - 190 - 40;
const xAxisHeight = 20;
const xAxisVis = holder.append("svg:svg")
.attr('class', 'xaxis-holder')
.attr("width", xAxisWidth)
.attr("height", xAxisHeight)
.attr('overflow', 'visible');
const offsetToUTC = new Date(minX * 1000).getTimezoneOffset() * 60;
const xScale = d3.time.scale()
.domain([new Date((minX + offsetToUTC) * 1000), new Date((maxX + offsetToUTC) * 1000)])
.rangeRound([0, xAxisWidth]);
const xAxis = d3.svg.axis()
.orient('top')
.scale(xScale);
xAxisVis.append("g")
.attr("class", "xaxis")
.attr("transform", "translate(0," + xAxisHeight + ")")
.call(xAxis.tickFormat(d3.timeFormat("%I %p")).tickSize(-10));
const notesClickListener = (e) => {
this.noteTime = new Date(e.x).toLocaleString('en-US', {timeZone: 'UTC'});
const timestamp = new Date(e.x).getTime() / 1000;
let noteIndex = 0;
for (var i = 0; i < this.json.notes.length; i++) {
if (this.json.notes[i].timestamp == timestamp) {
noteIndex = i;
break;
}
}
this.noteText = this.json.notes[noteIndex].note;
this.selectedNoteIndex = e.index;
this.notesChart.flush();
};
const notesBarLabeler = (v, id, i, j) => {
if (!v) {
return null;
}
const frac = v - Math.floor(v);
let index = 0;
if (frac > 0) {
if (frac < 0.09) {
index = +(frac.toFixed(3).substring(3));
}
else {
index = +(frac.toFixed(1).substring(2));
}
}
return notes[index].category;
}
const notesColorSelector = (color, d) => typeof d ==='object' && 'index' in d && d.index == this.selectedNoteIndex ? '#448e3d' : '#F2946B';
const notesChartAssigner = chart => this.notesChart = chart;
const allNoteTimes = notes.map(note => note.timestamp).sort();
let selectedNoteTime = allNoteTimes.length > 0 ? allNoteTimes[allNoteTimes.length - 1] : 0;
// Find the latest nursing note if one exists.
const allNursingNotes = notes.filter(note => note.category.indexOf('ursing') != -1);
if (allNursingNotes.length > 0) {
selectedNoteTime = allNursingNotes[allNursingNotes.length - 1].timestamp;
}
this.generateBarChart(allNoteTimes, notesClickListener, 'Notes', notesBarLabeler,
notesColorSelector, notesChartAssigner, minX, maxX, selectedNoteTime);
const labsClickListener = (e) => {
this.labsTime = new Date(e.x).toLocaleString('en-US', {timeZone: 'UTC'});
this.labsTimestamp = new Date(e.x).getTime() / 1000;
const selectedLabs = this.labResultsByTimestamp[this.labsTimestamp].map(item => item.key);
const resortedLabList = this.labsList.sort((a, b) => {
const aInSelected = selectedLabs.indexOf(a) != -1;
const bInSelected = selectedLabs.indexOf(b) != -1;
if (aInSelected && !bInSelected) {
return -1;
}
if (bInSelected && !aInSelected) {
return 1;
}
if (a in labOrdering && b in labOrdering) {
return labOrdering[a] - labOrdering[b];
}
if (a in labOrdering) {
return -1;
}
if (b in labOrdering) {
return 1;
}
else {
return a - b;
}});
// Reset labsList to rerender sparklines.
this.labsList = [];
requestAnimationFrame(() => this.labsList = resortedLabList);
this.selectedLabsIndex = e.index;
this.labsChart.flush();
};
const labResultsList = Object.keys(this.labResultsByTimestamp).sort();
const labsBarLabeler = (v, id, i, j) => {
if (!v) {
return null;
}
const frac = v - Math.floor(v);
let index = 0;
if (frac > 0) {
if (frac < 0.09) {
index = +(frac.toFixed(3).substring(3));
}
else {
index = +(frac.toFixed(1).substring(2));
}
}
const numLabs = this.labResultsByTimestamp[labResultsList[index]] ? this.labResultsByTimestamp[labResultsList[index]].length : 0;
return numLabs != 1 ? numLabs + ' labs' :
this.labResultsByTimestamp[labResultsList[index]][0].key.length < 10 ?
this.labResultsByTimestamp[labResultsList[index]][0].key :
this.labResultsByTimestamp[labResultsList[index]][0].key.substring(0, 8) + '...';
}
const labsColorSelector = (color, d) => typeof d ==='object' && 'index' in d && d.index == this.selectedLabsIndex ? '#1ca4bc' : '#F2946B';
const labsChartAssigner = chart => this.labsChart = chart;
let selectedLabsTime = labResultsList.length > 0 ? labResultsList[labResultsList.length - 1] : 0;
// Find the latest labs time with more than one lab, to pre-select.
for (let i = labResultsList.length - 1; i >= 0; i--) {
if (this.labResultsByTimestamp[labResultsList[i]] && this.labResultsByTimestamp[labResultsList[i]].length > 1) {
selectedLabsTime = labResultsList[i];
break;
}
}
this.generateBarChart(labResultsList, labsClickListener, 'Labs', labsBarLabeler,
labsColorSelector, labsChartAssigner, minX, maxX, selectedLabsTime);
for (idx in allEvents) {
const event = allEvents[idx];
if (event.category != currentCat) {
currentCat = event.category;
currentCatSelection = holder.append('div').classed('cat-holder', true);
const vitalsHeadingHolder = currentCatSelection.append('div');
vitalsHeadingHolder.classed('vitals-top-holder', true);
vitalsHeadingHolder.append('div').classed('cat-holder-inner', true).html(currentCat);
const xAxisVis = vitalsHeadingHolder.append("svg:svg")
.attr('class', 'vitals-axis-holder')
.attr("width", xAxisWidth)
.attr("height", xAxisHeight)
.attr('overflow', 'auto');
xAxisVis.append("g")
.attr("class", "xaxis")
.attr("transform", "translate(0," + xAxisHeight + ")")
.call(xAxis.tickFormat(d3.timeFormat("%I %p")).tickSize(-10));
}
const keyAndChartDiv = currentCatSelection.append('div').classed(
'key-and-chart-holder', true);
const chartUnits = this.getKeyUnits(event.key);
const chartRange = this.getKeyRange(event.key);
const chartTitleDiv = keyAndChartDiv.append('div');
chartTitleDiv.classed('key-holder', true).html(event.key);
if (chartUnits != null) {
chartTitleDiv.append('span').classed('chart-units', true).classed('lab-units', true).html('(' + chartUnits + ')');
}
const chartDiv = keyAndChartDiv.append('div')
.classed('chart-holder', true)
.append('div')
.classed('chart-holder-inner', true);
let x = Object.keys(event.events).filter(time => event.events[time].value_num != null);
let y = x.map(time => event.events[time].value_num);
if (!y.some((el) => el != null)) {
continue;
}
const key = event.key;
y = [event.key].concat(y);
x = x.map((x) => x * 1000);
x = ['time'].concat(x);
const gridData = {y: {show: true}};
if (chartRange != null) {
gridData.y['lines'] = [
{value: +chartRange[0], class: 'limit-line'},
{value: +chartRange[1], class: 'limit-line'},
];
}
const colorSelector = (color, d) => '#db5825';
const chart = c3.generate({
bindto: chartDiv,
data: {
x: 'time',
xFormat: '%Y-%m-%dT%H:%M:%S.%LZ',
columns: [x, y],
labels: {
format: (v, id, i, j) => +(+v).toFixed(2) + '',
},
color: colorSelector,
},
size: {
height: 60,
},
axis: {
x: {show: false, type: 'timeseries', localtime: false, min: minX * 1000, max: maxX * 1000},
y: {show: true, tick: { count: 3, format: d3.format('.1f')}, padding: {top: 20, bottom: 6} }
},
legend: {
show: false
},
grid: gridData,
});
}
},
generateBarChart(timeList, clickListener, label, barLabeler, colorSelector, assigner, minX,
maxX, timeOfPreselectedNote) {
const holder = d3.select(this.$.chartstop).append('div').classed('cat-holder', true);
const keyAndChartDiv = holder.append('div').classed(
'key-and-chart-holder', true);
keyAndChartDiv.append('div').classed('bar-key-holder', true).html(label);
const barDiv = keyAndChartDiv.append('div').classed('chart-holder', true).append('div')
.classed('chart-holder-inner-bar', true).classed('chart-holder-inner', true);
const allTimes = {};
timeList.sort();
timeList.forEach((time, i) => {
allTimes[time] = i;
});
// Add blank entries for times along the time axis to ensure true bars are placed correctly.
let curTime = minX;
const newTimes = []
while (curTime < maxX) {
if (timeList.indexOf(curTime) == -1) {
newTimes.push(curTime);
curTime += 60 * 60;
}
}
let times = Object.keys(allTimes).concat(newTimes).sort();
const selectedItem = {x: new Date(timeOfPreselectedNote * 1000),
index: times.indexOf(timeOfPreselectedNote)};
let y = times.map((time, i) => {
if (allTimes[time] == null) {
return 0;
}
if (timeList.indexOf(time) < 10) {
return 10 + (+('.' + timeList.indexOf(time).toString()));
} else {
return 10 + (+('.0' + timeList.indexOf(time).toString()));
}
return allTimes[time] == null ? 0 : 10 + (+('.' + timeList.indexOf(time).toString()));
});
y = ['notes'].concat(y);
times = times.map((x) => x * 1000);
times = ['time'].concat(times);
const chart = c3.generate({
bindto: barDiv,
data: {
x: 'time',
xFormat: '%Y-%m-%dT%H:%M:%S.%LZ',
columns: [times, y],
labels: {
format: (v, id, i, j) => barLabeler(v, id, i, j)
},
color: colorSelector,
type: 'bar',
onclick: clickListener,
},
size: {
height: 70,
},
axis: {
x: {show:false, type: 'timeseries', localtime: false, min: minX * 1000, max: maxX * 1000},
y: {show:false}
},
legend: {
show: false
},
tooltip: {
show: false
},
bar: {
width: 20
},
});
assigner(chart);
clickListener(selectedItem);
}
});
</script>
</dom-module>