chartjs-chart-sankey 0.19.0 → 0.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* chartjs-chart-sankey v0.19.
|
|
2
|
+
* chartjs-chart-sankey v0.19.1
|
|
3
3
|
* https://chartjs-chart-sankey.pages.dev/
|
|
4
4
|
* (c) 2026 Jukka Kurkela
|
|
5
5
|
* Released under the MIT license
|
|
@@ -777,6 +777,10 @@ class SankeyController extends chart_js.DatasetController {
|
|
|
777
777
|
}
|
|
778
778
|
return parsed.slice(start, start + count);
|
|
779
779
|
}
|
|
780
|
+
buildOrUpdateElements(resetNewElements) {
|
|
781
|
+
this.configure();
|
|
782
|
+
super.buildOrUpdateElements(resetNewElements);
|
|
783
|
+
}
|
|
780
784
|
getMinMax(scale) {
|
|
781
785
|
const vertical = this.options.orientation === 'vertical';
|
|
782
786
|
const columnScale = vertical ? this._cachedMeta.yScale : this._cachedMeta.xScale;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* chartjs-chart-sankey v0.19.
|
|
2
|
+
* chartjs-chart-sankey v0.19.1
|
|
3
3
|
* https://chartjs-chart-sankey.pages.dev/
|
|
4
4
|
* (c) 2026 Jukka Kurkela
|
|
5
5
|
* Released under the MIT license
|
|
@@ -774,6 +774,10 @@ class SankeyController extends DatasetController {
|
|
|
774
774
|
}
|
|
775
775
|
return parsed.slice(start, start + count);
|
|
776
776
|
}
|
|
777
|
+
buildOrUpdateElements(resetNewElements) {
|
|
778
|
+
this.configure();
|
|
779
|
+
super.buildOrUpdateElements(resetNewElements);
|
|
780
|
+
}
|
|
777
781
|
getMinMax(scale) {
|
|
778
782
|
const vertical = this.options.orientation === 'vertical';
|
|
779
783
|
const columnScale = vertical ? this._cachedMeta.yScale : this._cachedMeta.xScale;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* chartjs-chart-sankey v0.19.
|
|
2
|
+
* chartjs-chart-sankey v0.19.1
|
|
3
3
|
* https://chartjs-chart-sankey.pages.dev/
|
|
4
4
|
* (c) 2026 Jukka Kurkela
|
|
5
5
|
* Released under the MIT license
|
|
6
6
|
*/
|
|
7
|
-
!function(t,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("chart.js"),require("chart.js/helpers")):"function"==typeof define&&define.amd?define(["exports","chart.js","chart.js/helpers"],o):o((t="undefined"!=typeof globalThis?globalThis:t||self)["chartjs-chart-sankey"]={},t.Chart,t.Chart.helpers)}(this,function(t,o,e){"use strict";const n=t=>void 0!==t;function r(t){return t&&-1!==["min","max"].indexOf(t)?t:"max"}function i(t,o){return"function"==typeof t?t(o):t&&"object"==typeof t&&!function(t){const o=Object.prototype.toString.call(t);return"[object CanvasPattern]"===o||"[object CanvasGradient]"===o}(t)?t[o.key]:t}function a(t,o,e,n,r,i,a){t.save(),t.fillStyle=o,a>0?(!function(t,o,e,n,r,i){const a=Math.max(0,Math.min(i,n/2,r/2));t.beginPath(),t.moveTo(o+a,e),t.lineTo(o+n-a,e),t.quadraticCurveTo(o+n,e,o+n,e+a),t.lineTo(o+n,e+r-a),t.quadraticCurveTo(o+n,e+r,o+n-a,e+r),t.lineTo(o+a,e+r),t.quadraticCurveTo(o,e+r,o,e+r-a),t.lineTo(o,e+a),t.quadraticCurveTo(o,e,o+a,e),t.closePath()}(t,e,n,r,i,a),t.fill()):t.fillRect(e,n,r,i),t.restore()}function s(t,o,e){const n=function(t){if(!t)return[];const o=[],e=Array.isArray(t)?t:[t];for(;e.length;){const t=e.pop();"string"==typeof t?o.unshift(...t.split("\n")):Array.isArray(t)?e.push(...t):t&&o.unshift(`${t}`)}return o}(o);if(!n.length)return;const{backgroundColor:r,borderRadius:i,color:s,font:l,lineOffset:h,padding:c}=e,f=function(t,o){return"auto"===t?o:t}(e.position,e.autoPosition),d=Number(l.lineHeight);t.font=l.string;const u=Math.max(...n.map(o=>t.measureText(o).width)),p=d*n.length,y=function(t,o,e){const{borderWidth:n,height:r,padding:i,width:a,x:s,y:l}=o,h={align:"center",x:s+a/2,y:l+r/2};return"left"===t?(h.align="right",h.x=s-n-i):"right"===t?(h.align="left",h.x=s+a+n+i):"top"===t?h.y=l-i-e/2:"bottom"===t&&(h.y=l+r+i+e/2),h}(f,e,p);t.textAlign=y.align,t.textBaseline="middle";const x=u+2*c,g=p+2*c,m=function(t,o,e,n,r){return"left"===t?o-r:"right"===t?o-e-r:o-n/2}(y.align,y.x,u,x,c),w=1===n.length?y.y:y.y-p/2+h,b=w+(n.length-1)*d/2;void 0!==r&&a(t,r,m,b-g/2,x,g,i),t.fillStyle=s;for(let o=0;o<n.length;o++)t.fillText(n[o],y.x,w+o*d)}const l=(t,o)=>o.flow===t.flow?t.index-o.index:o.flow-t.flow;function h(t,{size:o,priority:e,column:n}){const i=new Map;for(let o=0;o<t.length;o++){const{from:e,to:n,flow:r}=t[o],a=i.get(e)??{from:[],in:0,key:e,out:0,size:0,to:[]},s=(e===n?a:i.get(n))??{from:[],in:0,key:n,out:0,size:0,to:[]};a.out+=r,a.to.push({addY:0,flow:r,index:o,key:n,node:s}),1===a.to.length&&i.set(e,a),s.in+=r,s.from.push({addY:0,flow:r,index:o,key:e,node:a}),1===s.from.length&&i.set(n,s)}return((t,o)=>{const e=r(o);for(const o of t.values())o.from.sort(l),o.to.sort(l),o.size=Math[e](o.in||o.out,o.out||o.in)})(i,o),((t,o)=>{if(o)for(const e of t.values())e.key in o&&(e.priority=o[e.key])})(i,e),((t,o)=>{if(o)for(const e of t.values())e.key in o&&(e.column=!0,e.x=o[e.key])})(i,n),i}const c=1e-6;function f(t){return t.x??0}function d(t){return t.y??0}const u=(t,o=new Set)=>{const e=[];for(const n of t)o.has(n.key)||(o.add(n.key),e.push(n.key,...u(n.to.map(t=>t.node),o)));return e},p=(t,o)=>{const e=o.filter(t=>0===t.from.length),n=e.map(t=>t.key),r=u(e),i=new Set(r);for(const o of t)i.has(o.from)||i.has(o.to)||(n.push(o.from),i.add(o.from)),i.add(o.to);return n},y=(t,o)=>{const e=new Set(t.filter(t=>o.has(t.from)).map(t=>t.to)),n=[...o],r=n.filter(t=>!e.has(t));return r.length?r:n.slice(0,1)};function x(t,o,e=new Set){let n=0;for(const r of t)e.has(r.node)||(e.add(r.node),n+=r.node[o].length+x(r.node[o],o,e));return n}const g=t=>(o,e)=>x(o.node[t],t)-x(e.node[t],t)||o.node[t].length-e.node[t].length;function m(t,o){if(!t.from.length)return o;t.from.sort(g("from"));for(const e of t.from){const t=e.node;n(t.y)||(t.y=o,m(t,o?o+c:0)),o=Math.max(t.y+t.out,o)}return d(t)+t.size}const w=(t,o)=>Boolean(o&&f(o)<f(t));function b(t,o){if(!t.to.length)return o;t.to.sort(g("to"));for(let e=0;e<t.to.length;e++){const r=t.to[e],i=r.node;n(i.y)||(i.y=o,b(i,o?o+c:0)),w(i,t.to[e+1]?.node)?o+=r.flow:o=Math.max(i.y+Math.max(i.in,i.out),o)}return d(t)+t.size}function M(t,o){return n(t.y)?t.y:(t.y=o,o)}function v(t,o){if(!t.length)return 0;const e=((t,o)=>{const e=[...t].sort((t,o)=>t.size-o.size),n=e[e.length-1].size,r=t.filter(t=>t.size===n),i=r[0];if(1===r.length)return i;if(r.sort((t,o)=>f(t)-f(o)),0===f(i))return i;const a=r[r.length-1];return f(a)===o?a:r[Math.floor(r.length/2)]})(t,o);return e.y=0,m(e,0),b(e,0),function(t,o){const e=t.filter(t=>0===t.x),r=t.filter(t=>t.x===o),i=e.filter(t=>!n(t.y)),a=r.filter(t=>!n(t.y)),s=t.filter(t=>f(t)>0&&f(t)<o&&!n(t.y));let l=e.reduce((t,o)=>Math.max(t,d(o)+o.out||0),0)+c,h=r.reduce((t,o)=>Math.max(t,d(o)+o.in||0),0)+c,u=0;l>=h?(i.forEach(t=>{l=M(t,l),l=Math.max(l+t.out,b(t,l))}),a.forEach(t=>{h=M(t,h),h=Math.max(h+t.in,m(t,h))})):(i.forEach(t=>{l=M(t,l)}),a.forEach(t=>{h=M(t,h),h=Math.max(h+t.in,m(t,h))})),s.forEach(o=>{let e=t.filter(t=>f(t)===f(o)&&n(t.y)).reduce((t,o)=>Math.max(t,d(o)+Math.max(o.in,o.out)),0);e=M(o,e),e=Math.max(e+o.in,m(o,e)),e=Math.max(e+o.out,b(o,e)),u=Math.max(u,e)}),Math.max(l,h,u)}(t,o),((t,o)=>{let e=0;for(let n=0;n<=o;n++){const o=t.filter(t=>f(t)===n).sort((t,o)=>d(t)-d(o));let r=0;for(const t of o)d(t)<r&&(t.y=r),r=d(t)+t.size;e=Math.max(e,r)}return e})(t,o)}const k=(t,o)=>f(t)!==f(o)?f(t)-f(o):d(t)===d(o)?t.size-o.size:d(t)-d(o);function C(t,o,e){const n=(o.get(t.key)??0)*e;return Math.max(0,n-t.size)/2}function z(t,o,e){const n=(o.get(t.key)??0)*e;return Math.max(t.size,n)}function P(t,o,e,n){let r=n;for(let n=0;n<o;n++){const o=t[n].yHistory;for(let t=0;t<o.length&&!(o[t]>e);t++)r=Math.max(t+1,r)}return r}function S(t,o,e,n,r){const i=t.realCount,a=o.before*n,s=o.after*n,l=i>0?Math.max(t.lastAfter,a)+t.lastExtraHalf+r:0,h=t.realCumOffset+l,c=e-i;return t.realCount=i+1,t.realCumOffset=h,t.lastAfter=s,t.lastExtraHalf=r,h+c*a}function _(t,o,e=1,n="auto",r=new Map){if(t.sort(k),"even"===n)return function(t,o,e,n){let r,i,a,s=0,l=0;for(const h of t){const t=f(h),c=o.get(h.key)??{after:0,before:0},u=C(h,n,e);t!==r?r=t:i&&a&&(h.y=d(i)+i.size+Math.max(a.after,c.before)*e+l+u),i=h,a=c,l=u;const p=z(h,n,e);s=Math.max(s,d(h)+Math.max(h.in,h.out),d(h)+(h.size+p)/2)}return s}(t,o,e,r);let i=0;const a=new Map,s=[],l=t=>{if(!a.has(t)){const o=s.length;return a.set(t,o),s.push({lastAfter:0,lastExtraHalf:0,realCount:0,realCumOffset:0,yHistory:[]}),o}return a.get(t)??0};for(const n of t){const t=l(f(n)),a=s[t],h=o.get(n.key)??{after:0,before:0},c=d(n),u=C(n,r,e);if(c){a.yHistory.push(c);let o=a.yHistory.length;if(n.in)for(o=P(s,t,c,o);a.yHistory.length<o;)a.yHistory.push(c);n.y=c+S(a,h,o,e,u)}else a.realCount+=1,a.lastAfter=h.after*e,a.lastExtraHalf=u;const p=z(n,r,e);i=Math.max(i,d(n)+Math.max(n.in,n.out),d(n)+(n.size+p)/2)}return i}function T(t,o,{priority:e,height:r,nodePadding:i,nodePaddingMode:a,nodeMinSize:s,modeX:l}){const h=[...t.values()],c=function(t,o,e){const r=o.filter(t=>t.from!==t.to),i=[...t.keys()],a=[...t.values()],s=new Set(i);let l=0;for(;s.size;){const e=0===l?p(o,a):y(r,s);if(!e.length)throw new Error("Fatal error: Unable to place nodes to columns. Please report this issue.");for(const o of e){const e=t.get(o);e&&!n(e.x)&&(e.x=l),s.delete(o)}s.size&&l++}const h=a.reduce((t,o)=>Math.max(t,f(o)),0);if("edge"===e){const e=new Set(o.map(t=>t.from));i.filter(t=>!e.has(t)).forEach(o=>{const e=t.get(o);e&&!e.column&&(e.x=h)})}return h}(t,o,l??"edge"),u=e?function(t,o){let e=0,n=0;for(let r=0;r<=o;r++){let o=n;const i=t.filter(t=>f(t)===r).sort((t,o)=>(t.priority??0)-(o.priority??0));if(i.length){const o=t.reduce((t,o)=>f(o)>r?Math.min(t,f(o)):t,1/0);n=i[0].to.filter(t=>f(t.node)>o).reduce((t,o)=>t+o.flow,0)||0}for(const t of i)t.y=o,o+=Math.max(t.out,t.in);e=Math.max(o,e)}return e}(h,c):v(h,c),x=_(h,i,u/r,a??"auto",s??new Map);return function(t){t.forEach(t=>{const o=t.size,e=o<t.in,n=o<t.out;let r=0,i=t.from.length;t.from.sort((t,o)=>d(t.node)+t.node.out/2-(d(o.node)+o.node.out/2)).forEach((t,n)=>{e?t.addY=n*(o-t.flow)/(i-1):(t.addY=r,r+=t.flow)}),r=0,i=t.to.length,t.to.sort((t,o)=>d(t.node)+t.node.in/2-(d(o.node)+o.node.in/2)).forEach((t,e)=>{n?t.addY=e*(o-t.flow)/(i-1):(t.addY=r,r+=t.flow)})})}(h),{maxX:c,maxY:x}}function F(t){return t.x??0}function E(t){return t.y??0}function O(t,o){return Math[o](t.in||t.out,t.out||t.in)}function R(t,o,e,n){return"vertical"===n?o<(e.top+e.bottom)/2?"bottom":"top":t<(e.left+e.right)/2?"right":"left"}function H(t,o,e){for(const n of t)if(n.key===o&&n.index===e)return n.addY;return 0}function W(t,o,e,n,r,i,a,s,l){return"vertical"===l?{_custom:{flow:r,from:t,height:a.parse(r,i),to:o,x:a.parse(n,i),y:s.parse(F(o),i)},x:a.parse(e,i),y:s.parse(F(t),i)}:{_custom:{flow:r,from:t,height:s.parse(r,i),to:o,x:a.parse(F(o),i),y:s.parse(n,i)},x:a.parse(F(t),i),y:s.parse(e,i)}}function X(t,o,e,n,r,i,a,s){const l=t._custom,h=o.getPixelForValue(t.x),c=e.getPixelForValue(t.y);return"vertical"===s?{flow:l.flow,from:l.from,height:0,to:l.to,width:Math.abs(o.getPixelForValue(t.x+l.height)-h),x:h,x2:o.getPixelForValue(l.x),y:Y(e,t.y,n,i)+r+a,y2:Y(e,l.y,n,i)-a}:{flow:l.flow,from:l.from,height:Math.abs(e.getPixelForValue(t.y+l.height)-c),to:l.to,width:0,x:Y(o,t.x,n,i)+r+a,x2:Y(o,l.x,n,i)-a,y:c,y2:e.getPixelForValue(l.y)}}function Y(t,o,e,n){const r=t.getPixelForValue(o);return e?r-o/e*n:r}function j(t,o,e){const n="vertical"===o?e.height-e.chartArea.bottom:e.width-e.chartArea.right;return Math.max(0,t+3-n)}function A(t,o,e,n,r,i,a,s,l){if("vertical"===s){const s=e.getPixelForValue(E(t)),h=e.getPixelForValue(E(t)+o),c=Math.min(s,h),f=Math.abs(h-s),d=Math.max(f,l);return{height:i,width:d,x:c-(d-f)/2,y:Y(n,F(t),r,a)}}const h=n.getPixelForValue(E(t)),c=n.getPixelForValue(E(t)+o),f=Math.min(h,c),d=Math.abs(c-h),u=Math.max(d,l);return{height:u,width:i,x:Y(e,F(t),r,a),y:f-(u-d)/2}}function V(t,o){const e=i(t??10,o)??10;return"number"==typeof e?{after:e,before:e}:{after:e.after??10,before:e.before??10}}function L(t,o){return i(t??0,o)??0}function q(t,o){const{backgroundColor:e,borderRadius:n=0,color:r,display:a,font:s,padding:l=4,position:h}=t.nodeLabels??{};return{backgroundColor:i(e,o),borderRadius:n,color:i(r,o)??t.color??"black",display:i(a,o)??!0,font:s,padding:l,position:i(h,o)??"auto"}}const D=new Set(["nodeMinSize","nodePadding"]);class $ extends o.DatasetController{parseObjectData(t,o,e,n){const r=((t,o)=>{const{from:e="from",to:n="to",flow:r="flow"}=o;return t.map(({[e]:t,[n]:o,[r]:i})=>({flow:i,from:t,to:o}))})(o,this.options.parsing),{xScale:i,yScale:a}=t,s=[],l=h(r,this.options),c=this.options.orientation??"horizontal";this._nodes=l;const f=new Map,d=new Map;for(const t of l.values())f.set(t.key,V(this.options.nodePadding,t)),d.set(t.key,L(this.options.nodeMinSize,t));const{maxX:u,maxY:p}=T(l,r,{height:"vertical"===c?this.chart.width:this.chart.height,modeX:this.options.modeX,nodeMinSize:d,nodePadding:f,nodePaddingMode:this.options.nodePaddingMode,priority:!!this.options.priority});if(this._maxX=u,this._maxY=p,!i||!a)return[];for(let t=0,o=r.length;t<o;++t){const o=r[t],e=l.get(o.from),n=l.get(o.to);if(!e||!n)continue;const h=E(e)+H(e.to,o.to,t),f=E(n)+H(n.from,o.from,t);s.push(W(e,n,h,f,o.flow,t,i,a,c))}return s.slice(e,e+n)}getMinMax(t){return{max:t===("vertical"===this.options.orientation?this._cachedMeta.yScale:this._cachedMeta.xScale)?this._maxX:this._maxY,min:0}}update(t){const{data:o}=this._cachedMeta;this.updateElements(o,0,o.length,t)}updateElements(t,o,e,n){const{xScale:r,yScale:i}=this._cachedMeta;if(!r||!i)return;const a=this.resolveDataElementOptions(o,n),s=this.getSharedOptions(a),{borderWidth:l,nodeWidth:h=10,orientation:c="horizontal"}=this.options,f=j(h,c,this.chart),d=l?l/2+.5:0;for(let a=o;a<o+e;a++){const o=this.getParsed(a);this.updateElement(t[a],a,{options:this.resolveDataElementOptions(a,n),progress:"reset"===n?0:1,...X(o,r,i,this._maxX,h,f,d,c)},n)}s&&this.updateSharedOptions(s,n,a)}_drawLabels(){const t=this.chart.ctx,n=this.options,i=this._nodes||new Map,a=r(n.size),l=n.labels,{borderWidth:h=1,nodeWidth:c=10,orientation:f="horizontal"}=n,d=j(c,f,this.chart),u=n.font??this.chart.options.font??o.Chart.defaults.font,{xScale:p,yScale:y}=this._cachedMeta;if(!p||!y)return;t.save();const x=this.chart.chartArea;for(const o of i.values()){const r=O(o,a),i=L(n.nodeMinSize,o),{height:g,width:m,x:w,y:b}=A(o,r,p,y,this._maxX,c,d,f,i),M=l?.[o.key]??o.key,v=q(n,o);if(v.display){const o=e.toFont(v.font??u);s(t,M,{autoPosition:R(w,b,x,f),backgroundColor:v.backgroundColor,borderRadius:v.borderRadius,borderWidth:h,color:v.color,font:o,height:g,lineOffset:e.valueOrDefault(n.padding,o.lineHeight/2),padding:v.padding,position:v.position,width:m,x:w,y:b})}}t.restore()}_drawNodes(){const t=this.chart.ctx,o=this._nodes||new Map,{borderColor:e,borderWidth:n=0,nodeWidth:i=10,orientation:a="horizontal",size:s}=this.options,l=j(i,a,this.chart),h=r(s),{xScale:c,yScale:f}=this._cachedMeta;t.save(),e&&n&&(t.strokeStyle=e,t.lineWidth=n);for(const e of o.values()){if(t.fillStyle=e.color??"black",!c||!f)return;const o=Math[h](e.in||e.out,e.out||e.in),r=L(this.options.nodeMinSize,e),{height:s,width:d,x:u,y:p}=A(e,o,c,f,this._maxX,i,l,a,r);n&&t.strokeRect(u,p,d,s),t.fillRect(u,p,d,s)}t.restore()}draw(){const t=this.chart.ctx,o=this.getMeta().data??[],e=[];for(let t=0,n=o.length;t<n;++t){const n=o[t];n.from&&n.to&&(n.from.color=n.options.colorFrom,n.to.color=n.options.colorTo,n.active&&e.push(n))}for(const t of e)t.from&&t.to&&(t.from.color=t.options.colorFrom,t.to.color=t.options.colorTo);this._drawNodes();for(let e=0,n=o.length;e<n;++e)o[e].draw(t);this._drawLabels()}constructor(...t){super(...t),this._nodes=new Map,this._maxX=0,this._maxY=0}}$.id="sankey",$.descriptors={_indexable:!1,_scriptable:t=>!D.has(t),nodeLabels:{_indexable:!1,_scriptable:!1}},$.defaults={animations:{colors:{properties:["colorFrom","colorTo"],type:"color"},numbers:{properties:["x","y","x2","y2","height","width"],type:"number"},progress:{delay:t=>"data"===t.type?500*t.parsed["vertical"===t.dataset.orientation?"y":"x"]+20*t.dataIndex:void 0,duration:t=>"data"===t.type?200*(t.parsed._custom["vertical"===t.dataset.orientation?"y":"x"]-t.parsed["vertical"===t.dataset.orientation?"y":"x"]):void 0,easing:"linear"}},borderColor:"black",borderWidth:1,color:"black",dataElementType:"flow",modeX:"edge",nodeMinSize:0,nodePadding:10,nodePaddingMode:"auto",nodeWidth:10,orientation:"horizontal",transitions:{hide:{animations:{colors:{properties:["colorFrom","colorTo"],to:"transparent",type:"color"}}},resize:{animations:{progress:{delay:0,duration:0}}},show:{animations:{colors:{from:"transparent",properties:["colorFrom","colorTo"],type:"color"}}}}},$.overrides={datasets:{clip:!1,parsing:{flow:"flow",from:"from",to:"to"}},interaction:{intersect:!0,mode:"nearest"},layout:{padding:{bottom:3,left:3,right:13,top:3}},plugins:{legend:{display:!1},tooltip:{callbacks:{label(t){const o=t.parsed._custom;return`${o.from.key} -> ${o.to.key}: ${o.flow}`},title:()=>""}}},scales:{x:{bounds:"data",display:!1,min:0,offset:!1,type:"linear"},y:{bounds:"data",display:!1,min:0,offset:!1,reverse:!0,type:"linear"}}};const G=(t,o,e,n,r)=>"vertical"===r?o<n?{cp1:{x:t,y:o+(n-o)/3*2},cp2:{x:e,y:o+(n-o)/3}}:{cp1:{x:0,y:o-(o-n)/3},cp2:{x:0,y:n+(o-n)/3}}:t<e?{cp1:{x:t+(e-t)/3*2,y:o},cp2:{x:t+(e-t)/3,y:n}}:{cp1:{x:t-(t-e)/3,y:0},cp2:{x:e+(t-e)/3,y:0}},N=(t,o,e)=>({x:t.x+e*(o.x-t.x),y:t.y+e*(o.y-t.y)}),B=(t,o)=>e.color(t).alpha(o).rgbString(),I=(t,o)=>"string"==typeof t?B(t,o):t,U=t=>"string"==typeof t?e.getHoverColor(t):t;class J extends o.Element{draw(t){const{x:n,x2:r,y:i,y2:a,height:l,progress:h,width:c}=this,f=this.options.orientation,d=G(n,i,r,a,f),u={height:l,width:c,x:n,x2:r,y:i,y2:a};if(0===h)return;t.save(),h<1&&function(t,{height:o,width:e,x:n,x2:r,y:i,y2:a},s,l){t.beginPath(),"vertical"===l?t.rect(Math.min(n,r),i,Math.abs(r-n)+e+1,(a-i)*s+1):t.rect(n,Math.min(i,a),(r-n)*s+1,Math.abs(a-i)+o+1),t.clip()}(t,u,h,f),function(t,{x:o,x2:e,y:n,y2:r,options:i}){let a="black";null!==i.flowColor?a=i.flowColor:"from"===i.colorMode?a=I(i.colorFrom,i.alpha):"to"===i.colorMode?a=I(i.colorTo,i.alpha):"string"==typeof i.colorFrom&&"string"==typeof i.colorTo&&(a="vertical"===i.orientation?t.createLinearGradient(0,n,0,r):t.createLinearGradient(o,0,e,0),a.addColorStop(0,B(i.colorFrom,i.alpha)),a.addColorStop(1,B(i.colorTo,i.alpha))),t.fillStyle=a,t.strokeStyle=a,t.lineWidth=.5}(t,this),function(t,{height:o,width:e,x:n,x2:r,y:i,y2:a},{cp1:s,cp2:l},h){t.beginPath(),t.moveTo(n,i),t.bezierCurveTo(s.x,s.y,l.x,l.y,r,a),"vertical"===h?(t.lineTo(r+e,a),t.bezierCurveTo(l.x+e,l.y,s.x+e,s.y,n+e,i)):(t.lineTo(r,a+o),t.bezierCurveTo(l.x,l.y+o,s.x,s.y+o,n,i+o)),t.lineTo(n,i),t.stroke(),t.closePath(),t.fill()}(t,u,d,f);const p=this.options.flowLabels;if(p.display){const n=e.toFont(p.font??o.Chart.defaults.font),r=function({height:t,width:o,x:e,x2:n,y:r,y2:i},a){return"vertical"===a?{height:i-r,width:Math.abs(n-e)+o,x:Math.min(e,n),y:r}:{height:Math.abs(i-r)+t,width:n-e,x:e,y:Math.min(r,i)}}(u,f);s(t,`${this.flow}`,{autoPosition:"center",backgroundColor:p.backgroundColor,borderRadius:p.borderRadius,borderWidth:0,color:p.color,font:n,height:r.height,lineOffset:n.lineHeight/2,padding:p.padding,position:p.position,width:r.width,x:r.x,y:r.y})}t.restore()}inRange(t,o,e){const{x:n,y:r,x2:i,y2:a,height:s,width:l}=this.getProps(["x","y","x2","y2","height","width"],e),h="vertical"===this.options.orientation;if(h?o<r||o>a:t<n||t>i)return!1;const{cp1:c,cp2:f}=G(n,r,i,a,this.options.orientation),d=h?(o-r)/(a-r):(t-n)/(i-n),u={x:i,y:a},p=N({x:n,y:r},c,d),y=N(c,f,d),x=N(f,u,d),g=N(p,y,d),m=N(y,x,d),w=N(g,m,d);return h?t>=w.x&&t<=w.x+l:o>=w.y&&o<=w.y+s}inXRange(t,o){const{x:e,x2:n,width:r}=this.getProps(["x","x2","width"],o),i=Math.min(e,n),a=Math.max(e,n)+("vertical"===this.options.orientation?r:0);return t>=i&&t<=a}inYRange(t,o){const{y:e,y2:n,height:r}=this.getProps(["y","y2","height"],o),i=Math.min(e,n),a=Math.max(e,n)+("vertical"===this.options.orientation?0:r);return t>=i&&t<=a}getCenterPoint(t){const{x:o,y:e,x2:n,y2:r,height:i,width:a}=this.getProps(["x","y","x2","y2","height","width"],t),s="vertical"===this.options.orientation;return{x:(o+n+(s?a:0))/2,y:(e+r+(s?0:i))/2}}tooltipPosition(t=!1){return this.getCenterPoint(t)}getRange(t){const o="vertical"===this.options.orientation;return"x"===t?o?this.width/2:0:o?0:this.height/2}constructor(t){super(),this.flow=0,this.x2=0,this.y2=0,this.width=0,this.height=0,this.progress=1,t&&Object.assign(this,t)}}J.id="flow",J.defaults={alpha:.5,colorFrom:"red",colorMode:"gradient",colorTo:"green",flowColor:null,flowLabels:{borderRadius:0,color:"black",display:!1,padding:4,position:"center"},hoverColorFrom:(t,o)=>U(o.colorFrom),hoverColorTo:(t,o)=>U(o.colorTo),orientation:"horizontal"},J.descriptors={_scriptable:!0,flowLabels:{_scriptable:!0}},o.Chart.register($,J),t.Flow=J,t.SankeyController=$});
|
|
7
|
+
!function(t,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("chart.js"),require("chart.js/helpers")):"function"==typeof define&&define.amd?define(["exports","chart.js","chart.js/helpers"],o):o((t="undefined"!=typeof globalThis?globalThis:t||self)["chartjs-chart-sankey"]={},t.Chart,t.Chart.helpers)}(this,function(t,o,e){"use strict";const r=t=>void 0!==t;function n(t){return t&&-1!==["min","max"].indexOf(t)?t:"max"}function i(t,o){return"function"==typeof t?t(o):t&&"object"==typeof t&&!function(t){const o=Object.prototype.toString.call(t);return"[object CanvasPattern]"===o||"[object CanvasGradient]"===o}(t)?t[o.key]:t}function a(t,o,e,r,n,i,a){t.save(),t.fillStyle=o,a>0?(!function(t,o,e,r,n,i){const a=Math.max(0,Math.min(i,r/2,n/2));t.beginPath(),t.moveTo(o+a,e),t.lineTo(o+r-a,e),t.quadraticCurveTo(o+r,e,o+r,e+a),t.lineTo(o+r,e+n-a),t.quadraticCurveTo(o+r,e+n,o+r-a,e+n),t.lineTo(o+a,e+n),t.quadraticCurveTo(o,e+n,o,e+n-a),t.lineTo(o,e+a),t.quadraticCurveTo(o,e,o+a,e),t.closePath()}(t,e,r,n,i,a),t.fill()):t.fillRect(e,r,n,i),t.restore()}function s(t,o,e){const r=function(t){if(!t)return[];const o=[],e=Array.isArray(t)?t:[t];for(;e.length;){const t=e.pop();"string"==typeof t?o.unshift(...t.split("\n")):Array.isArray(t)?e.push(...t):t&&o.unshift(`${t}`)}return o}(o);if(!r.length)return;const{backgroundColor:n,borderRadius:i,color:s,font:l,lineOffset:h,padding:c}=e,f=function(t,o){return"auto"===t?o:t}(e.position,e.autoPosition),d=Number(l.lineHeight);t.font=l.string;const u=Math.max(...r.map(o=>t.measureText(o).width)),p=d*r.length,y=function(t,o,e){const{borderWidth:r,height:n,padding:i,width:a,x:s,y:l}=o,h={align:"center",x:s+a/2,y:l+n/2};return"left"===t?(h.align="right",h.x=s-r-i):"right"===t?(h.align="left",h.x=s+a+r+i):"top"===t?h.y=l-i-e/2:"bottom"===t&&(h.y=l+n+i+e/2),h}(f,e,p);t.textAlign=y.align,t.textBaseline="middle";const x=u+2*c,g=p+2*c,m=function(t,o,e,r,n){return"left"===t?o-n:"right"===t?o-e-n:o-r/2}(y.align,y.x,u,x,c),w=1===r.length?y.y:y.y-p/2+h,b=w+(r.length-1)*d/2;void 0!==n&&a(t,n,m,b-g/2,x,g,i),t.fillStyle=s;for(let o=0;o<r.length;o++)t.fillText(r[o],y.x,w+o*d)}const l=(t,o)=>o.flow===t.flow?t.index-o.index:o.flow-t.flow;function h(t,{size:o,priority:e,column:r}){const i=new Map;for(let o=0;o<t.length;o++){const{from:e,to:r,flow:n}=t[o],a=i.get(e)??{from:[],in:0,key:e,out:0,size:0,to:[]},s=(e===r?a:i.get(r))??{from:[],in:0,key:r,out:0,size:0,to:[]};a.out+=n,a.to.push({addY:0,flow:n,index:o,key:r,node:s}),1===a.to.length&&i.set(e,a),s.in+=n,s.from.push({addY:0,flow:n,index:o,key:e,node:a}),1===s.from.length&&i.set(r,s)}return((t,o)=>{const e=n(o);for(const o of t.values())o.from.sort(l),o.to.sort(l),o.size=Math[e](o.in||o.out,o.out||o.in)})(i,o),((t,o)=>{if(o)for(const e of t.values())e.key in o&&(e.priority=o[e.key])})(i,e),((t,o)=>{if(o)for(const e of t.values())e.key in o&&(e.column=!0,e.x=o[e.key])})(i,r),i}const c=1e-6;function f(t){return t.x??0}function d(t){return t.y??0}const u=(t,o=new Set)=>{const e=[];for(const r of t)o.has(r.key)||(o.add(r.key),e.push(r.key,...u(r.to.map(t=>t.node),o)));return e},p=(t,o)=>{const e=o.filter(t=>0===t.from.length),r=e.map(t=>t.key),n=u(e),i=new Set(n);for(const o of t)i.has(o.from)||i.has(o.to)||(r.push(o.from),i.add(o.from)),i.add(o.to);return r},y=(t,o)=>{const e=new Set(t.filter(t=>o.has(t.from)).map(t=>t.to)),r=[...o],n=r.filter(t=>!e.has(t));return n.length?n:r.slice(0,1)};function x(t,o,e=new Set){let r=0;for(const n of t)e.has(n.node)||(e.add(n.node),r+=n.node[o].length+x(n.node[o],o,e));return r}const g=t=>(o,e)=>x(o.node[t],t)-x(e.node[t],t)||o.node[t].length-e.node[t].length;function m(t,o){if(!t.from.length)return o;t.from.sort(g("from"));for(const e of t.from){const t=e.node;r(t.y)||(t.y=o,m(t,o?o+c:0)),o=Math.max(t.y+t.out,o)}return d(t)+t.size}const w=(t,o)=>Boolean(o&&f(o)<f(t));function b(t,o){if(!t.to.length)return o;t.to.sort(g("to"));for(let e=0;e<t.to.length;e++){const n=t.to[e],i=n.node;r(i.y)||(i.y=o,b(i,o?o+c:0)),w(i,t.to[e+1]?.node)?o+=n.flow:o=Math.max(i.y+Math.max(i.in,i.out),o)}return d(t)+t.size}function M(t,o){return r(t.y)?t.y:(t.y=o,o)}function v(t,o){if(!t.length)return 0;const e=((t,o)=>{const e=[...t].sort((t,o)=>t.size-o.size),r=e[e.length-1].size,n=t.filter(t=>t.size===r),i=n[0];if(1===n.length)return i;if(n.sort((t,o)=>f(t)-f(o)),0===f(i))return i;const a=n[n.length-1];return f(a)===o?a:n[Math.floor(n.length/2)]})(t,o);return e.y=0,m(e,0),b(e,0),function(t,o){const e=t.filter(t=>0===t.x),n=t.filter(t=>t.x===o),i=e.filter(t=>!r(t.y)),a=n.filter(t=>!r(t.y)),s=t.filter(t=>f(t)>0&&f(t)<o&&!r(t.y));let l=e.reduce((t,o)=>Math.max(t,d(o)+o.out||0),0)+c,h=n.reduce((t,o)=>Math.max(t,d(o)+o.in||0),0)+c,u=0;l>=h?(i.forEach(t=>{l=M(t,l),l=Math.max(l+t.out,b(t,l))}),a.forEach(t=>{h=M(t,h),h=Math.max(h+t.in,m(t,h))})):(i.forEach(t=>{l=M(t,l)}),a.forEach(t=>{h=M(t,h),h=Math.max(h+t.in,m(t,h))})),s.forEach(o=>{let e=t.filter(t=>f(t)===f(o)&&r(t.y)).reduce((t,o)=>Math.max(t,d(o)+Math.max(o.in,o.out)),0);e=M(o,e),e=Math.max(e+o.in,m(o,e)),e=Math.max(e+o.out,b(o,e)),u=Math.max(u,e)}),Math.max(l,h,u)}(t,o),((t,o)=>{let e=0;for(let r=0;r<=o;r++){const o=t.filter(t=>f(t)===r).sort((t,o)=>d(t)-d(o));let n=0;for(const t of o)d(t)<n&&(t.y=n),n=d(t)+t.size;e=Math.max(e,n)}return e})(t,o)}const k=(t,o)=>f(t)!==f(o)?f(t)-f(o):d(t)===d(o)?t.size-o.size:d(t)-d(o);function C(t,o,e){const r=(o.get(t.key)??0)*e;return Math.max(0,r-t.size)/2}function z(t,o,e){const r=(o.get(t.key)??0)*e;return Math.max(t.size,r)}function P(t,o,e,r){let n=r;for(let r=0;r<o;r++){const o=t[r].yHistory;for(let t=0;t<o.length&&!(o[t]>e);t++)n=Math.max(t+1,n)}return n}function S(t,o,e,r,n){const i=t.realCount,a=o.before*r,s=o.after*r,l=i>0?Math.max(t.lastAfter,a)+t.lastExtraHalf+n:0,h=t.realCumOffset+l,c=e-i;return t.realCount=i+1,t.realCumOffset=h,t.lastAfter=s,t.lastExtraHalf=n,h+c*a}function _(t,o,e=1,r="auto",n=new Map){if(t.sort(k),"even"===r)return function(t,o,e,r){let n,i,a,s=0,l=0;for(const h of t){const t=f(h),c=o.get(h.key)??{after:0,before:0},u=C(h,r,e);t!==n?n=t:i&&a&&(h.y=d(i)+i.size+Math.max(a.after,c.before)*e+l+u),i=h,a=c,l=u;const p=z(h,r,e);s=Math.max(s,d(h)+Math.max(h.in,h.out),d(h)+(h.size+p)/2)}return s}(t,o,e,n);let i=0;const a=new Map,s=[],l=t=>{if(!a.has(t)){const o=s.length;return a.set(t,o),s.push({lastAfter:0,lastExtraHalf:0,realCount:0,realCumOffset:0,yHistory:[]}),o}return a.get(t)??0};for(const r of t){const t=l(f(r)),a=s[t],h=o.get(r.key)??{after:0,before:0},c=d(r),u=C(r,n,e);if(c){a.yHistory.push(c);let o=a.yHistory.length;if(r.in)for(o=P(s,t,c,o);a.yHistory.length<o;)a.yHistory.push(c);r.y=c+S(a,h,o,e,u)}else a.realCount+=1,a.lastAfter=h.after*e,a.lastExtraHalf=u;const p=z(r,n,e);i=Math.max(i,d(r)+Math.max(r.in,r.out),d(r)+(r.size+p)/2)}return i}function T(t,o,{priority:e,height:n,nodePadding:i,nodePaddingMode:a,nodeMinSize:s,modeX:l}){const h=[...t.values()],c=function(t,o,e){const n=o.filter(t=>t.from!==t.to),i=[...t.keys()],a=[...t.values()],s=new Set(i);let l=0;for(;s.size;){const e=0===l?p(o,a):y(n,s);if(!e.length)throw new Error("Fatal error: Unable to place nodes to columns. Please report this issue.");for(const o of e){const e=t.get(o);e&&!r(e.x)&&(e.x=l),s.delete(o)}s.size&&l++}const h=a.reduce((t,o)=>Math.max(t,f(o)),0);if("edge"===e){const e=new Set(o.map(t=>t.from));i.filter(t=>!e.has(t)).forEach(o=>{const e=t.get(o);e&&!e.column&&(e.x=h)})}return h}(t,o,l??"edge"),u=e?function(t,o){let e=0,r=0;for(let n=0;n<=o;n++){let o=r;const i=t.filter(t=>f(t)===n).sort((t,o)=>(t.priority??0)-(o.priority??0));if(i.length){const o=t.reduce((t,o)=>f(o)>n?Math.min(t,f(o)):t,1/0);r=i[0].to.filter(t=>f(t.node)>o).reduce((t,o)=>t+o.flow,0)||0}for(const t of i)t.y=o,o+=Math.max(t.out,t.in);e=Math.max(o,e)}return e}(h,c):v(h,c),x=_(h,i,u/n,a??"auto",s??new Map);return function(t){t.forEach(t=>{const o=t.size,e=o<t.in,r=o<t.out;let n=0,i=t.from.length;t.from.sort((t,o)=>d(t.node)+t.node.out/2-(d(o.node)+o.node.out/2)).forEach((t,r)=>{e?t.addY=r*(o-t.flow)/(i-1):(t.addY=n,n+=t.flow)}),n=0,i=t.to.length,t.to.sort((t,o)=>d(t.node)+t.node.in/2-(d(o.node)+o.node.in/2)).forEach((t,e)=>{r?t.addY=e*(o-t.flow)/(i-1):(t.addY=n,n+=t.flow)})})}(h),{maxX:c,maxY:x}}function F(t){return t.x??0}function E(t){return t.y??0}function O(t,o){return Math[o](t.in||t.out,t.out||t.in)}function R(t,o,e,r){return"vertical"===r?o<(e.top+e.bottom)/2?"bottom":"top":t<(e.left+e.right)/2?"right":"left"}function H(t,o,e){for(const r of t)if(r.key===o&&r.index===e)return r.addY;return 0}function W(t,o,e,r,n,i,a,s,l){return"vertical"===l?{_custom:{flow:n,from:t,height:a.parse(n,i),to:o,x:a.parse(r,i),y:s.parse(F(o),i)},x:a.parse(e,i),y:s.parse(F(t),i)}:{_custom:{flow:n,from:t,height:s.parse(n,i),to:o,x:a.parse(F(o),i),y:s.parse(r,i)},x:a.parse(F(t),i),y:s.parse(e,i)}}function X(t,o,e,r,n,i,a,s){const l=t._custom,h=o.getPixelForValue(t.x),c=e.getPixelForValue(t.y);return"vertical"===s?{flow:l.flow,from:l.from,height:0,to:l.to,width:Math.abs(o.getPixelForValue(t.x+l.height)-h),x:h,x2:o.getPixelForValue(l.x),y:Y(e,t.y,r,i)+n+a,y2:Y(e,l.y,r,i)-a}:{flow:l.flow,from:l.from,height:Math.abs(e.getPixelForValue(t.y+l.height)-c),to:l.to,width:0,x:Y(o,t.x,r,i)+n+a,x2:Y(o,l.x,r,i)-a,y:c,y2:e.getPixelForValue(l.y)}}function Y(t,o,e,r){const n=t.getPixelForValue(o);return e?n-o/e*r:n}function j(t,o,e){const r="vertical"===o?e.height-e.chartArea.bottom:e.width-e.chartArea.right;return Math.max(0,t+3-r)}function A(t,o,e,r,n,i,a,s,l){if("vertical"===s){const s=e.getPixelForValue(E(t)),h=e.getPixelForValue(E(t)+o),c=Math.min(s,h),f=Math.abs(h-s),d=Math.max(f,l);return{height:i,width:d,x:c-(d-f)/2,y:Y(r,F(t),n,a)}}const h=r.getPixelForValue(E(t)),c=r.getPixelForValue(E(t)+o),f=Math.min(h,c),d=Math.abs(c-h),u=Math.max(d,l);return{height:u,width:i,x:Y(e,F(t),n,a),y:f-(u-d)/2}}function V(t,o){const e=i(t??10,o)??10;return"number"==typeof e?{after:e,before:e}:{after:e.after??10,before:e.before??10}}function L(t,o){return i(t??0,o)??0}function q(t,o){const{backgroundColor:e,borderRadius:r=0,color:n,display:a,font:s,padding:l=4,position:h}=t.nodeLabels??{};return{backgroundColor:i(e,o),borderRadius:r,color:i(n,o)??t.color??"black",display:i(a,o)??!0,font:s,padding:l,position:i(h,o)??"auto"}}const D=new Set(["nodeMinSize","nodePadding"]);class $ extends o.DatasetController{parseObjectData(t,o,e,r){const n=((t,o)=>{const{from:e="from",to:r="to",flow:n="flow"}=o;return t.map(({[e]:t,[r]:o,[n]:i})=>({flow:i,from:t,to:o}))})(o,this.options.parsing),{xScale:i,yScale:a}=t,s=[],l=h(n,this.options),c=this.options.orientation??"horizontal";this._nodes=l;const f=new Map,d=new Map;for(const t of l.values())f.set(t.key,V(this.options.nodePadding,t)),d.set(t.key,L(this.options.nodeMinSize,t));const{maxX:u,maxY:p}=T(l,n,{height:"vertical"===c?this.chart.width:this.chart.height,modeX:this.options.modeX,nodeMinSize:d,nodePadding:f,nodePaddingMode:this.options.nodePaddingMode,priority:!!this.options.priority});if(this._maxX=u,this._maxY=p,!i||!a)return[];for(let t=0,o=n.length;t<o;++t){const o=n[t],e=l.get(o.from),r=l.get(o.to);if(!e||!r)continue;const h=E(e)+H(e.to,o.to,t),f=E(r)+H(r.from,o.from,t);s.push(W(e,r,h,f,o.flow,t,i,a,c))}return s.slice(e,e+r)}buildOrUpdateElements(t){this.configure(),super.buildOrUpdateElements(t)}getMinMax(t){return{max:t===("vertical"===this.options.orientation?this._cachedMeta.yScale:this._cachedMeta.xScale)?this._maxX:this._maxY,min:0}}update(t){const{data:o}=this._cachedMeta;this.updateElements(o,0,o.length,t)}updateElements(t,o,e,r){const{xScale:n,yScale:i}=this._cachedMeta;if(!n||!i)return;const a=this.resolveDataElementOptions(o,r),s=this.getSharedOptions(a),{borderWidth:l,nodeWidth:h=10,orientation:c="horizontal"}=this.options,f=j(h,c,this.chart),d=l?l/2+.5:0;for(let a=o;a<o+e;a++){const o=this.getParsed(a);this.updateElement(t[a],a,{options:this.resolveDataElementOptions(a,r),progress:"reset"===r?0:1,...X(o,n,i,this._maxX,h,f,d,c)},r)}s&&this.updateSharedOptions(s,r,a)}_drawLabels(){const t=this.chart.ctx,r=this.options,i=this._nodes||new Map,a=n(r.size),l=r.labels,{borderWidth:h=1,nodeWidth:c=10,orientation:f="horizontal"}=r,d=j(c,f,this.chart),u=r.font??this.chart.options.font??o.Chart.defaults.font,{xScale:p,yScale:y}=this._cachedMeta;if(!p||!y)return;t.save();const x=this.chart.chartArea;for(const o of i.values()){const n=O(o,a),i=L(r.nodeMinSize,o),{height:g,width:m,x:w,y:b}=A(o,n,p,y,this._maxX,c,d,f,i),M=l?.[o.key]??o.key,v=q(r,o);if(v.display){const o=e.toFont(v.font??u);s(t,M,{autoPosition:R(w,b,x,f),backgroundColor:v.backgroundColor,borderRadius:v.borderRadius,borderWidth:h,color:v.color,font:o,height:g,lineOffset:e.valueOrDefault(r.padding,o.lineHeight/2),padding:v.padding,position:v.position,width:m,x:w,y:b})}}t.restore()}_drawNodes(){const t=this.chart.ctx,o=this._nodes||new Map,{borderColor:e,borderWidth:r=0,nodeWidth:i=10,orientation:a="horizontal",size:s}=this.options,l=j(i,a,this.chart),h=n(s),{xScale:c,yScale:f}=this._cachedMeta;t.save(),e&&r&&(t.strokeStyle=e,t.lineWidth=r);for(const e of o.values()){if(t.fillStyle=e.color??"black",!c||!f)return;const o=Math[h](e.in||e.out,e.out||e.in),n=L(this.options.nodeMinSize,e),{height:s,width:d,x:u,y:p}=A(e,o,c,f,this._maxX,i,l,a,n);r&&t.strokeRect(u,p,d,s),t.fillRect(u,p,d,s)}t.restore()}draw(){const t=this.chart.ctx,o=this.getMeta().data??[],e=[];for(let t=0,r=o.length;t<r;++t){const r=o[t];r.from&&r.to&&(r.from.color=r.options.colorFrom,r.to.color=r.options.colorTo,r.active&&e.push(r))}for(const t of e)t.from&&t.to&&(t.from.color=t.options.colorFrom,t.to.color=t.options.colorTo);this._drawNodes();for(let e=0,r=o.length;e<r;++e)o[e].draw(t);this._drawLabels()}constructor(...t){super(...t),this._nodes=new Map,this._maxX=0,this._maxY=0}}$.id="sankey",$.descriptors={_indexable:!1,_scriptable:t=>!D.has(t),nodeLabels:{_indexable:!1,_scriptable:!1}},$.defaults={animations:{colors:{properties:["colorFrom","colorTo"],type:"color"},numbers:{properties:["x","y","x2","y2","height","width"],type:"number"},progress:{delay:t=>"data"===t.type?500*t.parsed["vertical"===t.dataset.orientation?"y":"x"]+20*t.dataIndex:void 0,duration:t=>"data"===t.type?200*(t.parsed._custom["vertical"===t.dataset.orientation?"y":"x"]-t.parsed["vertical"===t.dataset.orientation?"y":"x"]):void 0,easing:"linear"}},borderColor:"black",borderWidth:1,color:"black",dataElementType:"flow",modeX:"edge",nodeMinSize:0,nodePadding:10,nodePaddingMode:"auto",nodeWidth:10,orientation:"horizontal",transitions:{hide:{animations:{colors:{properties:["colorFrom","colorTo"],to:"transparent",type:"color"}}},resize:{animations:{progress:{delay:0,duration:0}}},show:{animations:{colors:{from:"transparent",properties:["colorFrom","colorTo"],type:"color"}}}}},$.overrides={datasets:{clip:!1,parsing:{flow:"flow",from:"from",to:"to"}},interaction:{intersect:!0,mode:"nearest"},layout:{padding:{bottom:3,left:3,right:13,top:3}},plugins:{legend:{display:!1},tooltip:{callbacks:{label(t){const o=t.parsed._custom;return`${o.from.key} -> ${o.to.key}: ${o.flow}`},title:()=>""}}},scales:{x:{bounds:"data",display:!1,min:0,offset:!1,type:"linear"},y:{bounds:"data",display:!1,min:0,offset:!1,reverse:!0,type:"linear"}}};const G=(t,o,e,r,n)=>"vertical"===n?o<r?{cp1:{x:t,y:o+(r-o)/3*2},cp2:{x:e,y:o+(r-o)/3}}:{cp1:{x:0,y:o-(o-r)/3},cp2:{x:0,y:r+(o-r)/3}}:t<e?{cp1:{x:t+(e-t)/3*2,y:o},cp2:{x:t+(e-t)/3,y:r}}:{cp1:{x:t-(t-e)/3,y:0},cp2:{x:e+(t-e)/3,y:0}},N=(t,o,e)=>({x:t.x+e*(o.x-t.x),y:t.y+e*(o.y-t.y)}),U=(t,o)=>e.color(t).alpha(o).rgbString(),B=(t,o)=>"string"==typeof t?U(t,o):t,I=t=>"string"==typeof t?e.getHoverColor(t):t;class J extends o.Element{draw(t){const{x:r,x2:n,y:i,y2:a,height:l,progress:h,width:c}=this,f=this.options.orientation,d=G(r,i,n,a,f),u={height:l,width:c,x:r,x2:n,y:i,y2:a};if(0===h)return;t.save(),h<1&&function(t,{height:o,width:e,x:r,x2:n,y:i,y2:a},s,l){t.beginPath(),"vertical"===l?t.rect(Math.min(r,n),i,Math.abs(n-r)+e+1,(a-i)*s+1):t.rect(r,Math.min(i,a),(n-r)*s+1,Math.abs(a-i)+o+1),t.clip()}(t,u,h,f),function(t,{x:o,x2:e,y:r,y2:n,options:i}){let a="black";null!==i.flowColor?a=i.flowColor:"from"===i.colorMode?a=B(i.colorFrom,i.alpha):"to"===i.colorMode?a=B(i.colorTo,i.alpha):"string"==typeof i.colorFrom&&"string"==typeof i.colorTo&&(a="vertical"===i.orientation?t.createLinearGradient(0,r,0,n):t.createLinearGradient(o,0,e,0),a.addColorStop(0,U(i.colorFrom,i.alpha)),a.addColorStop(1,U(i.colorTo,i.alpha))),t.fillStyle=a,t.strokeStyle=a,t.lineWidth=.5}(t,this),function(t,{height:o,width:e,x:r,x2:n,y:i,y2:a},{cp1:s,cp2:l},h){t.beginPath(),t.moveTo(r,i),t.bezierCurveTo(s.x,s.y,l.x,l.y,n,a),"vertical"===h?(t.lineTo(n+e,a),t.bezierCurveTo(l.x+e,l.y,s.x+e,s.y,r+e,i)):(t.lineTo(n,a+o),t.bezierCurveTo(l.x,l.y+o,s.x,s.y+o,r,i+o)),t.lineTo(r,i),t.stroke(),t.closePath(),t.fill()}(t,u,d,f);const p=this.options.flowLabels;if(p.display){const r=e.toFont(p.font??o.Chart.defaults.font),n=function({height:t,width:o,x:e,x2:r,y:n,y2:i},a){return"vertical"===a?{height:i-n,width:Math.abs(r-e)+o,x:Math.min(e,r),y:n}:{height:Math.abs(i-n)+t,width:r-e,x:e,y:Math.min(n,i)}}(u,f);s(t,`${this.flow}`,{autoPosition:"center",backgroundColor:p.backgroundColor,borderRadius:p.borderRadius,borderWidth:0,color:p.color,font:r,height:n.height,lineOffset:r.lineHeight/2,padding:p.padding,position:p.position,width:n.width,x:n.x,y:n.y})}t.restore()}inRange(t,o,e){const{x:r,y:n,x2:i,y2:a,height:s,width:l}=this.getProps(["x","y","x2","y2","height","width"],e),h="vertical"===this.options.orientation;if(h?o<n||o>a:t<r||t>i)return!1;const{cp1:c,cp2:f}=G(r,n,i,a,this.options.orientation),d=h?(o-n)/(a-n):(t-r)/(i-r),u={x:i,y:a},p=N({x:r,y:n},c,d),y=N(c,f,d),x=N(f,u,d),g=N(p,y,d),m=N(y,x,d),w=N(g,m,d);return h?t>=w.x&&t<=w.x+l:o>=w.y&&o<=w.y+s}inXRange(t,o){const{x:e,x2:r,width:n}=this.getProps(["x","x2","width"],o),i=Math.min(e,r),a=Math.max(e,r)+("vertical"===this.options.orientation?n:0);return t>=i&&t<=a}inYRange(t,o){const{y:e,y2:r,height:n}=this.getProps(["y","y2","height"],o),i=Math.min(e,r),a=Math.max(e,r)+("vertical"===this.options.orientation?0:n);return t>=i&&t<=a}getCenterPoint(t){const{x:o,y:e,x2:r,y2:n,height:i,width:a}=this.getProps(["x","y","x2","y2","height","width"],t),s="vertical"===this.options.orientation;return{x:(o+r+(s?a:0))/2,y:(e+n+(s?0:i))/2}}tooltipPosition(t=!1){return this.getCenterPoint(t)}getRange(t){const o="vertical"===this.options.orientation;return"x"===t?o?this.width/2:0:o?0:this.height/2}constructor(t){super(),this.flow=0,this.x2=0,this.y2=0,this.width=0,this.height=0,this.progress=1,t&&Object.assign(this,t)}}J.id="flow",J.defaults={alpha:.5,colorFrom:"red",colorMode:"gradient",colorTo:"green",flowColor:null,flowLabels:{borderRadius:0,color:"black",display:!1,padding:4,position:"center"},hoverColorFrom:(t,o)=>I(o.colorFrom),hoverColorTo:(t,o)=>I(o.colorTo),orientation:"horizontal"},J.descriptors={_scriptable:!0,flowLabels:{_scriptable:!0}},o.Chart.register($,J),t.Flow=J,t.SankeyController=$});
|
package/dist/controller.d.cts
CHANGED
|
@@ -122,6 +122,7 @@ export default class SankeyController extends DatasetController {
|
|
|
122
122
|
private _maxX;
|
|
123
123
|
private _maxY;
|
|
124
124
|
parseObjectData(meta: ChartMeta<'sankey', Flow>, data: AnyObject[], start: number, count: number): SankeyParsedData[];
|
|
125
|
+
buildOrUpdateElements(resetNewElements?: boolean): void;
|
|
125
126
|
getMinMax(scale: any): {
|
|
126
127
|
max: number;
|
|
127
128
|
min: number;
|
package/dist/controller.d.ts
CHANGED
|
@@ -122,6 +122,7 @@ export default class SankeyController extends DatasetController {
|
|
|
122
122
|
private _maxX;
|
|
123
123
|
private _maxY;
|
|
124
124
|
parseObjectData(meta: ChartMeta<'sankey', Flow>, data: AnyObject[], start: number, count: number): SankeyParsedData[];
|
|
125
|
+
buildOrUpdateElements(resetNewElements?: boolean): void;
|
|
125
126
|
getMinMax(scale: any): {
|
|
126
127
|
max: number;
|
|
127
128
|
min: number;
|
package/package.json
CHANGED