-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiobio.viz.js
4171 lines (3543 loc) · 117 KB
/
iobio.viz.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
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
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var hasOwn = Object.prototype.hasOwnProperty;
var toStr = Object.prototype.toString;
var undefined;
var isArray = function isArray(arr) {
if (typeof Array.isArray === 'function') {
return Array.isArray(arr);
}
return toStr.call(arr) === '[object Array]';
};
var isPlainObject = function isPlainObject(obj) {
'use strict';
if (!obj || toStr.call(obj) !== '[object Object]') {
return false;
}
var has_own_constructor = hasOwn.call(obj, 'constructor');
var has_is_property_of_method = obj.constructor && obj.constructor.prototype && hasOwn.call(obj.constructor.prototype, 'isPrototypeOf');
// Not own constructor property must be Object
if (obj.constructor && !has_own_constructor && !has_is_property_of_method) {
return false;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
var key;
for (key in obj) {}
return key === undefined || hasOwn.call(obj, key);
};
module.exports = function extend() {
'use strict';
var options, name, src, copy, copyIsArray, clone,
target = arguments[0],
i = 1,
length = arguments.length,
deep = false;
// Handle a deep copy situation
if (typeof target === 'boolean') {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
} else if ((typeof target !== 'object' && typeof target !== 'function') || target == null) {
target = {};
}
for (; i < length; ++i) {
options = arguments[i];
// Only deal with non-null/undefined values
if (options != null) {
// Extend the base object
for (name in options) {
src = target[name];
copy = options[name];
// Prevent never-ending loop
if (target === copy) {
continue;
}
// Recurse if we're merging plain objects or arrays
if (deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) {
if (copyIsArray) {
copyIsArray = false;
clone = src && isArray(src) ? src : [];
} else {
clone = src && isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
target[name] = extend(deep, clone, copy);
// Don't bring in undefined values
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
// Return the modified object
return target;
};
},{}],2:[function(require,module,exports){
var box = function() {
// Defaults
var value = function(d) { return +d },
quartiles = function(d) { return [d3.quantile(d, .25),d3.quantile(d, .5),d3.quantile(d, .75)]; },
whiskers = function(d) { return [0, d.length - 1]; },
includeData = true,
includeOutliers = true,
modifiedBoxPlot = true;
function layout(data) {
// Sort data and Compute the numeric values for each data element.
data = data.sort(function(i,j) { return value(j) - value(i); })
var values = data.map(function(d, i) { return value.call(this,d,i) });
// Compute quartiles
var quartileData = quartiles.call(this,values),
q1 = quartileData[2],
q3 = quartileData[0],
iqr = (q3-q1) * 1.5;
// if modified box plot, then use iqr to determine outliers
if(modifiedBoxPlot) {
var outliers = [];
var filtered = [];
values.forEach(function(d) {
if ( d>=(q1-iqr) && d <=(q3+iqr) )
filtered.push(d);
else
outliers.push(d);
})
values = filtered;
}
// Compute whiskers. Must return exactly 2 elements, or null.
var whiskerIndices = whiskers && whiskers.call(this, values),
whiskerData = whiskerIndices && whiskerIndices.map(function(i) { return +values[i]; });
var boxData = {
'quartiles' : quartileData,
'whiskers' : whiskerData
};
if (includeOutliers) boxData.outliers = outliers || [];
if (includeData) boxData.data = data;
return boxData;
}
/*
* Specifies the value function, which returns a nonnegative numeric value
* for each datum. The default value function is `return d`. The value function
* is passed two arguments: the current datum and the current index.
*/
layout.value = function(_) {
if (!arguments.length) return value;
value = _;
return layout;
};
/*
* Boolean for including the data in the final product or not
*/
layout.includeData = function(_) {
if (!arguments.length) return includeData;
includeData = _;
return layout;
};
/*
* Boolean for including the outliers in the final product or not
*/
layout.includeOutliers = function(_) {
if (!arguments.length) return includeOutliers;
includeOutliers = _;
return layout;
};
/*
* A modified box plot uses the interquartile range to determine the whisers.
* A standard box plot defines the whiskers by the max and min value.
* Default: true
*/
layout.modifiedBoxPlot = function(_) {
if (!arguments.length) return modifiedBoxPlot;
modifiedBoxPlot = _;
return layout;
};
/*
* Specifies how the quartiles are calculated
*
*/
layout.quartiles = function(_) {
if (!arguments.length) return quartiles;
quartiles = _;
return layout;
};
return layout;
};
module.exports = box;
},{}],3:[function(require,module,exports){
var utils = require('../utils.js');
var graph = function() {
// Defaults
var sources = function(d) { return d.sources },
targets = function(d) { return d.targets },
position = function(d) { return d.position };
function layout(root) {
var nodes = [];
var visited = {};
var uid = utils.getUID();
var stack = [ root ];
while ((node = stack.pop()) != null) {
if (node._visited == uid) continue;
nodes.push(node);
// mark as visited
node._visited = uid;
// see if multiple variants at this position
var v = visited[position(node)] || (visited[position(node)]=[]);
v.push(node);
if (v.length ==1 )
node.y = 0;
else
for (var i=0; i<v.length; i++) {v[i].y = (i/(v.length-1) || 0) * 2 - 1;}
// push unvisited neighbors on stack
var neighbors = [].concat(sources(node), targets(node));
stack = stack.concat( neighbors.filter(function(a) {return a._visited != uid;}) )
}
return nodes;
}
/*
* Identifies the links between all nodes
*/
layout.links = function(nodes) {
var links = [];
nodes.forEach(function(node) {
(node.targets || []).map(function(target) {
links.push( {
'source': node,
'target': target
});
});
})
return links;
}
/*
* Specifies the value function *sources*, which returns an array of node objects
* for each datum. The default value function is `return sources`. The value function
* is passed two arguments: the current datum and the current index.
*/
layout.sources = function(_) {
if (!arguments.length) return sources;
sources = _;
return chart;
}
/*
* Specifies the value function *targets*, which returns an array of node objects
* for each datum. The default value function is `return targets`. The value function
* is passed two arguments: the current datum and the current index.
*/
layout.targets = function(_) {
if (!arguments.length) return targets;
targets = _;
return chart;
}
/*
* Specifies the value function *position*, which returns a nonnegative numeric value
* for each datum. The default value function is `return position`. The value function
* is passed two arguments: the current datum and the current index.
*/
layout.position = function(_) {
if (!arguments.length) return position;
position = _;
return chart;
}
// TODO: do these functions still make sense?
// layout.size = function(x) {
// if (!arguments.length) return nodeSize ? null : size;
// nodeSize = (size = x) == null ? sizeNode : null;
// return tree;
// };
// layout.nodeSize = function(x) {
// if (!arguments.length) return nodeSize ? size : null;
// nodeSize = (size = x) == null ? null : sizeNode;
// return tree;
// };
return layout;
};
module.exports = graph;
},{"../utils.js":10}],4:[function(require,module,exports){
var layout = {};
// add layouts
layout.pileup = require('./pileup.js');
layout.graph = require('./graph.js');
layout.pointSmooth = require('./pointSmooth.js');
layout.outlier = require('./outlier.js');
layout.box = require('./box.js');
module.exports = layout;
},{"./box.js":2,"./graph.js":3,"./outlier.js":5,"./pileup.js":6,"./pointSmooth.js":7}],5:[function(require,module,exports){
var outlier = function() {
// Defaults
var value = function(d) { return d[0]; },
count = function(d) { return d[1]; };
function layout(data) {
var realMin = d3.min(data, function(d,i) {
return value(d);
});
var realMax = d3.max(data, function(d,i) {
return value(d);
});
var max = Math.abs(realMin) + Math.abs(realMax);
var scale = d3.scale.linear().domain([realMin,realMax]).range([0,max]);
data = data.sort(function(aItem,bItem) {
var a = value(aItem);
var b = value(bItem);
return a > b ? 1 : (a < b ? -1 : 0);
})
var q1 = quantile(data, scale, 0.25);
var q3 = quantile(data, scale, 0.75);
var iqr = (q3-q1) * 1.5; //
var filteredData = data.filter(function(d) {
return (scale(value(d)) >= (Math.max(q1-iqr,0)) && scale(value(d)) <= (q3+iqr));
});
return filteredData;
}
/*
* Determines quantile of array with given p
*/
function quantile(arr, scale, p) {
var length = arr.reduce(function(previousValue, currentValue, index, array){
return previousValue + count(currentValue);
}, 0) - 1;
var H = length * p + 1,
h = Math.floor(H);
var hValue, hMinus1Value, currValue = 0;
for (var i=0; i < arr.length; i++) {
currValue += count(arr[i]);
if (hMinus1Value == undefined && currValue >= (h-1))
hMinus1Value = scale(value(arr[i]));
if (hValue == undefined && currValue >= h) {
hValue = scale(value(arr[i]));
break;
}
}
var v = +hMinus1Value, e = H - h;
return e ? v + e * (hValue - v) : v;
}
/*
* Specifies the value function *value*, which returns a nonnegative numeric value
* for each datum. The default value function is `return d[0]`. The value function
* is passed two arguments: the current datum and the current index.
*/
layout.value = function(_) {
if (!arguments.length) return value;
value = _;
return layout;
};
/*
* Specifies the value function *count*, which returns a nonnegative numeric value
* for each datum. The default value function is `return d[1]`. The value function
* is passed two arguments: the current datum and the current index.
*/
layout.count = function(_) {
if (!arguments.length) return count;
count = _;
return layout;
};
return layout;
};
module.exports = outlier;
},{}],6:[function(require,module,exports){
var pileup = function() {
// Defaults
var startValue = function(d) { return d.start; },
endValue = function(d) { return d.end; },
sort = 'default',
size = 400,
buffer = 0;
function layout(data) {
// Compute the numeric values for each data element.
var values = data.map(function(d, i) { return [+startValue.call(layout, d, i),+endValue.call(layout, d, i)]; });
var xScale = d3.scale.linear()
.domain( [values[0][0], values[values.length-1][1]] )
.range([0, size]);
// Optionally sort the data.
var index = d3.range(data.length);
if (sort != null) index.sort(sort === 'default'
? function(i, j) { return values[j][0] - values[i][0]; }
: function(i, j) { return sort(data[i], data[j]); });
// Compute the piles!
// They are stored in the original data's order.
// TODO: handle widhts that are less than a pixel
var step;
var piles = [];
var furthestRight = [];
// initialize piles
var currPile = [];
var prevPile = [];
var prevPrevPile = [];
// initialize indices
var prevPileIndex = 1;
index.forEach(function(i) {
var start = values[i][0];
var end = values[i][1];
step = undefined;
for ( var k=0; k < furthestRight.length; k++) {
if ( (xScale(furthestRight[k])+buffer) < xScale(start) ) {
step = k;
furthestRight[k] = end;
break;
}
}
if (step == undefined) { step = furthestRight.length; furthestRight.push(end) }
piles[i] = {
data: data[i],
x: start,
w: end-start,
y: step
};
});
return piles;
}
/*
* Specifies the value function *start*, which returns a nonnegative numeric value
* for each datum. The default value function is `return start`. The value function
* is passed two arguments: the current datum and the current index.
*/
layout.start = function(_) {
if (!arguments.length) return startValue;
startValue = _;
return layout;
};
/*
* Specifies the value function *end*, which returns a nonnegative numeric value
* for each datum. The default value function is `return end`. The value function
* is passed two arguments: the current datum and the current index.
*/
layout.end = function(_) {
if (!arguments.length) return endValue;
endValue = _;
return layout;
};
/*
* Specifies the x scale for the layout. This is necessary to accurately predict
* which features will overlap in pixel space.
*/
layout.size = function(_) {
if (!arguments.length) return size;
size = _;
return layout;
};
/*
* Specifies the buffer needed between features to not be considered an overlap
*/
layout.buffer = function(_) {
if (!arguments.length) return buffer;
buffer = _;
return layout;
};
/*
* Specifies the sort function to be used or null if no sort
*/
layout.sort = function(_) {
if (!arguments.length) return sort;
sort = _;
return layout;
};
return layout;
};
module.exports = pileup;
},{}],7:[function(require,module,exports){
var pointSmooth = function() {
// Defaults
var pos = function(d) { return d.pos; },
depth = function(d) { return d.depth; },
size = 400;
epsilonRate = 0.3;
function layout(data) {
// Compute the numeric values for each data element and keep original data.
var points = data.map(function(d, i) {
return {
data: d,
pos: +pos.call(layout, d, i),
depth: +depth.call(layout, d, i)
};
});
var epislon = parseInt( epsilonRate * (points[points.length-1].pos - points[0].pos) / size );
// Compute the points!
// They are stored in the original data's order.
points = properRDP(points, epislon);
return points;
}
/*
* Specifies the value function *pos*, which returns a nonnegative numeric value
* for each datum. The default value function is `return pos`. The value function
* is passed two arguments: the current datum and the current index.
*/
layout.pos = function(_) {
if (!arguments.length) return pos;
pos = _;
return layout;
};
/*
* Specifies the value function *depth*, which returns a nonnegative numeric value
* for each datum. The default value function is `return depth`. The value function
* is passed two arguments: the current datum and the current index.
*/
layout.depth = function(_) {
if (!arguments.length) return depth;
depth = _;
return layout;
};
/*
* Specifies the x scale for the layout. This is necessary to accurately predict
* how smoothing will be necessary i.e. smaller size has less resolution and will
* require more smoothing.
*/
layout.size = function(_) {
if (!arguments.length) return size;
size = _;
return layout;
};
/*
* Specifies the epislon rate to determine the aggressiveness of the smoothing
*/
layout.epsilonRate = function(_) {
if (!arguments.length) return epsilonRate;
epsilonRate = _;
return layout;
};
return layout;
};
module.exports = pointSmooth;
/*
* properRDP
*
* @licence Feel free to use it as you please, a mention of my name is always nice.
*
* Marius Karthaus
* http://www.LowVoice.nl
*
*/
function properRDP(points,epsilon){
var firstPoint=points[0];
var lastPoint=points[points.length-1];
if (points.length<3){
return points;
}
var index=-1;
var dist=0;
for (var i=1;i<points.length-1;i++){
var cDist=findPerpendicularDistance(points[i],firstPoint,lastPoint);
if (cDist>dist){
dist=cDist;
index=i;
}
}
if (dist>epsilon){
// iterate
var l1=points.slice(0, index+1);
var l2=points.slice(index);
var r1=properRDP(l1,epsilon);
var r2=properRDP(l2,epsilon);
// concat r2 to r1 minus the end/startpoint that will be the same
var rs=r1.slice(0,r1.length-1).concat(r2);
return rs;
}else{
return [firstPoint,lastPoint];
}
}
function findPerpendicularDistance(p, p1,p2) {
// if start and end point are on the same x the distance is the difference in X.
var result;
var slope;
var intercept;
if (p1.pos==p2.pos){
result=Math.abs(p.pos-p1.pos);
}else{
slope = (p2.depth - p1.depth) / (p2.pos - p1.pos);
intercept = p1.depth - (slope * p1.pos);
result = Math.abs(slope * p.pos - p.depth + intercept) / Math.sqrt(Math.pow(slope, 2) + 1);
}
return result;
}
},{}],8:[function(require,module,exports){
var svg = {};
// add shapes
svg.variant = require('./variant.js');
module.exports = svg;
},{"./variant.js":9}],9:[function(require,module,exports){
var variant = function() {
// Value transformers
var xValue = function(d) { return d.x; },
yValue = function(d) { return d.y; },
wValue = function(d) { return d.w; },
hValue = function(d) { return d.h; };
var diagonal = d3.svg.diagonal()
function shape(d, i) {
diagonal
.source(function(d) { return {"x":hValue(d)*d.y, "y":d.x+Math.abs(d.w/2)}; })
.target(function(d) { return {"x":0, "y":d.x+d.w/2+Math.abs(d.w/2)}; })
.projection(function(d) { return [d.y, d.x]; });
var variantH = hValue(d);
var bulbW = Math.abs(variantH * 5/6);
// Create control points
var c1 = variantH * 1/6+yValue(d),
c2 = variantH*2/6+yValue(d),
c3 = variantH*0.625+yValue(d),
c4 = variantH*1.145+yValue(d);
if (wValue(d) <= Math.abs(bulbW/2))
return "M" +xValue(d)+","+yValue(d)+" C" +xValue(d)+ "," +c1+" "+parseInt(xValue(d)+wValue(d)/2-bulbW/2)+ "," +c2+" "+parseInt(xValue(d)+wValue(d)/2-bulbW/2)+ "," +c3+" C" +parseInt(xValue(d)+wValue(d)/2-bulbW/2)+ "," +c4+" "+parseInt(xValue(d)+wValue(d)/2+bulbW/2)+ "," +c4+" "+parseInt(xValue(d)+wValue(d)/2+bulbW/2)+ "," +c3+" C" +parseInt(xValue(d)+wValue(d)/2+bulbW/2)+ "," +c2+" "+parseInt(xValue(d)+wValue(d))+"," +c1+" "+parseInt(xValue(d)+wValue(d))+","+yValue(d);
else
return diagonal(d)+diagonal({x:xValue(d), y:yValue(d), w:-wValue(d)});
}
/*
* Specifies the value function *x*, which returns an integer for each datum
* The value function is passed two arguments: the current datum and the current index.
*/
shape.xValue = function(_) {
if (!arguments.length) return xValue;
xValue = _;
return shape;
}
/*
* Specifies the value function *y*, which returns an integer for each datum
* The value function is passed two arguments: the current datum and the current index.
*/
shape.yValue = function(_) {
if (!arguments.length) return yValue;
yValue = _;
return shape;
};
/*
* Specifies the value function *width*, which returns an integer for each datum
* The value function is passed two arguments: the current datum and the current index.
*/
shape.wValue = function(_) {
if (!arguments.length) return wValue;
wValue = _;
return shape;
};
/*
* Specifies the value function *height*, which returns an integer for each datum
* The value function is passed two arguments: the current datum and the current index.
*/
shape.hValue = function(_) {
if (!arguments.length) return hValue;
hValue = _;
return shape;
};
return shape;
};
module.exports = variant;
},{}],10:[function(require,module,exports){
module.exports.format_unit_names = function(d) {
if ((d / 1000000) >= 1)
d = d / 1000000 + "M";
else if ((d / 1000) >= 1)
d = d / 1000 + "K";
return d;
}
module.exports.format_percent = function(d, precision_places) {
var precision_places = precision_places || 1;
var corrector = 1;
for (var i=0; i < precision_places; i++) { corrector *= 10}
var percent = parseInt( d * (corrector*100) ) / corrector;
return percent;
}
module.exports.getUID = function(separator) {
var delim = separator || "-";
function S4() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
return (S4() + S4() + delim + S4() + delim + S4() + delim + S4() + delim + S4() + S4() + S4());
}
module.exports.value_accessor = function(value, d,i) {
return typeof value === 'function' ? value(d,i) : value;
}
module.exports.tooltipHelper = function(selection, tooltipElem, titleAccessor) {
var utils = require('./utils.js')
selection
.on("mouseover", function(d,i) {
utils.showTooltip(tooltipElem, titleAccessor, d);
})
.on("mouseout", function(d) {
utils.hideTooltip(tooltipElem);
})
}
module.exports.showTooltip = function(tooltipElem, titleAccessor, d) {
var utils = require('./utils.js')
var tooltipStr = utils.value_accessor(titleAccessor, d); // handle both function and constant string
var opacity = tooltipStr ? .9 : 0; // don't show if tooltipStr is null
var elemHeight = tooltipElem.node().getBoundingClientRect().height
tooltipElem.transition()
.duration(200)
.style("opacity", opacity);
tooltipElem.html(tooltipStr)
.style("left", (d3.event.clientX + 8) + "px")
.style("text-align", 'left')
.style("top", (d3.event.clientY - elemHeight - 8) + "px");
}
module.exports.endAll = function (transition, callback) {
var n;
if (transition.empty()) {
callback();
}
else {
n = transition.size();
transition.each("end", function () {
n--;
if (n === 0) {
callback();
}
});
}
}
// Takes svg and looks for matching styles and explicity defines them
// in a <styles> tag inside the svg elem.
module.exports.addStylesToSvg = function(svg) {
var used = "";
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
var rules = sheets[i].cssRules;
if(rules==null) continue;
for (var j = 0; j < rules.length; j++) {
var rule = rules[j];
if (typeof(rule.style) != "undefined") {
var elems = svg.querySelectorAll(rule.selectorText);
if (elems.length > 0) {
used += rule.selectorText + " { " + rule.style.cssText + " }\n";
}
}
}
}
var s = document.createElement('style');
s.setAttribute('type', 'text/css');
// s.innerHTML = "<![CDATA[\n" + used + "\n]]>";
s.innerHTML = used;
var defs = document.createElement('defs');
defs.appendChild(s);
svg.insertBefore(defs, svg.firstChild);
}
module.exports.hideTooltip = function(tooltipElem) {
tooltipElem.transition()
.duration(500)
.style("opacity", 0);
}
// Copies a variable number of methods from source to target.
module.exports.rebind = function(target, source) {
var i = 1, n = arguments.length, method;
while (++i < n) target[method = arguments[i]] = iobio_rebind(target, source, source[method]);
return target;
};
// Method is assumed to be a standard D3 getter-setter:
// If passed with no arguments, gets the value.
// If passed with arguments, sets the value and returns the target.
function iobio_rebind(target, source, method) {
return function() {
var value = method.apply(source, arguments);
return value === source ? target : value;
};
}
},{"./utils.js":10}],11:[function(require,module,exports){
(function (global){
/* Chase Miller (2015-2016) */
// Grab an existing iobio namespace object, or create a blank object
// if it doesn't exist
var iobio = global.iobio || {};
global.iobio = iobio;
// export if being used as a node module - needed for test framework
if ( typeof module === 'object' ) { module.exports = iobio;}
// Add visualizations
iobio.viz = require('./viz/viz.js')
// Add layouts
iobio.viz.layout = require('./layout/layout.js')
// Add shapes
iobio.viz.svg = require('./svg/svg.js')
// Add utils
iobio.viz.utils = require('./utils.js')
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"./layout/layout.js":4,"./svg/svg.js":8,"./utils.js":10,"./viz/viz.js":26}],12:[function(require,module,exports){
var alignment = function() {
// Import base chart
var base = require('./base.js')(),
utils = require('../utils.js'),
extend = require('extend');
// Value transformers
var directionValue = null;
// Defaults
var elemHeight = 4,
orientation = 'down',
events = [],
tooltip;
// Default Options
var defaults = { };
function chart(selection, opts) {
// Merge defaults and options
var options = {};
extend(options, defaults, opts);
// Call base chart
base.call(this, selection, options);
// Grab base functions for easy access
var x = base.x(),
y = base.y(),
id = base.id();
xValue = base.xValue(),
yValue = base.yValue(),
wValue = base.wValue(),
yAxis = base.yAxis(),
color = base.color(),
transitionDuration = base.transitionDuration();
// Change orientation of pileup
if (orientation == 'down') {
// swap y scale min and max
y.range([y.range()[1],y.range()[0]]);
// update y axis
if(yAxis)
selection.select(".iobio-y.iobio-axis").transition()
.duration(0)
.call(yAxis);
}
// Draw
var g = selection.select('g.iobio-container').classed('iobio-alignment', true); // grab container to draw into (created by base chart)
var aln = g.selectAll('.alignment')
.data(selection.datum());
// Enter
aln.enter().append('g')
.attr('id', function(d) { return id(d)})
.attr('class', 'alignment')
.attr('transform', function(d,i) {
var translate = 'translate('+parseInt(x(xValue(d,i) + wValue(d,i)/2))+','+ parseInt(y(yValue(d,i))-elemHeight/2) + ')'
if (directionValue && directionValue(d,i) == 'reverse')
return translate + ' rotate(180)';
else
return translate;
})
.style('fill', color)
.append('polygon')
.attr('points', function(d) {
// var rW = x(xValue(d)+wValue(d)) - x(xValue(d));
var rH = elemHeight;
// var arrW = Math.min(5, rW);
if (directionValue) // draw arrow
return ('-0.1,' + (-rH/2) +
' 0,' + (-rH/2) +
' 0.1,0' +
' 0,' + (rH/2) +
' -0.1,' + (rH/2));
else // draw rectangle
return ('-0.1,' + (-rH/2) +
' 0,' + (-rH/2) +
' 0,' + (rH/2) +
' -0.1,' + (rH/2));
})
aln.exit()
aln.transition()
.duration(transitionDuration)
.attr('transform', function(d,i) {
var translate = 'translate('+parseInt(x(xValue(d,i) + wValue(d,i)/2))+','+ parseInt(y(yValue(d,i))-elemHeight/2) + ')'
if (directionValue && directionValue(d,i) == 'reverse')
return translate + ' rotate(180)';
else
return translate;
})
.style('fill', color);
aln.select('polygon').transition()
.duration(transitionDuration)
.attr('points', function(d,i) {
var rW = x(xValue(d,i)+wValue(d,i)) - x(xValue(d,i));
var rH = elemHeight;
var arrW = Math.min(5, rW);
if (directionValue)
return ((-rW/2) + ',' + (-rH/2) + ' '
+ (rW/2-arrW) + ',' + (-rH/2) + ' '
+ (rW/2) + ',0 '
+ (rW/2-arrW) + ',' + (rH/2) + ' '
+ (-rW/2) + ',' + (rH/2));
else
return ((-rW/2) + ',' + (-rH/2) + ' '
+ (rW/2) + ',' + (-rH/2) + ' '
+ (rW/2) + ',' + (rH/2) + ' '
+ (-rW/2) + ',' + (rH/2));
})