-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicu_vis.js
429 lines (356 loc) · 12.6 KB
/
picu_vis.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
var m = [80, 100, 80, 80],
w = 930 - m[1] - m[3],
h = 250 - m[0] - m[2],
h0 = m[0];
parse = d3.time.format("%Y-%m-%dT%H:%M:%SZ").parse;
// Scales and axes. Note the inverted domain for the y-scale: bigger is up!
var x = d3.time.scale().range([0, w]),
xConstant = d3.time.scale().range([0, w]),
y = d3.scale.linear().range([h, 0]),
xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(false),
xConstantAxis = d3.svg.axis().scale(xConstant).tickSize(-30).tickSubdivide(false).orient("top"),
yAxis = d3.svg.axis().scale(y).ticks(4).orient("right");
var timeDomain;
// An area generator, for the light fill.
var area = d3.svg.area()
//.interpolate("monotone")
.x(function(d) { return x(d.time); })
.y0(h)
.y1(function(d) { return y(d.value); });
// A line generator, for the dark stroke.
var line = d3.svg.line()
//.interpolate("monotone")
.x(function(d) { return x(d.time); })
.y(function(d) { return y(d.value); });
// List to keep track of charts
var chart_data = [];
// Map to keep track of actual data names
var data_names = {};
// Variable to keep track of data
var data;
// Calculated values
var calculatedValues = ["PaO2 / FiO2", "SaO2 / FiO2"];
// Interention variables
var interventionVariables = ["Dopamine", "Epinephrine", "Mechanical Ventilation", "PT", "PT control", "PTT", "PTT control"];
d3.json("patient_data.json", function(json) {
// Keep track of data to add new graphs
data = json;
timeDomain = getTotalTimeOfStay(json);
// Draw timeline
drawTimeline(timeDomain);
// Adds text boxes for variable names
addInterventionCheckBoxes(json);
addVariableCheckBoxes(json);
addCalculatedValues(json);
// Allows toggling the graph on and off
$('input#variable').click(toggleGraphOnClick);
// Draw charts
drawChart("Dopamine", "Dopamine", json["Dopamine"], timeDomain, "intervention")
drawChart("Heart Rate", "HR", json["HR"], timeDomain, "variable");
drawChart("Temperature", "Temp Value", json["Temp Value"], timeDomain, "variable");
drawChart("SF Ratio", "SaO2 / FiO2", json["SaO2 / FiO2"], timeDomain, "variable");
drawChart("PF Ratio", "PaO2 / FiO2", json["PaO2 / FiO2"], timeDomain, "variable");
});
// Appends the graph or hides it on checkbox click
function toggleGraphOnClick() {
var graphName = $(this).attr("name");
var graph = $('#' + graphName);
if(graph.length == 1) {
graph.toggle('slow');
}
else {
var dataName = data_names[graphName];
if ($.inArray(dataName, interventionVariables) == -1)
drawChart(dataName, dataName, data[dataName], timeDomain, "variable");
else
drawChart(dataName, dataName, data[dataName], timeDomain, "intervention");
}
}
function drawTimeline(timeDomain) {
var h = 50;
// create svg for the timeline
var context = d3.select("#timeline")
.append("p")
.html("Time Range Selector")
.append("div")
.append("svg:svg")
.attr("height", 50)
.attr("width", w)
.append("svg:g")
.attr("class", "context");
// X-axis for timeline
xConstant.domain(timeDomain);
// Context view.
context.append("rect")
.attr("width", w)
.attr("height", h)
.attr("fill", "none")
.attr("pointer-events", "all")
.attr("cursor", "crosshair");
// Time selector rectangle within context view
context.append("g")
.attr("class", "brush")
.call(d3.svg.brush().x(xConstant)
.on("brushstart", brushstart)
.on("brush", function() {
// set x axis to time chosen by user
var s = d3.event.target.extent();
var start = s[0];
var end = s[1];
x.domain([start, end]);
// Click in the timeline resets timeline
if (start.toString() == end.toString()) {
x.domain(timeDomain);
d3.selectAll("rect.resize.e").attr("class", "resize e invisible");
d3.selectAll("rect.resize.w").attr("class", "resize w invisible");
} else {
d3.selectAll("rect.resize.e").attr("class", "resize e");
d3.selectAll("rect.resize.w").attr("class", "resize w"); }
// adjust each chart to the new time range
for (i = 0; i < chart_data.length; i++) {
// var t = d3.select(".chart"+i).transition().duration(1);
y.domain([d3.min(chart_data[i], function(d) { return d.value; })*.9,
d3.max(chart_data[i], function(d) { return d.value; })*1.1]).nice();
var chart = d3.select(".chart-area"+i);
chart.select(".x_"+i).call(xAxis);
chart.select(".area_"+i).attr("d", area(chart_data[i]));
chart.select(".line_"+i).attr("d", line(chart_data[i]));
console.log(i + " " + chart.selectAll('.point_'+i)[0].length);
if (chart.selectAll('.point_'+i)[0].length != 0) {
chart.selectAll('.point_'+i).remove();
chart.selectAll('.point_'+i)
.data(chart_data[i])
.enter().append("svg:circle")
.attr("clip-path", "url(#clip)")
.attr("cx", function(d) { return x(d.time); })
.attr("cy", function(d) { return y(d.value); })
.attr("r", 3)
.attr("class", "point point_" + i);
} else {
chart.selectAll('.point_intervention_'+i).remove();
chart.selectAll('.point_intervention_'+i)
.data(chart_data[i])
.enter().append("svg:circle")
.attr("clip-path", "url(#clip)")
.attr("cx", function(d) { return x(d.time); })
.attr("cy", function(d) { return y(d.value); })
.attr("r", 3)
.attr("class", "point intervention point_intervention_" + i);
}
}
})
.on("brushend", brushend))
.selectAll("rect")
.attr("height", h);
// Context view x axis
context.select(".brush").append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0," + 15 + ")")
.call(xConstantAxis);
}
function drawChart(readableName, dataName, values, timeDomain, mode) {
dataName = removeNonParsingChars(dataName);
// Set check box to checked
$('input#variable[name=' + dataName + ']').attr('checked', true);
// Load data
values.forEach(function(d) {
d.time = parse(d.time);
d.value = +d.value;
d.symbol = readableName;
});
// Grab index and store data for resizing charts later
var index = chart_data.length;
chart_data.push(values);
// set x and y
x.domain(timeDomain);
y.domain([d3.min(values, function(d) { return d.value; })*.9,
d3.max(values, function(d) { return d.value; })*1.1]).nice();
var svg = d3.select("#charts")
.append("li")
.attr("id", dataName)
.append("p")
.html("<img src=./button.png />"
+ "<div class=variable-name>" + readableName + "</div>")
.attr("class", "p_chart")
.append("div")
.attr("class", "chart")
.append("svg:svg")
.attr("class", "chart" + index)
.attr("height", h+30) // room for the axis, make a constant later
.attr("width", w+30) // room for the axis, make a constant later
.append("svg:g")
.attr("transform", "translate(0," + 10 + ")")
.attr("class", "chart-area" + index);
// Add the clip path.
svg.append("svg:clipPath")
.attr("id", "clip")
.append("svg:rect")
.attr("width", w)
.attr("height", h);
// Add the area path.
svg.append("svg:path")
.attr("class", "area " + "area_" + index)
.attr("clip-path", "url(#clip)")
.attr("d", area(values));
// Add the x-axis.
svg.append("svg:g")
.attr("class", "x axis " + "x_" + index)
.attr("transform", "translate(0," + h + ")")
.call(xAxis);
// Add the y-axis.
svg.append("svg:g")
.attr("class", "y axis")
.attr("transform", "translate(" + w + ",0)")
.call(yAxis);
if (mode != "intervention") {
// Add the line path.
svg.append("svg:path")
.attr("class", "line " + "line_" + index)
.attr("clip-path", "url(#clip)")
.attr("d", line(values));
}
// Add dots
svg.selectAll('.point')
.data(values)
.enter().append("svg:circle")
.attr("clip-path", "url(#clip)")
.attr("cx", function(d) { return x(d.time); })
.attr("cy", function(d) { return y(d.value); })
.attr("r", 3)
.attr("class", "point point_" +
(mode == "intervention" ? "intervention_" : "") + index
+ (mode == "intervention" ? " intervention" : ""));
}
function createInterventionPlot(interventionValues, interventionNames, timeDomain) {
// Parse time values for data
var timeFormat = d3.time.format("%Y-%m-%dT%H:%M:%SZ");
// Set variables
var h = 300,
// w = 700,
p_x = 40,
p_y = 25,
p_ordinal = 1.0,
fill = d3.scale.category10(),
// x = d3.time.scale().domain(timeDomain).range([p_x, w - p_x]),
y = d3.scale.ordinal().domain(interventionNames)
.rangePoints([h - p_y, p_y], p_ordinal);
// xAxis = d3.svg.axis().scale(x).ticks(xTicksNum).tickSize(5);
var chart = d3.select("#charts")
.append("li")
.append("div");
var name = "Intervention Data";
// Add chart name
chart.append("p")
.html(name);
var vis = chart.append("svg:svg").attr("class", "chart")
.attr("height", h)
.attr("width", w);
var rects = vis.append("svg:g");
// Add rectangles for each variable
for (var variable in interventionValues)
{
// Appends rectangles
rects.selectAll("rect.Shapes")
.data(interventionValues[variable])
.enter().append("svg:rect")
.attr("fill", fill(variable))
.attr("width", 4)
.attr("height", 4)
.attr("x", function (d) { return x(timeFormat.parse(d.time)); })
.attr("y", y(variable));
}
var axisGroup = vis.append("svg:g");
// Ordinal Y value labels
axisGroup.selectAll("text.yLabels")
.data(interventionNames)
.enter().append("svg:text")
.text(function (d) { return d; } )
.attr("y", function (d) { return y(d); })
.attr("x", p_x)
.attr("dy", ".35em")
.attr("class", "text.oLabel");
// X axis - Using xAxis callback
axisGroup.append("svg:g")
.attr("class", "xAxis")
.attr("transform", "translate(0," + (h - p_x) + ")")
.call(xAxis);
}
function sortDates(a, b)
{
return a.getTime() - b.getTime();
}
// Returns the earliest and latest date for
// patient information
function getTotalTimeOfStay(data) {
// Parse the time values into date objects
var timeFormat = d3.time.format("%Y-%m-%dT%H:%M:%SZ");
var times = [];
for(var variable in data) {
for(var row in variable) {
if (data[variable][row]) {
times.push(timeFormat.parse(data[variable][row].time));
}
}
}
// Get min and max date
var sorted = times.sort(sortDates);
return([sorted[0], sorted[sorted.length - 1]]);
}
function brushstart() {
}
function brush() {
var s = d3.event.target.extent();
console.log(s[0].toString());
console.log(s[1].toString());
}
function brushend() {
}
// Remove non-HTML parsing characters
function removeNonParsingChars(str) {
return str.replace(/[() "/]/g, '');
}
// Enables dragging of charts
$(function() {
$("#charts").sortable();
$("#charts").disableSelection();
});
// Append a check box for each variable in data
function addVariableCheckBoxes(data) {
var variableList = $(".variable-selection");
for(var variable in data) {
if(isFinite(data[variable][0].value) &&
$.inArray(variable, calculatedValues) == -1 &&
$.inArray(variable, interventionVariables) == -1)
addCheckBox(variableList, variable);
}
}
// Append a check box for each variable in data
function addInterventionCheckBoxes(data) {
var variableList = $(".intervention-variables");
for(var variable in data) {
if(isFinite(data[variable][0].value) &&
$.inArray(variable, calculatedValues) == -1 &&
$.inArray(variable, interventionVariables) != -1)
addCheckBox(variableList, variable);
}
}
function addCalculatedValues(data) {
var variableList = $(".calculated-values");
for(var variable in data) {
if(isFinite(data[variable][0].value) &&
$.inArray(variable, calculatedValues) != -1 &&
$.inArray(variable, interventionVariables) == -1)
addCheckBox(variableList, variable);
}
}
// Appends a check box to the variable list for the specified variable
function addCheckBox(variableList, variable) {
var variableID = removeNonParsingChars(variable);
data_names[variableID] = variable;
var element = '<li>';
element += '<input type="checkbox" name="' + variableID
+ '" id="variable" >';
element += '<span>' + variable + '</span>';
element += '</li>';
variableList.append(element);
}