/*! * chartjs-chart-treemap v0.0.0-development * https://chartjs-chart-treemap.pages.dev/ * (c) 2026 Jukka Kurkela * Released under the MIT license */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("chart.js"),require("chart.js/helpers")):"function"==typeof define&&define.amd?define(["exports","chart.js","chart.js/helpers"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["chartjs-chart-treemap"]={},t.Chart,t.Chart.helpers)}(this,function(t,e,n){"use strict";function i(t,e,n,i){const r=2*i,o=e.getPixelForValue(t.x),s=n.getPixelForValue(t.y),a=e.getPixelForValue(t.x+t.w)-o,h=n.getPixelForValue(t.y+t.h)-s;return{height:h-r,hidden:r>a||r>h,width:a-r,x:o+i,y:s+i}}function r(t,e){let n,i;if(!t||!e)return!0;if(t===e)return!1;if(t.length!==e.length)return!0;for(n=0,i=t.length;nt.rtl?t.x+t.iw-e:t.x+t._ix;function l(t,e,n,i){const r={_data:e._data,a:e._normalized,h:n.h,s:i,v:e.value,vs:e.values,w:n.w,x:h(t,n.w),y:t.y+t._iy};return e.group&&(r.g=e.group,r.l=e.level,r.gs=e.groupSum),r}class u{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{dir:e,side:n}=this,i="x"===e?"_ix":"_iy",r=t.nsum,o=t.get(),s=n*n,h=r*r,u=[];let c=0,d=0;for(const e of o){const n=a(e,s,h,i);d+=n.d1,c=Math.max(c,n.d2),u.push(l(this,e,n,t.sum)),this[i]+=n.d1}return this["x"===e?"_iy":"_ix"]+=c,this[i]-=d,u}constructor(t){t=t||{h:1,w:1},this.rtl=!!t.rtl,this.unsorted=!!t.unsorted,this.x=t.x||t.left||0,this.y=t.y||t.top||0,this._ix=0,this._iy=0,this.w=t.w||t.width||t.right-t.left,this.h=t.h||t.height||t.bottom-t.top}}const c=Math.min,d=Math.max;function f(t,e){const n=+e[t.key],i=n*t.ratio;return e._normalized=i,{max:d(t.max,n),min:c(t.min,n),nmax:d(t.nmax,i),nmin:c(t.nmin,i),nsum:t.nsum+i,sum:t.sum+n}}function p(t,e,n){t._arr.push(e),function(t,e){Object.assign(t,e)}(t,n)}class g{get length(){return this._arr.length}reset(){this._arr=[],this._hist=[],this.sum=0,this.nsum=0,this.min=1/0,this.max=-1/0,this.nmin=1/0,this.nmax=-1/0}push(t){p(this,t,f(this,t))}pushIf(t,e,...n){const i=f(this,t);if(!e((r=this,{max:r.max,min:r.min,nmax:r.nmax,nmin:r.nmin,nsum:r.nsum,sum:r.sum}),i,n))return t;var r;p(this,t,i)}get(){return this._arr}constructor(t,e){this._arr=[],this._hist=[],this.sum=0,this.nsum=0,this.min=1/0,this.max=-1/0,this.nmin=1/0,this.nmax=-1/0,this.key=t,this.ratio=e,this.reset()}}const m=(t,e)=>e>t||t.length>e.length&&t.startsWith(e),x=t=>String(t),y=t=>null!=t&&""!==t;function b(t,e,i,r=[],o=0,s=[]){const a=o-1;if(t[0]in i&&o>0){const n=r.reduce((t,e,n)=>(n!==a&&(t[x(n)]=e),t),{});n[e]=r[a],t.forEach(t=>{n[t]=i[t]}),s.push(n)}else for(const a of Object.keys(i)){const h=i[a];n.isObject(h)&&(r.push(a),b(t,e,h,r,o+1,s))}return r.splice(a,1),s}function v(t,e,n){const i=b(t,e,n);if(!i.length)return i;const r=i.reduce((t,e)=>{const n=Object.keys(e).length-2;return Math.max(t,n)},0);return i.forEach(t=>{for(let e=0;e{r[t]=0}),e[n]=[]}function k(t,e,n,i,r,o,s=[],a=s,h=s.length-1){const l=n[0],u=n.slice(1),c=Object.create(null),d=Object.create(null),f=[],p=a.length?a:[e],g=a.length?h:0;let m,x;for(m=0,x=t.length;m{a[t]+=e[t]}),c[s].path=_(p.slice(0,n.groupIndex+1),e,s),d[s].push(e)}return Object.keys(c).forEach(t=>{const n={children:d[t]};n[l]=+c[t].value,u.forEach(e=>{n[e]=+c[t][e]}),n[e]=c[t].label,n.group=c[t].group,n.groupIndex=c[t].groupIndex,n.label=t,n.path=c[t].path,r&&(n[r]=o),f.push(n)}),f}function T(t,e){let i,r=t.length;if(!r)return e;const o=n.isObject(t[0]);for(e=o?e:"v",i=0,r=t.length;i+n[e]-+t[e]):t.sort((t,e)=>+e-+t)}function R(t,e){let n,i,r;for(n=0,i=0,r=t.length;im?+p[t][m]:+p[t],y=t=>i&&p[t][i];for(d=0;d(e[n]=+t[n],e),{})}f=h.pushIf(f,E,l),f&&(s.push(a.map(h)),l=a.side,h.reset(),h.push(f))}return h.length&&s.push(a.map(h)),w(s)}const P=new Map;function S(t,e,i){if(!i||!1===i.display)return!1;if("headerBoxes"===t)return!0;const{w:r,h:o}=e,s=n.toFont(i.font||{}).lineHeight,a=Math.max(Math.min(2*n.valueOrDefault(i.padding,3),Math.min(r,o)),0);return r-a>s&&o-a>s}function D(t,e,n,i){if("headerBoxes"!==t)return n.lineHeight+2*i;const r=n.lineHeight+2*i;return e.h<2*r?e.h/3:r}function I(t,e,i,r,o){const{captions:s,labels:a,displayMode:h}=i;t.save(),t.beginPath(),t.rect(e.x,e.y,e.w,e.h),t.clip();const l=r&&(!n.defined(r.l)||r.isLeaf);l&&a.display?function(t,e,i,r,o){const s=i.labels,a=B(s.formatter,A(o,r));if(!a)return;const h=n.isArray(a)?a:[a];let l=function(t,e){const{font:i,hoverFont:r}=e,o=(t.active?r:i)||i;return Array.isArray(o)?o.map(t=>n.toFont(t)):[n.toFont(o)]}(e,s),u=F(t,h,l);const c=function(t,e,n,i){const{overflow:r,padding:o}=n,{width:s,height:a}=i;if("hidden"===r)return!(s+2*o>e.w||a+2*o>e.h);if("fit"===r){const t=Math.min(e.w/(s+2*o),e.h/(a+2*o));if(t<1)return t}return!0}(0,e,s,u);if(!c)return;n.isNumber(c)&&(u={height:u.height*c,width:u.width*c},l=function(t,e){return t.map(t=>{const{lineHeight:i,...r}=t;return n.toFont({...r,size:Math.floor(t.size*e)})})}(l,c));const{color:d,hoverColor:f,align:p}=s,g=(e.active?f:d)||d,m=n.isArray(g)?g:[g],x=function(t,e,n){const{align:i,position:r,padding:o}=e,s=z(t,i,o);let a;a="top"===r?t.y+o:"bottom"===r?t.y+t.h-o-n.height:t.y+(t.h-n.height)/2+o;return{x:s,y:a}}(e,s,u);t.textAlign=p,t.textBaseline="middle";let y=0;h.forEach((e,n)=>{const i=m[Math.min(n,m.length-1)],r=l[Math.min(n,l.length-1)],o=r.lineHeight;t.font=r.string,t.fillStyle=i,t.fillText(e,x.x,x.y+o/2+y),y+=o})}(t,e,i,r,o):!l&&S(h,e,s)&&function(t,e,i,r,o){const{captions:s,spacing:a,rtl:h,displayMode:l}=i,{color:u,hoverColor:c,font:d,hoverFont:f,padding:p,align:g}=s,m=(e.active?c:u)||u,x=g||(h?"right":"left"),y=(e.active?f:d)||d,b=n.toFont(y),v=[b];if(b.lineHeight>e.h)return;let w=function(t,e,n){return B(t.formatter,A(e,n))||n.g||""}(s,o,r);if(!w)return;const _=F(t,[w],v);_.width+2*p>e.w&&(w=function(t,e,n,i){const r="...",o=F(t,[r],i).width;if(o>=n)return"";let s,a=1,h=e.length;for(;a<=h;){const r=Math.floor((a+h)/2);s=F(t,[e.slice(0,r)],i).width,s+o>n?h=r-1:a=r+1}const l=e.slice(0,Math.max(0,a-1));return l?l+r:""}(t,w,e.w-2*p,v));const M=b.lineHeight/2,C=z(e,x,p);t.fillStyle=m,t.font=b.string,t.textAlign=x,t.textBaseline="middle";const k="headerBoxes"===l?e.y+e.h/2:e.y+p+a+M;t.fillText(w,C,k)}(t,e,i,r,o),t.restore()}function A(t,e){const n=t.$context||{active:!!t.active,raw:e,type:"data"};return n.active=!!t.active,n.raw=e,n}function B(t,e){return"function"==typeof t?t(e):t}function F(t,e,n){const i=n.reduce((t,e)=>t+=e.string,""),r=e.join()+i+(t._measureText?"-spriting":"");let o=P.get(r);if(!o){t.save();const i=e.length;let s=0,a=0;for(let r=0;re<=r),a,r),b=L(y,f,i,m,r,g),v=b.slice();return r{const i=function(t,n){const i="headerBoxes"===e.displayMode?{b:0,l:0,r:0,t:0}:s(e.borderWidth,t.w/2,t.h/2),r={...n,h:t.h-2*l-i.t-i.b,w:t.w-2*l-i.l-i.r,x:t.x+l+i.l,y:t.y+l+i.t};if(S(e.displayMode,r,u)){const t=D(e.displayMode,r,c,d);r.y+=t,r.h-=t}return r}(n,f),r=[],o=n._data.groupIndex+1;o{t.isLeaf=!0}),v}(t,0,r):L(t,r,i);return f.map(t=>{if("headerBoxes"!==e.displayMode||t.isLeaf)return t;if(!S(e.displayMode,t,u))return;const n=D(e.displayMode,t,c,d);return{...t,h:n}}).filter(Boolean)}function V(){const t=e.registry.plugins.get("tooltip");t&&!t.positioners.treemap&&(t.positioners.treemap=t=>{if(!t.length)return!1;return t.at(-1).element.tooltipPosition()})}class $ extends e.DatasetController{initialize(){V(),this.enableOptionSharing=!0,super.initialize()}getMinMax(t){return{max:"x"===t.axis?t.right-t.left:t.bottom-t.top,min:0}}configure(){super.configure();const{xScale:t,yScale:e}=this.getMeta();if(!t||!e)return;const n=t.right-t.left,i=e.bottom-e.top,r={h:i,rtl:!!this.options.rtl,unsorted:!!this.options.unsorted,w:n,x:0,y:0};var o,s;o=this._rect,s=r,o&&s&&o.x===s.x&&o.y===s.y&&o.w===s.w&&o.h===s.h&&o.rtl===s.rtl&&o.unsorted===s.unsorted||(this._rect=r,this._rectChanged=!0),this._rectChanged&&(t.max=n,t.configure(),e.max=i,e.configure())}update(t){const e=this.getDataset(),{data:n}=this.getMeta(),i=e.groups||[],o=[e.key||""].concat(e.sumKeys||[]);e.tree=e.tree||e.data||[];const s=e.tree,a=e.treeVersion;"reset"===t&&this.configure(),(this._rectChanged||r(this._keys||[],o)||r(this._groups||[],i)||this._prevTree!==s||this._prevTreeVersion!==a)&&(this._groups=i.slice(),this._keys=o.slice(),this._prevTree=s,this._prevTreeVersion=a,this._rectChanged=!1,e.data=H(s,e,this._keys,this._rect),this._dataCheck(),this._resyncElements()),this.updateElements(n,0,n.length,t)}updateElements(t,e,n,r){const o="reset"===r,s=this.getDataset(),a=this.resolveDataElementOptions(e,r);this._rect.options=a;const h=this.getSharedOptions(a),l=this.includeOptions(r,h||{}),{xScale:u,yScale:c}=this.getMeta();for(let a=e;a=s.left&&e<=s.right)&&(o||n>=s.top&&n<=s.bottom)}function N(t,e){t.rect(e.x,e.y,e.w,e.h)}$.id="treemap",$.version="0.0.0-development",$.defaults={animations:{numbers:{properties:["x","y","width","height"],type:"number"}},dataElementType:"treemap"},$.descriptors={_indexable:!1,_scriptable:!0,captions:{_fallback:!0,_scriptable:t=>"formatter"!==t},labels:{_fallback:!0,_scriptable:t=>"formatter"!==t}},$.overrides={hover:{},interaction:{includeInvisible:!0,intersect:!0,mode:"point"},plugins:{tooltip:{callbacks:{label(t){const e=t.dataset,n=e.data[t.dataIndex],i=n.g||n._data.label||e.label;return(i?`${i}: `:"")+n.v},title(t){if(t.length){return t[0].dataset.key||""}return""}},intersect:!0,position:"treemap"}},scales:{x:{alignToPixels:!0,bounds:"data",display:!1,type:"linear"},y:{alignToPixels:!0,bounds:"data",display:!1,reverse:!0,type:"linear"}}},$.beforeRegister=()=>{j("chart.js","3.8",e.Chart.version)},$.afterRegister=()=>{V()},$.afterUnregister=()=>{const t=e.registry.plugins.get("tooltip");t&&delete t.positioners.treemap};class G extends e.Element{draw(t,e){if(!e)return;const i=this.options,{inner:r,outer:o}=q(this),s=(a=o.radius).topLeft||a.topRight||a.bottomLeft||a.bottomRight?n.addRoundedRectPath:N;var a;t.save(),o.w===r.w&&o.h===r.h||(t.beginPath(),s(t,o),t.clip(),s(t,r),t.fillStyle=i.borderColor,t.fill("evenodd")),t.beginPath(),s(t,r),t.fillStyle=i.backgroundColor,t.fill(),function(t,e,n,i){const r=n.dividers,o=i._data?.children;if(!r.display||!Array.isArray(o)||!o.length)return;const{x:s,y:a,w:h,h:l}=e,{lineColor:u,lineCapStyle:c,lineDash:d,lineDashOffset:f,lineWidth:p}=r;if(t.save(),t.strokeStyle=u,t.lineCap=c,t.setLineDash(d),t.lineDashOffset=f,t.lineWidth=p,t.beginPath(),h>l){const e=h/2;t.moveTo(s+e,a),t.lineTo(s+e,a+l)}else{const e=l/2;t.moveTo(s,a+e),t.lineTo(s+h,a+e)}t.stroke(),t.restore()}(t,r,i,e),I(t,r,i,e,this),t.restore()}inRange(t,e,n){return K(this,t,e,n)}inXRange(t,e){return K(this,t,null,e)}inYRange(t,e){return K(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()}constructor(t){super(),this.width=void 0,this.height=void 0,t&&Object.assign(this,t)}}G.id="treemap",G.defaults={borderRadius:0,borderWidth:0,captions:{align:void 0,color:"black",display:!0,font:{},formatter:t=>{const e=t.raw._data?.label;return t.raw.g||("string"==typeof e?e:"")},padding:3},displayMode:"containerBoxes",dividers:{display:!1,lineCapStyle:"butt",lineColor:"black",lineDash:[],lineDashOffset:0,lineWidth:1},label:void 0,labels:{align:"center",color:"black",display:!1,font:{},formatter(t){const e=t.raw._data?.label;return t.raw.g?[t.raw.g,`${t.raw.v}`]:"string"==typeof e?[e,`${t.raw.v}`]:`${t.raw.v}`},overflow:"cut",padding:3,position:"middle"},rtl:!1,spacing:.5,unsorted:!1},G.descriptors={_indexable:!1,_scriptable:!0,captions:{_fallback:!0,_scriptable:t=>"formatter"!==t},labels:{_fallback:!0,_scriptable:t=>"formatter"!==t}},G.defaultRoutes={backgroundColor:"backgroundColor",borderColor:"borderColor"},e.Chart.register($,G),t.TreemapController=$,t.TreemapElement=G,t.flatten=w,t.getGroupKey=x,t.group=k,t.index=T,t.normalizeTreeToArray=v,t.requireVersion=j,t.sort=O,t.sum=R}); //# sourceMappingURL=chartjs-chart-treemap.min.js.map