UNPKG

d3-sankey-diagram

Version:
1 lines 39.8 kB
((e,t)=>{"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-array"),require("d3-collection"),require("d3-selection"),require("d3-transition"),require("d3-dispatch"),require("d3-format"),require("d3-interpolate")):"function"==typeof define&&define.amd?define(["exports","d3-array","d3-collection","d3-selection","d3-transition","d3-dispatch","d3-format","d3-interpolate"],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,L,N,P,V,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=l[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,c.decrease(t,o))}var h,u,l={},c=new z;for(o.nodes().forEach(function(e){var t=e===a?0:Number.POSITIVE_INFINITY;l[e]={distance:t},c.add(e,t)});0<c.size()&&(h=c.removeMin(),(u=l[h]).distance!==Number.POSITIVE_INFINITY);)i(h).forEach(d);return l}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 l(r){var n={},o={},a=[];if(r.sinks().forEach(function e(t){if(o.hasOwnProperty(t))throw new c;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 c;return a}class c extends Error{constructor(){super(...arguments)}}var f=l,p=(l.CycleException=c,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,s=[]){let i=new v.Graph({directed:!0});if(0!==a.nodes().length){s=((e,t)=>{for(let e=0;e<t.length;++e)if("min"===t[e].type)return t;var r=e.sources();return[{type:"min",nodes:[r=(r.length?r:e.nodes())[0]]}].concat(t)})(a,s);let o=L.map(),t,r,n,e;for(n=0;n<s.length;++n)if((t=s[n]).nodes&&0!==t.nodes.length){for(r=""+n,e=0;e<t.nodes.length;++e)o.set(t.nodes[e],r);i.setNode(r,{type:t.type,nodes:t.nodes})}a.nodes().forEach(e=>{o.has(e)||(r=""+n++,t={type:"same",nodes:[e]},o.set(e,r),i.setNode(r,t))}),a.edges().forEach(e=>{var t=o.get(e.v),r=o.get(e.w),n=i.edge(t,r)||{delta:0};t===r?(n.delta=0,i.setEdge(t,r,n)):a.node(e.v).backwards?(n.delta=Math.max(n.delta,a.node(e.w).backwards?1:0),i.setEdge(r,t,n)):(n.delta=Math.max(n.delta,a.node(e.w).backwards?0:1),i.setEdge(t,r,n))})}return i}function oe(r,e){let n=((t,e)=>{let r=L.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=L.set(),o=L.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(d,h,u){{var l=d;d=h,h=u;let e,t,r=(t=d.length>h.length?(e=d,h):(e=h,d)).length,n=[],o=(e.forEach(r=>{t.forEach((e,t)=>{(l.hasEdge(r,e)||l.hasEdge(e,r))&&n.push(t)})}),1);for(;o<r;)o*=2;let a=2*o-1,s=(--o,new Array(a));for(let e=0;e<a;e++)s[e]=0;let i=0;return n.forEach(e=>{let t=e+o;for(s[t]++;0<t;)t%2&&(i+=s[t+1]),t=Math.floor((t-1)/2),s[t]++}),i}}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 b(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 k(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=L.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=L.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=L.map();a[n].forEach(e=>{var t=b(k(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=L.map();a[n].forEach(e=>{var t=b(k(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 le(e){var r,n=O.max(e,e=>e.rank||0)||0,o=O.max(e,e=>e.band||0)||0,a=L.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 ce(e,t){return 1}function fe(){let f=ce;function p(u,l,c){u.forEach(e=>{let h=0;e.forEach((r,e)=>{e=u.bandValues[e]/O.sum(u.bandValues)*l;let n=c*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=L.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",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",r+=e.dy}),e.incoming.forEach(e=>{e=o.edge(e);e.y1=n+e.dy/2,e.d1=t.backwards?"l":"r",n+=e.dy})}),t.toElsewhere.forEach(e=>{e.y0=r+e.dy/2,e.d0=t.backwards?"l":"r",r+=e.dy})}),(t=e).nodes().forEach(e=>{e=t.node(e);M(e.toElsewhere,"r0"),M(e.fromElsewhere,"r1"),e.ports.forEach(e=>{M(e.outgoing.map(e=>t.edge(e)),"r0"),M(e.incoming.map(e=>t.edge(e)),"r1")})})}function M(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){let 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 Ie(e){return e.type}function Ce(e,t){return e.id.localeCompare(t.id)}function I(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 C(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 A(e){if("function"!=typeof e)throw new Error;return e}function Ae(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 je(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 j(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(){let M=Ve;function t(t){var e,r,n;if(1===t.points.length)return e=(r=t).points[0],r=Math.max(M(r),r.dy)/2,"down-right"===e.style?"M"+[e.x-20,e.y-r]+" L"+[e.x,e.y-r]+" L"+[e.x,e.y+r]+" L"+[e.x-20,e.y+r]+" Z":"right-down"===e.style?"M"+[e.x,e.y-r]+" L"+[e.x+20,e.y-r]+" L"+[e.x+20,e.y+r]+" L"+[e.x,e.y+r]+" Z":void 0;let o="";for(let e=0;e<t.points.length-1;++e)n={x0:t.points[e].x,y0:t.points[e].y,x1:t.points[e+1].x,y1:t.points[e+1].y,r0:t.points[e].ro,r1:t.points[e+1].ri,d0:t.points[e].d,d1:t.points[e+1].d,dy:t.dy},o+=(x=>{var b=(x.d0||"r")+(x.d1||"r");if(x.source&&x.source===x.target){var a=x;let r=Math.max(M(a),a.dy)/2,n=1.5*r,o=2*Math.PI,e=a.x0,t=a.y0;function s(e){var t=0<e?1:0,e=0<e?n+r:n-r;return"A"+e+" "+e+" "+Math.abs(o)+" 1 "+t+" "}return"M"+[e+.1,t-r]+" "+s(1)+[e-.1,t-r]+" L"+[e-.1,t+r]+" "+s(-1)+[e+.1,t+r]+" Z"}if("rl"===b){var v=x;let n=Math.max(M(v),v.dy)/2,e=v.x0,t=v.x1,r=v.y0,o=v.y1,a=v.x1-v.x0,s=v.y1-v.y0,i=(v.r0+v.r1)/2||5+n,d=Math.min(Math.abs(o-r)/2.1,i),h=Math.atan2(s-2*d,a),u=v.y1>v.y0?1:-1,l=n*Math.sin(h),c=n*Math.cos(h),f=v.x0+d*Math.sin(Math.abs(h)),p=v.x1+d*Math.sin(Math.abs(h)),g=v.y0+d*u*(1-Math.cos(h)),y=v.y1-d*u*(1-Math.cos(h));function m(e){var t=0<e*h?1:0;let r=0<e*h?d+n:d-n;return"A"+(r=0===h?d:r)+" "+r+" "+Math.abs(h)+" 0 "+t+" "}return"M"+[e,r-n]+" "+m(1)+[f+l,g-c]+" L"+[p+l,y-c]+" "+m(1)+[t,o+n]+" L"+[t,o-n]+" "+m(-1)+[p-l,y+c]+" L"+[f-l,g+c]+" "+m(-1)+[e,r+n]+" Z"}if("rd"===b){var d=x;let n=Math.max(M(d),d.dy)/2,e=d.x0,t=d.x1,r=d.y0,o=d.y1,a=Math.PI/2,s=Math.max(0,t-e),i=r+s;function h(e){var t=0<e*a?1:0;let r=0<e*a?s+n:s-n;return"A"+(r=0==a?s:r)+" "+r+" "+Math.abs(a)+" 0 "+t+" "}return"M"+[e,r-n]+" "+h(1)+[t+n,i]+" L"+[t+n,o]+" "+[t-n,o]+" "+[t-n,i]+" "+h(-1)+[e,r+n]+" Z"}if("dr"===b){var u=x;let n=Math.max(M(u),u.dy)/2,e=u.x0,t=u.x1,r=u.y0,o=u.y1,a=Math.PI/2,s=Math.max(0,t-e),i=o-s;function l(e){var t=0<e*a?1:0;let r=0<e*a?s+n:s-n;return"A"+(r=0==a?s:r)+" "+r+" "+Math.abs(a)+" 0 "+t+" "}return"M"+[e-n,r]+" L"+[e+n,r]+" "+[e+n,i]+" "+l(-1)+[t,o-n]+" L"+[t,o+n]+" "+l(1)+[e-n,i]+" Z"}if("lr"===b){var E=x;let n=Math.max(M(E),E.dy)/2,e=E.x0,t=E.x1,r=E.y0,o=E.y1,a=E.x1-E.x0,s=E.y1-E.y0,i=(E.r0+E.r1)/2||5+n,d=Math.min(Math.abs(s)/2.1,i),h=Math.atan2(s-2*d,a),u=E.y1>E.y0?1:-1,l=n*Math.sin(h),c=n*Math.cos(h),f=E.x0-d*Math.sin(Math.abs(h)),p=E.x1-d*Math.sin(Math.abs(h)),g=E.y0+d*u*(1-Math.cos(h)),y=E.y1-d*u*(1-Math.cos(h));function w(e){var t=0<e*h?1:0;let r=0<-e*h?d+n:d-n;return"A"+(r=0===h?d:r)+" "+r+" "+Math.abs(h)+" 0 "+t+" "}return"M"+[e,r-n]+" "+w(-1)+[f-l,g-c]+" L"+[p-l,y-c]+" "+w(-1)+[t,o+n]+" L"+[t,o-n]+" "+w(1)+[p+l,y-c]+" L"+[f+l,g-c]+" "+w(1)+[e,r+n]+" Z"}{let o=Math.max(M(x),x.dy)/2,e=x.x0,t=x.x1,a=x.y0,s=x.y1,r=(t<e&&([e,t]=[t,e],[a,s]=[s,a]),s>a?1:-1),n=(e=>{var t=e.x1-e.x0,r=e.y1-e.y0;return[e.dy/2,(t*t+r*r)/Math.abs(4*r)]})(x),i=Math.max(n[0],Math.min(n[1],(t-e)/3)),d=Math.max(n[0],Math.min(n[1],x.r0||i)),h=Math.max(n[0],Math.min(n[1],x.r1||i)),u=t-e,l=s-a-r*(d+h),c=Math.sqrt(u*u+l*l),f=-r*Math.acos(Math.min(1,(d+h)/c)),p=Math.atan2(l,u),g=Math.PI/2+r*(p+f),y=o*r*Math.sin(g),v=o*Math.cos(g),m=e+ +d*Math.sin(Math.abs(g)),E=t-+h*Math.sin(Math.abs(g)),w=a+d*r*(1-Math.cos(g)),_=s-h*r*(1-Math.cos(g));return(isNaN(g)||Math.abs(g)<.001)&&(g=d=h=0,m=e,E=t,w=a,_=s,y=0,v=o),b="M"+[e,a-o]+" "+k(1,d)+[m+y,w-v]+" L"+[E+y,_-v]+" "+k(-1,h)+[t,s-o]+" L"+[t,s+o]+" "+k(1,h)+[E-y,_+v]+" L"+[m-y,w+v]+" "+k(-1,d)+[e,a+o]+" Z",/NaN/.test(b)&&console.error("path NaN",x,b),b;function k(e,t){var r=0<e*(s-a)?1:0;let n=0<+e*(s-a)?t+o:t-o;return"A"+(n=0===g?t:n)+" "+n+" "+Math.abs(g)+" 0 "+r+" "}}})(n);return o}return t.minWidth=function(e){return arguments.length?(M=(e=>{if("function"!=typeof e)throw new Error;return e})(e),t):M},t}function Ge(){let l=e=>void 0!==e.title?e.title:e.id,c=e=>null,f=e=>!!l(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=N.select(this).select("title"),r=N.select(this).select(".node-value");let n=N.select(this).select(".node-title"),o=N.select(this).select("line"),a=N.select(this).select(".node-body"),s=N.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=l(e)+(!d&&c(e)?" ("+c(e)+")":"");t.text(e),r.text(c).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?(l=D(e),t):l},t.nodeValue=function(e){return arguments.length?(c=D(e),t):c},t}function Se(e){return"translate("+e.x0+","+e.y0+")"}function qe(e,t){let r=N.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}function Ze(e){return!e.source||e.target&&"d"===e.target.direction?-2:!e.target||e.source&&"d"===e.source.direction?-1:e.dy}function ze(e){return e.reduce((e,t)=>e+t,0)}e.sankey=function(){let t=ke,h=Me,u=Le,l=Ne,c=Pe,f=Oe,p=Ie,g=null,y=[],v=null,m=Ce,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)};let r=xe(e,u,l,c,f,p,M);if(I(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]}),C(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),I(r,M),null===g&&de(r,25);var n=le(r.nodes().map(e=>r.node(e)));P(r,n),C(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})}),Ae(r),je(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){let r=xe(e,u,l,c,f,p,M);I(r,M);var n=le(r.nodes().map(e=>r.node(e)));return P(r,n),C(r,k),pe(r,m),ye(r),we(r),Ae(r),je(r),e},r.nodes=function(e){return arguments.length?(t=A(e),r):t},r.links=function(e){return arguments.length?(h=A(e),r):h},r.nodeId=function(e){return arguments.length?(u=A(e),r):u},r.nodeBackwards=function(e){return arguments.length?(l=A(e),r):l},r.sourceId=function(e){return arguments.length?(c=A(e),r):c},r.targetId=function(e){return arguments.length?(f=A(e),r):f},r.linkType=function(e){return arguments.length?(p=A(e),r):p},r.sortPorts=function(e){return arguments.length?(m=A(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=A(e),r):N},r},e.sankeyDiagram=function(){let l={top:0,right:0,bottom:0,left:0},c=null,f=[],t=F.format(".3s"),p=Ge(),g=Fe(),y=e=>null,v=T(p.nodeTitle(),e=>e.type,t),m=Be,E=Ze,w=ze,_=V.dispatch("selectNode","selectGroup","selectLink");function r(u){(u.selection?u.selection():u).each(function(t){var r=N.select(this);let n=r.selectAll(".sankey").data([{type:"sankey"}]);var o=n.enter().append("g").classed("sankey",!0);o.append("g").classed("groups",!0),o.append("g").classed("links",!0),o.append("g").classed("nodes",!0),o.append("g").classed("slice-titles",!0),(n=n.merge(o)).attr("transform","translate("+l.left+","+l.top+")");let a=L.map(t.nodes,e=>e.id);o=(f||[]).map(e=>{{var r=a;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 s=Array.from(t.links);t.nodes.forEach(e=>{Array.prototype.push.apply(s,e.fromElsewhere||[]),Array.prototype.push.apply(s,e.toElsewhere||[])});{var i=n,d=u;t=t.nodes;let e=i.select(".nodes").selectAll(".node").data(t,e=>e.id);e.exit().remove(),e=e.merge(e.enter().append("g").attr("class","node").call(p).on("click",M)),(d instanceof P.transition?e.transition(d):e).call(p)}{i=n;var h=u;t=s;let e=i.select(".links").selectAll(".link").data(t,e=>e.source.id+"-"+e.target.id+"-"+e.type);e.exit().remove(),(i=e.enter().append("g").attr("class","link").on("click",k)).append("path").attr("d",g).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);{i=t;let n=new Map;i.forEach(e=>{var t=e.source.id+"-"+e.target.id,r=(n.has(t)||n.set(t,[]),E(e));e._importance=r,n.get(t).push({edge:e,importance:r})}),n.forEach(e=>{var t=e.map(e=>e.importance);let r=w(t);e.forEach(e=>{e.edge._groupImportance=r})})}e.classed("selected",e=>e.id===c),e.sort(b),h instanceof P.transition?(e=e.transition(h)).select("path").style("fill",y).each(function(e){N.select(this).transition(h).attrTween("d",x)}):e.select("path").style("fill",y).attr("d",g),e.select("title").text(v),e.select(".label").text(m).attr("x",e=>e.points[0].x+4).attr("y",e=>e.points[0].y)}{t=o;let e=(d=r).select(".groups").selectAll(".group").data(t);e.exit().remove(),(d=e.enter().append("g").attr("class","group")).append("rect"),d.append("text").attr("x",-10).attr("y",-25),(e=e.merge(d)).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)}r.on("click",function(){_.call("selectNode",this,null),_.call("selectLink",this,null)})})}function x(e){e.points.forEach(function(e){1e3<e.ri&&(e.ri=1e3),1e3<e.ro&&(e.ro=1e3)});let t=G.interpolate({points:(r=this._current).points,dy:r.dy},e);var r;let n=this;return function(e){return n._current=t(e),g(n._current)}}function b(e,t){return e.id===c?1:t.id===c?-1:e._groupImportance-t._groupImportance}function k(e){N.event.stopPropagation();var t=N.select(this).node();_.call("selectLink",t,e)}function M(e){N.event.stopPropagation();var t=N.select(this).node();_.call("selectNode",t,e)}return r.margins=function(e){return arguments.length?(l={top:(void 0===e.top?l:e).top,left:(void 0===e.left?l:e).left,bottom:(void 0===e.bottom?l:e).bottom,right:(void 0===e.right?l:e).right},this):l},r.groups=function(e){return arguments.length?(f=e,this):f},r.nodeTitle=function(e){return arguments.length?(p.nodeTitle(e),v=T(e,e=>e.type,t),this):p.nodeTitle()},r.nodeValue=function(e){return arguments.length?(p.nodeValue(e),this):p.nodeValue()},r.linkTitle=function(e){return arguments.length?(v=e,this):v},r.linkLabel=function(e){return arguments.length?(m=e,this):m},r.linkColor=function(e){return arguments.length?(y=e,this):y},r.linkMinWidth=function(e){return arguments.length?(g.minWidth(e),this):g.minWidth()},r.linkImportance=function(e){return arguments.length?(E=e,this):E},r.linkImportanceAgg=function(e){return arguments.length?(w=e,this):w},r.selectNode=function(e){return this},r.selectLink=function(e){return c=e,this},r.on=function(){var e=_.on.apply(_,arguments);return e===_?r:e},r},e.sankeyLink=Fe,e.sankeyLinkTitle=T,e.sankeyNode=Ge,e.sankeyPositionJustified=fe,e.sankeyPositionRelaxation=function(){let n=25,i=8;function t(o,s){o.forEach(e=>{let t=0;e.forEach(e=>{e.forEach(e=>{e.y=t++})})}),r();for(let e=1,t=n;0<t;--t)(r=>{function n(e){return j(e.target)*e.value}o.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-j(e))*r)})})})})(e*=.99),r(),(r=>{function n(e){return j(e.source)*e.value}o.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-j(e))*r)})})})})(e),r();function r(){o.forEach(e=>{e.forEach(e=>{let t,r,n=0;var o=e.length;let a;for(e.sort(Te),a=0;a<o;++a)t=e[a],0<(r=n-t.y)&&(t.y+=r),n=t.y+t.dy+i;if(0<(r=n-i-s))for(n=t.y-=r,a=o-2;0<=a;--a)t=e[a],0<(r=t.y+t.dy+i-n)&&(t.y-=r),n=t.y})})}}return t.iterations=function(e){return arguments.length?(n=+e,t):n},t.padding=function(e){return arguments.length?(i=+e,t):i},t}});