chartjs-chart-treemap
Advanced tools
Comparing version 1.0.0-beta.2 to 1.0.0-beta.3
/*! | ||
* chartjs-chart-treemap v1.0.0-beta.2 | ||
* chartjs-chart-treemap v1.0.0-beta.3 | ||
* https://github.com/kurkle/chartjs-chart-treemap#readme | ||
@@ -8,3 +8,3 @@ * (c) 2020 Jukka Kurkela | ||
import { DatasetController, Element } from 'chart.js'; | ||
import { toFont } from 'chart.js/helpers'; | ||
import { toFont, valueOrDefault } from 'chart.js/helpers'; | ||
@@ -113,3 +113,3 @@ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat | ||
function round(v, n) { | ||
return +(Math.round(v + 'e+' + n) + 'e-' + n); | ||
return (+(Math.round(v + 'e+' + n) + 'e-' + n)) || 0; | ||
} | ||
@@ -128,5 +128,7 @@ | ||
const getX = (rect, w) => round(rect.rtl ? rect.x + rect.w - rect._ix - w : rect.x + rect._ix, 4); | ||
function buildRow(rect, itm, dims, sum) { | ||
const r = { | ||
x: round(rect.x + rect._ix, 4), | ||
x: getX(rect, dims.w), | ||
y: round(rect.y + rect._iy, 4), | ||
@@ -147,2 +149,3 @@ w: round(dims.w, 4), | ||
} | ||
class Rect { | ||
@@ -152,2 +155,3 @@ constructor(r) { | ||
r = r || {w: 1, h: 1}; | ||
me.rtl = !!r.rtl; | ||
me.x = r.x || r.left || 0; | ||
@@ -187,3 +191,2 @@ me.y = r.y || r.top || 0; | ||
const row = arr.get(); | ||
const n = row.length; | ||
const dir = me.dir; | ||
@@ -196,10 +199,6 @@ const side = me.side; | ||
let totd1 = 0; | ||
let i, itm, dims; | ||
for (i = 0; i < n; ++i) { | ||
itm = row[i]; | ||
dims = getDims(itm, w2, s2, key); | ||
for (const itm of row) { | ||
const dims = getDims(itm, w2, s2, key); | ||
totd1 += dims.d1; | ||
if (dims.d2 > maxd2) { | ||
maxd2 = dims.d2; | ||
} | ||
maxd2 = Math.max(maxd2, dims.d2); | ||
ret.push(buildRow(me, itm, dims, arr.sum)); | ||
@@ -401,5 +400,6 @@ me[key] += dims.d1; | ||
} else if (opts.groupLabels) { | ||
ctx.textAlign = 'start'; | ||
ctx.textAlign = opts.rtl ? 'end' : 'start'; | ||
ctx.textBaseline = 'top'; | ||
ctx.fillText(item.g, rect.x + opts.borderWidth + 3, rect.y + opts.borderWidth + 3); | ||
const x = opts.rtl ? rect.x + rect.width - opts.borderWidth - 3 : rect.x + opts.borderWidth + 3; | ||
ctx.fillText(item.g, x, rect.y + opts.borderWidth + 3); | ||
} | ||
@@ -452,3 +452,3 @@ ctx.restore(); | ||
if (dataset.groupLabels && shouldDrawCaption(sq, font)) { | ||
if (valueOrDefault(dataset.groupLabels, true) && shouldDrawCaption(sq, font)) { | ||
subRect.y += font.lineHeight; | ||
@@ -494,4 +494,5 @@ subRect.h -= font.lineHeight; | ||
const key = dataset.key || ''; | ||
const rtl = !!dataset.rtl; | ||
const mainRect = {x: area.left, y: area.top, w: area.right - area.left, h: area.bottom - area.top}; | ||
const mainRect = {x: area.left, y: area.top, w: area.right - area.left, h: area.bottom - area.top, rtl}; | ||
@@ -605,3 +606,4 @@ if (mode === 'reset' || rectNotEqual(me._rect, mainRect) || me._key !== key || arrayNotEqual(me._groups, groups)) { | ||
'spacing', | ||
'label' | ||
'label', | ||
'rtl' | ||
], | ||
@@ -608,0 +610,0 @@ hover: { |
/*! | ||
* chartjs-chart-treemap v1.0.0-beta.2 | ||
* chartjs-chart-treemap v1.0.0-beta.3 | ||
* https://github.com/kurkle/chartjs-chart-treemap#readme | ||
@@ -115,3 +115,3 @@ * (c) 2020 Jukka Kurkela | ||
function round(v, n) { | ||
return +(Math.round(v + 'e+' + n) + 'e-' + n); | ||
return (+(Math.round(v + 'e+' + n) + 'e-' + n)) || 0; | ||
} | ||
@@ -130,5 +130,7 @@ | ||
const getX = (rect, w) => round(rect.rtl ? rect.x + rect.w - rect._ix - w : rect.x + rect._ix, 4); | ||
function buildRow(rect, itm, dims, sum) { | ||
const r = { | ||
x: round(rect.x + rect._ix, 4), | ||
x: getX(rect, dims.w), | ||
y: round(rect.y + rect._iy, 4), | ||
@@ -149,2 +151,3 @@ w: round(dims.w, 4), | ||
} | ||
class Rect { | ||
@@ -154,2 +157,3 @@ constructor(r) { | ||
r = r || {w: 1, h: 1}; | ||
me.rtl = !!r.rtl; | ||
me.x = r.x || r.left || 0; | ||
@@ -189,3 +193,2 @@ me.y = r.y || r.top || 0; | ||
const row = arr.get(); | ||
const n = row.length; | ||
const dir = me.dir; | ||
@@ -198,10 +201,6 @@ const side = me.side; | ||
let totd1 = 0; | ||
let i, itm, dims; | ||
for (i = 0; i < n; ++i) { | ||
itm = row[i]; | ||
dims = getDims(itm, w2, s2, key); | ||
for (const itm of row) { | ||
const dims = getDims(itm, w2, s2, key); | ||
totd1 += dims.d1; | ||
if (dims.d2 > maxd2) { | ||
maxd2 = dims.d2; | ||
} | ||
maxd2 = Math.max(maxd2, dims.d2); | ||
ret.push(buildRow(me, itm, dims, arr.sum)); | ||
@@ -403,5 +402,6 @@ me[key] += dims.d1; | ||
} else if (opts.groupLabels) { | ||
ctx.textAlign = 'start'; | ||
ctx.textAlign = opts.rtl ? 'end' : 'start'; | ||
ctx.textBaseline = 'top'; | ||
ctx.fillText(item.g, rect.x + opts.borderWidth + 3, rect.y + opts.borderWidth + 3); | ||
const x = opts.rtl ? rect.x + rect.width - opts.borderWidth - 3 : rect.x + opts.borderWidth + 3; | ||
ctx.fillText(item.g, x, rect.y + opts.borderWidth + 3); | ||
} | ||
@@ -454,3 +454,3 @@ ctx.restore(); | ||
if (dataset.groupLabels && shouldDrawCaption(sq, font)) { | ||
if (helpers.valueOrDefault(dataset.groupLabels, true) && shouldDrawCaption(sq, font)) { | ||
subRect.y += font.lineHeight; | ||
@@ -496,4 +496,5 @@ subRect.h -= font.lineHeight; | ||
const key = dataset.key || ''; | ||
const rtl = !!dataset.rtl; | ||
const mainRect = {x: area.left, y: area.top, w: area.right - area.left, h: area.bottom - area.top}; | ||
const mainRect = {x: area.left, y: area.top, w: area.right - area.left, h: area.bottom - area.top, rtl}; | ||
@@ -607,3 +608,4 @@ if (mode === 'reset' || rectNotEqual(me._rect, mainRect) || me._key !== key || arrayNotEqual(me._groups, groups)) { | ||
'spacing', | ||
'label' | ||
'label', | ||
'rtl' | ||
], | ||
@@ -610,0 +612,0 @@ hover: { |
/*! | ||
* chartjs-chart-treemap v1.0.0-beta.2 | ||
* chartjs-chart-treemap v1.0.0-beta.3 | ||
* https://github.com/kurkle/chartjs-chart-treemap#readme | ||
@@ -7,2 +7,2 @@ * (c) 2020 Jukka Kurkela | ||
*/ | ||
!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 n(t){const e=typeof t;return"function"===e||"object"===e&&!!t}function i(t,e){return+(Math.round(t+"e+"+e)+"e-"+e)}function r(t,e,n,i){const r=t._normalized,o=e*r/n,s=Math.sqrt(r*o),a=r/s;return{d1:s,d2:a,w:"_ix"===i?s:a,h:"_ix"===i?a:s}}function o(t,e,n,r){const o={x:i(t.x+t._ix,4),y:i(t.y+t._iy,4),w:i(n.w,4),h:i(n.h,4),a:i(e._normalized,4),v:e.value,s:r,_data:e._data};return e.group&&(o.g=e.group,o.l=e.level,o.gs=e.groupSum),o}class s{constructor(t){const e=this;t=t||{w:1,h:1},e.x=t.x||t.left||0,e.y=t.y||t.top||0,e._ix=0,e._iy=0,e.w=t.w||t.width||t.right-t.left,e.h=t.h||t.height||t.bottom-t.top}get area(){return this.w*this.h}get iw(){return this.w-this._ix}get ih(){return this.h-this._iy}get dir(){const t=this.ih;return t<=this.iw&&t>0?"y":"x"}get side(){return"x"===this.dir?this.iw:this.ih}map(t){const e=this,n=[],i=t.nsum,s=t.get(),a=s.length,h=e.dir,l=e.side,u=l*l,d="x"===h?"_ix":"_iy",c=i*i;let g,p,f,m=0,x=0;for(g=0;g<a;++g)p=s[g],f=r(p,u,c,d),x+=f.d1,f.d2>m&&(m=f.d2),n.push(o(e,p,f,t.sum)),e[d]+=f.d1;return e["y"===h?"_ix":"_iy"]+=m,e[d]-=x,n}}const a=Math.min,h=Math.max;function l(t,e){const n=+e[t.key],i=n*t.ratio;return e._normalized=i,{min:a(t.min,n),max:h(t.max,n),sum:t.sum+n,nmin:a(t.nmin,i),nmax:h(t.nmax,i),nsum:t.nsum+i}}function u(t,e,n){t._arr.push(e),function(t,e){Object.assign(t,e)}(t,n)}class d{constructor(t,e){const n=this;n.key=t,n.ratio=e,n.reset()}get length(){return this._arr.length}reset(){const t=this;t._arr=[],t._hist=[],t.sum=0,t.nsum=0,t.min=1/0,t.max=-1/0,t.nmin=1/0,t.nmax=-1/0}push(t){u(this,t,l(this,t))}pushIf(t,e,...n){const i=l(this,t);if(!e((r=this,{min:r.min,max:r.max,sum:r.sum,nmin:r.nmin,nmax:r.nmax,nsum:r.nsum}),i,n))return t;var r;u(this,t,i)}get(){return this._arr}}function c(t,e,n){if(0===t.sum)return!0;const[i]=n,r=t.nsum*t.nsum,o=e.nsum*e.nsum,s=i*i,a=Math.max(s*t.nmax/r,r/(s*t.nmin));return Math.max(s*e.nmax/o,o/(s*e.nmin))<=a}function g(t,e,i,r,o,a){t=t||[];const h=[],l=new s(e),u=new d("value",l.area/function(t,e){let n,i,r;for(n=0,i=0,r=t.length;i<r;++i)n+=e?+t[i][e]:+t[i];return n}(t,i));let g=l.side;const p=t.length;let f,m;if(!p)return h;const x=t.slice();i=function(t,e){let i,r=t.length;if(!r)return e;const o=n(t[0]);for(e=o?e:"v",i=0,r=t.length;i<r;++i)o?t[i]._idx=i:t[i]={v:t[i],_idx:i};return e}(x,i),function(t,e){e?t.sort(((t,n)=>+n[e]-+t[e])):t.sort(((t,e)=>+e-+t))}(x,i);const y=t=>r&&x[t][r];for(f=0;f<p;++f)m={value:(v=f,i?+x[v][i]:+x[v]),groupSum:a,_data:t[x[f]._idx]},r&&(m.level=o,m.group=y(f)),m=u.pushIf(m,c,g),m&&(h.push(l.map(u)),g=l.side,u.reset(),u.push(m));var v;return u.length&&h.push(l.map(u)),function(t){const e=[...t],n=[];for(;e.length;){const t=e.pop();Array.isArray(t)?e.push(...t):n.push(t)}return n.reverse()}(h)}function p(t,e){if(!e)return!1;const n=t.width||t.w,i=t.height||t.h,r=2*e.lineHeight;return n>r&&i>r}function f(t,e,n,i,r){t.save(),t.fillStyle=i.font.color,t.font=i.font.string,t.beginPath(),t.rect(e.x,e.y,e.width,e.height),t.clip(),"l"in n&&n.l!==r?i.groupLabels&&(t.textAlign="start",t.textBaseline="top",t.fillText(n.g,e.x+i.borderWidth+3,e.y+i.borderWidth+3)):(t.textAlign="center",t.textBaseline="middle",function(t,e,n){const i=n.options,r=i.font.lineHeight,o=(i.label||e.g+"\n"+e.v).split("\n"),s=n.y+n.height/2-o.length*r/4;o.forEach(((e,i)=>t.fillText(e,n.x+n.width/2,s+i*r)))}(t,n,e)),t.restore()}function m(t,e){const n=e.options,i=e.width||e.w,r=e.height||e.h;if(t.save(),t.strokeStyle=n.dividerColor||"black",t.lineCap=n.dividerCapStyle,t.setLineDash(n.dividerDash||[]),t.lineDashOffset=n.dividerDashOffset,t.lineWidth=n.dividerWidth,t.beginPath(),i>r){const n=i/2;t.moveTo(e.x+n,e.y),t.lineTo(e.x+n,e.y+r)}else{const n=r/2;t.moveTo(e.x,e.y+n),t.lineTo(e.x+i,e.y+n)}t.stroke(),t.restore()}function x(t,e,n){const i=t.key||"";let r=t.tree||[];const o=t.groups||[],s=o.length,a=(t.spacing||0)+(t.borderWidth||0);return!r.length&&t.data.length&&(r=t.tree=t.data),s?function e(h,l,u,d){const c=o[h],f=h>0&&o[h-1],m=g(function(t,e,n,i,r){const o=Object.create(null),s=Object.create(null),a=[];let h,l,u,d;for(l=0,u=t.length;l<u;++l)d=t[l],i&&d[i]!==r||(h=d[e]||"",h in o||(o[h]=0,s[h]=[]),o[h]+=+d[n],s[h].push(d));return Object.keys(o).forEach((t=>{d={children:s[t]},d[n]=+o[t],d[e]=t,i&&(d[i]=r),a.push(d)})),a}(r,c,i,f,u),l,i,c,h,d),x=m.slice();let y;return h<s-1&&m.forEach((i=>{y={x:i.x+a,y:i.y+a,w:i.w-2*a,h:i.h-2*a},t.groupLabels&&p(i,n)&&(y.y+=n.lineHeight,y.h-=n.lineHeight),x.push(...e(h+1,y,i.g,i.s))})),x}(0,e):g(r,e,i)}class y extends t.DatasetController{initialize(){this.enableOptionSharing=!0,super.initialize()}update(t){const n=this,i=n.getMeta(),r=n.getDataset(),o=r.groups||(r.groups=[]),s=e.toFont(r.font,n.chart.options.font),a=n.chart.chartArea,h=r.key||"",l={x:a.left,y:a.top,w:a.right-a.left,h:a.bottom-a.top};var u,d;"reset"!==t&&(u=n._rect,d=l,u&&d&&u.x===d.x&&u.y===d.y&&u.w===d.w&&u.h===d.h)&&n._key===h&&!function(t,e){let n,i;if(t.lenght!==e.length)return!0;for(n=0,i=t.length;n<i;++n)if(t[n]!==e[n])return!0;return!1}(n._groups,o)||(n._rect=l,n._groups=o.slice(),n._key=h,r.data=x(r,l,s),n._dataCheck(),n._resyncElements()),n.updateElements(i.data,0,i.data.length,t)}resolveDataElementOptions(t,n){const i=super.resolveDataElementOptions(t,n),r=Object.isFrozen(i)?Object.assign({},i):i;return r.font=e.toFont(i.font,this.chart.options.font),r}updateElements(t,e,n,i){const r=this,o="reset"===i,s=r.getDataset(),a=r._rect.options=r.resolveDataElementOptions(e,i),h=r.getSharedOptions(a),l=r.includeOptions(i,h);for(let a=e;a<e+n;a++){const e=s.data[a],n=h||r.resolveDataElementOptions(a,i),u=o?0:e.h-2*n.spacing,d=o?0:e.w-2*n.spacing,c={x:e.x+n.spacing,y:e.y+n.spacing,width:d,height:u};l&&(c.options=n),r.updateElement(t[a],a,c,i)}r.updateSharedOptions(h,i,a)}_drawDividers(t,e,n){for(let i=0,r=n.length;i<r;++i){const r=n[i],o=e[i];r.options.groupDividers&&o._data.children.length>1&&m(t,r)}this.getDataset().groupDividers&&m(t,this._rect)}_drawRects(t,e,n,i){for(let r=0,o=n.length;r<o;++r){const o=n[r],s=e[r];if(!o.hidden){o.draw(t);const e=o.options;p(o,e.font)&&s.g&&f(t,o,s,e,i)}}}draw(){const t=this,e=t.chart.ctx,n=t.getMeta().data||[],i=t.getDataset(),r=(i.groups||[]).length-1,o=i.data||[];t._drawRects(e,o,n,r),t._drawDividers(e,o,n)}}function v(t,e){const{x:n,y:i,width:r,height:o}=t.getProps(["x","y","width","height"],e);return{left:n,top:i,right:n+r,bottom:i+o}}function b(t,e,n){return Math.max(Math.min(t,n),e)}function w(t){const e=v(t),i=e.right-e.left,r=e.bottom-e.top,o=function(t,e,i){let r,o,s,a;return n(t)?(r=+t.top||0,o=+t.right||0,s=+t.bottom||0,a=+t.left||0):r=o=s=a=+t||0,{t:b(r,0,i),r:b(o,0,e),b:b(s,0,i),l:b(a,0,e)}}(t.options.borderWidth,i/2,r/2);return{outer:{x:e.left,y:e.top,w:i,h:r},inner:{x:e.left+o.l,y:e.top+o.t,w:i-o.l-o.r,h:r-o.t-o.b}}}function _(t,e,n,i){const r=null===e,o=null===n,s=!(!t||r&&o)&&v(t,i);return s&&(r||e>=s.left&&e<=s.right)&&(o||n>=s.top&&n<=s.bottom)}y.id="treemap",y.defaults={dataElementType:"treemap",dataElementOptions:["backgroundColor","borderColor","borderSkipped","borderWidth","dividerColor","dividerDash","dividerDashOffset","dividerWidth","font","groupLabels","groupDividers","spacing","label"],hover:{mode:"point",intersect:!0},tooltips:{mode:"point",position:"treemap",intersect:!0,callbacks:{title(t){if(t.length){return t[0].dataset.key||""}return""},label(t){const e=t.dataset,n=e.data[t.dataIndex];return e.label+": "+n.v}}},datasets:{groupLabels:!0,borderWidth:0,spacing:.5,groupDividers:!1,dividerWidth:1},scales:{x:{type:"linear",display:!1},y:{type:"linear",display:!1}}};class D extends t.Element{constructor(t){super(),this.options=void 0,this.width=void 0,this.height=void 0,t&&Object.assign(this,t)}draw(t){const e=this.options,{inner:n,outer:i}=w(this);t.save(),i.w!==n.w||i.h!==n.h?(t.beginPath(),t.rect(i.x,i.y,i.w,i.h),t.clip(),t.rect(n.x,n.y,n.w,n.h),t.fillStyle=e.backgroundColor,t.fill(),t.fillStyle=e.borderColor,t.fill("evenodd")):(t.fillStyle=e.backgroundColor,t.fillRect(n.x,n.y,n.w,n.h)),t.restore()}inRange(t,e,n){return _(this,t,e,n)}inXRange(t,e){return _(this,t,null,e)}inYRange(t,e){return _(this,null,t,e)}getCenterPoint(t){const{x:e,y:n,width:i,height:r}=this.getProps(["x","y","width","height"],t);return{x:e+i/2,y:n+r/2}}tooltipPosition(){return this.getCenterPoint()}getRange(t){return"x"===t?this.width/2:this.height/2}}D.id="treemap",D.defaults={dividerCapStyle:"butt",dividerColor:"black",dividerDash:void 0,dividerDashOffset:0,dividerWidth:0,groupDividers:!1},D.defaultRoutes={backgroundColor:"color",borderColor:"color"},t.Chart.register(y,D);t.Chart.registry.plugins.get("tooltip").positioners.treemap=function(t){if(!t.length)return!1;return t[t.length-1].element.tooltipPosition()}})); | ||
!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 n(t){const e=typeof t;return"function"===e||"object"===e&&!!t}function i(t,e){return+(Math.round(t+"e+"+e)+"e-"+e)||0}function r(t,e,n,i){const r=t._normalized,o=e*r/n,s=Math.sqrt(r*o),a=r/s;return{d1:s,d2:a,w:"_ix"===i?s:a,h:"_ix"===i?a:s}}const o=(t,e)=>i(t.rtl?t.x+t.w-t._ix-e:t.x+t._ix,4);function s(t,e,n,r){const s={x:o(t,n.w),y:i(t.y+t._iy,4),w:i(n.w,4),h:i(n.h,4),a:i(e._normalized,4),v:e.value,s:r,_data:e._data};return e.group&&(s.g=e.group,s.l=e.level,s.gs=e.groupSum),s}class a{constructor(t){const e=this;t=t||{w:1,h:1},e.rtl=!!t.rtl,e.x=t.x||t.left||0,e.y=t.y||t.top||0,e._ix=0,e._iy=0,e.w=t.w||t.width||t.right-t.left,e.h=t.h||t.height||t.bottom-t.top}get area(){return this.w*this.h}get iw(){return this.w-this._ix}get ih(){return this.h-this._iy}get dir(){const t=this.ih;return t<=this.iw&&t>0?"y":"x"}get side(){return"x"===this.dir?this.iw:this.ih}map(t){const e=this,n=[],i=t.nsum,o=t.get(),a=e.dir,h=e.side,l=h*h,u="x"===a?"_ix":"_iy",d=i*i;let c=0,g=0;for(const i of o){const o=r(i,l,d,u);g+=o.d1,c=Math.max(c,o.d2),n.push(s(e,i,o,t.sum)),e[u]+=o.d1}return e["y"===a?"_ix":"_iy"]+=c,e[u]-=g,n}}const h=Math.min,l=Math.max;function u(t,e){const n=+e[t.key],i=n*t.ratio;return e._normalized=i,{min:h(t.min,n),max:l(t.max,n),sum:t.sum+n,nmin:h(t.nmin,i),nmax:l(t.nmax,i),nsum:t.nsum+i}}function d(t,e,n){t._arr.push(e),function(t,e){Object.assign(t,e)}(t,n)}class c{constructor(t,e){const n=this;n.key=t,n.ratio=e,n.reset()}get length(){return this._arr.length}reset(){const t=this;t._arr=[],t._hist=[],t.sum=0,t.nsum=0,t.min=1/0,t.max=-1/0,t.nmin=1/0,t.nmax=-1/0}push(t){d(this,t,u(this,t))}pushIf(t,e,...n){const i=u(this,t);if(!e((r=this,{min:r.min,max:r.max,sum:r.sum,nmin:r.nmin,nmax:r.nmax,nsum:r.nsum}),i,n))return t;var r;d(this,t,i)}get(){return this._arr}}function g(t,e,n){if(0===t.sum)return!0;const[i]=n,r=t.nsum*t.nsum,o=e.nsum*e.nsum,s=i*i,a=Math.max(s*t.nmax/r,r/(s*t.nmin));return Math.max(s*e.nmax/o,o/(s*e.nmin))<=a}function p(t,e,i,r,o,s){t=t||[];const h=[],l=new a(e),u=new c("value",l.area/function(t,e){let n,i,r;for(n=0,i=0,r=t.length;i<r;++i)n+=e?+t[i][e]:+t[i];return n}(t,i));let d=l.side;const p=t.length;let f,m;if(!p)return h;const x=t.slice();i=function(t,e){let i,r=t.length;if(!r)return e;const o=n(t[0]);for(e=o?e:"v",i=0,r=t.length;i<r;++i)o?t[i]._idx=i:t[i]={v:t[i],_idx:i};return e}(x,i),function(t,e){e?t.sort(((t,n)=>+n[e]-+t[e])):t.sort(((t,e)=>+e-+t))}(x,i);const y=t=>r&&x[t][r];for(f=0;f<p;++f)m={value:(v=f,i?+x[v][i]:+x[v]),groupSum:s,_data:t[x[f]._idx]},r&&(m.level=o,m.group=y(f)),m=u.pushIf(m,g,d),m&&(h.push(l.map(u)),d=l.side,u.reset(),u.push(m));var v;return u.length&&h.push(l.map(u)),function(t){const e=[...t],n=[];for(;e.length;){const t=e.pop();Array.isArray(t)?e.push(...t):n.push(t)}return n.reverse()}(h)}function f(t,e){if(!e)return!1;const n=t.width||t.w,i=t.height||t.h,r=2*e.lineHeight;return n>r&&i>r}function m(t,e,n,i,r){if(t.save(),t.fillStyle=i.font.color,t.font=i.font.string,t.beginPath(),t.rect(e.x,e.y,e.width,e.height),t.clip(),"l"in n&&n.l!==r){if(i.groupLabels){t.textAlign=i.rtl?"end":"start",t.textBaseline="top";const r=i.rtl?e.x+e.width-i.borderWidth-3:e.x+i.borderWidth+3;t.fillText(n.g,r,e.y+i.borderWidth+3)}}else t.textAlign="center",t.textBaseline="middle",function(t,e,n){const i=n.options,r=i.font.lineHeight,o=(i.label||e.g+"\n"+e.v).split("\n"),s=n.y+n.height/2-o.length*r/4;o.forEach(((e,i)=>t.fillText(e,n.x+n.width/2,s+i*r)))}(t,n,e);t.restore()}function x(t,e){const n=e.options,i=e.width||e.w,r=e.height||e.h;if(t.save(),t.strokeStyle=n.dividerColor||"black",t.lineCap=n.dividerCapStyle,t.setLineDash(n.dividerDash||[]),t.lineDashOffset=n.dividerDashOffset,t.lineWidth=n.dividerWidth,t.beginPath(),i>r){const n=i/2;t.moveTo(e.x+n,e.y),t.lineTo(e.x+n,e.y+r)}else{const n=r/2;t.moveTo(e.x,e.y+n),t.lineTo(e.x+i,e.y+n)}t.stroke(),t.restore()}function y(t,n,i){const r=t.key||"";let o=t.tree||[];const s=t.groups||[],a=s.length,h=(t.spacing||0)+(t.borderWidth||0);return!o.length&&t.data.length&&(o=t.tree=t.data),a?function n(l,u,d,c){const g=s[l],m=l>0&&s[l-1],x=p(function(t,e,n,i,r){const o=Object.create(null),s=Object.create(null),a=[];let h,l,u,d;for(l=0,u=t.length;l<u;++l)d=t[l],i&&d[i]!==r||(h=d[e]||"",h in o||(o[h]=0,s[h]=[]),o[h]+=+d[n],s[h].push(d));return Object.keys(o).forEach((t=>{d={children:s[t]},d[n]=+o[t],d[e]=t,i&&(d[i]=r),a.push(d)})),a}(o,g,r,m,d),u,r,g,l,c),y=x.slice();let v;return l<a-1&&x.forEach((r=>{v={x:r.x+h,y:r.y+h,w:r.w-2*h,h:r.h-2*h},e.valueOrDefault(t.groupLabels,!0)&&f(r,i)&&(v.y+=i.lineHeight,v.h-=i.lineHeight),y.push(...n(l+1,v,r.g,r.s))})),y}(0,n):p(o,n,r)}class v extends t.DatasetController{initialize(){this.enableOptionSharing=!0,super.initialize()}update(t){const n=this,i=n.getMeta(),r=n.getDataset(),o=r.groups||(r.groups=[]),s=e.toFont(r.font,n.chart.options.font),a=n.chart.chartArea,h=r.key||"",l=!!r.rtl,u={x:a.left,y:a.top,w:a.right-a.left,h:a.bottom-a.top,rtl:l};var d,c;"reset"!==t&&(d=n._rect,c=u,d&&c&&d.x===c.x&&d.y===c.y&&d.w===c.w&&d.h===c.h)&&n._key===h&&!function(t,e){let n,i;if(t.lenght!==e.length)return!0;for(n=0,i=t.length;n<i;++n)if(t[n]!==e[n])return!0;return!1}(n._groups,o)||(n._rect=u,n._groups=o.slice(),n._key=h,r.data=y(r,u,s),n._dataCheck(),n._resyncElements()),n.updateElements(i.data,0,i.data.length,t)}resolveDataElementOptions(t,n){const i=super.resolveDataElementOptions(t,n),r=Object.isFrozen(i)?Object.assign({},i):i;return r.font=e.toFont(i.font,this.chart.options.font),r}updateElements(t,e,n,i){const r=this,o="reset"===i,s=r.getDataset(),a=r._rect.options=r.resolveDataElementOptions(e,i),h=r.getSharedOptions(a),l=r.includeOptions(i,h);for(let a=e;a<e+n;a++){const e=s.data[a],n=h||r.resolveDataElementOptions(a,i),u=o?0:e.h-2*n.spacing,d=o?0:e.w-2*n.spacing,c={x:e.x+n.spacing,y:e.y+n.spacing,width:d,height:u};l&&(c.options=n),r.updateElement(t[a],a,c,i)}r.updateSharedOptions(h,i,a)}_drawDividers(t,e,n){for(let i=0,r=n.length;i<r;++i){const r=n[i],o=e[i];r.options.groupDividers&&o._data.children.length>1&&x(t,r)}this.getDataset().groupDividers&&x(t,this._rect)}_drawRects(t,e,n,i){for(let r=0,o=n.length;r<o;++r){const o=n[r],s=e[r];if(!o.hidden){o.draw(t);const e=o.options;f(o,e.font)&&s.g&&m(t,o,s,e,i)}}}draw(){const t=this,e=t.chart.ctx,n=t.getMeta().data||[],i=t.getDataset(),r=(i.groups||[]).length-1,o=i.data||[];t._drawRects(e,o,n,r),t._drawDividers(e,o,n)}}function b(t,e){const{x:n,y:i,width:r,height:o}=t.getProps(["x","y","width","height"],e);return{left:n,top:i,right:n+r,bottom:i+o}}function w(t,e,n){return Math.max(Math.min(t,n),e)}function _(t){const e=b(t),i=e.right-e.left,r=e.bottom-e.top,o=function(t,e,i){let r,o,s,a;return n(t)?(r=+t.top||0,o=+t.right||0,s=+t.bottom||0,a=+t.left||0):r=o=s=a=+t||0,{t:w(r,0,i),r:w(o,0,e),b:w(s,0,i),l:w(a,0,e)}}(t.options.borderWidth,i/2,r/2);return{outer:{x:e.left,y:e.top,w:i,h:r},inner:{x:e.left+o.l,y:e.top+o.t,w:i-o.l-o.r,h:r-o.t-o.b}}}function D(t,e,n,i){const r=null===e,o=null===n,s=!(!t||r&&o)&&b(t,i);return s&&(r||e>=s.left&&e<=s.right)&&(o||n>=s.top&&n<=s.bottom)}v.id="treemap",v.defaults={dataElementType:"treemap",dataElementOptions:["backgroundColor","borderColor","borderSkipped","borderWidth","dividerColor","dividerDash","dividerDashOffset","dividerWidth","font","groupLabels","groupDividers","spacing","label","rtl"],hover:{mode:"point",intersect:!0},tooltips:{mode:"point",position:"treemap",intersect:!0,callbacks:{title(t){if(t.length){return t[0].dataset.key||""}return""},label(t){const e=t.dataset,n=e.data[t.dataIndex];return e.label+": "+n.v}}},datasets:{groupLabels:!0,borderWidth:0,spacing:.5,groupDividers:!1,dividerWidth:1},scales:{x:{type:"linear",display:!1},y:{type:"linear",display:!1}}};class C extends t.Element{constructor(t){super(),this.options=void 0,this.width=void 0,this.height=void 0,t&&Object.assign(this,t)}draw(t){const e=this.options,{inner:n,outer:i}=_(this);t.save(),i.w!==n.w||i.h!==n.h?(t.beginPath(),t.rect(i.x,i.y,i.w,i.h),t.clip(),t.rect(n.x,n.y,n.w,n.h),t.fillStyle=e.backgroundColor,t.fill(),t.fillStyle=e.borderColor,t.fill("evenodd")):(t.fillStyle=e.backgroundColor,t.fillRect(n.x,n.y,n.w,n.h)),t.restore()}inRange(t,e,n){return D(this,t,e,n)}inXRange(t,e){return D(this,t,null,e)}inYRange(t,e){return D(this,null,t,e)}getCenterPoint(t){const{x:e,y:n,width:i,height:r}=this.getProps(["x","y","width","height"],t);return{x:e+i/2,y:n+r/2}}tooltipPosition(){return this.getCenterPoint()}getRange(t){return"x"===t?this.width/2:this.height/2}}C.id="treemap",C.defaults={dividerCapStyle:"butt",dividerColor:"black",dividerDash:void 0,dividerDashOffset:0,dividerWidth:0,groupDividers:!1},C.defaultRoutes={backgroundColor:"color",borderColor:"color"},t.Chart.register(v,C);t.Chart.registry.plugins.get("tooltip").positioners.treemap=function(t){if(!t.length)return!1;return t[t.length-1].element.tooltipPosition()}})); |
{ | ||
"name": "chartjs-chart-treemap", | ||
"version": "1.0.0-beta.2", | ||
"version": "1.0.0-beta.3", | ||
"description": "Chart.js module for creating treemap charts", | ||
@@ -34,12 +34,10 @@ "main": "dist/chartjs-chart-treemap.js", | ||
"devDependencies": { | ||
"@babel/preset-env": "^7.11.5", | ||
"@rollup/plugin-babel": "^5.2.1", | ||
"@rollup/plugin-commonjs": "^15.1.0", | ||
"@rollup/plugin-node-resolve": "^9.0.0", | ||
"chart.js": "^3.0.0-beta.3", | ||
"eslint": "^7.10.0", | ||
"@rollup/plugin-commonjs": "^16.0.0", | ||
"@rollup/plugin-node-resolve": "^10.0.0", | ||
"chart.js": "^3.0.0-beta.6", | ||
"eslint": "^7.13.0", | ||
"eslint-config-chartjs": "^0.2.0", | ||
"eslint-config-esnext": "^4.1.0", | ||
"eslint-plugin-html": "^6.1.0", | ||
"jasmine": "^3.6.1", | ||
"eslint-plugin-html": "^6.1.1", | ||
"jasmine": "^3.6.3", | ||
"jasmine-core": "^3.6.0", | ||
@@ -50,3 +48,3 @@ "karma": "^5.2.3", | ||
"karma-coverage-istanbul-reporter": "^3.0.3", | ||
"karma-firefox-launcher": "^1.3.0", | ||
"karma-firefox-launcher": "^2.1.0", | ||
"karma-jasmine": "^4.0.1", | ||
@@ -56,6 +54,6 @@ "karma-jasmine-html-reporter": "^1.5.4", | ||
"karma-spec-reporter": "0.0.32", | ||
"karma-summary-reporter": "^1.8.0", | ||
"karma-summary-reporter": "^1.9.0", | ||
"minimist": "^1.2.5", | ||
"pixelmatch": "^5.2.1", | ||
"rollup": "^2.28.2", | ||
"rollup": "^2.33.2", | ||
"rollup-plugin-terser": "^7.0.2" | ||
@@ -62,0 +60,0 @@ }, |
@@ -42,3 +42,4 @@ # chartjs-chart-treemap | ||
return Color('blue').alpha(alpha).rgbString(); | ||
} | ||
}, | ||
rtl: false // control in which direction the squares are positioned | ||
}] | ||
@@ -45,0 +46,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50851
23
1435
94