d3js-plugins-rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +28 -0
- data/Rakefile +1 -0
- data/d3js-plugins-rails.gemspec +21 -0
- data/lib/d3js-plugins-rails/version.rb +7 -0
- data/lib/d3js-plugins-rails.rb +10 -0
- data/vendor/assets/javascripts/d3/plugins/.gitignore +1 -0
- data/vendor/assets/javascripts/d3/plugins/LICENSE +26 -0
- data/vendor/assets/javascripts/d3/plugins/Makefile +87 -0
- data/vendor/assets/javascripts/d3/plugins/README.md +19 -0
- data/vendor/assets/javascripts/d3/plugins/box/box.js +301 -0
- data/vendor/assets/javascripts/d3/plugins/bullet/README.md +14 -0
- data/vendor/assets/javascripts/d3/plugins/bullet/bullet.js +241 -0
- data/vendor/assets/javascripts/d3/plugins/chernoff/README.md +15 -0
- data/vendor/assets/javascripts/d3/plugins/chernoff/chernoff.js +176 -0
- data/vendor/assets/javascripts/d3/plugins/cie/README.md +54 -0
- data/vendor/assets/javascripts/d3/plugins/cie/cie.js +155 -0
- data/vendor/assets/javascripts/d3/plugins/fisheye/README.md +43 -0
- data/vendor/assets/javascripts/d3/plugins/fisheye/fisheye.js +85 -0
- data/vendor/assets/javascripts/d3/plugins/force_labels/README.md +29 -0
- data/vendor/assets/javascripts/d3/plugins/force_labels/force_labels.js +56 -0
- data/vendor/assets/javascripts/d3/plugins/geo/polyhedron/README.md +7 -0
- data/vendor/assets/javascripts/d3/plugins/geo/polyhedron/polyhedron.js +436 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/README.md +107 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/aitoff.js +40 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/armadillo.js +79 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/august.js +15 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/baker.js +28 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/berghaus.js +60 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/boggs.js +27 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/bonne.js +29 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/bromley.js +5 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/collignon.js +17 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/conic-conformal.js +29 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/conic-equidistant.js +27 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/craig.js +24 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/craster.js +18 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/cylindrical-equal-area.js +23 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/eckert1.js +18 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/eckert2.js +17 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/eckert3.js +17 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/eckert4.js +24 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/eckert5.js +17 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/eckert6.js +22 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/eisenlohr.js +16 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/end.js +1 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/gringorten.js +111 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/guyou.js +46 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/hammer-retroazimuthal.js +75 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/hammer.js +51 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/hatano.js +25 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/healpix.js +64 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/hill.js +64 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/homolosine.js +17 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/interrupt.js +111 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/kavrayskiy7.js +15 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/lagrange.js +27 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/larrivee.js +39 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/laskowski.js +31 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/littrow.js +21 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/loximuthal.js +29 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/miller.js +15 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/mollweide.js +35 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/mt-flat-polar-parabolic.js +21 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/mt-flat-polar-quartic.js +23 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/mt-flat-polar-sinusoidal.js +25 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/natural-earth.js +22 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/nell-hammer.js +20 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/parallel1.js +12 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/parallel2.js +13 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/peirce-quincuncial.js +14 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/polyconic.js +25 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/projection.js +29 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/robinson.js +83 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/satellite.js +68 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/sinu-mollweide.js +19 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/sinusoidal.js +15 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/start.js +1 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/aitoff-test.js +26 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/armadillo-test.js +28 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/baker-test.js +26 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/boggs-test.js +26 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/bonne-test.js +45 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/craig-test.js +25 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/craster-test.js +25 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/env.js +32 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/equirectangular-test.js +97 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/hammer-test.js +29 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/hatano-test.js +25 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/hill-test.js +26 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/larrivee-test.js +25 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/laskowski-test.js +26 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/littrow-test.js +24 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/loximuthal-test.js +25 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/mollweide-test.js +26 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/mt-flat-polar-parabolic-test.js +25 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/mt-flat-polar-quartic-test.js +25 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/mt-flat-polar-sinusoidal-test.js +25 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/natural-earth-test.js +26 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/quartic-authalic-test.js +0 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/robinson-test.js +26 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/wagner6-test.js +25 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/test/winkel3-test.js +26 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/van-der-grinten.js +41 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/van-der-grinten4.js +22 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/wagner4.js +9 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/wagner6.js +15 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/wagner7.js +22 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/wiechel.js +14 -0
- data/vendor/assets/javascripts/d3/plugins/geo/projection/winkel3.js +40 -0
- data/vendor/assets/javascripts/d3/plugins/geo/tile/README.md +6 -0
- data/vendor/assets/javascripts/d3/plugins/geo/tile/tile.js +53 -0
- data/vendor/assets/javascripts/d3/plugins/geodesic/README.md +3 -0
- data/vendor/assets/javascripts/d3/plugins/geodesic/geodesic.js +130 -0
- data/vendor/assets/javascripts/d3/plugins/geom/contour/README.md +5 -0
- data/vendor/assets/javascripts/d3/plugins/geom/contour/contour.js +72 -0
- data/vendor/assets/javascripts/d3/plugins/graph/README.md +148 -0
- data/vendor/assets/javascripts/d3/plugins/graph/data/cities-matrix.json +37 -0
- data/vendor/assets/javascripts/d3/plugins/graph/data/cities.csv +36 -0
- data/vendor/assets/javascripts/d3/plugins/graph/data/miserables.json +338 -0
- data/vendor/assets/javascripts/d3/plugins/graph/graph.js +161 -0
- data/vendor/assets/javascripts/d3/plugins/graph/index.html +37 -0
- data/vendor/assets/javascripts/d3/plugins/hexbin/README.md +58 -0
- data/vendor/assets/javascripts/d3/plugins/hexbin/hexbin.js +96 -0
- data/vendor/assets/javascripts/d3/plugins/hive/README.md +5 -0
- data/vendor/assets/javascripts/d3/plugins/hive/hive.js +80 -0
- data/vendor/assets/javascripts/d3/plugins/horizon/README.md +11 -0
- data/vendor/assets/javascripts/d3/plugins/horizon/horizon.js +192 -0
- data/vendor/assets/javascripts/d3/plugins/interpolate-zoom/README.md +11 -0
- data/vendor/assets/javascripts/d3/plugins/interpolate-zoom/interpolate-zoom.js +75 -0
- data/vendor/assets/javascripts/d3/plugins/jsonp/README.md +26 -0
- data/vendor/assets/javascripts/d3/plugins/jsonp/jsonp.js +25 -0
- data/vendor/assets/javascripts/d3/plugins/keybinding/README.md +17 -0
- data/vendor/assets/javascripts/d3/plugins/keybinding/keybinding.js +120 -0
- data/vendor/assets/javascripts/d3/plugins/longscroll/README.md +10 -0
- data/vendor/assets/javascripts/d3/plugins/longscroll/longscroll.js +75 -0
- data/vendor/assets/javascripts/d3/plugins/package.json +20 -0
- data/vendor/assets/javascripts/d3/plugins/qq/README.md +3 -0
- data/vendor/assets/javascripts/d3/plugins/qq/qq.js +249 -0
- data/vendor/assets/javascripts/d3/plugins/rollup/README.md +5 -0
- data/vendor/assets/javascripts/d3/plugins/rollup/rollup.js +131 -0
- data/vendor/assets/javascripts/d3/plugins/sankey/README.md +17 -0
- data/vendor/assets/javascripts/d3/plugins/sankey/sankey.js +292 -0
- data/vendor/assets/javascripts/d3/plugins/simplify/README.md +6 -0
- data/vendor/assets/javascripts/d3/plugins/simplify/simplify-test.js +193 -0
- data/vendor/assets/javascripts/d3/plugins/simplify/simplify.js +446 -0
- data/vendor/assets/javascripts/d3/plugins/superformula/superformula.js +98 -0
- data/vendor/assets/javascripts/d3/plugins/urlencode/urlencode-test.js +68 -0
- data/vendor/assets/javascripts/d3/plugins/urlencode/urlencode.js +37 -0
- metadata +218 -0
@@ -0,0 +1,5 @@
|
|
1
|
+
# Rollup (PivotGraph) Layout
|
2
|
+
|
3
|
+
Example: <http://bl.ocks.org/4343153>
|
4
|
+
|
5
|
+
The d3.rollup plugin implements the “PivotGraph” technique for collapsing large graphs along categorical dimensions, as described by Martin Wattenberg in [Visual Exploration of Multivariate Graphs](http://hint.fm/papers/pivotgraph.pdf).
|
@@ -0,0 +1,131 @@
|
|
1
|
+
d3.rollup = function() {
|
2
|
+
var directed = true,
|
3
|
+
x_ = rollupX,
|
4
|
+
y_ = rollupY,
|
5
|
+
nodes_ = rollupNodes,
|
6
|
+
links_ = rollupLinks,
|
7
|
+
linkValue = rollupLinkValue,
|
8
|
+
linkSource = rollupLinkSource,
|
9
|
+
linkTarget = rollupLinkTarget;
|
10
|
+
|
11
|
+
function rollup(d, i) {
|
12
|
+
var nodes = nodes_.call(this, d, i),
|
13
|
+
links = links_.call(this, d, i),
|
14
|
+
n = nodes.length,
|
15
|
+
m = links.length,
|
16
|
+
i = -1,
|
17
|
+
x = [],
|
18
|
+
y = [],
|
19
|
+
rnindex = 0,
|
20
|
+
rnodes = {},
|
21
|
+
rlinks = {};
|
22
|
+
|
23
|
+
// Compute rollup nodes.
|
24
|
+
while (++i < n) {
|
25
|
+
(d = nodes[i]).index = i;
|
26
|
+
x[i] = x_.call(this, d, i);
|
27
|
+
y[i] = y_.call(this, d, i);
|
28
|
+
var nodeId = id(i),
|
29
|
+
rn = rnodes[nodeId];
|
30
|
+
if (!rn) {
|
31
|
+
rn = rnodes[nodeId] = {
|
32
|
+
index: rnindex++,
|
33
|
+
x: x[i],
|
34
|
+
y: y[i],
|
35
|
+
nodes: []
|
36
|
+
};
|
37
|
+
}
|
38
|
+
rn.nodes.push(d);
|
39
|
+
}
|
40
|
+
|
41
|
+
// Compute rollup links.
|
42
|
+
i = -1; while (++i < m) {
|
43
|
+
var value = linkValue.call(this, d = links[i], i),
|
44
|
+
source = linkSource.call(this, d, i),
|
45
|
+
target = linkTarget.call(this, d, i),
|
46
|
+
rsource = rnodes[id(typeof source === "number" ? source : source.index)],
|
47
|
+
rtarget = rnodes[id(typeof target === "number" ? target : target.index)],
|
48
|
+
linkId = !directed && rsource.index > rtarget.index
|
49
|
+
? rtarget.index + "," + rsource.index
|
50
|
+
: rsource.index + "," + rtarget.index,
|
51
|
+
rl = rlinks[linkId];
|
52
|
+
if (!rl) {
|
53
|
+
rl = rlinks[linkId] = {
|
54
|
+
source: rsource,
|
55
|
+
target: rtarget,
|
56
|
+
value: 0,
|
57
|
+
links: []
|
58
|
+
};
|
59
|
+
}
|
60
|
+
rl.links.push(links[i]);
|
61
|
+
rl.value += value;
|
62
|
+
}
|
63
|
+
|
64
|
+
return {
|
65
|
+
nodes: d3.values(rnodes),
|
66
|
+
links: d3.values(rlinks)
|
67
|
+
};
|
68
|
+
|
69
|
+
function id(i) {
|
70
|
+
return x[i] + "," + y[i];
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
rollup.x = function(x) {
|
75
|
+
if (!arguments.length) return x_;
|
76
|
+
x_ = x;
|
77
|
+
return rollup;
|
78
|
+
};
|
79
|
+
|
80
|
+
rollup.y = function(x) {
|
81
|
+
if (!arguments.length) return y_;
|
82
|
+
y_ = x;
|
83
|
+
return rollup;
|
84
|
+
};
|
85
|
+
|
86
|
+
rollup.nodes = function(x) {
|
87
|
+
if (!arguments.length) return nodes_;
|
88
|
+
nodes_ = x;
|
89
|
+
return rollup;
|
90
|
+
};
|
91
|
+
|
92
|
+
rollup.links = function(x) {
|
93
|
+
if (!arguments.length) return links_;
|
94
|
+
links_ = x;
|
95
|
+
return rollup;
|
96
|
+
};
|
97
|
+
|
98
|
+
rollup.linkSource = function(x) {
|
99
|
+
if (!arguments.length) return linkSource;
|
100
|
+
linkSource = x;
|
101
|
+
return rollup;
|
102
|
+
};
|
103
|
+
|
104
|
+
rollup.linkTarget = function(x) {
|
105
|
+
if (!arguments.length) return linkTarget;
|
106
|
+
linkTarget = x;
|
107
|
+
return rollup;
|
108
|
+
};
|
109
|
+
|
110
|
+
rollup.linkValue = function(x) {
|
111
|
+
if (!arguments.length) return linkValue;
|
112
|
+
linkValue = x;
|
113
|
+
return rollup;
|
114
|
+
};
|
115
|
+
|
116
|
+
rollup.directed = function(x) {
|
117
|
+
if (!arguments.length) return directed;
|
118
|
+
directed = x;
|
119
|
+
return rollup;
|
120
|
+
};
|
121
|
+
|
122
|
+
return rollup;
|
123
|
+
|
124
|
+
function rollupX(d) { return d.x; }
|
125
|
+
function rollupY(d) { return d.y; }
|
126
|
+
function rollupNodes(d) { return d.nodes; }
|
127
|
+
function rollupLinks(d) { return d.links; }
|
128
|
+
function rollupLinkValue(d) { return 1; }
|
129
|
+
function rollupLinkSource(d) { return d.source; }
|
130
|
+
function rollupLinkTarget(d) { return d.target; }
|
131
|
+
};
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Sankey Diagrams
|
2
|
+
|
3
|
+
Demo: <http://bost.ocks.org/mike/sankey/>
|
4
|
+
|
5
|
+
```js
|
6
|
+
var sankey = d3.sankey()
|
7
|
+
.size([width, height])
|
8
|
+
.nodeWidth(15)
|
9
|
+
.nodePadding(10)
|
10
|
+
.nodes(energy.nodes)
|
11
|
+
.links(energy.links)
|
12
|
+
.layout(32);
|
13
|
+
```
|
14
|
+
|
15
|
+
```
|
16
|
+
var path = sankey.link();
|
17
|
+
```
|
@@ -0,0 +1,292 @@
|
|
1
|
+
d3.sankey = function() {
|
2
|
+
var sankey = {},
|
3
|
+
nodeWidth = 24,
|
4
|
+
nodePadding = 8,
|
5
|
+
size = [1, 1],
|
6
|
+
nodes = [],
|
7
|
+
links = [];
|
8
|
+
|
9
|
+
sankey.nodeWidth = function(_) {
|
10
|
+
if (!arguments.length) return nodeWidth;
|
11
|
+
nodeWidth = +_;
|
12
|
+
return sankey;
|
13
|
+
};
|
14
|
+
|
15
|
+
sankey.nodePadding = function(_) {
|
16
|
+
if (!arguments.length) return nodePadding;
|
17
|
+
nodePadding = +_;
|
18
|
+
return sankey;
|
19
|
+
};
|
20
|
+
|
21
|
+
sankey.nodes = function(_) {
|
22
|
+
if (!arguments.length) return nodes;
|
23
|
+
nodes = _;
|
24
|
+
return sankey;
|
25
|
+
};
|
26
|
+
|
27
|
+
sankey.links = function(_) {
|
28
|
+
if (!arguments.length) return links;
|
29
|
+
links = _;
|
30
|
+
return sankey;
|
31
|
+
};
|
32
|
+
|
33
|
+
sankey.size = function(_) {
|
34
|
+
if (!arguments.length) return size;
|
35
|
+
size = _;
|
36
|
+
return sankey;
|
37
|
+
};
|
38
|
+
|
39
|
+
sankey.layout = function(iterations) {
|
40
|
+
computeNodeLinks();
|
41
|
+
computeNodeValues();
|
42
|
+
computeNodeBreadths();
|
43
|
+
computeNodeDepths(iterations);
|
44
|
+
computeLinkDepths();
|
45
|
+
return sankey;
|
46
|
+
};
|
47
|
+
|
48
|
+
sankey.relayout = function() {
|
49
|
+
computeLinkDepths();
|
50
|
+
return sankey;
|
51
|
+
};
|
52
|
+
|
53
|
+
sankey.link = function() {
|
54
|
+
var curvature = .5;
|
55
|
+
|
56
|
+
function link(d) {
|
57
|
+
var x0 = d.source.x + d.source.dx,
|
58
|
+
x1 = d.target.x,
|
59
|
+
xi = d3.interpolateNumber(x0, x1),
|
60
|
+
x2 = xi(curvature),
|
61
|
+
x3 = xi(1 - curvature),
|
62
|
+
y0 = d.source.y + d.sy + d.dy / 2,
|
63
|
+
y1 = d.target.y + d.ty + d.dy / 2;
|
64
|
+
return "M" + x0 + "," + y0
|
65
|
+
+ "C" + x2 + "," + y0
|
66
|
+
+ " " + x3 + "," + y1
|
67
|
+
+ " " + x1 + "," + y1;
|
68
|
+
}
|
69
|
+
|
70
|
+
link.curvature = function(_) {
|
71
|
+
if (!arguments.length) return curvature;
|
72
|
+
curvature = +_;
|
73
|
+
return link;
|
74
|
+
};
|
75
|
+
|
76
|
+
return link;
|
77
|
+
};
|
78
|
+
|
79
|
+
// Populate the sourceLinks and targetLinks for each node.
|
80
|
+
// Also, if the source and target are not objects, assume they are indices.
|
81
|
+
function computeNodeLinks() {
|
82
|
+
nodes.forEach(function(node) {
|
83
|
+
node.sourceLinks = [];
|
84
|
+
node.targetLinks = [];
|
85
|
+
});
|
86
|
+
links.forEach(function(link) {
|
87
|
+
var source = link.source,
|
88
|
+
target = link.target;
|
89
|
+
if (typeof source === "number") source = link.source = nodes[link.source];
|
90
|
+
if (typeof target === "number") target = link.target = nodes[link.target];
|
91
|
+
source.sourceLinks.push(link);
|
92
|
+
target.targetLinks.push(link);
|
93
|
+
});
|
94
|
+
}
|
95
|
+
|
96
|
+
// Compute the value (size) of each node by summing the associated links.
|
97
|
+
function computeNodeValues() {
|
98
|
+
nodes.forEach(function(node) {
|
99
|
+
node.value = Math.max(
|
100
|
+
d3.sum(node.sourceLinks, value),
|
101
|
+
d3.sum(node.targetLinks, value)
|
102
|
+
);
|
103
|
+
});
|
104
|
+
}
|
105
|
+
|
106
|
+
// Iteratively assign the breadth (x-position) for each node.
|
107
|
+
// Nodes are assigned the maximum breadth of incoming neighbors plus one;
|
108
|
+
// nodes with no incoming links are assigned breadth zero, while
|
109
|
+
// nodes with no outgoing links are assigned the maximum breadth.
|
110
|
+
function computeNodeBreadths() {
|
111
|
+
var remainingNodes = nodes,
|
112
|
+
nextNodes,
|
113
|
+
x = 0;
|
114
|
+
|
115
|
+
while (remainingNodes.length) {
|
116
|
+
nextNodes = [];
|
117
|
+
remainingNodes.forEach(function(node) {
|
118
|
+
node.x = x;
|
119
|
+
node.dx = nodeWidth;
|
120
|
+
node.sourceLinks.forEach(function(link) {
|
121
|
+
nextNodes.push(link.target);
|
122
|
+
});
|
123
|
+
});
|
124
|
+
remainingNodes = nextNodes;
|
125
|
+
++x;
|
126
|
+
}
|
127
|
+
|
128
|
+
//
|
129
|
+
moveSinksRight(x);
|
130
|
+
scaleNodeBreadths((size[0] - nodeWidth) / (x - 1));
|
131
|
+
}
|
132
|
+
|
133
|
+
function moveSourcesRight() {
|
134
|
+
nodes.forEach(function(node) {
|
135
|
+
if (!node.targetLinks.length) {
|
136
|
+
node.x = d3.min(node.sourceLinks, function(d) { return d.target.x; }) - 1;
|
137
|
+
}
|
138
|
+
});
|
139
|
+
}
|
140
|
+
|
141
|
+
function moveSinksRight(x) {
|
142
|
+
nodes.forEach(function(node) {
|
143
|
+
if (!node.sourceLinks.length) {
|
144
|
+
node.x = x - 1;
|
145
|
+
}
|
146
|
+
});
|
147
|
+
}
|
148
|
+
|
149
|
+
function scaleNodeBreadths(kx) {
|
150
|
+
nodes.forEach(function(node) {
|
151
|
+
node.x *= kx;
|
152
|
+
});
|
153
|
+
}
|
154
|
+
|
155
|
+
function computeNodeDepths(iterations) {
|
156
|
+
var nodesByBreadth = d3.nest()
|
157
|
+
.key(function(d) { return d.x; })
|
158
|
+
.sortKeys(d3.ascending)
|
159
|
+
.entries(nodes)
|
160
|
+
.map(function(d) { return d.values; });
|
161
|
+
|
162
|
+
//
|
163
|
+
initializeNodeDepth();
|
164
|
+
resolveCollisions();
|
165
|
+
for (var alpha = 1; iterations > 0; --iterations) {
|
166
|
+
relaxRightToLeft(alpha *= .99);
|
167
|
+
resolveCollisions();
|
168
|
+
relaxLeftToRight(alpha);
|
169
|
+
resolveCollisions();
|
170
|
+
}
|
171
|
+
|
172
|
+
function initializeNodeDepth() {
|
173
|
+
var ky = d3.min(nodesByBreadth, function(nodes) {
|
174
|
+
return (size[1] - (nodes.length - 1) * nodePadding) / d3.sum(nodes, value);
|
175
|
+
});
|
176
|
+
|
177
|
+
nodesByBreadth.forEach(function(nodes) {
|
178
|
+
nodes.forEach(function(node, i) {
|
179
|
+
node.y = i;
|
180
|
+
node.dy = node.value * ky;
|
181
|
+
});
|
182
|
+
});
|
183
|
+
|
184
|
+
links.forEach(function(link) {
|
185
|
+
link.dy = link.value * ky;
|
186
|
+
});
|
187
|
+
}
|
188
|
+
|
189
|
+
function relaxLeftToRight(alpha) {
|
190
|
+
nodesByBreadth.forEach(function(nodes, breadth) {
|
191
|
+
nodes.forEach(function(node) {
|
192
|
+
if (node.targetLinks.length) {
|
193
|
+
var y = d3.sum(node.targetLinks, weightedSource) / d3.sum(node.targetLinks, value);
|
194
|
+
node.y += (y - center(node)) * alpha;
|
195
|
+
}
|
196
|
+
});
|
197
|
+
});
|
198
|
+
|
199
|
+
function weightedSource(link) {
|
200
|
+
return center(link.source) * link.value;
|
201
|
+
}
|
202
|
+
}
|
203
|
+
|
204
|
+
function relaxRightToLeft(alpha) {
|
205
|
+
nodesByBreadth.slice().reverse().forEach(function(nodes) {
|
206
|
+
nodes.forEach(function(node) {
|
207
|
+
if (node.sourceLinks.length) {
|
208
|
+
var y = d3.sum(node.sourceLinks, weightedTarget) / d3.sum(node.sourceLinks, value);
|
209
|
+
node.y += (y - center(node)) * alpha;
|
210
|
+
}
|
211
|
+
});
|
212
|
+
});
|
213
|
+
|
214
|
+
function weightedTarget(link) {
|
215
|
+
return center(link.target) * link.value;
|
216
|
+
}
|
217
|
+
}
|
218
|
+
|
219
|
+
function resolveCollisions() {
|
220
|
+
nodesByBreadth.forEach(function(nodes) {
|
221
|
+
var node,
|
222
|
+
dy,
|
223
|
+
y0 = 0,
|
224
|
+
n = nodes.length,
|
225
|
+
i;
|
226
|
+
|
227
|
+
// Push any overlapping nodes down.
|
228
|
+
nodes.sort(ascendingDepth);
|
229
|
+
for (i = 0; i < n; ++i) {
|
230
|
+
node = nodes[i];
|
231
|
+
dy = y0 - node.y;
|
232
|
+
if (dy > 0) node.y += dy;
|
233
|
+
y0 = node.y + node.dy + nodePadding;
|
234
|
+
}
|
235
|
+
|
236
|
+
// If the bottommost node goes outside the bounds, push it back up.
|
237
|
+
dy = y0 - nodePadding - size[1];
|
238
|
+
if (dy > 0) {
|
239
|
+
y0 = node.y -= dy;
|
240
|
+
|
241
|
+
// Push any overlapping nodes back up.
|
242
|
+
for (i = n - 2; i >= 0; --i) {
|
243
|
+
node = nodes[i];
|
244
|
+
dy = node.y + node.dy + nodePadding - y0;
|
245
|
+
if (dy > 0) node.y -= dy;
|
246
|
+
y0 = node.y;
|
247
|
+
}
|
248
|
+
}
|
249
|
+
});
|
250
|
+
}
|
251
|
+
|
252
|
+
function ascendingDepth(a, b) {
|
253
|
+
return a.y - b.y;
|
254
|
+
}
|
255
|
+
}
|
256
|
+
|
257
|
+
function computeLinkDepths() {
|
258
|
+
nodes.forEach(function(node) {
|
259
|
+
node.sourceLinks.sort(ascendingTargetDepth);
|
260
|
+
node.targetLinks.sort(ascendingSourceDepth);
|
261
|
+
});
|
262
|
+
nodes.forEach(function(node) {
|
263
|
+
var sy = 0, ty = 0;
|
264
|
+
node.sourceLinks.forEach(function(link) {
|
265
|
+
link.sy = sy;
|
266
|
+
sy += link.dy;
|
267
|
+
});
|
268
|
+
node.targetLinks.forEach(function(link) {
|
269
|
+
link.ty = ty;
|
270
|
+
ty += link.dy;
|
271
|
+
});
|
272
|
+
});
|
273
|
+
|
274
|
+
function ascendingSourceDepth(a, b) {
|
275
|
+
return a.source.y - b.source.y;
|
276
|
+
}
|
277
|
+
|
278
|
+
function ascendingTargetDepth(a, b) {
|
279
|
+
return a.target.y - b.target.y;
|
280
|
+
}
|
281
|
+
}
|
282
|
+
|
283
|
+
function center(node) {
|
284
|
+
return node.y + node.dy / 2;
|
285
|
+
}
|
286
|
+
|
287
|
+
function value(link) {
|
288
|
+
return link.value;
|
289
|
+
}
|
290
|
+
|
291
|
+
return sankey;
|
292
|
+
};
|