chartjs-chart-sankey 0.6.0 → 0.8.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,7 +1,7 @@
1
1
  /*!
2
- * chartjs-chart-sankey v0.6.0
2
+ * chartjs-chart-sankey v0.8.1
3
3
  * https://github.com/kurkle/chartjs-chart-sankey#readme
4
- * (c) 2021 Jukka Kurkela
4
+ * (c) 2022 Jukka Kurkela
5
5
  * Released under the MIT license
6
6
  */
7
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(require("chart.js"),require("chart.js/helpers")):"function"==typeof define&&define.amd?define(["chart.js","chart.js/helpers"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).Chart,t.Chart.helpers)}(this,(function(t,e){"use strict";function o(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=o(t);function a(t,e){const o=t.filter((t=>!e.has(t)));return o.length?o:t.slice(0,1)}const n=t=>void 0!==t,s=(t,e)=>t.x!==e.x?t.x-e.x:t.y-e.y,l=(t,e)=>t.reduce(((t,o)=>t+o.node[e].length+l(o.node[e],e)),0),i=t=>(e,o)=>l(e.node[t],t)-l(o.node[t],t);function c(t,e){return t.from.sort(i("from")).forEach((t=>{const o=t.node;n(o.y)||(o.y=e,e=Math.max(e+o.out,c(o,e)))})),e}function f(t,e){return t.to.sort(i("to")).forEach((t=>{const o=t.node;n(o.y)||(o.y=e,e=Math.max(e+o.in,f(o,e)))})),e}function h(t,e){return n(t.y)?t.y:(t.y=e,e)}function d(t,e){const o=function(t){return t.sort(((t,e)=>Math.max(e.in,e.out)-Math.max(t.in,t.out)))[0]}(t);o.y=0;const r=c(o,0),a=f(o,0),s=function(t,e){const o=t.filter((t=>0===t.x)),r=t.filter((t=>t.x===e)),a=o.filter((t=>!n(t.y))),s=r.filter((t=>!n(t.y)));let l=o.reduce(((t,e)=>Math.max(t,e.y+e.out||0)),0),i=r.reduce(((t,e)=>Math.max(t,e.y+e.in||0)),0);return l>=i?(a.forEach((t=>{l=h(t,l),l=Math.max(l+t.out,f(t,l))})),s.forEach((t=>{i=h(t,i),i=Math.max(i+t.in,f(t,i))}))):(s.forEach((t=>{i=h(t,i),i=Math.max(i+t.in,f(t,i))})),a.forEach((t=>{l=h(t,l),l=Math.max(l+t.out,f(t,l))}))),Math.max(l,i)}(t,e);return Math.max(r,a,s)}function u(t,e,o){const r=[...t.values()],n=function(t,e){const o=new Set(e.map((t=>t.to))),r=new Set(e.map((t=>t.from))),n=new Set([...t.keys()]);let s=0;for(;n.size;){const r=a([...n],o);for(let e=0;e<r.length;e++)t.get(r[e]).x=s,n.delete(r[e]);n.size&&(o.clear(),e.filter((t=>n.has(t.from))).forEach((t=>o.add(t.to))),s++)}return[...t.keys()].filter((t=>!r.has(t))).forEach((e=>{t.get(e).x=s})),s}(t,e),l=o?function(t,e){let o=0;for(let r=0;r<=e;r++){let e=0;const a=t.filter((t=>t.x===r)).sort(((t,e)=>t.priority-e.priority));for(const t of a)t.y=e,e+=Math.max(t.out,t.in);o=Math.max(e,o)}return o}(r,n):d(r,n),i=function(t,e){let o=1,r=0,a=0,n=0;const l=[];return t.sort(s).forEach((t=>{if(t.y){if(0===t.x)l.push(t.y);else{for(r!==t.x&&(r=t.x,a=0),o=a+1;o<l.length&&!(l[o]>t.y);o++);a=o}t.y+=o*e,n=Math.max(n,t.y+Math.max(t.in,t.out)),o++}})),n}(r,.03*l);return function(t){t.forEach((t=>{let e=0;t.from.sort(((t,e)=>t.node.y+t.node.out/2-(e.node.y+e.node.out/2))).forEach((t=>{t.addY=e,e+=t.flow})),e=0,t.to.sort(((t,e)=>t.node.y+t.node.in/2-(e.node.y+e.node.in/2))).forEach((t=>{t.addY=e,e+=t.flow}))}))}(r),{maxX:n,maxY:i}}function y(t,e){for(let o=0;o<t.length;o++)if(t[o].key===e)return t[o].addY;return 0}class x extends t.DatasetController{parseObjectData(t,e,o,r){if(0===r)return[];const a=this,{xScale:n,yScale:s}=t,l=[],i=a._nodes=function(t){const e=new Map;for(let o=0;o<t.length;o++){const r=t[o];if(e.has(r.from)){const t=e.get(r.from);t.out+=r.flow,t.to.push({key:r.to,flow:r.flow})}else e.set(r.from,{key:r.from,in:0,out:r.flow,from:[],to:[{key:r.to,flow:r.flow}]});if(e.has(r.to)){const t=e.get(r.to);t.in+=r.flow,t.from.push({key:r.from,flow:r.flow})}else e.set(r.to,{key:r.to,in:r.flow,out:0,from:[{key:r.from,flow:r.flow}],to:[]})}const o=(t,e)=>e.flow-t.flow;return[...e.values()].forEach((t=>{let r=0;t.from=t.from.sort(o),t.from.forEach((t=>{t.node=e.get(t.key),t.addY=r,r+=t.flow})),r=0,t.to=t.to.sort(o),t.to.forEach((t=>{t.node=e.get(t.key),t.addY=r,r+=t.flow}))})),e}(e),c=a.getDataset().priority;if(c)for(const t of i.values())t.key in c&&(t.priority=c[t.key]);const{maxX:f,maxY:h}=u(i,e,!!c);a._maxX=f,a._maxY=h;for(let t=0,o=e.length;t<o;++t){const o=e[t],r=i.get(o.from),a=i.get(o.to),c=r.y+y(r.to,o.to),f=a.y+y(a.from,o.from);l.push({x:n.parse(r.x,t),y:s.parse(c,t),_custom:{from:r,to:a,x:n.parse(a.x,t),y:s.parse(f,t),height:s.parse(o.flow,t)}})}return l.slice(o,o+r)}getMinMax(t){return{min:0,max:t===this._cachedMeta.xScale?this._maxX:this._maxY}}update(t){const e=this._cachedMeta;this.updateElements(e.data,0,e.data.length,t)}updateElements(t,o,r,a){const n=this,{xScale:s,yScale:l}=n._cachedMeta,i=n.resolveDataElementOptions(o,a),c=n.getSharedOptions(a,t[o],i),f=n.getDataset(),h=e.valueOrDefault(f.borderWidth,1)/2+.5,d=e.valueOrDefault(f.nodeWidth,10);for(let e=o;e<o+r;e++){const o=n.getParsed(e),r=o._custom,i=l.getPixelForValue(o.y);n.updateElement(t[e],e,{x:s.getPixelForValue(o.x)+d+h,y:i,x2:s.getPixelForValue(r.x)-h,y2:l.getPixelForValue(r.y),from:r.from,to:r.to,progress:"reset"===a?0:1,height:Math.abs(l.getPixelForValue(o.y+r.height)-i),options:n.resolveDataElementOptions(e,a)},a)}n.updateSharedOptions(c,a)}_drawLabels(){const t=this,o=t._ctx,r=t._nodes||new Map,a=t.getDataset(),n=e.valueOrDefault(a.borderWidth,1),s=e.valueOrDefault(a.nodeWidth,10),l=a.labels,{xScale:i,yScale:c}=t._cachedMeta;o.save();const f=t.chart.chartArea;for(const t of r.values()){const e=i.getPixelForValue(t.x),r=c.getPixelForValue(t.y),h=Math.max(t.in,t.out),d=Math.abs(c.getPixelForValue(t.y+h)-r),u=l&&l[t.key]||t.key;let y=e;o.fillStyle=a.color||"black",o.textBaseline="middle",e<f.width/2?(o.textAlign="left",y+=s+n+4):(o.textAlign="right",y-=n+4),o.fillText(u,y,r+d/2)}o.restore()}_drawNodes(){const t=this,o=t._ctx,r=t._nodes||new Map,a=t.getDataset(),{xScale:n,yScale:s}=t._cachedMeta,l=e.valueOrDefault(a.borderWidth,1),i=e.valueOrDefault(a.nodeWidth,10);o.save(),o.strokeStyle=a.borderColor||"black",o.lineWidth=l;for(const t of r.values()){o.fillStyle=t.color;const e=n.getPixelForValue(t.x),r=s.getPixelForValue(t.y),a=Math.max(t.in,t.out),c=Math.abs(s.getPixelForValue(t.y+a)-r);l&&o.strokeRect(e,r,i,c),o.fillRect(e,r,i,c)}o.restore()}draw(){const t=this,e=t._ctx,o=t.getMeta().data||[];for(let t=0,e=o.length;t<e;++t){const e=o[t];e.from.color=e.options.colorFrom,e.to.color=e.options.colorTo}t._drawNodes();for(let t=0,r=o.length;t<r;++t)o[t].draw(e);t._drawLabels()}}x.id="sankey",x.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"]}},transitions:{hide:{animations:{colors:{type:"color",properties:["colorFrom","colorTo"],to:"transparent"}}},show:{animations:{colors:{type:"color",properties:["colorFrom","colorTo"],from:"transparent"}}}}},x.overrides={interaction:{mode:"nearest",intersect:!0},datasets:{color:()=>"#efefef",clip:!1,parsing:!0},plugins:{tooltip:{callbacks:{title:()=>"",label(t){const e=t.dataset.data[t.dataIndex];return e.from+" -> "+e.to+": "+e.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 p=(t,e,o,r)=>t<o?{cp1:{x:t+(o-t)/3*2,y:e},cp2:{x:t+(o-t)/3,y:r}}:{cp1:{x:t-(t-o)/3,y:0},cp2:{x:o+(t-o)/3,y:0}},m=(t,e,o)=>({x:t.x+o*(e.x-t.x),y:t.y+o*(e.y-t.y)});class g extends t.Element{constructor(t){super(),this.options=void 0,this.x=void 0,this.y=void 0,this.x2=void 0,this.y2=void 0,this.height=void 0,t&&Object.assign(this,t)}draw(t){const{x:o,x2:r,y:a,y2:n,height:s,progress:l}=this,{cp1:i,cp2:c}=p(o,a,r,n),f=this.options;if(0===l)return;let h;t.save(),l<1&&(t.beginPath(),t.rect(o,Math.min(a,n),(r-o)*l+1,Math.abs(n-a)+s+1),t.clip()),"from"===f.colorMode?h=e.color(f.colorFrom).alpha(.5).rgbString():"to"===f.colorMode?h=e.color(f.colorTo).alpha(.5).rgbString():(h=t.createLinearGradient(o,0,r,0),h.addColorStop(0,e.color(f.colorFrom).alpha(.5).rgbString()),h.addColorStop(1,e.color(f.colorTo).alpha(.5).rgbString())),t.fillStyle=h,t.strokeStyle=h,t.lineWidth=.5,t.beginPath(),t.moveTo(o,a),t.bezierCurveTo(i.x,i.y,c.x,c.y,r,n),t.lineTo(r,n+s),t.bezierCurveTo(c.x,c.y+s,i.x,i.y+s,o,a+s),t.lineTo(o,a),t.stroke(),t.closePath(),t.fill(),t.restore()}inRange(t,e,o){const{x:r,y:a,x2:n,y2:s,height:l}=this.getProps(["x","y","x2","y2","height"],o);if(t<r||t>n)return!1;const{cp1:i,cp2:c}=p(r,a,n,s),f=(t-r)/(n-r),h={x:n,y:s},d=m({x:r,y:a},i,f),u=m(i,c,f),y=m(c,h,f),x=m(d,u,f),g=m(u,y,f),M=m(x,g,f).y;return e>=M&&e<=M+l}inXRange(t,e){const{x:o,x2:r}=this.getProps(["x","x2"],e);return t>=o&&t<=r}inYRange(t,e){const{y:o,y2:r,height:a}=this.getProps(["y","y2","height"],e),n=Math.min(o,r),s=Math.max(o,r)+a;return t>=n&&t<=s}getCenterPoint(t){const{x:e,y:o,x2:r,y2:a,height:n}=this.getProps(["x","y","x2","y2","height"],t);return{x:(e+r)/2,y:(o+a+n)/2}}tooltipPosition(){return this.getCenterPoint()}getRange(t){return"x"===t?this.width/2:this.height/2}}g.id="flow",g.defaults={colorFrom:"red",colorTo:"green",colorMode:"gradient"},r.default.register(x,g)}));
7
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(require("chart.js"),require("chart.js/helpers")):"function"==typeof define&&define.amd?define(["chart.js","chart.js/helpers"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).Chart,t.Chart.helpers)}(this,(function(t,e){"use strict";function o(t,e){const o=t.filter((t=>!e.has(t)));return o.length?o:t.slice(0,1)}const r=t=>void 0!==t,a=(t,e)=>t.x!==e.x?t.x-e.x:t.y-e.y,n=(t,e)=>t.reduce(((t,o)=>t+o.node[e].length+n(o.node[e],e)),0),i=t=>(e,o)=>n(e.node[t],t)-n(o.node[t],t)||e.node[t].length-o.node[t].length;function s(t,e){return t.from.sort(i("from")).forEach((t=>{const o=t.node;r(o.y)||(o.y=e,s(o,e)),e=Math.max(o.y+o.out,e)})),e}function l(t,e){return t.to.sort(i("to")).forEach((t=>{const o=t.node;r(o.y)||(o.y=e,l(o,e)),e=Math.max(o.y+o.in,e)})),e}function c(t,e){return r(t.y)?t.y:(t.y=e,e)}function h(t,e){const o=(t=>t.sort(((t,e)=>Math.max(e.in,e.out)-Math.max(t.in,t.out)))[0])(t);o.y=0;const a=s(o,0),n=l(o,0),i=function(t,e){const o=t.filter((t=>0===t.x)),a=t.filter((t=>t.x===e)),n=o.filter((t=>!r(t.y))),i=a.filter((t=>!r(t.y))),h=t.filter((t=>t.x>0&&t.x<e&&!r(t.y)));let d=o.reduce(((t,e)=>Math.max(t,e.y+e.out||0)),0),f=a.reduce(((t,e)=>Math.max(t,e.y+e.in||0)),0),u=0;return d>=f?(n.forEach((t=>{d=c(t,d),d=Math.max(d+t.out,l(t,d))})),i.forEach((t=>{f=c(t,f),f=Math.max(f+t.in,l(t,f))}))):(i.forEach((t=>{f=c(t,f),f=Math.max(f+t.in,l(t,f))})),n.forEach((t=>{d=c(t,d),d=Math.max(d+t.out,l(t,d))}))),h.forEach((e=>{let o=t.filter((t=>t.x===e.x&&r(t.y))).reduce(((t,e)=>Math.max(t,e.y+Math.max(e.in,e.out))),0);o=c(e,o),o=Math.max(o+e.in,s(e,o)),o=Math.max(o+e.out,l(e,o)),u=Math.max(u,o)})),Math.max(d,f,u)}(t,e);return Math.max(a,n,i)}function d(t,e,r,n){const i=[...t.values()],s=function(t,e){const r=new Set(e.map((t=>t.to))),a=new Set(e.map((t=>t.from))),n=new Set([...t.keys()]);let i=0;for(;n.size;){const a=o([...n],r);for(let e=0;e<a.length;e++)t.get(a[e]).x=i,n.delete(a[e]);n.size&&(r.clear(),e.filter((t=>n.has(t.from))).forEach((t=>r.add(t.to))),i++)}return[...t.keys()].filter((t=>!a.has(t))).forEach((e=>{t.get(e).x=i})),i}(t,e),l=r?function(t,e){let o=0,r=0;for(let a=0;a<=e;a++){let e=r;const n=t.filter((t=>t.x===a)).sort(((t,e)=>t.priority-e.priority));r=n[0].to.filter((t=>t.node.x>a+1)).reduce(((t,e)=>t+e.flow),0)||0;for(const t of n)t.y=e,e+=Math.max(t.out,t.in);o=Math.max(e,o)}return o}(i,s):h(i,s),c=function(t,e){let o=1,r=0,n=0,i=0;const s=[];return t.sort(a).forEach((t=>{if(t.y){if(0===t.x)s.push(t.y);else{for(r!==t.x&&(r=t.x,n=0),o=n+1;o<s.length&&!(s[o]>t.y);o++);n=o}t.y+=o*e,o++}i=Math.max(i,t.y+Math.max(t.in,t.out))})),i}(i,.03*l);return function(t,e){t.forEach((t=>{const o=Math[e](t.in||t.out,t.out||t.in),r=o<t.in,a=o<t.out;let n=0,i=t.from.length;t.from.sort(((t,e)=>t.node.y+t.node.out/2-(e.node.y+e.node.out/2))).forEach(((t,e)=>{r?t.addY=e*(o-t.flow)/(i-1):(t.addY=n,n+=t.flow)})),n=0,i=t.to.length,t.to.sort(((t,e)=>t.node.y+t.node.in/2-(e.node.y+e.node.in/2))).forEach(((t,e)=>{a?t.addY=e*(o-t.flow)/(i-1):(t.addY=n,n+=t.flow)}))}))}(i,n),{maxX:s,maxY:c}}function f(t,e,o){for(const r of t)if(r.key===e&&r.index===o)return r.addY;return 0}function u(t){return t&&-1!==["min","max"].indexOf(t)?t:"max"}class x extends t.DatasetController{parseObjectData(t,e,o,r){if(0===r)return[];const a=this,{xScale:n,yScale:i}=t,s=[],l=a._nodes=function(t){const e=new Map;for(let o=0;o<t.length;o++){const{from:r,to:a,flow:n}=t[o];if(e.has(r)){const t=e.get(r);t.out+=n,t.to.push({key:a,flow:n,index:o})}else e.set(r,{key:r,in:0,out:n,from:[],to:[{key:a,flow:n,index:o}]});if(e.has(a)){const t=e.get(a);t.in+=n,t.from.push({key:r,flow:n,index:o})}else e.set(a,{key:a,in:n,out:0,from:[{key:r,flow:n,index:o}],to:[]})}const o=(t,e)=>e.flow-t.flow;return[...e.values()].forEach((t=>{t.from=t.from.sort(o),t.from.forEach((t=>{t.node=e.get(t.key)})),t.to=t.to.sort(o),t.to.forEach((t=>{t.node=e.get(t.key)}))})),e}(e),{priority:c,size:h}=a.getDataset();if(c)for(const t of l.values())t.key in c&&(t.priority=c[t.key]);const{maxX:x,maxY:y}=d(l,e,!!c,u(h));a._maxX=x,a._maxY=y;for(let t=0,o=e.length;t<o;++t){const o=e[t],r=l.get(o.from),a=l.get(o.to),c=r.y+f(r.to,o.to,t),h=a.y+f(a.from,o.from,t);s.push({x:n.parse(r.x,t),y:i.parse(c,t),_custom:{from:r,to:a,x:n.parse(a.x,t),y:i.parse(h,t),height:i.parse(o.flow,t)}})}return s.slice(o,o+r)}getMinMax(t){return{min:0,max:t===this._cachedMeta.xScale?this._maxX:this._maxY}}update(t){const e=this._cachedMeta;this.updateElements(e.data,0,e.data.length,t)}updateElements(t,o,r,a){const n=this,{xScale:i,yScale:s}=n._cachedMeta,l=n.resolveDataElementOptions(o,a),c=n.getSharedOptions(a,t[o],l),h=n.getDataset(),d=e.valueOrDefault(h.borderWidth,1)/2+.5,f=e.valueOrDefault(h.nodeWidth,10);for(let e=o;e<o+r;e++){const o=n.getParsed(e),r=o._custom,l=s.getPixelForValue(o.y);n.updateElement(t[e],e,{x:i.getPixelForValue(o.x)+f+d,y:l,x2:i.getPixelForValue(r.x)-d,y2:s.getPixelForValue(r.y),from:r.from,to:r.to,progress:"reset"===a?0:1,height:Math.abs(s.getPixelForValue(o.y+r.height)-l),options:n.resolveDataElementOptions(e,a)},a)}n.updateSharedOptions(c,a)}_drawLabels(){const t=this,o=t._ctx,r=t._nodes||new Map,a=t.getDataset(),n=u(a.size),i=e.valueOrDefault(a.borderWidth,1),s=e.valueOrDefault(a.nodeWidth,10),l=a.labels,{xScale:c,yScale:h}=t._cachedMeta;o.save();const d=t.chart.chartArea;for(const t of r.values()){const e=c.getPixelForValue(t.x),r=h.getPixelForValue(t.y),f=Math[n](t.in||t.out,t.out||t.in),u=Math.abs(h.getPixelForValue(t.y+f)-r),x=l&&l[t.key]||t.key;let y=e;o.fillStyle=a.color||"black",o.textBaseline="middle",e<d.width/2?(o.textAlign="left",y+=s+i+4):(o.textAlign="right",y-=i+4),this._drawLabel(x,r,u,o,y)}o.restore()}_drawLabel(t,o,r,a,n){const i=this,s=e.toFont(i.options.font,i.chart.options.font),l=e.isNullOrUndef(t)?[]:i.toTextLines(t),c=l.length,h=o+r/2,d=s.lineHeight,f=e.valueOrDefault(i.options.padding,d/2);if(a.font=s.string,c>1){const t=h-d*c/2+f;for(let e=0;e<c;e++)a.fillText(l[e],n,t+e*d)}else a.fillText(t,n,h)}toTextLines(t){let o,r=[];for(t=[].concat(t);t.length;)o=t.pop(),"string"==typeof o?r.unshift.apply(r,o.split("\n")):Array.isArray(o)?t.push.apply(t,o):e.isNullOrUndef(t)||r.unshift(""+o);return r}_drawNodes(){const t=this,o=t._ctx,r=t._nodes||new Map,a=t.getDataset(),n=u(a.size),{xScale:i,yScale:s}=t._cachedMeta,l=e.valueOrDefault(a.borderWidth,1),c=e.valueOrDefault(a.nodeWidth,10);o.save(),o.strokeStyle=a.borderColor||"black",o.lineWidth=l;for(const t of r.values()){o.fillStyle=t.color;const e=i.getPixelForValue(t.x),r=s.getPixelForValue(t.y),a=Math[n](t.in||t.out,t.out||t.in),h=Math.abs(s.getPixelForValue(t.y+a)-r);l&&o.strokeRect(e,r,c,h),o.fillRect(e,r,c,h)}o.restore()}draw(){const t=this,e=t._ctx,o=t.getMeta().data||[];for(let t=0,e=o.length;t<e;++t){const e=o[t];e.from.color=e.options.colorFrom,e.to.color=e.options.colorTo}t._drawNodes();for(let t=0,r=o.length;t<r;++t)o[t].draw(e);t._drawLabels()}}x.id="sankey",x.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"]}},transitions:{hide:{animations:{colors:{type:"color",properties:["colorFrom","colorTo"],to:"transparent"}}},show:{animations:{colors:{type:"color",properties:["colorFrom","colorTo"],from:"transparent"}}}}},x.overrides={interaction:{mode:"nearest",intersect:!0},datasets:{color:()=>"#efefef",clip:!1,parsing:!0},plugins:{tooltip:{callbacks:{title:()=>"",label(t){const e=t.dataset.data[t.dataIndex];return e.from+" -> "+e.to+": "+e.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 y=(t,e,o,r)=>t<o?{cp1:{x:t+(o-t)/3*2,y:e},cp2:{x:t+(o-t)/3,y:r}}:{cp1:{x:t-(t-o)/3,y:0},cp2:{x:o+(t-o)/3,y:0}},p=(t,e,o)=>({x:t.x+o*(e.x-t.x),y:t.y+o*(e.y-t.y)});class g extends t.Element{constructor(t){super(),this.options=void 0,this.x=void 0,this.y=void 0,this.x2=void 0,this.y2=void 0,this.height=void 0,t&&Object.assign(this,t)}draw(t){const{x:o,x2:r,y:a,y2:n,height:i,progress:s}=this,{cp1:l,cp2:c}=y(o,a,r,n),h=this.options;if(0===s)return;let d;t.save(),s<1&&(t.beginPath(),t.rect(o,Math.min(a,n),(r-o)*s+1,Math.abs(n-a)+i+1),t.clip()),"from"===h.colorMode?d=e.color(h.colorFrom).alpha(.5).rgbString():"to"===h.colorMode?d=e.color(h.colorTo).alpha(.5).rgbString():(d=t.createLinearGradient(o,0,r,0),d.addColorStop(0,e.color(h.colorFrom).alpha(.5).rgbString()),d.addColorStop(1,e.color(h.colorTo).alpha(.5).rgbString())),t.fillStyle=d,t.strokeStyle=d,t.lineWidth=.5,t.beginPath(),t.moveTo(o,a),t.bezierCurveTo(l.x,l.y,c.x,c.y,r,n),t.lineTo(r,n+i),t.bezierCurveTo(c.x,c.y+i,l.x,l.y+i,o,a+i),t.lineTo(o,a),t.stroke(),t.closePath(),t.fill(),t.restore()}inRange(t,e,o){const{x:r,y:a,x2:n,y2:i,height:s}=this.getProps(["x","y","x2","y2","height"],o);if(t<r||t>n)return!1;const{cp1:l,cp2:c}=y(r,a,n,i),h=(t-r)/(n-r),d={x:n,y:i},f=p({x:r,y:a},l,h),u=p(l,c,h),x=p(c,d,h),g=p(f,u,h),m=p(u,x,h),M=p(g,m,h).y;return e>=M&&e<=M+s}inXRange(t,e){const{x:o,x2:r}=this.getProps(["x","x2"],e);return t>=o&&t<=r}inYRange(t,e){const{y:o,y2:r,height:a}=this.getProps(["y","y2","height"],e),n=Math.min(o,r),i=Math.max(o,r)+a;return t>=n&&t<=i}getCenterPoint(t){const{x:e,y:o,x2:r,y2:a,height:n}=this.getProps(["x","y","x2","y2","height"],t);return{x:(e+r)/2,y:(o+a+n)/2}}tooltipPosition(t){return this.getCenterPoint(t)}getRange(t){return"x"===t?this.width/2:this.height/2}}g.id="flow",g.defaults={colorFrom:"red",colorTo:"green",colorMode:"gradient"},t.Chart.register(x,g)}));
package/package.json CHANGED
@@ -1,14 +1,19 @@
1
1
  {
2
2
  "name": "chartjs-chart-sankey",
3
- "version": "0.6.0",
3
+ "version": "0.8.1",
4
4
  "description": "Chart.js module for creating sankey diagrams",
5
5
  "main": "dist/chartjs-chart-sankey.js",
6
6
  "module": "dist/chartjs-chart-sankey.esm.js",
7
+ "types": "types/index.esm.d.ts",
7
8
  "scripts": {
8
- "lint": "eslint --ignore-path .gitignore .",
9
9
  "build": "rollup -c",
10
10
  "autobuild": "rollup -c -w",
11
11
  "dev": "karma start --no-single-run --auto-watch --browsers chrome",
12
+ "dev:ff": "karma start --no-single-run --auto-watch --browsers firefox",
13
+ "lint": "concurrently -r \"npm:lint-*\"",
14
+ "lint-js": "eslint \"src/**/*.js\" \"test/**/*.js\"",
15
+ "lint-md": "eslint \"**/*.md\"",
16
+ "lint-types": "eslint \"types/**/*.ts\" && tsc -p types/tests/",
12
17
  "test": "cross-env NODE_ENV=test concurrently \"npm:test-*\"",
13
18
  "test-lint": "npm run lint",
14
19
  "test-karma": "karma start --auto-watch --single-run"
@@ -23,7 +28,8 @@
23
28
  "sankey"
24
29
  ],
25
30
  "files": [
26
- "dist/*.js"
31
+ "dist/*.js",
32
+ "types/index.esm.d.ts"
27
33
  ],
28
34
  "author": "Jukka Kurkela",
29
35
  "license": "MIT",
@@ -33,6 +39,8 @@
33
39
  "homepage": "https://github.com/kurkle/chartjs-chart-sankey#readme",
34
40
  "devDependencies": {
35
41
  "@rollup/plugin-node-resolve": "^13.0.0",
42
+ "@typescript-eslint/eslint-plugin": "^4.26.1",
43
+ "@typescript-eslint/parser": "^4.26.1",
36
44
  "chart.js": "^3.0.0",
37
45
  "chartjs-adapter-date-fns": "^2.0.0",
38
46
  "chartjs-test-utils": "^0.3.0",
@@ -43,6 +51,7 @@
43
51
  "eslint-config-chartjs": "^0.3.0",
44
52
  "eslint-plugin-es": "^4.1.0",
45
53
  "eslint-plugin-html": "^6.1.2",
54
+ "eslint-plugin-markdown": "^2.2.0",
46
55
  "jasmine-core": "^3.7.1",
47
56
  "karma": "^6.2.0",
48
57
  "karma-chrome-launcher": "^3.1.0",
@@ -52,10 +61,10 @@
52
61
  "karma-jasmine-html-reporter": "^1.5.4",
53
62
  "karma-rollup-preprocessor": "^7.0.7",
54
63
  "karma-spec-reporter": "0.0.32",
55
- "merge2": "^1.4.1",
56
64
  "rollup": "^2.42.1",
57
65
  "rollup-plugin-istanbul": "^3.0.0",
58
- "rollup-plugin-terser": "^7.0.2"
66
+ "rollup-plugin-terser": "^7.0.2",
67
+ "typescript": "~4.3"
59
68
  },
60
69
  "peerDependencies": {
61
70
  "chart.js": "^3.0.0"
@@ -0,0 +1,111 @@
1
+ import {
2
+ Chart,
3
+ ChartComponent,
4
+ DatasetController,
5
+ Element,
6
+ FontSpec
7
+ } from 'chart.js';
8
+
9
+ declare module 'chart.js' {
10
+
11
+ /* raw data element */
12
+ interface SankeyDataPoint {
13
+ from: string;
14
+ to: string;
15
+ flow: number;
16
+ }
17
+
18
+ /* dataset configuration */
19
+ interface SankeyControllerDatasetOptions {
20
+ label: string;
21
+ data: Array<SankeyDataPoint>;
22
+ colorFrom: (data: ScriptableContext<'sankey'>) => string;
23
+ colorTo: (data: ScriptableContext<'sankey'>) => string;
24
+ colorMode: 'gradient' | 'from' | 'to';
25
+ /* Map<node.key, priority_value> */
26
+ priority?: Record<string, number>
27
+ /* Map<node.key, label> */
28
+ labels?: Record<string, string>
29
+
30
+ size?: 'min' | 'max' /* defaults to max */
31
+ borderWidth?: number /* defaults to 1 */
32
+ nodeWidth?: number /* defaults to 10 */
33
+ color?: string /* defaults to 'black' */
34
+ borderColor?: string /* defaults to 'black' */
35
+ font?: FontSpec /* defaults to chart.options.font */
36
+ padding?: number /* defaults to font.lineHeight / 2 */
37
+ }
38
+
39
+ type FromToElement = {
40
+ addY: number
41
+ flow: number
42
+ key: string
43
+ node: SankeyNode
44
+ }
45
+
46
+ type SankeyNode = {
47
+ /* unique key of a node */
48
+ key: string
49
+ /* number of => in-connections */
50
+ in: number
51
+ /* number of out => connections */
52
+ out: number
53
+ from: Array<FromToElement>
54
+ to: Array<FromToElement>
55
+ /* priority extracted from the SankeyControllerDatasetOptions.priority map */
56
+ priority?: number
57
+ y?: number
58
+ x?: number
59
+ }
60
+
61
+ interface SankeyParsedData {
62
+ x: number
63
+ y: number
64
+ _custom: {
65
+ from: SankeyNode,
66
+ to: SankeyNode
67
+ x: number
68
+ y: number
69
+ height: number,
70
+ }
71
+ }
72
+
73
+ interface ChartTypeRegistry {
74
+ sankey: {
75
+ datasetOptions: SankeyControllerDatasetOptions;
76
+ defaultDataPoint: SankeyDataPoint;
77
+ parsedDataType: SankeyParsedData;
78
+ metaExtensions:{}
79
+ /* TODO: define sankey chart options */
80
+ chartOptions: any;
81
+ scales: keyof CartesianScaleTypeRegistry;
82
+ };
83
+ }
84
+ }
85
+
86
+ type FlowOptions = {
87
+ colorMode: 'gradient' | 'from' | 'to';
88
+ colorFrom: string
89
+ colorTo: string
90
+ }
91
+
92
+ type FlowConfig = {
93
+ x: number;
94
+ y: number;
95
+ x2: number;
96
+ y2: number;
97
+ height: number;
98
+ options: FlowOptions
99
+ };
100
+
101
+ export type SankeyController = DatasetController
102
+ export const SankeyController: ChartComponent & {
103
+ prototype: SankeyController;
104
+ new(chart: Chart, datasetIndex: number): SankeyController;
105
+ };
106
+
107
+ export type Flow = Element<FlowConfig, FlowOptions>
108
+ export const Flow: ChartComponent & {
109
+ prototype: Flow;
110
+ new(cfg: FlowConfig): Flow;
111
+ };