UNPKG

d3-sankey-diagram

Version:
1 lines 39.5 kB
((e,t)=>{"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-array"),require("d3-collection"),require("d3-interpolate"),require("d3-selection"),require("d3-transition"),require("d3-dispatch"),require("d3-format")):"function"==typeof define&&define.amd?define(["exports","d3-array","d3-collection","d3-interpolate","d3-selection","d3-transition","d3-dispatch","d3-format"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).d3=e.d3||{},e.d3,e.d3,e.d3,e.d3,e.d3,e.d3,e.d3)})(this,function(e,O,b,V,k,M,F,G){function S(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var n="\0";function s(e,t){e[t]?e[t]++:e[t]=1}function o(e,t){--e[t]||delete e[t]}function i(e,t,r,n){t=""+t,r=""+r;return!e&&r<t&&(e=t,t=r,r=e),t+""+r+""+(void 0===n?"\0":n)}function a(e,t){return i(e,t.v,t.w,t.name)}var t=class{_isDirected=!0;_isMultigraph=!1;_isCompound=!1;_label;_defaultNodeLabelFn=()=>{};_defaultEdgeLabelFn=()=>{};_nodes={};_in={};_preds={};_out={};_sucs={};_edgeObjs={};_edgeLabels={};_nodeCount=0;_edgeCount=0;_parent;_children;constructor(e){e&&(this._isDirected=!e.hasOwnProperty("directed")||e.directed,this._isMultigraph=!!e.hasOwnProperty("multigraph")&&e.multigraph,this._isCompound=!!e.hasOwnProperty("compound")&&e.compound),this._isCompound&&(this._parent={},this._children={},this._children[n]={})}isDirected(){return this._isDirected}isMultigraph(){return this._isMultigraph}isCompound(){return this._isCompound}setGraph(e){return this._label=e,this}graph(){return this._label}setDefaultNodeLabel(e){return"function"!=typeof(this._defaultNodeLabelFn=e)&&(this._defaultNodeLabelFn=()=>e),this}nodeCount(){return this._nodeCount}nodes(){return Object.keys(this._nodes)}sources(){var t=this;return this.nodes().filter(e=>0===Object.keys(t._in[e]).length)}sinks(){var t=this;return this.nodes().filter(e=>0===Object.keys(t._out[e]).length)}setNodes(e,t){var r=arguments,n=this;return e.forEach(function(e){1<r.length?n.setNode(e,t):n.setNode(e)}),this}setNode(e,t){return this._nodes.hasOwnProperty(e)?1<arguments.length&&(this._nodes[e]=t):(this._nodes[e]=1<arguments.length?t:this._defaultNodeLabelFn(e),this._isCompound&&(this._parent[e]=n,this._children[e]={},this._children[n][e]=!0),this._in[e]={},this._preds[e]={},this._out[e]={},this._sucs[e]={},++this._nodeCount),this}node(e){return this._nodes[e]}hasNode(e){return this._nodes.hasOwnProperty(e)}removeNode(e){var t,r=this;return this._nodes.hasOwnProperty(e)&&(t=e=>r.removeEdge(r._edgeObjs[e]),delete this._nodes[e],this._isCompound&&(this._removeFromParentsChildList(e),delete this._parent[e],this.children(e).forEach(function(e){r.setParent(e)}),delete this._children[e]),Object.keys(this._in[e]).forEach(t),delete this._in[e],delete this._preds[e],Object.keys(this._out[e]).forEach(t),delete this._out[e],delete this._sucs[e],--this._nodeCount),this}setParent(e,t){if(!this._isCompound)throw new Error("Cannot set parent in a non-compound graph");if(void 0===t)t=n;else{for(var r=t+="";void 0!==r;r=this.parent(r))if(r===e)throw new Error("Setting "+t+" as parent of "+e+" would create a cycle");this.setNode(t)}return this.setNode(e),this._removeFromParentsChildList(e),this._parent[e]=t,this._children[t][e]=!0,this}_removeFromParentsChildList(e){delete this._children[this._parent[e]][e]}parent(e){if(this._isCompound){e=this._parent[e];if(e!==n)return e}}children(e=n){var t;return this._isCompound?(t=this._children[e])?Object.keys(t):void 0:e===n?this.nodes():this.hasNode(e)?[]:void 0}predecessors(e){e=this._preds[e];if(e)return Object.keys(e)}successors(e){e=this._sucs[e];if(e)return Object.keys(e)}neighbors(e){var t=this.predecessors(e);if(t){var r,n=new Set(t);for(r of this.successors(e))n.add(r);return Array.from(n.values())}}isLeaf(e){e=this.isDirected()?this.successors(e):this.neighbors(e);return 0===e.length}filterNodes(r){var n=new this.constructor({directed:this._isDirected,multigraph:this._isMultigraph,compound:this._isCompound}),o=(n.setGraph(this.graph()),this),a=(Object.entries(this._nodes).forEach(function([e,t]){r(e)&&n.setNode(e,t)}),Object.values(this._edgeObjs).forEach(function(e){n.hasNode(e.v)&&n.hasNode(e.w)&&n.setEdge(e,o.edge(e))}),{});return this._isCompound&&n.nodes().forEach(e=>n.setParent(e,function e(t){var r=o.parent(t);return void 0===r||n.hasNode(r)?a[t]=r:r in a?a[r]:e(r)}(e))),n}setDefaultEdgeLabel(e){return"function"!=typeof(this._defaultEdgeLabelFn=e)&&(this._defaultEdgeLabelFn=()=>e),this}edgeCount(){return this._edgeCount}edges(){return Object.values(this._edgeObjs)}setPath(e,r){var n=this,o=arguments;return e.reduce(function(e,t){return 1<o.length?n.setEdge(e,t,r):n.setEdge(e,t),t}),this}setEdge(){var e,t,r=!1,n=arguments[0],n=("object"==typeof n&&null!==n&&"v"in n?(o=n.v,a=n.w,e=n.name,2===arguments.length&&(t=arguments[1],r=!0)):(o=n,a=arguments[1],e=arguments[3],2<arguments.length&&(t=arguments[2],r=!0)),i(this._isDirected,o=""+o,a=""+a,e=void 0!==e?""+e:e));if(this._edgeLabels.hasOwnProperty(n))r&&(this._edgeLabels[n]=t);else{if(void 0!==e&&!this._isMultigraph)throw new Error("Cannot set a named edge when isMultigraph = false");this.setNode(o),this.setNode(a),this._edgeLabels[n]=r?t:this._defaultEdgeLabelFn(o,a,e);var r=((e,t,r,n)=>(t=""+t,r=""+r,!e&&r<t&&(e=t,t=r,r=e),e={v:t,w:r},n&&(e.name=n),e))(this._isDirected,o,a,e),o=r.v,a=r.w;Object.freeze(r),this._edgeObjs[n]=r,s(this._preds[a],o),s(this._sucs[o],a),this._in[a][n]=r,this._out[o][n]=r,this._edgeCount++}return this}edge(e,t,r){e=1===arguments.length?a(this._isDirected,e):i(this._isDirected,e,t,r);return this._edgeLabels[e]}edgeAsObj(){var e=this.edge(...arguments);return"object"!=typeof e?{label:e}:e}hasEdge(e,t,r){e=1===arguments.length?a(this._isDirected,e):i(this._isDirected,e,t,r);return this._edgeLabels.hasOwnProperty(e)}removeEdge(e,t,r){var r=1===arguments.length?a(this._isDirected,arguments[0]):i(this._isDirected,e,t,r),n=this._edgeObjs[r];return n&&(e=n.v,t=n.w,delete this._edgeLabels[r],delete this._edgeObjs[r],o(this._preds[t],e),o(this._sucs[e],t),delete this._in[t][r],delete this._out[e][r],this._edgeCount--),this}inEdges(e,t){var e=this._in[e];if(e)return e=Object.values(e),t?e.filter(e=>e.v===t):e}outEdges(e,t){var e=this._out[e];if(e)return e=Object.values(e),t?e.filter(e=>e.w===t):e}nodeEdges(e,t){var r=this.inEdges(e,t);if(r)return r.concat(this.outEdges(e,t))}},r={Graph:t,version:"2.2.3"},q=t,B={write:function(e){var t={options:{directed:e.isDirected(),multigraph:e.isMultigraph(),compound:e.isCompound()},nodes:(n=>n.nodes().map(function(e){var t=n.node(e),r=n.parent(e),e={v:e};return void 0!==t&&(e.value=t),void 0!==r&&(e.parent=r),e}))(e),edges:(n=>n.edges().map(function(e){var t=n.edge(e),r={v:e.v,w:e.w};return void 0!==e.name&&(r.name=e.name),void 0!==t&&(r.value=t),r}))(e)};void 0!==e.graph()&&(t.value=structuredClone(e.graph()));return t},read:function(e){var t=new q(e.options).setGraph(e.value);return e.nodes.forEach(function(e){t.setNode(e.v,e.value),e.parent&&t.setParent(e.v,e.parent)}),e.edges.forEach(function(e){t.setEdge({v:e.v,w:e.w,name:e.name},e.value)}),t}};function Z(t){var r,n={},o=[];function a(e){n.hasOwnProperty(e)||(n[e]=!0,r.push(e),t.successors(e).forEach(a),t.predecessors(e).forEach(a))}return t.nodes().forEach(function(e){r=[],a(e),r.length&&o.push(r)}),o}function d(t,e,r,n){var o=t,a=String(e),s=r||W,i=n||function(e){return t.outEdges(e)};function d(e){var t=e.v!==h?e.v:e.w,r=c[t],n=s(e),o=u.distance+n;if(n<0)throw new Error("dijkstra does not allow negative edge weights. Bad edge: "+e+" Weight: "+n);o<r.distance&&(r.distance=o,r.predecessor=h,l.decrease(t,o))}var h,u,c={},l=new z;for(o.nodes().forEach(function(e){var t=e===a?0:Number.POSITIVE_INFINITY;c[e]={distance:t},l.add(e,t)});0<l.size()&&(h=l.removeMin(),(u=c[h]).distance!==Number.POSITIVE_INFINITY);)i(h).forEach(d);return c}var h=class{_arr=[];_keyIndices={};size(){return this._arr.length}keys(){return this._arr.map(function(e){return e.key})}has(e){return this._keyIndices.hasOwnProperty(e)}priority(e){e=this._keyIndices[e];if(void 0!==e)return this._arr[e].priority}min(){if(0===this.size())throw new Error("Queue underflow");return this._arr[0].key}add(e,t){var r,n,o=this._keyIndices;return e=String(e),!o.hasOwnProperty(e)&&(n=(r=this._arr).length,o[e]=n,r.push({key:e,priority:t}),this._decrease(n),!0)}removeMin(){this._swap(0,this._arr.length-1);var e=this._arr.pop();return delete this._keyIndices[e.key],this._heapify(0),e.key}decrease(e,t){var r=this._keyIndices[e];if(t>this._arr[r].priority)throw new Error("New priority is greater than current priority. Key: "+e+" Old: "+this._arr[r].priority+" New: "+t);this._arr[r].priority=t,this._decrease(r)}_heapify(e){var t=this._arr,r=2*e,n=1+r,o=e;r<t.length&&(o=t[r].priority<t[o].priority?r:o,(o=n<t.length?t[n].priority<t[o].priority?n:o:o)!==e)&&(this._swap(e,o),this._heapify(o))}_decrease(e){for(var t,r=this._arr,n=r[e].priority;0!==e&&!(r[t=e>>1].priority<n);)this._swap(e,t),e=t}_swap(e,t){var r=this._arr,n=this._keyIndices,o=r[e],a=r[t];r[e]=a,r[t]=o,n[a.key]=e,n[o.key]=t}},z=h,W=()=>1;function Y(r,n,o){return r.nodes().reduce(function(e,t){return e[t]=R(r,t,n,o),e},{})}var R=d;function u(a){var s=0,i=[],d={},h=[];return a.nodes().forEach(function(e){d.hasOwnProperty(e)||!function t(e){var r=d[e]={onStack:!0,lowlink:s,index:s++};if(i.push(e),a.successors(e).forEach(function(e){d.hasOwnProperty(e)?d[e].onStack&&(r.lowlink=Math.min(r.lowlink,d[e].index)):(t(e),r.lowlink=Math.min(r.lowlink,d[e].lowlink))}),r.lowlink===r.index){for(var n,o=[];n=i.pop(),d[n].onStack=!1,o.push(n),e!==n;);h.push(o)}}(e)}),h}function U(t){return X(t).filter(function(e){return 1<e.length||1===e.length&&t.hasEdge(e[0],e[0])})}var X=u;function $(t,e,r){var n=e||J,o=r||function(e){return t.outEdges(e)},s={},i=t.nodes();return i.forEach(function(r){s[r]={},s[r][r]={distance:0},i.forEach(function(e){r!==e&&(s[r][e]={distance:Number.POSITIVE_INFINITY})}),o(r).forEach(function(e){var t=e.v===r?e.w:e.v,e=n(e);s[r][t]={distance:e,predecessor:r}})}),i.forEach(function(o){var a=s[o];i.forEach(function(e){var n=s[e];i.forEach(function(e){var t=n[o],r=a[e],e=n[e],t=t.distance+r.distance;t<e.distance&&(e.distance=t,e.predecessor=r.predecessor)})})}),s}var J=()=>1;function c(r){var n={},o={},a=[];if(r.sinks().forEach(function e(t){if(o.hasOwnProperty(t))throw new l;n.hasOwnProperty(t)||(n[t]=o[t]=!0,r.predecessors(t).forEach(e),delete o[t],a.push(t))}),Object.keys(n).length!==r.nodeCount())throw new l;return a}class l extends Error{constructor(){super(...arguments)}}var f=c,p=(c.CycleException=l,f);function g(t,e,r){Array.isArray(e)||(e=[e]);var n=t.isDirected()?e=>t.successors(e):e=>t.neighbors(e),o="post"===r?K:Q,a=[],s={};return e.forEach(e=>{if(!t.hasNode(e))throw new Error("Graph does not have node: "+e);o(e,n,s,a)}),a}function K(e,t,r,n){for(var o=[[e,!1]];0<o.length;){var a=o.pop();a[1]?n.push(a[0]):r.hasOwnProperty(a[0])||(r[a[0]]=!0,o.push([a[0],!0]),y(t(a[0]),e=>o.push([e,!1])))}}function Q(e,t,r,n){for(var o=[e];0<o.length;){var a=o.pop();r.hasOwnProperty(a)||(r[a]=!0,n.push(a),y(t(a),e=>o.push(e)))}}function y(e,t){for(var r=e.length;r--;)t(e[r],r,e)}var H=g;var ee=g;var te=t,re=h;var v={Graph:r.Graph,json:B,alg:{components:Z,dijkstra:d,dijkstraAll:Y,findCycles:U,floydWarshall:$,isAcyclic:function(e){try{p(e)}catch(e){if(e instanceof p.CycleException)return!1;throw e}return!0},postorder:function(e,t){return H(e,t,"post")},preorder:function(e,t){return ee(e,t,"pre")},prim:function(e,n){var o,t=new te,a={},s=new re;function r(e){var t=e.v===o?e.w:e.v,r=s.priority(t);void 0!==r&&(e=n(e))<r&&(a[t]=o,s.decrease(t,e))}if(0!==e.nodeCount()){e.nodes().forEach(function(e){s.add(e,Number.POSITIVE_INFINITY),t.setNode(e)}),s.decrease(e.nodes()[0],0);for(var i=!1;0<s.size();){if(o=s.removeMin(),a.hasOwnProperty(o))t.setEdge(o,a[o]);else{if(i)throw new Error("Input graph is not connected: "+e);i=!0}e.nodeEdges(o).forEach(r)}}return t},tarjan:u,topsort:f},version:r.version},t=S(v);function ne(a,e=[]){let s=new v.Graph({directed:!0});if(0!==a.nodes().length){e=((e,t)=>{for(var r=0;r<t.length;++r)if("min"===t[r].type)return t;var n=e.sources();return[{type:"min",nodes:[(n.length?n:e.nodes())[0]]}].concat(t)})(a,e);let o=b.map();for(var t,r,n,i=0;i<e.length;++i)if((t=e[i]).nodes&&0!==t.nodes.length){for(r=""+i,n=0;n<t.nodes.length;++n)o.set(t.nodes[n],r);s.setNode(r,{type:t.type,nodes:t.nodes})}a.nodes(),a.nodes().forEach(e=>{a.node(e),o.has(e)||(r=""+i++,t={type:"same",nodes:[e]},o.set(e,r),s.setNode(r,t))}),a.edges().forEach(e=>{a.edge(e);var t=o.get(e.v),r=o.get(e.w),n=s.edge(t,r)||{delta:0};t===r?(n.delta=0,s.setEdge(t,r,n)):a.node(e.v).backwards?(n.delta=Math.max(n.delta,a.node(e.w).backwards?1:0),s.setEdge(r,t,n)):(n.delta=Math.max(n.delta,a.node(e.w).backwards?0:1),s.setEdge(t,r,n))})}return s}function oe(r,e){let n=((t,e)=>{let r=b.set(),n=new v.Graph({directed:!0}),o=[];if(t.hasNode(e))return m(t,e,r,n,o),t.nodes().forEach(e=>{r.has(e)||m(t,e,r,n,o)}),o.forEach((e,t)=>{n.node(e).thread=t+1<o.length?o[t+1]:o[0]}),n;throw Error("node not in graph")})(r,e);r.edges().forEach(e=>{var t;((t,r,n)=>{var o=t.node(r),a=t.node(n);if(o.depth<a.depth){let e=o.thread;for(;t.node(e).depth>o.depth;){if(e===n)return 1;e=t.node(e).thread}}else if(a.depth<o.depth){let e=a.thread;for(;t.node(e).depth>a.depth;){if(e===r)return-1;e=t.node(e).thread}}return 0})(n,e.v,e.w)<0&&((t=r.edge(e)||{}).reversed=!0,r.removeEdge(e),r.setEdge(e.w,e.v,t))})}function m(r,n,o,a,s,i=0){o.has(n)||(o.add(n),s.push(n),a.setNode(n,{depth:i}),r.outEdges(n).map(e=>e.w).forEach((e,t)=>{o.has(e)||a.setEdge(n,e,{delta:1}),m(r,e,o,a,s,i+1)}))}function E(e){return e.v+""+e.w+""+e.name}function ae(r,e){let a=ne(r,e);if(0!==a.nodeCount()){var t,n;(t=a).sources().forEach(e=>{"0"!==e&&t.setEdge("0",e,{temp:!0,delta:0})}),oe(a,"0");{var s=a;let t=s.sources(),n=b.set(),o=b.set();for(s.edges().forEach(e=>{e.v===e.w&&o.add(E(e))}),s.nodes().forEach(e=>{s.node(e).rank=0});0<t.length;){var i=t.shift();n.add(i);let r=s.node(i);r||s.setNode(i,r={}),r.rank=0,s.inEdges(i).forEach(e=>{var t=void 0===s.edge(e).delta?1:s.edge(e).delta;r.rank=Math.max(r.rank,s.node(e.v).rank+t)}),s.outEdges(i).forEach(e=>{o.add(E(e))}),s.nodes().forEach(e=>{t.indexOf(e)<0&&!n.has(e)&&!s.inEdges(e).some(e=>!o.has(E(e)))&&t.push(e)})}}(n=a).edges().forEach(e=>{var t;n.edge(e).temp&&(e=e.w,t=n.node(e),void 0!==(e=O.min(n.outEdges(e),e=>n.node(e.w).rank-n.edge(e).delta)))&&(t.rank=e)}),a.nodes().forEach(e=>{let t=a.node(e);t.nodes.forEach(e=>{r.node(e).rank=t.rank})})}}let se=t.alg;function w(i,d,h){{var u=i;i=d,d=h;let e,t,r,n=(t=i.length>d.length?(e=i,d):(e=d,i),r=t.length,[]),o=(e.forEach(r=>{t.forEach((e,t)=>{(u.hasEdge(r,e)||u.hasEdge(e,r))&&n.push(t)})}),1);for(;o<r;)o*=2;var c=2*o-1;--o;let a=new Array(c);for(let e=0;e<c;e++)a[e]=0;let s=0;return n.forEach(e=>{let t=e+o;for(a[t]++;0<t;)t%2&&(s+=a[t+1]),t=Math.floor((t-1)/2),a[t]++}),s}}function _(e,t,r){let n=0;return 0<r&&(n+=w(e,t[r-1],t[r])),r+1<t.length&&(n+=w(e,t[r],t[r+1])),n}function x(e,t,r){var n=e[t];e[t]=e[r],e[r]=n}function L(e){var t,r,n=Math.floor(e.length/2);return 0===e.length?-1:e.length%2==1?e[n]:2===e.length?(e[0]+e[1])/2:(t=e[n-1]-e[0],(e[n-1]*(r=e[e.length-1]-e[n])+e[n]*t)/(t+r))}function N(r,e,t,n,o,a=!1){t=e[t];let s=[];return e[n].forEach((e,t)=>{0<r.nodeEdges(e,o).length&&s.push(t)}),0===s.length&&a&&t.forEach((e,t)=>{0<r.nodeEdges(e,o).length&&s.push(t+.5)}),s.sort((e,t)=>e-t),s}function ie(o,a){var s=b.map(o.map((e,t)=>[e,t]),e=>e[0]);for(let e=1;e<o.length;++e)for(let n=e;0<n;--n){let e=n-1,t=a.get(o[e]),r=a.get(o[n]);for(;-1===(t=a.get(o[e]))&&0<e;)e--;if(-1===r||-1===t)break;if(t===r&&(t=s.get(o[e]),r=s.get(o[n])),r>=t)break;[o[n],o[e]]=[o[e],o[n]]}}function de(n,e=25){var t=(r=>{let n=[];return r.nodes().forEach(e=>{for(var t=r.node(e).rank||0;t>=n.length;)n.push([]);n[t].push(e)}),n})(n),o=((e,t)=>{let o=[];if(0!==t.length){let r=e.sources(),n=b.map();t.forEach((e,t)=>{o.push([]),e.forEach(e=>{0===t&&r.indexOf(e)<0&&r.push(e),n.set(e,t)})}),se.preorder(e,r).forEach(e=>{o[n.get(e)].push(e)})}return o})(n,t);let r=o,a=0;for(;a++<e;){((o,a,e,s=!1)=>{if(0<e)for(let n=1;n<a.length;++n){let r=b.map();a[n].forEach(e=>{var t=L(N(o,a,n,n-1,e,s));r.set(e,t)}),ie(a[n],r)}else for(let n=a.length-2;0<=n;--n){let r=b.map();a[n].forEach(e=>{var t=L(N(o,a,n,n+1,e,s));r.set(e,t)}),ie(a[n],r)}})(n,o,a%2==0);{s=void 0;i=void 0;d=void 0;h=void 0;var s=n;var i=o;let r=!0;for(;r;){r=!1;for(let t=0;t<i.length;++t)for(let e=0;e<i[t].length-1;++e){var d=_(s,i,t),h=(x(i[t],e,e+1),_(s,i,t));h<d?r=!0:x(i[t],e,e+1)}}}he(n,o)<he(n,r)&&(r=(e=>{let t=[];return e.forEach(e=>{t.push(e.map(e=>e))}),t})(o))}r.forEach(e=>{e.forEach((e,t)=>{n.node(e).depth=t})})}function he(t,r){let n=0;for(let e=0;e<r.length-1;++e)n+=w(t,r[e],r[e+1]);return n}function ue(r){"object"!=typeof r.graph()&&r.setGraph({}),r.graph().dummyChains=[],r.edges().forEach(t=>{{var a=r,s=t;let o=a.edge(s),e=((e,t)=>{let r=[],n=e.rank;if(n+1<=t.rank){for(e.backwards&&r.push({rank:n,backwards:!1});++n<t.rank;)r.push({rank:n,backwards:!1});t.backwards&&r.push({rank:n,backwards:!1})}else if(n>t.rank){for(e.backwards||r.push({rank:n,backwards:!0});n-- >t.rank+1;)r.push({rank:n,backwards:!0});t.backwards||r.push({rank:n,backwards:!0})}return r})(a.node(s.v),a.node(s.w));if(0!==e.length){a.removeEdge(s);let n=s.v;function i(e,t){var r={points:[],value:o.value,origEdge:s,origLabel:o};a.setEdge(e,t,r,s.name)}e.forEach((e,t)=>{var r=`__${s.v}_${s.w}_`+t;a.hasNode(r)||(e.dummy="edge",a.setNode(r,e),0===t&&a.graph().dummyChains.push(r)),i(n,n=r)}),i(n,s.w)}}})}function ce(e){var r,n=O.max(e,e=>e.rank||0)||0,o=O.max(e,e=>e.band||0)||0,a=b.nest().key(e=>e.rank||0).key(e=>e.band||0).sortValues((e,t)=>e.depth-t.depth).map(e),s=new Array(n+1);for(let t=0;t<=n;++t)if(s[t]=new Array(o+1),r=a.get(t))for(let e=0;e<=o;++e)s[t][e]=r.get(e)||[];else for(let e=0;e<=o;++e)s[t][e]=[];return s.bandValues=(e=>{if(0===e.length||0===e[0].length)return[];let t=e[0].length,r=new Array(t);for(let e=0;e<t;e++)r[e]=0;return e.forEach(e=>{e.forEach((e,t)=>{e=O.sum(e,e=>e.value);r[t]=Math.max(r[t],e)})}),r})(s),s}function le(e,t){return 1}function fe(){var f=le;function p(u,c,l){u.forEach(e=>{let h=0;e.forEach((r,e)=>{e=u.bandValues[e]/O.sum(u.bandValues)*c;let n=l*e/5,o=e-2*n;var t=O.sum(r,e=>e.dy);let a=r.map((e,t)=>e.value&&r[t+1]?f(r[t],r[t+1],p):0);t=Math.max(0,o-t);let s=O.sum(a)?t/O.sum(a):0,i=h+n,d=(1===r.length&&(i+=(o-r[0].dy)/2),Number.MAX_VALUE);r.forEach((e,t)=>{e.y=i,e.spaceAbove=d,e.spaceBelow=a[t]*s,i+=e.dy+e.spaceBelow,d=e.spaceBelow,e.data&&void 0!==e.data.forceY&&(e.y=n+e.data.forceY*(o-e.dy))}),0<r.length&&(r[r.length-1].spaceBelow=Number.MAX_VALUE),h+=e})})}return p.separation=function(e){return arguments.length?(f=(e=>{if("function"!=typeof e)throw new Error;return e})(e),p):f},p}function pe(i,d){i.nodes().forEach(e=>{let n=i.node(e),r=b.map();function o(e,t){return r.has(e)?r.get(e):(t={id:e,node:n.data,side:t,incoming:[],outgoing:[]},r.set(e,t),t)}i.inEdges(e).forEach(e=>{var t=i.edge(e),r=o(t.targetPortId||"in","l"!==n.direction?"west":"east");r.incoming.push(e),t.targetPort=r}),i.outEdges(e).forEach(e=>{var t=i.edge(e),r=o(t.sourcePortId||"out","l"!==n.direction?"east":"west");r.outgoing.push(e),t.sourcePort=r}),n.ports=r.values(),n.ports.sort(d),n.fromElsewhere=n.fromElsewhere||[],n.toElsewhere=n.toElsewhere||[];let t=0,a=(n.fromElsewhere.forEach(e=>{e.x1=n.x0,t+=e.dy}),{west:t,east:0}),s={west:0,east:0};n.ports.forEach(t=>{t.y=a[t.side],t.index=s[t.side],t.dy=Math.max(O.sum(t.incoming,e=>i.edge(e).dy),O.sum(t.outgoing,e=>i.edge(e).dy));let r="west"===t.side?n.x0:n.x1;t.outgoing.forEach(e=>{e=i.edge(e);e.x0=r,e.y0=n.y+t.y+e.dy/2}),t.incoming.forEach(e=>{e=i.edge(e);e.x1=r,e.y1=n.y+t.y+e.dy/2}),a[t.side]+=t.dy,s[t.side]+=1}),n.toElsewhere.forEach(e=>{e.x0=n.x1})})}function ge(e,t,r=!0){return t.v===t.w?Math.PI/2*(r?1:-1):(r=e.edge(t),Math.atan2(r.y1-r.y0,r.x1-r.x0))}function ye(n){n.nodes().forEach(e=>{{var r=n;let t=r.node(e);t.ports.forEach(e=>{e.incoming.sort(ve(r,t,!1)),e.outgoing.sort(ve(r,t,!0))})}})}function ve(a,e,s=!0){return function(e,t){var r=ge(a,e,s),n=ge(a,t,s),o=s?1:-1;return e.v===t.v&&e.w===t.w&&Math.abs(r-n)<.001?"number"==typeof e.name&&"number"==typeof t.name?e.name-t.name:"string"==typeof e.name&&"string"==typeof t.name?e.name.localeCompare(t.name):0:Math.abs(r-n)<.001?e.w===t.w?a.node(t.v).y-a.node(e.v).y:e.v===t.v?a.node(t.w).y-a.node(e.w).y:0:o*(r-n)}}function me(t,r){let n=null;for(let e=0;e<t.length;++e)if(r(t[e])){n=e;break}return n}function Ee(o,a){if(0!==o.length){let t=0,r;for(let e=o.length-1;0<=e;--e)r=o[e].dy/2,o[e][a]-r<t&&(o[e][a]=Math.min(o[e].Rmax,t+r)),t=o[e][a]+r;let n=o[0].Rmax+o[0].dy/2;for(let e=0;e<o.length;++e)r=o[e].dy/2,o[e][a]+r>n&&(o[e][a]=Math.max(r,n-r)),n=o[e][a]-r}}function we(e){var o,t;(o=e).nodes().forEach(e=>{let t=o.node(e),r=t.y,n=t.y;t.fromElsewhere.forEach(e=>{e.y1=n+e.dy/2,e.d1=t.backwards?"l":"r",e.dy=e.dy,n+=e.dy}),t.ports.forEach(e=>{r=t.y+e.y,n=t.y+e.y,e.outgoing.forEach(e=>{e=o.edge(e);e.y0=r+e.dy/2,e.d0=t.backwards?"l":"r",e.dy=e.dy,r+=e.dy}),e.incoming.forEach(e=>{e=o.edge(e);e.y1=n+e.dy/2,e.d1=t.backwards?"l":"r",e.dy=e.dy,n+=e.dy})}),t.toElsewhere.forEach(e=>{e.y0=r+e.dy/2,e.d0=t.backwards?"l":"r",e.dy=e.dy,r+=e.dy})}),(t=e).nodes().forEach(e=>{e=t.node(e);P(e.toElsewhere,"r0"),P(e.fromElsewhere,"r1"),e.ports.forEach(e=>{P(e.outgoing.map(e=>t.edge(e)),"r0"),P(e.incoming.map(e=>t.edge(e)),"r1")})})}function P(t,n){t.forEach(e=>{var t,r;e.Rmax=(t=e.x1-e.x0,r=e.y1-e.y0,e.d0!==e.d1?Math.abs(r)/2.1:0!=r?(t*t+r*r)/Math.abs(4*r):1/0),e[n]=Math.max(e.dy/2,e.d0===e.d1?.6*e.Rmax:5+e.dy/2)});let r=me(t,"r0"===n?e=>e.y1>e.y0:e=>e.y0>e.y1);if(null===r&&(r=t.length),Ee(t.slice(r),n),0<r){var o=[];for(let e=r-1;0<=e;e--)o.push(t[e]);Ee(o,n)}}let _e=t.Graph;function xe(e,n,o,a,s,i,d){var h=new _e({directed:!0,multigraph:!0});return e.nodes.forEach(function(e,t){var r=n(e,t);if(h.hasNode(r))throw new Error("duplicate: "+r);h.setNode(r,{data:e,index:t,backwards:o(e,t),fromElsewhere:e.fromElsewhere||[],toElsewhere:e.toElsewhere||[],x0:e.x0,x1:e.x1,y:e.y0})}),e.links.forEach(function(e,t){var r=be(a(e,t)),n=be(s(e,t)),o={data:e,sourcePortId:r.port,targetPortId:n.port,index:t,points:[],value:d(e,t),type:i(e,t)};if(!h.hasNode(r.id))throw new Error("missing: "+r.id);if(!h.hasNode(n.id))throw new Error("missing: "+n.id);h.setEdge(r.id,n.id,o,i(e,t))}),h.setGraph({}),h}function be(e){return"object"==typeof e?e:{id:e,port:void 0}}function ke(e){return e.nodes}function Me(e){return e.links}function Le(e){return e.id}function Ne(e){return e.direction&&"l"===e.direction.toLowerCase()}function Pe(e){return{id:"object"==typeof e.source?e.source.id:e.source,port:"object"==typeof e.sourcePort?e.sourcePort.id:e.sourcePort}}function Oe(e){return{id:"object"==typeof e.target?e.target.id:e.target,port:"object"==typeof e.targetPort?e.targetPort.id:e.targetPort}}function Ce(e){return e.type}function Ie(e,t){return e.id.localeCompare(t.id)}function C(n,o){n.nodes().forEach(e=>{var t=n.node(e),r=O.sum(n.inEdges(e),e=>n.edge(e).value),e=O.sum(n.outEdges(e),e=>n.edge(e).value);r+=O.sum(t.fromElsewhere||[],e=>o(e)),e+=O.sum(t.toElsewhere|[],e=>o(e)),t.value=Math.max(r,e)})}function I(n,o){n.edges().forEach(e=>{e=n.edge(e);e.dy=e.value*o}),n.nodes().forEach(t=>{let r=n.node(t);r.dy=r.value*o,r.fromElsewhere=r.fromElsewhere||[],r.toElsewhere=r.toElsewhere||[],r.fromElsewhere.forEach(e=>{e.dy=e.value*o,e.source={id:"__from_elsewhere_"+t},e.target=r.data}),r.toElsewhere.forEach(e=>{e.dy=e.value*o,e.source=r.data,e.target={id:"__to_elsewhere_"+t}})})}function j(e){if("function"!=typeof e)throw new Error;return e}function je(t){t.edges().forEach(e=>{e=t.edge(e);e.points.unshift({x:e.x0,y:e.y0,ro:e.r0,d:e.d0}),e.points.push({x:e.x1,y:e.y1,ri:e.r1,d:e.d1})}),t.nodes().forEach(e=>{e=t.node(e);e.fromElsewhere.forEach(e=>{e.points=[{x:e.x1,y:e.y1,ri:e.r1,d:e.d1,style:"down-right"}]}),e.toElsewhere.forEach(e=>{e.points=[{x:e.x0,y:e.y0,ri:e.r0,d:e.d0,style:"right-down"}]})})}function Ae(r){r.nodes().forEach(e=>{e=r.node(e);e.data.incoming=[],e.data.outgoing=[],e.data.ports=e.ports,e.data.ports.forEach(e=>{e.incoming=[],e.outgoing=[]}),e.data.dy=e.dy,e.data.x0=e.x0,e.data.x1=e.x1,e.data.y0=e.y,e.data.y1=e.y+e.dy,e.data.rank=e.rank,e.data.band=e.band,e.data.depth=e.depth,e.data.value=e.value,e.data.spaceAbove=e.spaceAbove,e.data.spaceBelow=e.spaceBelow}),r.edges().forEach(e=>{var t=r.edge(e);t.data.source=r.node(e.v).data,t.data.target=r.node(e.w).data,t.data.sourcePort=t.sourcePort,t.data.targetPort=t.targetPort,t.data.source.outgoing.push(t.data),t.data.target.incoming.push(t.data),t.data.sourcePort&&t.data.sourcePort.outgoing.push(t.data),t.data.targetPort&&t.data.targetPort.incoming.push(t.data),t.data.value=t.value,t.data.type=t.type,t.data.dy=t.dy,t.data.points=t.points||[]})}function A(e){return e.y+e.dy/2}function De(e){return e.value}function Te(e,t){return e.y-t.y}function Ve(e){return 0===e.dy?0:2}function Fe(){var ie=Ve;function t(e){var t,r;if(1===e.points.length)return t=(r=e).points[0],r=Math.max(ie(r),r.dy)/2,"down-right"===t.style?"M"+[t.x-20,t.y-r]+" L"+[t.x,t.y-r]+" L"+[t.x,t.y+r]+" L"+[t.x-20,t.y+r]+" Z":"right-down"===t.style?"M"+[t.x,t.y-r]+" L"+[t.x+20,t.y-r]+" L"+[t.x+20,t.y+r]+" L"+[t.x,t.y+r]+" Z":void 0;for(var n="",o=0;o<e.points.length-1;++o)n+=(v=>{var e,r,V,F,t,n,G,S,o,a,s,i,d,h,u,q,B,Z,c,l,f,p,z,W,g,y,m,E,w,_,Y,R,U,x,b,k,X,$,J,K,M,L,N,P,O,Q,H,ee,C,I=(v.d0||"r")+(v.d1||"r");function te(e){var t=0<e?1:0,e=0<e?V+r:V-r;return"A"+e+" "+e+" "+Math.abs(F)+" 1 "+t+" "}if(v.source&&v.source===v.target)return e=v,r=Math.max(ie(e),e.dy)/2,V=1.5*r,F=2*Math.PI,"M"+[(t=e.x0)+.1,(e=e.y0)-r]+" "+te(1)+[t-.1,e-r]+" L"+[t-.1,e+r]+" "+te(-1)+[t+.1,e+r]+" Z";function re(e){var t=0<e*i?1:0,e=0===i?s:0<e*i?s+n:s-n;return"A"+e+" "+e+" "+Math.abs(i)+" 0 "+t+" "}if("rl"===I)return c=v,n=Math.max(ie(c),c.dy)/2,G=c.x0,S=c.x1,o=c.y0,u=c.x1-c.x0,h=(a=c.y1)-c.y0,d=(c.r0+c.r1)/2||5+n,s=Math.min(Math.abs(a-o)/2.1,d),i=Math.atan2(h-2*s,u),d=c.y1>c.y0?1:-1,h=n*Math.sin(i),u=n*Math.cos(i),q=c.x0+s*Math.sin(Math.abs(i)),B=c.x1+s*Math.sin(Math.abs(i)),Z=c.y0+s*d*(1-Math.cos(i)),c=c.y1-s*d*(1-Math.cos(i)),"M"+[G,o-n]+" "+re(1)+[q+h,Z-u]+" L"+[B+h,c-u]+" "+re(1)+[S,a+n]+" L"+[S,a-n]+" "+re(-1)+[B-h,c+u]+" L"+[q-h,Z+u]+" "+re(-1)+[G,o+n]+" Z";function ne(e){var t=0<e*g?1:0,e=0==g?y:0<e*g?y+l:y-l;return"A"+e+" "+e+" "+Math.abs(g)+" 0 "+t+" "}if("rd"===I)return m=v,l=Math.max(ie(m),m.dy)/2,f=m.x0,p=m.x1,z=m.y0,W=m.y1,g=Math.PI/2,y=Math.max(0,p-f),m.y1,m=z+y,"M"+[f,z-l]+" "+ne(1)+[p+l,m]+" L"+[p+l,W]+" "+[p-l,W]+" "+[p-l,m]+" "+ne(-1)+[f,z+l]+" Z";function oe(e){var t=0<e*x?1:0,e=0==x?b:0<e*x?b+w:b-w;return"A"+e+" "+e+" "+Math.abs(x)+" 0 "+t+" "}if("dr"===I)return E=v,w=Math.max(ie(E),E.dy)/2,_=E.x0,Y=E.x1,R=E.y0,U=E.y1,x=Math.PI/2,b=Math.max(0,Y-_),E.y1,"M"+[_-w,R]+" L"+[_+w,R]+" "+[_+w,E=U-b]+" "+oe(-1)+[Y,U-w]+" L"+[Y,U+w]+" "+oe(1)+[_-w,E]+" Z";function ae(e){var t=0<e*L?1:0,e=0===L?M:0<-e*L?M+k:M-k;return"A"+e+" "+e+" "+Math.abs(L)+" 0 "+t+" "}if("lr"===I)return C=v,k=Math.max(ie(C),C.dy)/2,X=C.x0,$=C.x1,J=C.y0,O=C.x1-C.x0,P=(K=C.y1)-C.y0,N=(C.r0+C.r1)/2||5+k,M=Math.min(Math.abs(P)/2.1,N),L=Math.atan2(P-2*M,O),N=C.y1>C.y0?1:-1,P=k*Math.sin(L),O=k*Math.cos(L),Q=C.x0-M*Math.sin(Math.abs(L)),H=C.x1-M*Math.sin(Math.abs(L)),ee=C.y0+M*N*(1-Math.cos(L)),C=C.y1-M*N*(1-Math.cos(L)),"M"+[X,J-k]+" "+ae(-1)+[Q-P,ee-O]+" L"+[H-P,C-O]+" "+ae(-1)+[$,K+k]+" L"+[$,K-k]+" "+ae(1)+[H+P,C-O]+" L"+[Q+P,ee-O]+" "+ae(1)+[X,J+k]+" Z";{var j=Math.max(ie(v),v.dy)/2,I=v.x0,A=v.x1,D=v.y0,T=v.y1;A<I&&([I,A]=[A,I],[D,T]=[T,D]);let e=D<T?1:-1,t=(e=>{var t=e.x1-e.x0,r=e.y1-e.y0;return[e.dy/2,(t*t+r*r)/Math.abs(4*r)]})(v),r=Math.max(t[0],Math.min(t[1],(A-I)/3)),n=Math.max(t[0],Math.min(t[1],v.r0||r)),o=Math.max(t[0],Math.min(t[1],v.r1||r)),a=A-I,s=T-D-e*(n+o),i=Math.sqrt(a*a+s*s),d=-e*Math.acos(Math.min(1,(n+o)/i)),h=Math.atan2(s,a),u=Math.PI/2+e*(h+d),c=j*e*Math.sin(u),l=j*Math.cos(u),f=I+ +n*Math.sin(Math.abs(u)),p=A-+o*Math.sin(Math.abs(u)),g=D+n*e*(1-Math.cos(u)),y=T-o*e*(1-Math.cos(u));return(isNaN(u)||Math.abs(u)<.001)&&(u=n=o=0,f=I,p=A,g=D,y=T,c=0,l=j),A="M"+[I,D-j]+" "+se(1,n)+[f+c,g-l]+" L"+[p+c,y-l]+" "+se(-1,o)+[A,T-j]+" L"+[A,T+j]+" "+se(1,o)+[p-c,y+l]+" L"+[f-c,g+l]+" "+se(-1,n)+[I,D+j]+" Z",/NaN/.test(A)&&console.error("path NaN",v,A),A;function se(e,t){var r=0<e*(T-D)?1:0,e=0<+e*(T-D)?t+j:t-j;return"A"+(e=0===u?t:e)+" "+e+" "+Math.abs(u)+" 0 "+r+" "}}})({x0:e.points[o].x,y0:e.points[o].y,x1:e.points[o+1].x,y1:e.points[o+1].y,r0:e.points[o].ro,r1:e.points[o+1].ri,d0:e.points[o].d,d1:e.points[o+1].d,dy:e.dy});return n}return t.minWidth=function(e){return arguments.length?(ie=(e=>{if("function"!=typeof e)throw new Error;return e})(e),t):ie},t}function Ge(){let c=e=>void 0!==e.title?e.title:e.id,l=e=>null,f=e=>!!c(e);function t(h){let u=h.selection?h.selection():h;u.select("text").empty()&&(u.append("title"),u.append("line").attr("x1",0).attr("x2",0),u.append("rect").attr("class","node-body"),u.append("text").attr("class","node-value").attr("dy",".35em").attr("text-anchor","middle"),u.append("text").attr("class","node-title").attr("dy",".35em"),u.append("rect").attr("class","node-click-target").attr("x",-5).attr("y",-5).attr("width",10).style("fill","none").style("visibility","hidden").style("pointer-events","all"),u.attr("transform",Se)),u.each(function(e){var t=k.select(this).select("title"),r=k.select(this).select(".node-value");let n=k.select(this).select(".node-title"),o=k.select(this).select("line"),a=k.select(this).select(".node-body"),s=k.select(this).select(".node-click-target"),i=(e=>{let t=!1,r=!1;return t=20<e.spaceAbove&&"type"!==e.style,0===e.incoming.length?(r=!0,t=!1):0===e.outgoing.length&&(r=!1,t=!1),{titleAbove:t,right:r}})(e);i.dy=e.y0===e.y1?0:Math.max(1,e.y1-e.y0);var d=2<e.x1-e.x0,e=c(e)+(!d&&l(e)?" ("+l(e)+")":"");t.text(e),r.text(l).style("display",d?"inline":"none"),n.attr("text-anchor",i.right?"end":"start").text(e).each(qe,100),h!==u&&(n=n.transition(h),o=o.transition(h),a=a.transition(h),s=s.transition(h)),h.attr("transform",Se),o.attr("y1",function(e){return i.titleAbove?-5:0}).attr("y2",function(e){return i.dy}).style("display",function(e){return e.y0!==e.y1&&f(e)?"inline":"none"}),s.attr("height",function(e){return i.dy+5}),a.attr("width",function(e){return e.x1-e.x0}).attr("height",function(e){return i.dy}),n.attr("transform",function(e){let t=i,r=t.titleAbove?-10:(e.y1-e.y0)/2,n;n=t.titleAbove?t.right?4:-4:t.right?-4:e.x1-e.x0+4;return"translate("+n+","+r+")"}).style("display",function(e){return e.y0!==e.y1&&f(e)?"inline":"none"}),r.style("font-size",function(e){return Math.min(e.x1-e.x0-4,e.y1-e.y0-4)+"px"}).attr("transform",function(e){var t=e.x1-e.x0,e=e.y1-e.y0;return"translate("+t/2+","+e/2+") rotate("+(e<t?0:-90)+")"})})}return t.nodeVisible=function(e){return arguments.length?(f=D(e),t):f},t.nodeTitle=function(e){return arguments.length?(c=D(e),t):c},t.nodeValue=function(e){return arguments.length?(l=D(e),t):l},t}function Se(e){return"translate("+e.x0+","+e.y0+")"}function qe(e,t){var r=k.select(this),n=r.text().split(/\n/);1!==n.length&&(r.text(null),n.forEach(function(e,t){r.append("tspan").attr("x",0).attr("dy",1.1*(0===t?.7-n.length/2:1)+"em").text(e)}))}function D(e){if("function"!=typeof e)throw new Error;return e}function T(a,s,i){return function(e){var t=[],r=a(e.source),n=a(e.target),o=s(e);return t.push(r+" → "+n),o&&t.push(o),t.push(i(e.value)),t.join("\n")}}function Be(e){return null}e.sankey=function(){var t=ke,h=Me,u=Le,c=Ne,l=Pe,f=Oe,p=Ce,g=null,y=[],v=null,m=Ie,E=0,w=0,_=1,x=1,b=1,k=null,M=function(e){return e.value},L=.5,N=fe();function r(){var o,a,e={nodes:t.apply(null,arguments),links:h.apply(null,arguments)},r=xe(e,u,c,l,f,p,M);if(C(r,M),v)r.nodes().forEach(e=>{var e=r.node(e),t=v(e.data);e.x0=t[0],e.x1=t[0]+b,e.y=t[1]}),I(r,k);else{null!==g?(o=r,g.forEach((e,n)=>{e.forEach((e,r)=>{e.forEach?e.forEach((e,t)=>{e=o.node(e);e&&(e.rank=n,e.band=r,e.depth=t)}):(e=o.node(e))&&(e.rank=n,e.depth=r)})})):(ae(r,y),de(r,25)),ue(r),C(r,M),null===g&&de(r,25);var n=ce(r.nodes().map(e=>r.node(e)));P(0,n),I(r,k),N(n,x-w,L);{var s=r;n=_-E;var i=b;var d=O.max(s.nodes(),e=>s.node(e).rank||0)||0;let t=(n-i)/d;s.nodes().forEach(e=>{e=s.node(e);e.x0=t*(e.rank||0),e.x1=e.x0+i})}r.nodes().forEach(e=>{e=r.node(e);e.x0+=E,e.x1+=E,e.y+=w})}return pe(r,m),ye(r),we(r),((a=r).graph().dummyChains||[]).forEach(e=>{let r=a.node(e),t=a.inEdges(e).map(e=>a.edge(e)),n=(t.forEach(e=>{e.origLabel.dy=e.dy,e.origLabel.x0=e.x0,e.origLabel.y0=e.y0,e.origLabel.r0=e.r0,e.origLabel.d0=e.d0,a.setEdge(e.origEdge,e.origLabel)}),t.map(e=>e.r1));for(var o;r.dummy;)(t=a.outEdges(e).map(e=>a.edge(e))).forEach((e,t)=>{e.origLabel.points.push({x:(r.x0+r.x1)/2,y:e.y0,d:e.d0,ro:e.r0,ri:n[t]})}),n=t.map(e=>e.r1),o=a.successors(e)[0],a.removeNode(e),r=a.node(e=o);t.forEach(e=>{e.origLabel.x1=e.x1,e.origLabel.y1=e.y1,e.origLabel.r1=e.r1,e.origLabel.d1=e.d1})}),je(r),Ae(r),e}function P(e,t){null===k&&((t=O.sum(t.bandValues))<=0?k=1:(k=(x-w)/t,1!==L&&(k*=1-L)))}return r.update=function(e,t){var r=xe(e,u,c,l,f,p,M),n=(C(r,M),ce(r.nodes().map(e=>r.node(e))));return P(0,n),I(r,k),pe(r,m),ye(r),we(r),je(r),Ae(r),e},r.nodes=function(e){return arguments.length?(t=j(e),r):t},r.links=function(e){return arguments.length?(h=j(e),r):h},r.nodeId=function(e){return arguments.length?(u=j(e),r):u},r.nodeBackwards=function(e){return arguments.length?(c=j(e),r):c},r.sourceId=function(e){return arguments.length?(l=j(e),r):l},r.targetId=function(e){return arguments.length?(f=j(e),r):f},r.linkType=function(e){return arguments.length?(p=j(e),r):p},r.sortPorts=function(e){return arguments.length?(m=j(e),r):m},r.ordering=function(e){return arguments.length?(g=e,r):g},r.rankSets=function(e){return arguments.length?(y=e,r):y},r.nodeWidth=function(e){return arguments.length?(b=e,r):b},r.nodePosition=function(e){return arguments.length?(v=e,r):v},r.size=function(e){return arguments.length?(E=w=0,_=+e[0],x=+e[1],r):[_-E,x-w]},r.extent=function(e){return arguments.length?(E=+e[0][0],w=+e[0][1],_=+e[1][0],x=+e[1][1],r):[[E,w],[_,x]]},r.whitespace=function(e){return arguments.length?(L=e,r):L},r.scale=function(e){return arguments.length?(k=e,r):k},r.linkValue=function(e){return arguments.length?(M=e,r):M},r.verticalLayout=function(e){return arguments.length?(N=j(e),r):N},r},e.sankeyDiagram=function(){let u={top:0,right:0,bottom:0,left:0},c=null,l=[],t=G.format(".3s"),f=Ge(),p=Fe(),g=e=>null,y=T(f.nodeTitle(),e=>e.type,t),v=Be,m=F.dispatch("selectNode","selectGroup","selectLink");function r(h){(h.selection?h.selection():h).each(function(e){var t=k.select(this);let r=t.selectAll(".sankey").data([{type:"sankey"}]);var n=r.enter().append("g").classed("sankey",!0);n.append("g").classed("groups",!0),n.append("g").classed("links",!0),n.append("g").classed("nodes",!0),n.append("g").classed("slice-titles",!0),(r=r.merge(n)).attr("transform","translate("+u.left+","+u.top+")");let o=b.map(e.nodes,e=>e.id);n=(l||[]).map(e=>{{var r=o;let t={top:Number.MAX_VALUE,left:Number.MAX_VALUE,bottom:0,right:0};return e.nodes.forEach(e=>{e=r.get(e);e&&(e.x0<t.left&&(t.left=e.x0),e.x1>t.right&&(t.right=e.x1),e.y0<t.top&&(t.top=e.y0),e.y1>t.bottom)&&(t.bottom=e.y1)}),e.rect=t,e}});let a=Array.from(e.links);e.nodes.forEach(e=>{Array.prototype.push.apply(a,e.fromElsewhere||[]),Array.prototype.push.apply(a,e.toElsewhere||[])});var s=r,i=h,e=e.nodes,e=((s=s.select(".nodes").selectAll(".node").data(e,e=>e.id)).exit().remove(),s=s.merge(s.enter().append("g").attr("class","node").call(f).on("click",x)),(i instanceof M.transition?s.transition(i):s).call(f),r),d=h,i=a;(e=e.select(".links").selectAll(".link").data(i,e=>e.source.id+"-"+e.target.id+"-"+e.type)).exit().remove(),(i=e.enter().append("g").attr("class","link").on("click",_)).append("path").attr("d",p).style("fill","white").each(function(e){this._current=e}),i.append("title"),i.append("text").attr("class","label").attr("dy","0.35em").attr("x",e=>e.points[0].x+4).attr("y",e=>e.points[0].y),(e=e.merge(i)).classed("selected",e=>e.id===c),e.sort(w),d instanceof M.transition?(e=e.transition(d)).select("path").style("fill",g).each(function(e){k.select(this).transition(d).attrTween("d",E)}):e.select("path").style("fill",g).attr("d",p),e.select("title").text(y),e.select(".label").text(v).attr("x",e=>e.points[0].x+4).attr("y",e=>e.points[0].y);{i=n;let e=(s=t).select(".groups").selectAll(".group").data(i);e.exit().remove(),(s=e.enter().append("g").attr("class","group")).append("rect"),s.append("text").attr("x",-10).attr("y",-25),(e=e.merge(s)).style("display",e=>e.title?"inline":"none").attr("transform",e=>`translate(${e.rect.left},${e.rect.top})`).select("rect").attr("x",-10).attr("y",-20).attr("width",e=>e.rect.right-e.rect.left+20).attr("height",e=>e.rect.bottom-e.rect.top+30),e.select("text").text(e=>e.title)}t.on("click",function(){m.call("selectNode",this,null),m.call("selectLink",this,null)})})}function E(e){e.points.forEach(function(e){1e3<e.ri&&(e.ri=1e3),1e3<e.ro&&(e.ro=1e3)});var t,r=V.interpolate({points:(t=this._current).points,dy:t.dy},e),n=this;return function(e){return n._current=r(e),p(n._current)}}function w(e,t){return e.id===c?1:t.id===c||!e.source||e.target&&"d"===e.target.direction?-1:!t.source||t.target&&"d"===t.target.direction?1:!e.target||e.source&&"d"===e.source.direction?-1:!t.target||t.source&&"d"===t.source.direction?1:e.dy-t.dy}function _(e){k.event.stopPropagation();var t=k.select(this).node();m.call("selectLink",t,e)}function x(e){k.event.stopPropagation();var t=k.select(this).node();m.call("selectNode",t,e)}return r.margins=function(e){return arguments.length?(u={top:(void 0===e.top?u:e).top,left:(void 0===e.left?u:e).left,bottom:(void 0===e.bottom?u:e).bottom,right:(void 0===e.right?u:e).right},this):u},r.groups=function(e){return arguments.length?(l=e,this):l},r.nodeTitle=function(e){return arguments.length?(f.nodeTitle(e),y=T(e,e=>e.type,t),this):f.nodeTitle()},r.nodeValue=function(e){return arguments.length?(f.nodeValue(e),this):f.nodeValue()},r.linkTitle=function(e){return arguments.length?(y=e,this):y},r.linkLabel=function(e){return arguments.length?(v=e,this):v},r.linkColor=function(e){return arguments.length?(g=e,this):g},r.linkMinWidth=function(e){return arguments.length?(p.minWidth(e),this):p.minWidth()},r.selectNode=function(e){return this},r.selectLink=function(e){return c=e,this},r.on=function(){var e=m.on.apply(m,arguments);return e===m?r:e},r},e.sankeyLink=Fe,e.sankeyLinkTitle=T,e.sankeyNode=Ge,e.sankeyPositionJustified=fe,e.sankeyPositionRelaxation=function(){var o=25,i=8;function t(e,s){e.forEach(e=>{let t=0;e.forEach(e=>{e.forEach(e=>{e.y=t++})})}),n();for(var t=1,r=o;0<r;--r)(r=>{function n(e){return A(e.target)*e.value}e.slice().reverse().forEach(e=>{e.forEach(e=>{e.forEach(e=>{var t=e.outgoing||e.edges;t.length&&(t=O.sum(t,n)/O.sum(t,De),e.y+=(t-A(e))*r)})})})})(t*=.99),n(),(r=>{function n(e){return A(e.source)*e.value}e.forEach(e=>{e.forEach(e=>{e.forEach(e=>{var t=e.incoming||e.edges;t.length&&(t=O.sum(t,n)/O.sum(t,De),e.y+=(t-A(e))*r)})})})})(t),n();function n(){e.forEach(e=>{e.forEach(e=>{var t,r,n,o=0,a=e.length;for(e.sort(Te),n=0;n<a;++n)0<(r=o-(t=e[n]).y)&&(t.y+=r),o=t.y+t.dy+i;if(0<(r=o-i-s))for(o=t.y-=r,n=a-2;0<=n;--n)0<(r=(t=e[n]).y+t.dy+i-o)&&(t.y-=r),o=t.y})})}}return t.iterations=function(e){return arguments.length?(o=+e,t):o},t.padding=function(e){return arguments.length?(i=+e,t):i},t}});