chartjs-chart-sankey
Version:
Chart.js module for creating sankey diagrams
8 lines (7 loc) • 11.9 kB
JavaScript
/*!
* chartjs-chart-sankey v0.14.0
* https://github.com/kurkle/chartjs-chart-sankey#readme
* (c) 2024 Jukka Kurkela
* Released under the MIT license
*/
!function(t,o){"object"==typeof exports&&"undefined"!=typeof module?o(require("chart.js"),require("chart.js/helpers")):"function"==typeof define&&define.amd?define(["chart.js","chart.js/helpers"],o):o((t="undefined"!=typeof globalThis?globalThis:t||self).Chart,t.Chart.helpers)}(this,(function(t,o){"use strict";const e=t=>void 0!==t;function r(t){return t&&-1!==["min","max"].indexOf(t)?t:"max"}const n=(t,o)=>o.flow===t.flow?t.index-o.index:o.flow-t.flow;function s(t,{size:o,priority:e,column:s}){const i=new Map;for(let o=0;o<t.length;o++){const{from:e,to:r,flow:n}=t[o],s=i.get(e)??{key:e,in:0,out:0,size:0,from:[],to:[]},a=(e===r?s:i.get(r))??{key:r,in:0,out:0,size:0,from:[],to:[]};s.out+=n,s.to.push({key:r,flow:n,index:o,node:a,addY:0}),1===s.to.length&&i.set(e,s),a.in+=n,a.from.push({key:e,flow:n,index:o,node:s,addY:0}),1===a.from.length&&i.set(r,a)}return((t,o)=>{const e=r(o);for(const o of t.values())o.from.sort(n),o.to.sort(n),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,s),i}const i=1e-6,a=(t,o=new Set)=>{const e=[];for(const r of t)o.has(r.key)||(o.add(r.key),e.push(r.key,...a(r.to.map((t=>t.node)),o)));return e},l=(t,o)=>{const e=o.filter((t=>0===t.from.length)),r=e.map((t=>t.key)),n=a(e),s=new Set(n);for(const o of t)s.has(o.from)||s.has(o.to)||(r.push(o.from),s.add(o.from)),s.add(o.to);return r},c=(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)};let h=-1;function f(t,o,e=function(){return h=h<100?h+1:0,h}()){let r=0;for(const n of t)n.node._visited!==e&&(n.node._visited=e,r+=n.node[o].length+f(n.node[o],o,e));return r}const d=t=>(o,e)=>f(o.node[t],t)-f(e.node[t],t)||o.node[t].length-e.node[t].length;function u(t,o){if(!t.from.length)return o;t.from.sort(d("from"));for(const r of t.from){const t=r.node;e(t.y)||(t.y=o,u(t,o?o+i:0)),o=Math.max(t.y+t.out,o)}return t.y+t.size}function y(t,o){if(!t.to.length)return o;t.to.sort(d("to"));for(const r of t.to){const t=r.node;e(t.y)||(t.y=o,y(t,o?o+i:0)),o=Math.max(t.y+Math.max(t.in,t.out),o)}return t.y+t.size}function p(t,o){return e(t.y)?t.y:(t.y=o,o)}function x(t,o){if(!t.length)return 0;const r=((t,o)=>{const e=[...t].sort(((t,o)=>t.size-o.size)).pop().size,r=t.filter((t=>t.size===e));return 1===r.length?r[0]:(r.sort(((t,o)=>t.x-o.x)),0===r[0].x?r[0]:r[r.length-1].x===o?r.pop():r[Math.floor(r.length/2)])})(t,o);return r.y=0,u(r,0),y(r,0),function(t,o){const r=t.filter((t=>0===t.x)),n=t.filter((t=>t.x===o)),s=r.filter((t=>!e(t.y))),a=n.filter((t=>!e(t.y))),l=t.filter((t=>t.x>0&&t.x<o&&!e(t.y)));let c=r.reduce(((t,o)=>Math.max(t,o.y+o.out||0)),0)+i,h=n.reduce(((t,o)=>Math.max(t,o.y+o.in||0)),0)+i,f=0;c>=h?(s.forEach((t=>{c=p(t,c),c=Math.max(c+t.out,y(t,c))})),a.forEach((t=>{h=p(t,h),h=Math.max(h+t.in,u(t,h))}))):(s.forEach((t=>{c=p(t,c)})),a.forEach((t=>{h=p(t,h),h=Math.max(h+t.in,u(t,h))}))),l.forEach((o=>{let r=t.filter((t=>t.x===o.x&&e(t.y))).reduce(((t,o)=>Math.max(t,o.y+Math.max(o.in,o.out))),0);r=p(o,r),r=Math.max(r+o.in,u(o,r)),r=Math.max(r+o.out,y(o,r)),f=Math.max(f,r)})),Math.max(c,h,f)}(t,o),((t,o)=>{let e=0;for(let r=0;r<=o;r++){const o=t.filter((t=>t.x===r)).sort(((t,o)=>t.y-o.y));let n=0;for(const t of o)t.y<n&&(t.y=n),n=t.y+t.size;e=Math.max(e,n)}return e})(t,o)}const m=(t,o)=>t.x!==o.x?t.x-o.x:t.y===o.y?t.size-o.size:t.y-o.y;function g(t,o,{priority:r,height:n,nodePadding:s,modeX:i}){const a=[...t.values()],h=function(t,o,r){const n=o.filter((t=>t.from!==t.to)),s=[...t.keys()],i=[...t.values()],a=new Set(s);let h=0;for(;a.size;){const r=0===h?l(o,i):c(n,a);if(!r.length)throw new Error("Fatal error: Unable to place nodes to columns. Please report this issue.");for(const o of r){const r=t.get(o);r&&!e(r.x)&&(r.x=h),a.delete(o)}a.size&&h++}const f=i.reduce(((t,o)=>Math.max(t,o.x)),0);if("edge"===r){const e=new Set(o.map((t=>t.from)));s.filter((t=>!e.has(t))).forEach((o=>{const e=t.get(o);e&&!e.column&&(e.x=f)}))}return f}(t,o,i),f=r?function(t,o){let e=0,r=0;for(let n=0;n<=o;n++){let o=r;const s=t.filter((t=>t.x===n)).sort(((t,o)=>(t.priority??0)-(o.priority??0)));r=s.length&&s[0].to.filter((t=>t.node.x>n+1)).reduce(((t,o)=>t+o.flow),0)||0;for(const t of s)t.y=o,o+=Math.max(t.out,t.in);e=Math.max(o,e)}return e}(a,h):x(a,h),d=function(t,o){let e=0;const r=new Map,n=[];t.sort(m);for(const i of t){const t=(s=i.x,r.has(s)||(r.set(s,n.length),n.push([])),r.get(s)),a=n[t];if(i.y){a.push(i.y);let e=a.length;if(i.in){for(let o=0;o<t;o++){const t=n[o];for(let o=0;o<t.length&&!(t[o]>i.y);o++)e=Math.max(o+1,e)}for(;a.length<e;)a.push(i.y)}i.y+=e*o}e=Math.max(e,i.y+Math.max(i.in,i.out))}var s;return e}(a,f/n*s);return function(t){t.forEach((t=>{const o=t.size,e=o<t.in,r=o<t.out;let n=0,s=t.from.length;t.from.sort(((t,o)=>t.node.y+t.node.out/2-(o.node.y+o.node.out/2))).forEach(((t,r)=>{e?t.addY=r*(o-t.flow)/(s-1):(t.addY=n,n+=t.flow)})),n=0,s=t.to.length,t.to.sort(((t,o)=>t.node.y+t.node.in/2-(o.node.y+o.node.in/2))).forEach(((t,e)=>{r?t.addY=e*(o-t.flow)/(s-1):(t.addY=n,n+=t.flow)}))}))}(a),{maxX:h,maxY:d}}function M(t,o,e){for(const r of t)if(r.key===o&&r.index===e)return r.addY;return 0}class w extends t.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]:s})=>({from:t,to:o,flow:s})))})(o,this.options.parsing),{xScale:i,yScale:a}=t,l=[],c=this._nodes=s(n,this.options),{maxX:h,maxY:f}=g(c,n,{priority:!!this.options.priority,height:this.chart.canvas.height,nodePadding:this.options.nodePadding,modeX:this.options.modeX});if(this._maxX=h,this._maxY=f,!i||!a)return[];for(let t=0,o=n.length;t<o;++t){const o=n[t],e=c.get(o.from),r=c.get(o.to);if(!e||!r)continue;const s=(e.y??0)+M(e.to,o.to,t),h=(r.y??0)+M(r.from,o.from,t);l.push({x:i.parse(e.x,t),y:a.parse(s,t),_custom:{from:e,to:r,x:i.parse(r.x,t),y:a.parse(h,t),height:a.parse(o.flow,t),flow:o.flow}})}return l.slice(e,e+r)}getMinMax(t){return{min:0,max:t===this._cachedMeta.xScale?this._maxX:this._maxY}}update(t){const{data:o}=this._cachedMeta;this.updateElements(o,0,o.length,t)}updateElements(t,o,e,r){const{xScale:n,yScale:s}=this._cachedMeta;if(!n||!s)return;const i=this.resolveDataElementOptions(o,r),a=this.getSharedOptions(i),{borderWidth:l,nodeWidth:c=10}=this.options,h=l?l/2+.5:0;for(let i=o;i<o+e;i++){const o=this.getParsed(i),e=o._custom,a=s.getPixelForValue(o.y);this.updateElement(t[i],i,{x:n.getPixelForValue(o.x)+c+h,y:a,x2:n.getPixelForValue(e.x)-h,y2:s.getPixelForValue(e.y),from:e.from,to:e.to,progress:"reset"===r?0:1,height:Math.abs(s.getPixelForValue(o.y+e.height)-a),options:this.resolveDataElementOptions(i,r)},r)}this.updateSharedOptions(a,r,i)}_drawLabels(){const t=this.chart.ctx,o=this.options,e=this._nodes||new Map,n=r(o.size),s=o.borderWidth??1,i=o.nodeWidth??10,a=o.labels,{xScale:l,yScale:c}=this._cachedMeta;if(!l||!c)return;t.save();const h=this.chart.chartArea;for(const r of e.values()){const e=l.getPixelForValue(r.x),f=c.getPixelForValue(r.y),d=Math[n](r.in||r.out,r.out||r.in),u=Math.abs(c.getPixelForValue(r.y+d)-f),y=a?.[r.key]??r.key;let p=e;t.fillStyle=o.color??"black",t.textBaseline="middle",e<h.width/2?(t.textAlign="left",p+=i+s+4):(t.textAlign="right",p-=s+4),this._drawLabel(y,f,u,t,p)}t.restore()}_drawLabel(t,e,r,n,s){const i=o.toFont(this.options.font,this.chart.options.font),a=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}(t),l=a.length,c=e+r/2,h=i.lineHeight,f=o.valueOrDefault(this.options.padding,h/2);if(n.font=i.string,l>1){const t=c-h*l/2+f;for(let o=0;o<l;o++)n.fillText(a[o],s,t+o*h)}else n.fillText(t,s,c)}_drawNodes(){const t=this.chart.ctx,o=this._nodes||new Map,{borderColor:e,borderWidth:n=0,nodeWidth:s=10,size:i}=this.options,a=r(i),{xScale:l,yScale:c}=this._cachedMeta;t.save(),e&&n&&(t.strokeStyle=e,t.lineWidth=n);for(const e of o.values()){t.fillStyle=e.color??"black";const o=l.getPixelForValue(e.x),r=c.getPixelForValue(e.y),i=Math[a](e.in||e.out,e.out||e.in),h=Math.abs(c.getPixelForValue(e.y+i)-r);n&&t.strokeRect(o,r,s,h),t.fillRect(o,r,s,h)}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.color=r.options.colorFrom,r.to.color=r.options.colorTo,r.active&&e.push(r)}for(const t of e)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()}}w.id="sankey",w.defaults={dataElementType:"flow",animations:{numbers:{type:"number",properties:["x","y","x2","y2","height"]},progress:{easing:"linear",duration:t=>"data"===t.type?200*(t.parsed._custom.x-t.parsed.x):void 0,delay:t=>"data"===t.type?500*t.parsed.x+20*t.dataIndex:void 0},colors:{type:"color",properties:["colorFrom","colorTo"]}},color:"black",borderColor:"black",borderWidth:1,modeX:"edge",nodeWidth:10,nodePadding:10,transitions:{hide:{animations:{colors:{type:"color",properties:["colorFrom","colorTo"],to:"transparent"}}},show:{animations:{colors:{type:"color",properties:["colorFrom","colorTo"],from:"transparent"}}}}},w.overrides={interaction:{mode:"nearest",intersect:!0},datasets:{clip:!1,parsing:{from:"from",to:"to",flow:"flow"}},plugins:{tooltip:{callbacks:{title:()=>"",label(t){const o=t.parsed._custom;return o.from.key+" -> "+o.to.key+": "+o.flow}}},legend:{display:!1}},scales:{x:{type:"linear",bounds:"data",display:!1,min:0,offset:!1},y:{type:"linear",bounds:"data",display:!1,min:0,reverse:!0,offset:!1}},layout:{padding:{top:3,left:3,right:13,bottom:3}}};const b=(t,o,e,r)=>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}},v=(t,o,e)=>({x:t.x+e*(o.x-t.x),y:t.y+e*(o.y-t.y)}),k=(t,e)=>o.color(t).alpha(e).rgbString(),P=(t,o)=>"string"==typeof t?k(t,o):t;class _ extends t.Element{draw(t){const{x:o,x2:e,y:r,y2:n,height:s,progress:i}=this,{cp1:a,cp2:l}=b(o,r,e,n);0!==i&&(t.save(),i<1&&(t.beginPath(),t.rect(o,Math.min(r,n),(e-o)*i+1,Math.abs(n-r)+s+1),t.clip()),function(t,{x:o,x2:e,options:r}){let n="black";"from"===r.colorMode?n=P(r.colorFrom,r.alpha):"to"===r.colorMode?n=P(r.colorTo,r.alpha):"string"==typeof r.colorFrom&&"string"==typeof r.colorTo&&(n=t.createLinearGradient(o,0,e,0),n.addColorStop(0,k(r.colorFrom,r.alpha)),n.addColorStop(1,k(r.colorTo,r.alpha))),t.fillStyle=n,t.strokeStyle=n,t.lineWidth=.5}(t,this),t.beginPath(),t.moveTo(o,r),t.bezierCurveTo(a.x,a.y,l.x,l.y,e,n),t.lineTo(e,n+s),t.bezierCurveTo(l.x,l.y+s,a.x,a.y+s,o,r+s),t.lineTo(o,r),t.stroke(),t.closePath(),t.fill(),t.restore())}inRange(t,o,e){const{x:r,y:n,x2:s,y2:i,height:a}=this.getProps(["x","y","x2","y2","height"],e);if(t<r||t>s)return!1;const{cp1:l,cp2:c}=b(r,n,s,i),h=(t-r)/(s-r),f={x:s,y:i},d=v({x:r,y:n},l,h),u=v(l,c,h),y=v(c,f,h),p=v(d,u,h),x=v(u,y,h),m=v(p,x,h).y;return o>=m&&o<=m+a}inXRange(t,o){const{x:e,x2:r}=this.getProps(["x","x2"],o);return t>=e&&t<=r}inYRange(t,o){const{y:e,y2:r,height:n}=this.getProps(["y","y2","height"],o),s=Math.min(e,r),i=Math.max(e,r)+n;return t>=s&&t<=i}getCenterPoint(t){const{x:o,y:e,x2:r,y2:n,height:s}=this.getProps(["x","y","x2","y2","height"],t);return{x:(o+r)/2,y:(e+n+s)/2}}tooltipPosition(t){return this.getCenterPoint(t)}getRange(t){return"x"===t?this.width/2:this.height/2}constructor(t){super(),t&&Object.assign(this,t)}}_.id="flow",_.defaults={colorFrom:"red",colorTo:"green",colorMode:"gradient",alpha:.5,hoverColorFrom:(t,e)=>o.getHoverColor(e.colorFrom),hoverColorTo:(t,e)=>o.getHoverColor(e.colorTo)},_.descriptors={_scriptable:!0},t.Chart.register(w,_)}));