-
Notifications
You must be signed in to change notification settings - Fork 0
/
cephos_IT_flows.html
522 lines (430 loc) · 15.4 KB
/
cephos_IT_flows.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
<html>
<head>
<title>Psychiatric Patients migration flows in Italy</title>
<meta charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="layout.css">
<link rel="stylesheet" type="text/css" href="vis_layout.css">
<link rel="stylesheet" href="jquery-ui-1.9.2.custom.min.css">
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="jshashtable-2.min.js"></script>
<script type="text/javascript" src="jquery.numberformatter-1.2.3.min.js"></script>
<script type="text/javascript" src="d3.v3.min.js"></script>
<script type="text/javascript" src="flowShape.js"></script>
<script type="text/javascript" src="string-operations.js"></script>
<script type="text/javascript" src="d3.tip.min.js"></script>
<script type="application/javascript">
/* Constant identifiers */
var DIMENSION_GAP = 12.0;
var DIMENSION_LABEL_MARGIN = 30.0;
var AGGREGATE_LABEL_MARGIN = 8.0;
var AGGREGATE_LABEL_MIN_SIZE = 10.0;
var AGGREGATE_LABEL_MAX_SIZE = 14.0;
var AGGREGATE_SCALE_FACTOR = 450.0;
var CURRENT_DIM_POS_X = 40.0;
var FLOW_GAP = 4.0;
var width_per_dimension = 350;
var INTERACTION_WIDTH = 100;
var MARGIN_TOP = 60.0;
var current_year = 2012;
var currDimPosX = CURRENT_DIM_POS_X;
var current_dimension = 0;
var min_value = -1;
var max_value = -1;
var load_finished;
var aggregates = ["patient residence region", "hospitalization region", "hospital ownership"];
var data;
var dimensions;
var divisionColors;
var category20b_sq = ['#393b79', '#bd9e39', '#ad494a', '#637939', '#7b4173', "#003c30","#543005", '#6b6ecf', '#e7ba52','#d6616b','#b5cf6b', '#ce6dbd','#35978f','#bf812d','#000000','#fa0019','#0000ff','#ff7c01','#08f708','#0f8ef5'];
//var category20b_sq = ['#000000','#fa0019','#0000ff'];
var sum_values;
var formatPercentage = d3.format(".0%");
var formatNumber = d3.format(",");
$(document).ready(function(){
initializeFlow();
//Load aggregated data
loadAggregate();
});
function refresh()
{
if(load_finished)
{
loadingFinished(false);
clearSVGCanvas();
loadAggregate();
}
}
function drawDimension(aggregated_data, dimension) {
"use strict";
var sumValue = sumDimension(aggregated_data);
var currPosY = MARGIN_TOP;
var currPosLabelY = MARGIN_TOP;
d3.select('.svg_canvas')
.append('g')
.attr('class', 'dimension_group')
.selectAll('.dimension_line')
.data(aggregated_data)
.enter()
.append('path')
.attr('class', 'dimension_line')
.attr('dim_axis', dimension)
.attr('x', currDimPosX)
.attr('height', function(aggregate){
return (parseFloat(aggregate.value)/sumValue) * AGGREGATE_SCALE_FACTOR;
})
.attr('id', function(aggregate){
return StringOperations.buildID(aggregate.dimension);
})
.attr('d', function(aggregate)
{
var posY = (parseFloat(aggregate.value)/sumValue) * AGGREGATE_SCALE_FACTOR;
var path_data = "M " + currDimPosX + " " + currPosY + " L " + currDimPosX + " " + (currPosY + posY);
d3.select(this).attr('y', currPosY);
d3.select(this).attr('dimValue', aggregate.value);
currPosY += posY + DIMENSION_GAP;
return path_data;
})
.style('stroke-opacity', 0.0)
.style("stroke", function(d) {
return d.color = divisionColors(d.dimension.substring(1, d.dimension.length));})
.transition()
.duration(500)
.style('stroke-opacity', 0.75);
addDimensionLabels(dimension, currDimPosX);
currDimPosX += width_per_dimension;
current_dimension += 1;
if(current_dimension < aggregates.length)
{
loadAggregate();
}
else
{
current_dimension = 0;
currDimPosX = CURRENT_DIM_POS_X;
loadFlows();
}
}
function loadAggregate()
{
d3.json("agg_sum4.json", function(error, json) {
if (error) return console.warn(error);
data = json;
var aggregates = d3.nest().key(function(d) {return d.aggregate;}).entries(data);
dimensions = d3.nest().key(function(d) {return d.dimension.substring(1, d.dimension.length);}).entries(data).map(function (d) {return d.key;});
// divisionColors = d3.scale.category20b().domain(dimensions);
divisionColors = d3.scale.ordinal().domain(dimensions).range(category20b_sq);
drawDimension(aggregates[current_dimension].values, aggregates[current_dimension].key);
});
}
function addDimensionLabels(dimension, currDimPosX)
{
//Add labels of the dimension
d3.select('.svg_canvas')
.append('text')
.text(dimension.toUpperCase())
.attr('class', 'dimension_header')
.attr('x', function() { return currDimPosX;} )
.attr('y', DIMENSION_LABEL_MARGIN)
.style('fill-opacity', 0.0)
.transition()
.duration(2500)
.style('fill-opacity', 1.0);
}
function addAggregateLabels()
{
var svg_canvas = d3.select('.svg_canvas');
d3.selectAll('.dimension_line').each(function(aggregate)
{
var dim_line = d3.select(this);
svg_canvas.append('text')
.text(aggregate.dimension.substring(1,aggregate.dimension.length).replace(" and ", "&"))
.attr('id', (aggregate.dimension))
.attr('class', 'aggregate_label')
.attr('x', parseFloat(dim_line.attr('x')) + AGGREGATE_LABEL_MARGIN)
.attr('y', parseFloat(dim_line.attr('y')) + (parseFloat(dim_line.attr('height')) / 2.0))
.style('font-size', function()
{
if(parseFloat(dim_line.attr('height')) < 10.0)
{
return Math.max(AGGREGATE_LABEL_MIN_SIZE, parseFloat(dim_line.attr('height')));
}
else
{
return AGGREGATE_LABEL_MAX_SIZE;
}
})
.style('fill-opacity', 0.0)
.transition()
.duration(2500)
.style('fill-opacity', 1.0);
});
}
function loadFlows()
{
d3.json("test2-csv.json", function(error, json) {
if (error) return console.warn(error);
data = json;
drawFlows(data);
});
}
function initializeFlow()
{
var svg_width = $('body').width() - $('#navigation').width() - (CURRENT_DIM_POS_X * 2.0) - 100;
d3.select('#content')
.append('svg')
.attr('class', 'svg_canvas')
.attr('width', svg_width);
width_per_dimension = (svg_width / (aggregates.length - .5)) - CURRENT_DIM_POS_X;
}
function drawFlows(raw_flow_data)
{
d3.select('.svg_canvas')
.selectAll('.flow_line')
.data(raw_flow_data)
.enter()
.append('path')
.attr('class', 'flow_line')
.attr('d', function(flow_line)
{
var value = parseFloat(flow_line.value);
if(value > 0.0)
{
var path_data = "";
for(var i = 0; i < aggregates.length - 1; i++)
{
var dimAxisFrom = d3.select("#" + StringOperations.buildID(flow_line[aggregates[i]]));
var dimAxisTo = d3.select("#" + StringOperations.buildID(flow_line[aggregates[i + 1]]));
var startHeight = (value / parseFloat(dimAxisFrom.attr('dimValue'))) * parseFloat(dimAxisFrom.attr('height'));
var endHeight = (value / parseFloat(dimAxisTo.attr('dimValue'))) * parseFloat(dimAxisTo.attr('height'));
var startX = parseFloat(dimAxisFrom.attr('x'));
var startY;
if(dimAxisFrom.attr('currPosY') == null)
startY = parseFloat(dimAxisFrom.attr('y'));
else
startY = parseFloat(dimAxisFrom.attr('currPosY'));
dimAxisFrom.attr('currPosY', startY + startHeight);
var endX = parseFloat(dimAxisTo.attr('x'));
var endY;
if(dimAxisTo.attr('currPosY') == null)
endY = parseFloat(dimAxisTo.attr('y'));
else
endY = parseFloat(dimAxisTo.attr('currPosY'));
if(i == aggregates.length - 2)
dimAxisTo.attr('currPosY', endY + endHeight);
var flowShape = new FlowShape(startX + FLOW_GAP, startY, endX - FLOW_GAP, endY, startHeight, endHeight);
path_data += " " + flowShape.getPathData();
}
return path_data;
}
// else
// {
// return "";
// }
});
// tooltips : show tooltips only for links that are highlighted.
// NOTE: turns out this doesn't make much UI sense; the flows are way too small for tooltips to really show up very easily
// var linkTip = d3.tip()
// .attr('class', 'd3-tip')
// .offset([-10, 50])
// .html(function(d) { return d3.select(this).attr("class") === "flow_line_highlighted" ? "<span style='color:#ef8a62'>" + d.value + "</span> " : "";});
//set up the tool tips
// d3.selectAll(".flow_line").call(linkTip);
// d3.selectAll(".flow_line").on('mouseover', linkTip.show)
// .on('mouseout', linkTip.hide);
addAggregateLabels();
drawInteractionShapes();
loadingFinished(true);
}
function drawInteractionShapes()
{
var svg_canvas = d3.select('.svg_canvas');
d3.selectAll(".dimension_line").each(function()
{
svg_canvas.append('rect')
.data(d3.select(this).data())
.attr('dim_axis', d3.select(this).attr('dim_axis'))
.attr('x', d3.select(this).attr('x') - (INTERACTION_WIDTH / 2.0))
.attr('y', d3.select(this).attr('y'))
.attr('width', INTERACTION_WIDTH)
.attr('height', d3.select(this).attr('height'))
.attr('class', 'interaction_shape')
.on('mouseover', highlightNode)
.on('mouseout', unhighlightNode)
.on('click', highlightFlows);
});
}
function highlightNode() {
var dim = d3.select(this).data()[0].dimension;
var dim_axis = d3.select(this).attr('dim_axis');
d3.selectAll('.dimension_line').filter(function(d) {return d.dimension == dim;}).style("stroke-opacity",0.50);
// d3.selectAll('.aggregate_label,id=' + dim + '"').style("font-color","steelblue");
}
function unhighlightNode() {
var dim = d3.select(this).data()[0].dimension;
d3.selectAll('.dimension_line').filter(function(d) {return d.dimension == dim;}).style("stroke-opacity",0.85);
}
function clearSVGCanvas()
{
d3.selectAll('.flow_line_highlighted').remove();
d3.selectAll('.flow_line').remove();
d3.selectAll('.flow_line_hide').remove();
d3.selectAll('.dimension_group').remove();
d3.selectAll('.dimension_line').remove();
d3.selectAll('.interaction_shape').remove();
d3.selectAll('.aggregate_label').remove();
d3.selectAll('.value_label').remove();
d3.selectAll('.dimension_header').remove();
}
/*-------------*/
/* Interaction */
/*-------------*/
function highlightFlows()
{
var dim = d3.select(this).data()[0].dimension;
var dim_axis = d3.select(this).attr('dim_axis');
deselectFlows();
d3.selectAll('.flow_line')
.attr('class', function(flow_data)
{
if(flow_data[dim_axis] == dim)
return 'flow_line_highlighted';
else
return 'flow_line_hide';
});
showValues();
//showCommentary(dim);
}
function deselectFlows()
{
d3.selectAll('.flow_line_highlighted')
.attr('class', 'flow_line');
d3.selectAll('.flow_line_hide')
.attr('class', 'flow_line');
d3.selectAll('.value_label').remove();
}
function showValues()
{
var highlighted_flows = d3.selectAll('.flow_line_highlighted');
sum_values = new Array();
for(var i = 0; i < aggregates.length; i++)
{
sum_values[i] = new Object();
}
if(!highlighted_flows.empty())
{
highlighted_flows.each(function(flow_data){
for(var i = 0; i < aggregates.length; i++)
{
var dim = flow_data[aggregates[i]];
if(sum_values[i][dim] == undefined)
{
sum_values[i][dim] = parseFloat(flow_data.value);
}
else
{
sum_values[i][dim] += parseFloat(flow_data.value);
}
}
});
}
else
{
d3.selectAll('.flow_line').each(function(flow_data){
for(var i = 0; i < aggregates.length; i++)
{
var dim = flow_data[aggregates[i]];
if(sum_values[i][dim] == undefined)
{
sum_values[i][dim] = parseFloat(flow_data.value);
}
else
{
sum_values[i][dim] += parseFloat(flow_data.value);
}
}
});
}
if(showNumberCheckbox.checked)
{
var svg_canvas = d3.select('.svg_canvas');
d3.selectAll('.dimension_line').each(function(aggregate)
{
var dim_line = d3.select(this);
svg_canvas.append('text')
.text(function()
{
var value = sum_values[jQuery.inArray(dim_line.attr('dim_axis'), aggregates)][aggregate.dimension];
if(value == undefined)
return "";
else
return $.formatNumber(value, {format:"#,###", locale:"us"});
})
.attr('class', 'value_label')
.attr('x', parseFloat(dim_line.attr('x')) - AGGREGATE_LABEL_MARGIN)
.attr('y', parseFloat(dim_line.attr('y')) + (parseFloat(dim_line.attr('height')) / 2.0))
.style('font-size', function()
{
if(parseFloat(dim_line.attr('height')) < 10.0)
{
return Math.max(AGGREGATE_LABEL_MIN_SIZE, parseFloat(dim_line.attr('height')));
}
else
{
return AGGREGATE_LABEL_MAX_SIZE;
}
})
.style('fill-opacity', 0.0)
.transition()
.duration(2500)
.style('fill-opacity', 1.0);
});
}
else
{
d3.selectAll('.value_label').remove();
}
}
function sumDimension(dimension)
{
var totalValue = 0.0;
$.each(dimension, function(index, property)
{
totalValue += parseFloat(property.value);
});
return totalValue;
}
function loadingFinished(ready)
{
load_finished = ready;
}
function getOverview()
{
deselectFlows();
showValues();
//showCommentary(null);
}
function dispatchBar() {
console.log(this.__data__.college);
var bar_dim = this.__data__.college;
// super huge thanks to Mike Bostock and his example for how to do this: http://bl.ocks.org/mbostock/4679202
var shape = d3.selectAll(".interaction_shape").filter(function (d) {return d.dimension == bar_dim;}).each(highlightFlows);
}
</script>
</head>
<body>
<div id="navigation">
<h1>CephosLink project - Italian study Cohort 2012-2013</h1>
<h2>psychiatric hospital discharge migrations in Italy Regions</h2>
<h2>data source:Italian Minister of Health SDO database, Register of discharges from hospitals</h2>
<!-- <button class="overviewbutton" onclick="getOverview()">Reload</button> -->
<div class="box">
<div class="checkboxText">Show numbers of psychiatric hospitalizations: </div>
<input id="showNumberCheckbox" type="checkbox" onClick="showValues()" value='TRUE' >
</div>
</div>
<div>
<div id="content" ></div>
</div>
</body>
</html>