Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

d3-sankey

Package Overview
Dependencies
Maintainers
2
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

d3-sankey - npm Package Compare versions

Comparing version 0.5.0 to 0.6.0

127

build/d3-sankey.js

@@ -1,2 +0,2 @@

// https://github.com/d3/d3-sankey Version 0.5.0. Copyright 2017 Mike Bostock.
// https://github.com/d3/d3-sankey Version 0.6.0. Copyright 2017 Mike Bostock.
(function (global, factory) {

@@ -14,12 +14,12 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-array'), require('d3-collection'), require('d3-shape')) :

function ascendingSourceDepth(a, b) {
return a.source.y - b.source.y;
function ascendingSourceBreadth(a, b) {
return ascendingBreadth(a.source, b.source) || a.index - b.index;
}
function ascendingTargetDepth(a, b) {
return a.target.y - b.target.y;
function ascendingTargetBreadth(a, b) {
return ascendingBreadth(a.target, b.target) || a.index - b.index;
}
function ascendingDepth(a, b) {
return a.y - b.y;
function ascendingBreadth(a, b) {
return a.y0 - b.y0;
}

@@ -32,3 +32,3 @@

function nodeCenter(node) {
return node.y + node.dy / 2;
return (node.y0 + node.y1) / 2;
}

@@ -64,5 +64,5 @@

computeNodeValues(graph);
computeNodeBreadths(graph);
computeNodeDepths(graph, iterations);
computeLinkDepths(graph);
computeNodeDepths(graph);
computeNodeBreadths(graph, iterations);
computeLinkBreadths(graph);
return graph;

@@ -72,3 +72,3 @@ }

sankey.update = function(graph) {
computeLinkDepths(graph);
computeLinkBreadths(graph);
return graph;

@@ -108,10 +108,12 @@ };

function computeNodeLinks(graph) {
graph.nodes.forEach(function(node) {
graph.nodes.forEach(function(node, i) {
node.index = i;
node.sourceLinks = [];
node.targetLinks = [];
});
graph.links.forEach(function(link) {
graph.links.forEach(function(link, i) {
var source = link.source, target = link.target;
if (typeof source === "number") source = link.source = graph.nodes[link.source];
if (typeof target === "number") target = link.target = graph.nodes[link.target];
link.index = i;
source.sourceLinks.push(link);

@@ -132,10 +134,10 @@ target.targetLinks.push(link);

// Iteratively assign the breadth (x-position) for each node.
// Nodes are assigned the maximum breadth of incoming neighbors plus one;
// nodes with no incoming links are assigned breadth zero, while
// nodes with no outgoing links are assigned the maximum breadth.
function computeNodeBreadths(graph) {
// Iteratively assign the depth (x-position) for each node.
// Nodes are assigned the maximum depth of incoming neighbors plus one;
// nodes with no incoming links are assigned depth zero, while
// nodes with no outgoing links are assigned the maximum depth.
function computeNodeDepths(graph) {
var remainingNodes = graph.nodes,
nextNodes,
x = 0;
depth = 0;

@@ -145,4 +147,3 @@ while (remainingNodes.length) {

remainingNodes.forEach(function(node) {
node.x = x;
node.dx = dx;
node.depth = depth;
node.sourceLinks.forEach(function(link) {

@@ -155,8 +156,8 @@ if (nextNodes.indexOf(link.target) < 0) {

remainingNodes = nextNodes;
++x;
++depth;
}
//
moveSinksRight(graph, x);
scaleNodeBreadths(graph, (x1 - x0 - dx) / (x - 1));
moveSinksRight(graph, depth);
scaleNodeDepths(graph, depth);
}

@@ -167,3 +168,3 @@

// if (!node.targetLinks.length) {
// node.x = min(node.sourceLinks, function(d) { return d.target.x; }) - 1;
// node.depth = min(node.sourceLinks, function(d) { return d.target.depth; }) - 1;
// }

@@ -173,6 +174,6 @@ // });

function moveSinksRight(graph, x) {
function moveSinksRight(graph, depth) {
graph.nodes.forEach(function(node) {
if (!node.sourceLinks.length) {
node.x = x - 1;
node.depth = depth - 1;
}

@@ -182,11 +183,12 @@ });

function scaleNodeBreadths(graph, kx) {
function scaleNodeDepths(graph, depth) {
var kx = (x1 - x0 - dx) / (depth - 1);
graph.nodes.forEach(function(node) {
node.x = x0 + node.x * kx;
node.x1 = (node.x0 = x0 + node.depth * kx) + dx;
});
}
function computeNodeDepths(graph) {
var nodesByBreadth = d3Collection.nest()
.key(function(d) { return d.x; })
function computeNodeBreadths(graph) {
var nodesByDepth = d3Collection.nest()
.key(function(d) { return d.depth; })
.sortKeys(d3Array.ascending)

@@ -197,3 +199,3 @@ .entries(graph.nodes)

//
initializeNodeDepth();
initializeNodeBreadth();
resolveCollisions();

@@ -207,11 +209,10 @@ for (var alpha = 1, n = iterations; n > 0; --n) {

function initializeNodeDepth() {
var ky = d3Array.min(nodesByBreadth, function(nodes) {
function initializeNodeBreadth() {
var ky = d3Array.min(nodesByDepth, function(nodes) {
return (y1 - y0 - (nodes.length - 1) * py) / d3Array.sum(nodes, value);
});
nodesByBreadth.forEach(function(nodes) {
nodesByDepth.forEach(function(nodes) {
nodes.forEach(function(node, i) {
node.y = i;
node.dy = node.value * ky;
node.y1 = (node.y0 = i) + node.value * ky;
});

@@ -221,3 +222,3 @@ });

graph.links.forEach(function(link) {
link.dy = link.value * ky;
link.width = link.value * ky;
});

@@ -227,6 +228,7 @@ }

function relaxLeftToRight(alpha) {
nodesByBreadth.forEach(function(nodes) {
nodesByDepth.forEach(function(nodes) {
nodes.forEach(function(node) {
if (node.targetLinks.length) {
node.y += (d3Array.sum(node.targetLinks, weightedSource) / d3Array.sum(node.targetLinks, value) - nodeCenter(node)) * alpha;
var dy = (d3Array.sum(node.targetLinks, weightedSource) / d3Array.sum(node.targetLinks, value) - nodeCenter(node)) * alpha;
node.y0 += dy, node.y1 += dy;
}

@@ -238,6 +240,7 @@ });

function relaxRightToLeft(alpha) {
nodesByBreadth.slice().reverse().forEach(function(nodes) {
nodesByDepth.slice().reverse().forEach(function(nodes) {
nodes.forEach(function(node) {
if (node.sourceLinks.length) {
node.y += (d3Array.sum(node.sourceLinks, weightedTarget) / d3Array.sum(node.sourceLinks, value) - nodeCenter(node)) * alpha;
var dy = (d3Array.sum(node.sourceLinks, weightedTarget) / d3Array.sum(node.sourceLinks, value) - nodeCenter(node)) * alpha;
node.y0 += dy, node.y1 += dy;
}

@@ -249,3 +252,3 @@ });

function resolveCollisions() {
nodesByBreadth.forEach(function(nodes) {
nodesByDepth.forEach(function(nodes) {
var node,

@@ -258,8 +261,8 @@ dy,

// Push any overlapping nodes down.
nodes.sort(ascendingDepth);
nodes.sort(ascendingBreadth);
for (i = 0; i < n; ++i) {
node = nodes[i];
dy = y - node.y;
if (dy > 0) node.y += dy;
y = node.y + node.dy + py;
dy = y - node.y0;
if (dy > 0) node.y0 += dy, node.y1 += dy;
y = node.y1 + py;
}

@@ -270,3 +273,3 @@

if (dy > 0) {
y = node.y -= dy;
y = (node.y0 -= dy), node.y1 -= dy;

@@ -276,5 +279,5 @@ // Push any overlapping nodes back up.

node = nodes[i];
dy = node.y + node.dy + py - y;
if (dy > 0) node.y -= dy;
y = node.y;
dy = node.y1 + py - y;
if (dy > 0) node.y0 -= dy, node.y1 -= dy;
y = node.y0;
}

@@ -286,16 +289,14 @@ }

function computeLinkDepths(graph) {
function computeLinkBreadths(graph) {
graph.nodes.forEach(function(node) {
node.sourceLinks.sort(ascendingTargetDepth);
node.targetLinks.sort(ascendingSourceDepth);
node.sourceLinks.sort(ascendingTargetBreadth);
node.targetLinks.sort(ascendingSourceBreadth);
});
graph.nodes.forEach(function(node) {
var sy = 0, ty = 0;
var y0 = node.y0, y1 = y0;
node.sourceLinks.forEach(function(link) {
link.sy = sy;
sy += link.dy;
link.y0 = y0 + link.width / 2, y0 += link.width;
});
node.targetLinks.forEach(function(link) {
link.ty = ty;
ty += link.dy;
link.y1 = y1 + link.width / 2, y1 += link.width;
});

@@ -309,7 +310,7 @@ });

function horizontalSource(d) {
return [d.source.x + d.source.dx, d.source.y + d.sy + d.dy / 2];
return [d.source.x1, d.y0];
}
function horizontalTarget(d) {
return [d.target.x, d.target.y + d.ty + d.dy / 2];
return [d.target.x0, d.y1];
}

@@ -316,0 +317,0 @@

@@ -1,2 +0,2 @@

// https://github.com/d3/d3-sankey Version 0.5.0. Copyright 2017 Mike Bostock.
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-array"),require("d3-collection"),require("d3-shape")):"function"==typeof define&&define.amd?define(["exports","d3-array","d3-collection","d3-shape"],t):t(n.d3=n.d3||{},n.d3,n.d3,n.d3)}(this,function(n,t,e,r){"use strict";function o(n){return function(){return n}}function u(n,t){return n.source.y-t.source.y}function i(n,t){return n.target.y-t.target.y}function c(n,t){return n.y-t.y}function f(n){return n.value}function s(n){return n.y+n.dy/2}function a(n){return s(n.source)*n.value}function d(n){return s(n.target)*n.value}function y(n){return n.nodes}function l(n){return n.links}function h(n){return[n.source.x+n.source.dx,n.source.y+n.sy+n.dy/2]}function g(n){return[n.target.x,n.target.y+n.ty+n.dy/2]}var k=function(){function n(){var n={nodes:j.apply(null,arguments),links:H.apply(null,arguments)};return r(n),h(n),g(n),v(n),E(n),n}function r(n){n.nodes.forEach(function(n){n.sourceLinks=[],n.targetLinks=[]}),n.links.forEach(function(t){var e=t.source,r=t.target;"number"==typeof e&&(e=t.source=n.nodes[t.source]),"number"==typeof r&&(r=t.target=n.nodes[t.target]),e.sourceLinks.push(t),r.targetLinks.push(t)})}function h(n){n.nodes.forEach(function(n){n.value=Math.max(t.sum(n.sourceLinks,f),t.sum(n.targetLinks,f))})}function g(n){for(var t,e=n.nodes,r=0;e.length;)t=[],e.forEach(function(n){n.x=r,n.dx=q,n.sourceLinks.forEach(function(n){t.indexOf(n.target)<0&&t.push(n.target)})}),e=t,++r;k(n,r),p(n,(m-L-q)/(r-1))}function k(n,t){n.nodes.forEach(function(n){n.sourceLinks.length||(n.x=t-1)})}function p(n,t){n.nodes.forEach(function(n){n.x=L+n.x*t})}function v(n){function r(){o.forEach(function(n){var t,e,r,o=x,u=n.length;for(n.sort(c),r=0;r<u;++r)t=n[r],e=o-t.y,e>0&&(t.y+=e),o=t.y+t.dy+z;if((e=o-z-b)>0)for(o=t.y-=e,r=u-2;r>=0;--r)t=n[r],e=t.y+t.dy+z-o,e>0&&(t.y-=e),o=t.y})}var o=e.nest().key(function(n){return n.x}).sortKeys(t.ascending).entries(n.nodes).map(function(n){return n.values});!function(){var e=t.min(o,function(n){return(b-x-(n.length-1)*z)/t.sum(n,f)});o.forEach(function(n){n.forEach(function(n,t){n.y=t,n.dy=n.value*e})}),n.links.forEach(function(n){n.dy=n.value*e})}(),r();for(var u=1,i=M;i>0;--i)!function(n){o.slice().reverse().forEach(function(e){e.forEach(function(e){e.sourceLinks.length&&(e.y+=(t.sum(e.sourceLinks,d)/t.sum(e.sourceLinks,f)-s(e))*n)})})}(u*=.99),r(),function(n){o.forEach(function(e){e.forEach(function(e){e.targetLinks.length&&(e.y+=(t.sum(e.targetLinks,a)/t.sum(e.targetLinks,f)-s(e))*n)})})}(u),r()}function E(n){n.nodes.forEach(function(n){n.sourceLinks.sort(i),n.targetLinks.sort(u)}),n.nodes.forEach(function(n){var t=0,e=0;n.sourceLinks.forEach(function(n){n.sy=t,t+=n.dy}),n.targetLinks.forEach(function(n){n.ty=e,e+=n.dy})})}var L=0,x=0,m=1,b=1,q=24,z=8,j=y,H=l,M=32;return n.update=function(n){return E(n),n},n.nodeWidth=function(t){return arguments.length?(q=+t,n):q},n.nodePadding=function(t){return arguments.length?(z=+t,n):z},n.nodes=function(t){return arguments.length?(j="function"==typeof t?t:o(t),n):j},n.links=function(t){return arguments.length?(H="function"==typeof t?t:o(t),n):H},n.size=function(t){return arguments.length?(L=x=0,m=+t[0],b=+t[1],n):[m-L,b-x]},n.extent=function(t){return arguments.length?(L=+t[0][0],m=+t[1][0],x=+t[0][1],b=+t[1][1],n):[[L,x],[m,b]]},n.iterations=function(t){return arguments.length?(M=+t,n):M},n},p=function(){return r.linkHorizontal().source(h).target(g)};n.sankey=k,n.sankeyLinkHorizontal=p,Object.defineProperty(n,"__esModule",{value:!0})});
// https://github.com/d3/d3-sankey Version 0.6.0. Copyright 2017 Mike Bostock.
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-array"),require("d3-collection"),require("d3-shape")):"function"==typeof define&&define.amd?define(["exports","d3-array","d3-collection","d3-shape"],t):t(n.d3=n.d3||{},n.d3,n.d3,n.d3)}(this,function(n,t,e,r){"use strict";function o(n){return function(){return n}}function u(n,t){return c(n.source,t.source)||n.index-t.index}function i(n,t){return c(n.target,t.target)||n.index-t.index}function c(n,t){return n.y0-t.y0}function f(n){return n.value}function s(n){return(n.y0+n.y1)/2}function a(n){return s(n.source)*n.value}function d(n){return s(n.target)*n.value}function h(n){return n.nodes}function l(n){return n.links}function y(n){return[n.source.x1,n.y0]}function g(n){return[n.target.x0,n.y1]}var k=function(){function n(){var n={nodes:z.apply(null,arguments),links:j.apply(null,arguments)};return r(n),y(n),g(n),v(n),E(n),n}function r(n){n.nodes.forEach(function(n,t){n.index=t,n.sourceLinks=[],n.targetLinks=[]}),n.links.forEach(function(t,e){var r=t.source,o=t.target;"number"==typeof r&&(r=t.source=n.nodes[t.source]),"number"==typeof o&&(o=t.target=n.nodes[t.target]),t.index=e,r.sourceLinks.push(t),o.targetLinks.push(t)})}function y(n){n.nodes.forEach(function(n){n.value=Math.max(t.sum(n.sourceLinks,f),t.sum(n.targetLinks,f))})}function g(n){for(var t,e=n.nodes,r=0;e.length;)t=[],e.forEach(function(n){n.depth=r,n.sourceLinks.forEach(function(n){t.indexOf(n.target)<0&&t.push(n.target)})}),e=t,++r;k(n,r),p(n,r)}function k(n,t){n.nodes.forEach(function(n){n.sourceLinks.length||(n.depth=t-1)})}function p(n,t){var e=(m-L-b)/(t-1);n.nodes.forEach(function(n){n.x1=(n.x0=L+n.depth*e)+b})}function v(n){function r(){o.forEach(function(n){var t,e,r,o=x,u=n.length;for(n.sort(c),r=0;r<u;++r)t=n[r],e=o-t.y0,e>0&&(t.y0+=e,t.y1+=e),o=t.y1+q;if((e=o-q-w)>0)for(o=t.y0-=e,t.y1-=e,r=u-2;r>=0;--r)t=n[r],e=t.y1+q-o,e>0&&(t.y0-=e,t.y1-=e),o=t.y0})}var o=e.nest().key(function(n){return n.depth}).sortKeys(t.ascending).entries(n.nodes).map(function(n){return n.values});!function(){var e=t.min(o,function(n){return(w-x-(n.length-1)*q)/t.sum(n,f)});o.forEach(function(n){n.forEach(function(n,t){n.y1=(n.y0=t)+n.value*e})}),n.links.forEach(function(n){n.width=n.value*e})}(),r();for(var u=1,i=H;i>0;--i)!function(n){o.slice().reverse().forEach(function(e){e.forEach(function(e){if(e.sourceLinks.length){var r=(t.sum(e.sourceLinks,d)/t.sum(e.sourceLinks,f)-s(e))*n;e.y0+=r,e.y1+=r}})})}(u*=.99),r(),function(n){o.forEach(function(e){e.forEach(function(e){if(e.targetLinks.length){var r=(t.sum(e.targetLinks,a)/t.sum(e.targetLinks,f)-s(e))*n;e.y0+=r,e.y1+=r}})})}(u),r()}function E(n){n.nodes.forEach(function(n){n.sourceLinks.sort(i),n.targetLinks.sort(u)}),n.nodes.forEach(function(n){var t=n.y0,e=t;n.sourceLinks.forEach(function(n){n.y0=t+n.width/2,t+=n.width}),n.targetLinks.forEach(function(n){n.y1=e+n.width/2,e+=n.width})})}var L=0,x=0,m=1,w=1,b=24,q=8,z=h,j=l,H=32;return n.update=function(n){return E(n),n},n.nodeWidth=function(t){return arguments.length?(b=+t,n):b},n.nodePadding=function(t){return arguments.length?(q=+t,n):q},n.nodes=function(t){return arguments.length?(z="function"==typeof t?t:o(t),n):z},n.links=function(t){return arguments.length?(j="function"==typeof t?t:o(t),n):j},n.size=function(t){return arguments.length?(L=x=0,m=+t[0],w=+t[1],n):[m-L,w-x]},n.extent=function(t){return arguments.length?(L=+t[0][0],m=+t[1][0],x=+t[0][1],w=+t[1][1],n):[[L,x],[m,w]]},n.iterations=function(t){return arguments.length?(H=+t,n):H},n},p=function(){return r.linkHorizontal().source(y).target(g)};n.sankey=k,n.sankeyLinkHorizontal=p,Object.defineProperty(n,"__esModule",{value:!0})});
{
"name": "d3-sankey",
"version": "0.5.0",
"version": "0.6.0",
"description": "Visualize flow between nodes in a directed acyclic network.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -5,3 +5,3 @@ # d3-sankey

[<img alt="Sankey diagram" src="https://raw.githubusercontent.com/d3/d3-sankey/master/img/energy.png" width="960" height="500">](https://bl.ocks.org/mbostock/ca9a0bb7ba204d12974bca90acc507c0)
[<img alt="Sankey diagram" src="https://raw.githubusercontent.com/d3/d3-sankey/master/img/energy.png" width="960">](https://bl.ocks.org/mbostock/ca9a0bb7ba204d12974bca90acc507c0)

@@ -46,4 +46,4 @@ Source: Department of Energy & Climate Change, Tom Counsell.

* *link*.sy - the link’s vertical starting position (at source node)
* *link*.ty - the link’s vertical end position (at target node)
* *link*.y0 - the link’s vertical starting position (at source node)
* *link*.y1 - the link’s vertical end position (at target node)

@@ -69,6 +69,8 @@ This method is intended to be called after computing the initial [Sankey layout](#_sankey), for example when the diagram is repositioned interactively.

* *node*.value - the node’s value; the sum of *link*.value for the node’s incoming [links](#sankey_links)
* *node*x - the node’s horizontal position (derived from the graph topology)
* *node*.dx - the node’s node width
* *node*.y - the node’s vertical position
* *node*.dy - the node’s height (proportional to its value)
* *node*.index - the node’s zero-based index within the array of nodes
* *node*.depth - the node’s zero-based graph depth, derived from the graph topology
* *node*.x0 - the node’s minimum horizontal position, derived from *node*.depth
* *node*.x1 - the node’s maximum horizontal position (*node*.x0 + [*sankey*.nodeWidth](#sankey_nodeWidth))
* *node*.y0 - the node’s minimum vertical position
* *node*.y1 - the node’s maximum vertical position (*node*.y1 - *node*.y0 is proportional to *node*.value)

@@ -95,7 +97,8 @@ See also [*sankey*.links](#sankey_links).

For convenience, a link’s source and target may be initialized using the zero-based index of corresponding node in the array of nodes passed to the [Sankey generator](#_sankey) rather than object references. The following properties are assigned to each link by the [Sankey generator](#_sankey):
For convenience, a link’s source and target may be initialized using the zero-based index of corresponding node in the array of nodes returned by the [nodes accessor](#sankey_nodes) of the [Sankey generator](#_sankey) rather than object references. The following properties are assigned to each link by the [Sankey generator](#_sankey):
* *link*.dy - the link’s breadth (proportional to its value)
* *link*.sy - the link’s vertical starting position (at source node)
* *link*.ty - the link’s vertical end position (at target node)
* *link*.y0 - the link’s vertical starting position (at source node)
* *link*.y1 - the link’s vertical end position (at target node)
* *link*.width - the link’s width (proportional to *link*.value)
* *link*.index - the zero-based index of *link* within the array of links

@@ -112,3 +115,3 @@ <a name="sankey_nodeWidth" href="#sankey_nodeWidth">#</a> <i>sankey</i>.<b>nodeWidth</b>([<i>width</i>]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js#L64 "Source")

If *extent* is specified, sets the extent of the Sankey layout to the specified bounds and returns the layout. The *extent* bounds are specified as an array \[\[<i>x0</i>, <i>y0</i>\], \[<i>x1</i>, <i>y1</i>\]\], where *x0* is the left side of the extent, *y0* is the top, *x1* is the right and *y1* is the bottom. If *extent* is not specified, returns the current extent which defaults to null.
If *extent* is specified, sets the extent of the Sankey layout to the specified bounds and returns the layout. The *extent* bounds are specified as an array \[\[<i>x0</i>, <i>y0</i>\], \[<i>x1</i>, <i>y1</i>\]\], where *x0* is the left side of the extent, *y0* is the top, *x1* is the right and *y1* is the bottom. If *extent* is not specified, returns the current extent which defaults to [[0, 0], [1, 1]].

@@ -129,3 +132,3 @@ <a name="sankey_size" href="#sankey_size">#</a> <i>sankey</i>.<b>size</b>([<i>size</i>]) [<>](https://github.com/d3/d3-sankey/blob/master/src/sankey.js#L80 "Source")

<a name="sankeyLinkHorizontal" href="#sankeyLinkHorizontal">#</a> d3.<i>sankeyLinkHorizontal</i>() [<>](https://github.com/d3/d3-sankey/blob/master/src/sankeyLinkHorizontal.js "Source")
<a name="sankeyLinkHorizontal" href="#sankeyLinkHorizontal">#</a> d3.<b>sankeyLinkHorizontal</b>() [<>](https://github.com/d3/d3-sankey/blob/master/src/sankeyLinkHorizontal.js "Source")

@@ -136,3 +139,3 @@ Returns a [horizontal link shape](https://github.com/d3/d3-shape/blob/master/README.md#linkHorizontal) suitable for a Sankey diagram. The [source accessor](https://github.com/d3/d3-shape/blob/master/README.md#link_source) is defined as:

function source(d) {
return [d.source.x + d.source.dx, d.source.y + d.sy + d.dy / 2];
return [d.source.x1, d.y0];
}

@@ -145,3 +148,3 @@ ```

function target(d) {
return [d.target.x, d.target.y + d.ty + d.dy / 2];
return [d.target.x0, d.y1];
}

@@ -161,3 +164,3 @@ ```

.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke-width", function(d) { return d.dy; });
.attr("stroke-width", function(d) { return d.width; });
```

@@ -5,12 +5,12 @@ import {ascending, min, sum} from "d3-array";

function ascendingSourceDepth(a, b) {
return a.source.y - b.source.y;
function ascendingSourceBreadth(a, b) {
return ascendingBreadth(a.source, b.source) || a.index - b.index;
}
function ascendingTargetDepth(a, b) {
return a.target.y - b.target.y;
function ascendingTargetBreadth(a, b) {
return ascendingBreadth(a.target, b.target) || a.index - b.index;
}
function ascendingDepth(a, b) {
return a.y - b.y;
function ascendingBreadth(a, b) {
return a.y0 - b.y0;
}

@@ -23,3 +23,3 @@

function nodeCenter(node) {
return node.y + node.dy / 2;
return (node.y0 + node.y1) / 2;
}

@@ -55,5 +55,5 @@

computeNodeValues(graph);
computeNodeBreadths(graph);
computeNodeDepths(graph, iterations);
computeLinkDepths(graph);
computeNodeDepths(graph);
computeNodeBreadths(graph, iterations);
computeLinkBreadths(graph);
return graph;

@@ -63,3 +63,3 @@ }

sankey.update = function(graph) {
computeLinkDepths(graph);
computeLinkBreadths(graph);
return graph;

@@ -99,10 +99,12 @@ };

function computeNodeLinks(graph) {
graph.nodes.forEach(function(node) {
graph.nodes.forEach(function(node, i) {
node.index = i;
node.sourceLinks = [];
node.targetLinks = [];
});
graph.links.forEach(function(link) {
graph.links.forEach(function(link, i) {
var source = link.source, target = link.target;
if (typeof source === "number") source = link.source = graph.nodes[link.source];
if (typeof target === "number") target = link.target = graph.nodes[link.target];
link.index = i;
source.sourceLinks.push(link);

@@ -123,10 +125,10 @@ target.targetLinks.push(link);

// Iteratively assign the breadth (x-position) for each node.
// Nodes are assigned the maximum breadth of incoming neighbors plus one;
// nodes with no incoming links are assigned breadth zero, while
// nodes with no outgoing links are assigned the maximum breadth.
function computeNodeBreadths(graph) {
// Iteratively assign the depth (x-position) for each node.
// Nodes are assigned the maximum depth of incoming neighbors plus one;
// nodes with no incoming links are assigned depth zero, while
// nodes with no outgoing links are assigned the maximum depth.
function computeNodeDepths(graph) {
var remainingNodes = graph.nodes,
nextNodes,
x = 0;
depth = 0;

@@ -136,4 +138,3 @@ while (remainingNodes.length) {

remainingNodes.forEach(function(node) {
node.x = x;
node.dx = dx;
node.depth = depth;
node.sourceLinks.forEach(function(link) {

@@ -146,8 +147,8 @@ if (nextNodes.indexOf(link.target) < 0) {

remainingNodes = nextNodes;
++x;
++depth;
}
//
moveSinksRight(graph, x);
scaleNodeBreadths(graph, (x1 - x0 - dx) / (x - 1));
moveSinksRight(graph, depth);
scaleNodeDepths(graph, depth);
}

@@ -158,3 +159,3 @@

// if (!node.targetLinks.length) {
// node.x = min(node.sourceLinks, function(d) { return d.target.x; }) - 1;
// node.depth = min(node.sourceLinks, function(d) { return d.target.depth; }) - 1;
// }

@@ -164,6 +165,6 @@ // });

function moveSinksRight(graph, x) {
function moveSinksRight(graph, depth) {
graph.nodes.forEach(function(node) {
if (!node.sourceLinks.length) {
node.x = x - 1;
node.depth = depth - 1;
}

@@ -173,11 +174,12 @@ });

function scaleNodeBreadths(graph, kx) {
function scaleNodeDepths(graph, depth) {
var kx = (x1 - x0 - dx) / (depth - 1);
graph.nodes.forEach(function(node) {
node.x = x0 + node.x * kx;
node.x1 = (node.x0 = x0 + node.depth * kx) + dx;
});
}
function computeNodeDepths(graph) {
var nodesByBreadth = nest()
.key(function(d) { return d.x; })
function computeNodeBreadths(graph) {
var nodesByDepth = nest()
.key(function(d) { return d.depth; })
.sortKeys(ascending)

@@ -188,3 +190,3 @@ .entries(graph.nodes)

//
initializeNodeDepth();
initializeNodeBreadth();
resolveCollisions();

@@ -198,11 +200,10 @@ for (var alpha = 1, n = iterations; n > 0; --n) {

function initializeNodeDepth() {
var ky = min(nodesByBreadth, function(nodes) {
function initializeNodeBreadth() {
var ky = min(nodesByDepth, function(nodes) {
return (y1 - y0 - (nodes.length - 1) * py) / sum(nodes, value);
});
nodesByBreadth.forEach(function(nodes) {
nodesByDepth.forEach(function(nodes) {
nodes.forEach(function(node, i) {
node.y = i;
node.dy = node.value * ky;
node.y1 = (node.y0 = i) + node.value * ky;
});

@@ -212,3 +213,3 @@ });

graph.links.forEach(function(link) {
link.dy = link.value * ky;
link.width = link.value * ky;
});

@@ -218,6 +219,7 @@ }

function relaxLeftToRight(alpha) {
nodesByBreadth.forEach(function(nodes) {
nodesByDepth.forEach(function(nodes) {
nodes.forEach(function(node) {
if (node.targetLinks.length) {
node.y += (sum(node.targetLinks, weightedSource) / sum(node.targetLinks, value) - nodeCenter(node)) * alpha;
var dy = (sum(node.targetLinks, weightedSource) / sum(node.targetLinks, value) - nodeCenter(node)) * alpha;
node.y0 += dy, node.y1 += dy;
}

@@ -229,6 +231,7 @@ });

function relaxRightToLeft(alpha) {
nodesByBreadth.slice().reverse().forEach(function(nodes) {
nodesByDepth.slice().reverse().forEach(function(nodes) {
nodes.forEach(function(node) {
if (node.sourceLinks.length) {
node.y += (sum(node.sourceLinks, weightedTarget) / sum(node.sourceLinks, value) - nodeCenter(node)) * alpha;
var dy = (sum(node.sourceLinks, weightedTarget) / sum(node.sourceLinks, value) - nodeCenter(node)) * alpha;
node.y0 += dy, node.y1 += dy;
}

@@ -240,3 +243,3 @@ });

function resolveCollisions() {
nodesByBreadth.forEach(function(nodes) {
nodesByDepth.forEach(function(nodes) {
var node,

@@ -249,8 +252,8 @@ dy,

// Push any overlapping nodes down.
nodes.sort(ascendingDepth);
nodes.sort(ascendingBreadth);
for (i = 0; i < n; ++i) {
node = nodes[i];
dy = y - node.y;
if (dy > 0) node.y += dy;
y = node.y + node.dy + py;
dy = y - node.y0;
if (dy > 0) node.y0 += dy, node.y1 += dy;
y = node.y1 + py;
}

@@ -261,3 +264,3 @@

if (dy > 0) {
y = node.y -= dy;
y = (node.y0 -= dy), node.y1 -= dy;

@@ -267,5 +270,5 @@ // Push any overlapping nodes back up.

node = nodes[i];
dy = node.y + node.dy + py - y;
if (dy > 0) node.y -= dy;
y = node.y;
dy = node.y1 + py - y;
if (dy > 0) node.y0 -= dy, node.y1 -= dy;
y = node.y0;
}

@@ -277,16 +280,14 @@ }

function computeLinkDepths(graph) {
function computeLinkBreadths(graph) {
graph.nodes.forEach(function(node) {
node.sourceLinks.sort(ascendingTargetDepth);
node.targetLinks.sort(ascendingSourceDepth);
node.sourceLinks.sort(ascendingTargetBreadth);
node.targetLinks.sort(ascendingSourceBreadth);
});
graph.nodes.forEach(function(node) {
var sy = 0, ty = 0;
var y0 = node.y0, y1 = y0;
node.sourceLinks.forEach(function(link) {
link.sy = sy;
sy += link.dy;
link.y0 = y0 + link.width / 2, y0 += link.width;
});
node.targetLinks.forEach(function(link) {
link.ty = ty;
ty += link.dy;
link.y1 = y1 + link.width / 2, y1 += link.width;
});

@@ -293,0 +294,0 @@ });

import {linkHorizontal} from "d3-shape";
function horizontalSource(d) {
return [d.source.x + d.source.dx, d.source.y + d.sy + d.dy / 2];
return [d.source.x1, d.y0];
}
function horizontalTarget(d) {
return [d.target.x, d.target.y + d.ty + d.dy / 2];
return [d.target.x0, d.y1];
}

@@ -10,0 +10,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc