chartjs-chart-treemap
Advanced tools
Comparing version 2.2.0 to 2.3.0
/*! | ||
* chartjs-chart-treemap v2.2.0 | ||
* chartjs-chart-treemap v2.3.0 | ||
* https://chartjs-chart-treemap.pages.dev/ | ||
* (c) 2022 Jukka Kurkela | ||
* (c) 2023 Jukka Kurkela | ||
* Released under the MIT license | ||
*/ | ||
import { Element, Chart, registry, DatasetController } from 'chart.js'; | ||
import { isObject, addRoundedRectPath, defined, toFont, isArray, toTRBL, toTRBLCorners, valueOrDefault, clipArea, unclipArea } from 'chart.js/helpers'; | ||
import { isObject, addRoundedRectPath, defined, toFont, isArray, isNumber, toTRBL, toTRBLCorners, valueOrDefault, clipArea, unclipArea } from 'chart.js/helpers'; | ||
@@ -14,5 +14,5 @@ const isOlderPart = (act, req) => req > act || (act.length > req.length && act.slice(0, req.length) === req); | ||
function scanTreeObject(key, treeLeafKey, obj, tree = [], lvl = 0, result = []) { | ||
function scanTreeObject(keys, treeLeafKey, obj, tree = [], lvl = 0, result = []) { | ||
const objIndex = lvl - 1; | ||
if (key in obj && lvl > 0) { | ||
if (keys[0] in obj && lvl > 0) { | ||
const record = tree.reduce(function(reduced, item, i) { | ||
@@ -25,3 +25,5 @@ if (i !== objIndex) { | ||
record[treeLeafKey] = tree[objIndex]; | ||
record[key] = obj[key]; | ||
keys.forEach(function(k) { | ||
record[k] = obj[k]; | ||
}); | ||
result.push(record); | ||
@@ -33,3 +35,3 @@ } else { | ||
tree.push(childKey); | ||
scanTreeObject(key, treeLeafKey, child, tree, lvl + 1, result); | ||
scanTreeObject(keys, treeLeafKey, child, tree, lvl + 1, result); | ||
} | ||
@@ -42,4 +44,4 @@ } | ||
function normalizeTreeToArray(key, treeLeafKey, obj) { | ||
const data = scanTreeObject(key, treeLeafKey, obj); | ||
function normalizeTreeToArray(keys, treeLeafKey, obj) { | ||
const data = scanTreeObject(keys, treeLeafKey, obj); | ||
if (!data.length) { | ||
@@ -51,4 +53,4 @@ return data; | ||
// on top to groups ones | ||
const keys = Object.keys(element).length - 2; | ||
return maxVal > keys ? maxVal : keys; | ||
const ikeys = Object.keys(element).length - 2; | ||
return maxVal > ikeys ? maxVal : ikeys; | ||
}); | ||
@@ -103,3 +105,3 @@ data.forEach(function(element) { | ||
* @param {string} grp | ||
* @param {string} key | ||
* @param {[string]} keys | ||
* @param {string} treeeLeafKey | ||
@@ -110,3 +112,5 @@ * @param {string} [mainGrp] | ||
*/ | ||
function group(values, grp, key, treeLeafKey, mainGrp, mainValue, groups = []) { | ||
function group(values, grp, keys, treeLeafKey, mainGrp, mainValue, groups = []) { | ||
const key = keys[0]; | ||
const addKeys = keys.slice(1); | ||
const tmp = Object.create(null); | ||
@@ -123,3 +127,6 @@ const data = Object.create(null); | ||
if (!(g in tmp)) { | ||
tmp[g] = {value: 0}; | ||
const tmpRef = tmp[g] = {value: 0}; | ||
addKeys.forEach(function(k) { | ||
tmpRef[k] = 0; | ||
}); | ||
data[g] = []; | ||
@@ -129,2 +136,6 @@ } | ||
tmp[g].label = v[grp] || ''; | ||
const tmpRef = tmp[g]; | ||
addKeys.forEach(function(k) { | ||
tmpRef[k] += v[k]; | ||
}); | ||
tmp[g].path = getPath(groups, v, g); | ||
@@ -137,2 +148,5 @@ data[g].push(v); | ||
v[key] = +tmp[k].value; | ||
addKeys.forEach(function(ak) { | ||
v[ak] = +tmp[k][ak]; | ||
}); | ||
v[grp] = tmp[k].label; | ||
@@ -379,2 +393,10 @@ v.label = k; | ||
function toFonts(fonts, fitRatio) { | ||
return fonts.map(function(f) { | ||
f.size = Math.floor(f.size * fitRatio); | ||
f.lineHeight = undefined; | ||
return toFont(f); | ||
}); | ||
} | ||
function labelToDraw(ctx, rect, options, labelSize) { | ||
@@ -385,2 +407,7 @@ const {overflow, padding} = options; | ||
return !((width + padding * 2) > rect.w || (height + padding * 2) > rect.h); | ||
} else if (overflow === 'fit') { | ||
const ratio = Math.min(rect.w / (width + padding * 2), rect.h / (height + padding * 2)); | ||
if (ratio < 1) { | ||
return ratio; | ||
} | ||
} | ||
@@ -390,2 +417,8 @@ return true; | ||
function getFontFromOptions(rect, labels) { | ||
const {font, hoverFont} = labels; | ||
const optFont = (rect.active ? hoverFont : font) || font; | ||
return isArray(optFont) ? optFont.map(f => toFont(f)) : [toFont(optFont)]; | ||
} | ||
function drawLabel(ctx, rect, options) { | ||
@@ -398,9 +431,12 @@ const labels = options.labels; | ||
const contents = isArray(content) ? content : [content]; | ||
const {font, hoverFont} = labels; | ||
const optFont = (rect.active ? hoverFont : font) || font; | ||
const fonts = isArray(optFont) ? optFont.map(f => toFont(f)) : [toFont(optFont)]; | ||
const labelSize = measureLabelSize(ctx, contents, fonts); | ||
if (!labelToDraw(ctx, rect, labels, labelSize)) { | ||
let fonts = getFontFromOptions(rect, labels); | ||
let labelSize = measureLabelSize(ctx, contents, fonts); | ||
const lblToDraw = labelToDraw(ctx, rect, labels, labelSize); | ||
if (!lblToDraw) { | ||
return; | ||
} | ||
if (isNumber(lblToDraw)) { | ||
labelSize = {width: labelSize.width * lblToDraw, height: labelSize.height * lblToDraw}; | ||
fonts = toFonts(fonts, lblToDraw); | ||
} | ||
const {color, hoverColor, align} = labels; | ||
@@ -628,2 +664,3 @@ const optColor = (rect.active ? hoverColor : color) || color; | ||
v: itm.value, | ||
vs: itm.values, | ||
s: sum, | ||
@@ -799,7 +836,7 @@ _data: itm._data | ||
*/ | ||
function squarify(values, rectangle, key, grp, lvl, gsum) { | ||
function squarify(values, rectangle, keys = [], grp, lvl, gsum) { | ||
values = values || []; | ||
const rows = []; | ||
const rect = new Rect(rectangle); | ||
const row = new StatArray('value', rect.area / sum(values, key)); | ||
const row = new StatArray('value', rect.area / sum(values, keys[0])); | ||
let length = rect.side; | ||
@@ -814,3 +851,3 @@ const n = values.length; | ||
const tmp = values.slice(); | ||
key = index(tmp, key); | ||
let key = index(tmp, keys[0]); | ||
sort(tmp, key); | ||
@@ -826,2 +863,7 @@ | ||
o.group = gval(i); | ||
const tmpRef = tmp[i]; | ||
o.values = keys.reduce(function(obj, k) { | ||
obj[k] = +tmpRef[k]; | ||
return obj; | ||
}, {}); | ||
} | ||
@@ -842,3 +884,3 @@ o = row.pushIf(o, compareAspectRatio, length); | ||
var version = "2.2.0"; | ||
var version = "2.3.0"; | ||
@@ -892,7 +934,6 @@ function scaleRect(sq, xScale, yScale, sp) { | ||
function buildData(tree, dataset, mainRect) { | ||
const key = dataset.key || ''; | ||
function buildData(tree, dataset, keys, mainRect) { | ||
const treeLeafKey = dataset.treeLeafKey || '_leaf'; | ||
if (isObject(tree)) { | ||
tree = normalizeTreeToArray(key, treeLeafKey, tree); | ||
tree = normalizeTreeToArray(keys, treeLeafKey, tree); | ||
} | ||
@@ -909,4 +950,4 @@ const groups = dataset.groups || []; | ||
const pg = (gidx > 0) && getGroupKey(groups[gidx - 1]); | ||
const gdata = group(tree, g, key, treeLeafKey, pg, parent, groups.filter((item, index) => index <= gidx)); | ||
const gsq = squarify(gdata, rect, key, g, gidx, gs); | ||
const gdata = group(tree, g, keys, treeLeafKey, pg, parent, groups.filter((item, index) => index <= gidx)); | ||
const gsq = squarify(gdata, rect, keys, g, gidx, gs); | ||
const ret = gsq.slice(); | ||
@@ -935,3 +976,3 @@ if (gidx < glen - 1) { | ||
? recur(0, mainRect) | ||
: squarify(tree, mainRect, key); | ||
: squarify(tree, mainRect, keys); | ||
} | ||
@@ -944,3 +985,3 @@ | ||
this._groups = undefined; | ||
this._key = undefined; | ||
this._keys = undefined; | ||
this._rect = undefined; | ||
@@ -990,4 +1031,4 @@ this._rectChanged = true; | ||
const {data} = this.getMeta(); | ||
const groups = dataset.groups || (dataset.groups = []); | ||
const key = dataset.key; | ||
const groups = dataset.groups || []; | ||
const keys = [dataset.key || ''].concat(dataset.sumKeys || []); | ||
const tree = dataset.tree = dataset.tree || dataset.data || []; | ||
@@ -1000,9 +1041,9 @@ | ||
if (this._rectChanged || this._key !== key || arrayNotEqual(this._groups, groups) || this._prevTree !== tree) { | ||
if (this._rectChanged || arrayNotEqual(this._keys, keys) || arrayNotEqual(this._groups, groups) || this._prevTree !== tree) { | ||
this._groups = groups.slice(); | ||
this._key = key; | ||
this._keys = keys.slice(); | ||
this._prevTree = tree; | ||
this._rectChanged = false; | ||
dataset.data = buildData(tree, dataset, this._rect); | ||
dataset.data = buildData(tree, dataset, this._keys, this._rect); | ||
// @ts-ignore using private stuff | ||
@@ -1009,0 +1050,0 @@ this._dataCheck(); |
/*! | ||
* chartjs-chart-treemap v2.2.0 | ||
* chartjs-chart-treemap v2.3.0 | ||
* https://chartjs-chart-treemap.pages.dev/ | ||
* (c) 2022 Jukka Kurkela | ||
* (c) 2023 Jukka Kurkela | ||
* Released under the MIT license | ||
@@ -17,5 +17,5 @@ */ | ||
function scanTreeObject(key, treeLeafKey, obj, tree = [], lvl = 0, result = []) { | ||
function scanTreeObject(keys, treeLeafKey, obj, tree = [], lvl = 0, result = []) { | ||
const objIndex = lvl - 1; | ||
if (key in obj && lvl > 0) { | ||
if (keys[0] in obj && lvl > 0) { | ||
const record = tree.reduce(function(reduced, item, i) { | ||
@@ -28,3 +28,5 @@ if (i !== objIndex) { | ||
record[treeLeafKey] = tree[objIndex]; | ||
record[key] = obj[key]; | ||
keys.forEach(function(k) { | ||
record[k] = obj[k]; | ||
}); | ||
result.push(record); | ||
@@ -36,3 +38,3 @@ } else { | ||
tree.push(childKey); | ||
scanTreeObject(key, treeLeafKey, child, tree, lvl + 1, result); | ||
scanTreeObject(keys, treeLeafKey, child, tree, lvl + 1, result); | ||
} | ||
@@ -45,4 +47,4 @@ } | ||
function normalizeTreeToArray(key, treeLeafKey, obj) { | ||
const data = scanTreeObject(key, treeLeafKey, obj); | ||
function normalizeTreeToArray(keys, treeLeafKey, obj) { | ||
const data = scanTreeObject(keys, treeLeafKey, obj); | ||
if (!data.length) { | ||
@@ -54,4 +56,4 @@ return data; | ||
// on top to groups ones | ||
const keys = Object.keys(element).length - 2; | ||
return maxVal > keys ? maxVal : keys; | ||
const ikeys = Object.keys(element).length - 2; | ||
return maxVal > ikeys ? maxVal : ikeys; | ||
}); | ||
@@ -106,3 +108,3 @@ data.forEach(function(element) { | ||
* @param {string} grp | ||
* @param {string} key | ||
* @param {[string]} keys | ||
* @param {string} treeeLeafKey | ||
@@ -113,3 +115,5 @@ * @param {string} [mainGrp] | ||
*/ | ||
function group(values, grp, key, treeLeafKey, mainGrp, mainValue, groups = []) { | ||
function group(values, grp, keys, treeLeafKey, mainGrp, mainValue, groups = []) { | ||
const key = keys[0]; | ||
const addKeys = keys.slice(1); | ||
const tmp = Object.create(null); | ||
@@ -126,3 +130,6 @@ const data = Object.create(null); | ||
if (!(g in tmp)) { | ||
tmp[g] = {value: 0}; | ||
const tmpRef = tmp[g] = {value: 0}; | ||
addKeys.forEach(function(k) { | ||
tmpRef[k] = 0; | ||
}); | ||
data[g] = []; | ||
@@ -132,2 +139,6 @@ } | ||
tmp[g].label = v[grp] || ''; | ||
const tmpRef = tmp[g]; | ||
addKeys.forEach(function(k) { | ||
tmpRef[k] += v[k]; | ||
}); | ||
tmp[g].path = getPath(groups, v, g); | ||
@@ -140,2 +151,5 @@ data[g].push(v); | ||
v[key] = +tmp[k].value; | ||
addKeys.forEach(function(ak) { | ||
v[ak] = +tmp[k][ak]; | ||
}); | ||
v[grp] = tmp[k].label; | ||
@@ -382,2 +396,10 @@ v.label = k; | ||
function toFonts(fonts, fitRatio) { | ||
return fonts.map(function(f) { | ||
f.size = Math.floor(f.size * fitRatio); | ||
f.lineHeight = undefined; | ||
return helpers.toFont(f); | ||
}); | ||
} | ||
function labelToDraw(ctx, rect, options, labelSize) { | ||
@@ -388,2 +410,7 @@ const {overflow, padding} = options; | ||
return !((width + padding * 2) > rect.w || (height + padding * 2) > rect.h); | ||
} else if (overflow === 'fit') { | ||
const ratio = Math.min(rect.w / (width + padding * 2), rect.h / (height + padding * 2)); | ||
if (ratio < 1) { | ||
return ratio; | ||
} | ||
} | ||
@@ -393,2 +420,8 @@ return true; | ||
function getFontFromOptions(rect, labels) { | ||
const {font, hoverFont} = labels; | ||
const optFont = (rect.active ? hoverFont : font) || font; | ||
return helpers.isArray(optFont) ? optFont.map(f => helpers.toFont(f)) : [helpers.toFont(optFont)]; | ||
} | ||
function drawLabel(ctx, rect, options) { | ||
@@ -401,9 +434,12 @@ const labels = options.labels; | ||
const contents = helpers.isArray(content) ? content : [content]; | ||
const {font, hoverFont} = labels; | ||
const optFont = (rect.active ? hoverFont : font) || font; | ||
const fonts = helpers.isArray(optFont) ? optFont.map(f => helpers.toFont(f)) : [helpers.toFont(optFont)]; | ||
const labelSize = measureLabelSize(ctx, contents, fonts); | ||
if (!labelToDraw(ctx, rect, labels, labelSize)) { | ||
let fonts = getFontFromOptions(rect, labels); | ||
let labelSize = measureLabelSize(ctx, contents, fonts); | ||
const lblToDraw = labelToDraw(ctx, rect, labels, labelSize); | ||
if (!lblToDraw) { | ||
return; | ||
} | ||
if (helpers.isNumber(lblToDraw)) { | ||
labelSize = {width: labelSize.width * lblToDraw, height: labelSize.height * lblToDraw}; | ||
fonts = toFonts(fonts, lblToDraw); | ||
} | ||
const {color, hoverColor, align} = labels; | ||
@@ -631,2 +667,3 @@ const optColor = (rect.active ? hoverColor : color) || color; | ||
v: itm.value, | ||
vs: itm.values, | ||
s: sum, | ||
@@ -802,7 +839,7 @@ _data: itm._data | ||
*/ | ||
function squarify(values, rectangle, key, grp, lvl, gsum) { | ||
function squarify(values, rectangle, keys = [], grp, lvl, gsum) { | ||
values = values || []; | ||
const rows = []; | ||
const rect = new Rect(rectangle); | ||
const row = new StatArray('value', rect.area / sum(values, key)); | ||
const row = new StatArray('value', rect.area / sum(values, keys[0])); | ||
let length = rect.side; | ||
@@ -817,3 +854,3 @@ const n = values.length; | ||
const tmp = values.slice(); | ||
key = index(tmp, key); | ||
let key = index(tmp, keys[0]); | ||
sort(tmp, key); | ||
@@ -829,2 +866,7 @@ | ||
o.group = gval(i); | ||
const tmpRef = tmp[i]; | ||
o.values = keys.reduce(function(obj, k) { | ||
obj[k] = +tmpRef[k]; | ||
return obj; | ||
}, {}); | ||
} | ||
@@ -845,3 +887,3 @@ o = row.pushIf(o, compareAspectRatio, length); | ||
var version = "2.2.0"; | ||
var version = "2.3.0"; | ||
@@ -895,7 +937,6 @@ function scaleRect(sq, xScale, yScale, sp) { | ||
function buildData(tree, dataset, mainRect) { | ||
const key = dataset.key || ''; | ||
function buildData(tree, dataset, keys, mainRect) { | ||
const treeLeafKey = dataset.treeLeafKey || '_leaf'; | ||
if (helpers.isObject(tree)) { | ||
tree = normalizeTreeToArray(key, treeLeafKey, tree); | ||
tree = normalizeTreeToArray(keys, treeLeafKey, tree); | ||
} | ||
@@ -912,4 +953,4 @@ const groups = dataset.groups || []; | ||
const pg = (gidx > 0) && getGroupKey(groups[gidx - 1]); | ||
const gdata = group(tree, g, key, treeLeafKey, pg, parent, groups.filter((item, index) => index <= gidx)); | ||
const gsq = squarify(gdata, rect, key, g, gidx, gs); | ||
const gdata = group(tree, g, keys, treeLeafKey, pg, parent, groups.filter((item, index) => index <= gidx)); | ||
const gsq = squarify(gdata, rect, keys, g, gidx, gs); | ||
const ret = gsq.slice(); | ||
@@ -938,3 +979,3 @@ if (gidx < glen - 1) { | ||
? recur(0, mainRect) | ||
: squarify(tree, mainRect, key); | ||
: squarify(tree, mainRect, keys); | ||
} | ||
@@ -947,3 +988,3 @@ | ||
this._groups = undefined; | ||
this._key = undefined; | ||
this._keys = undefined; | ||
this._rect = undefined; | ||
@@ -993,4 +1034,4 @@ this._rectChanged = true; | ||
const {data} = this.getMeta(); | ||
const groups = dataset.groups || (dataset.groups = []); | ||
const key = dataset.key; | ||
const groups = dataset.groups || []; | ||
const keys = [dataset.key || ''].concat(dataset.sumKeys || []); | ||
const tree = dataset.tree = dataset.tree || dataset.data || []; | ||
@@ -1003,9 +1044,9 @@ | ||
if (this._rectChanged || this._key !== key || arrayNotEqual(this._groups, groups) || this._prevTree !== tree) { | ||
if (this._rectChanged || arrayNotEqual(this._keys, keys) || arrayNotEqual(this._groups, groups) || this._prevTree !== tree) { | ||
this._groups = groups.slice(); | ||
this._key = key; | ||
this._keys = keys.slice(); | ||
this._prevTree = tree; | ||
this._rectChanged = false; | ||
dataset.data = buildData(tree, dataset, this._rect); | ||
dataset.data = buildData(tree, dataset, this._keys, this._rect); | ||
// @ts-ignore using private stuff | ||
@@ -1012,0 +1053,0 @@ this._dataCheck(); |
/*! | ||
* chartjs-chart-treemap v2.2.0 | ||
* chartjs-chart-treemap v2.3.0 | ||
* https://chartjs-chart-treemap.pages.dev/ | ||
* (c) 2022 Jukka Kurkela | ||
* (c) 2023 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";const i=(t,e)=>e>t||t.length>e.length&&t.slice(0,e.length)===e,r=t=>""+t;function o(t,e,i,s=[],a=0,h=[]){const l=a-1;if(t in i&&a>0){const n=s.reduce((function(t,e,n){return n!==l&&(t[r(n)]=e),t}),{});n[e]=s[l],n[t]=i[t],h.push(n)}else for(const r of Object.keys(i)){const l=i[r];n.isObject(l)&&(s.push(r),o(t,e,l,s,a+1,h))}return s.splice(l,1),h}function s(t,e,n){const i=o(t,e,n);if(!i.length)return i;const s=i.reduce((function(t,e){const n=Object.keys(e).length-2;return t>n?t:n}));return i.forEach((function(t){for(let e=0;e<s;e++){const n=r(e);t[n]||(t[n]="")}})),i}function a(t){const e=[...t],n=[];for(;e.length;){const t=e.pop();Array.isArray(t)?e.push(...t):n.push(t)}return n.reverse()}function h(t,e,n){if(!t.length)return;const i=[];for(const r of t){const t=e[r];if(""===t){i.push(n);break}i.push(t)}return i.length?i.join("."):n}function l(t,e,n,i,r,o,s=[]){const a=Object.create(null),l=Object.create(null),u=[];let c,g,d;for(g=0,d=t.length;g<d;++g){const u=t[g];r&&u[r]!==o||(c=u[e]||u[i]||"",c in a||(a[c]={value:0},l[c]=[]),a[c].value+=+u[n],a[c].label=u[e]||"",a[c].path=h(s,u,c),l[c].push(u))}return Object.keys(a).forEach((t=>{const i={children:l[t]};i[n]=+a[t].value,i[e]=a[t].label,i.label=t,i.path=a[t].path,r&&(i[r]=o),u.push(i)})),u}function u(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<r;++i)o?t[i]._idx=i:t[i]={v:t[i],_idx:i};return e}function c(t,e){e?t.sort(((t,n)=>+n[e]-+t[e])):t.sort(((t,e)=>+e-+t))}function g(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}function d(t,e,n,r=!0){const o=n.split(".");let s=0;for(const a of e.split(".")){const h=o[s++];if(parseInt(a,10)<parseInt(h,10))break;if(i(h,a)){if(r)throw new Error(`${t} v${n} is not supported. v${e} or newer is required.`);return!1}}return!0}const f=new Map;function p(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 m(t,e,n){return Math.max(Math.min(t,n),e)}function x(t,e,i){const r=n.toTRBL(t);return{t:m(r.top,0,i),r:m(r.right,0,e),b:m(r.bottom,0,i),l:m(r.left,0,e)}}function b(t){const e=p(t),i=e.right-e.left,r=e.bottom-e.top,o=x(t.options.borderWidth,i/2,r/2),s=function(t,e,i){const r=n.toTRBLCorners(t),o=Math.min(e,i);return{topLeft:m(r.topLeft,0,o),topRight:m(r.topRight,0,o),bottomLeft:m(r.bottomLeft,0,o),bottomRight:m(r.bottomRight,0,o)}}(t.options.borderRadius,i/2,r/2),a={x:e.left,y:e.top,w:i,h:r,active:t.active,radius:s};return{outer:a,inner:{x:a.x+o.l,y:a.y+o.t,w:a.w-o.l-o.r,h:a.h-o.t-o.b,active:t.active,radius:{topLeft:Math.max(0,s.topLeft-Math.max(o.t,o.l)),topRight:Math.max(0,s.topRight-Math.max(o.t,o.r)),bottomLeft:Math.max(0,s.bottomLeft-Math.max(o.b,o.l)),bottomRight:Math.max(0,s.bottomRight-Math.max(o.b,o.r))}}}}function y(t,e,n,i){const r=null===e,o=null===n,s=!(!t||r&&o)&&p(t,i);return s&&(r||e>=s.left&&e<=s.right)&&(o||n>=s.top&&n<=s.bottom)}function v(t,e){t.rect(e.x,e.y,e.w,e.h)}function w(t,e){if(!e||!1===e.display)return!1;const{w:i,h:r}=t,o=n.toFont(e.font).lineHeight,s=m(2*n.valueOrDefault(e.padding,3),0,Math.min(i,r));return i-s>o&&r-s>o}function _(t,e,i,r,o){const{captions:s,labels:a}=i;t.save(),t.beginPath(),t.rect(e.x,e.y,e.w,e.h),t.clip();const h=r&&(!n.defined(r.l)||r.l===o);h&&a.display?function(t,e,i){const r=i.labels,o=r.formatter;if(!o)return;const s=n.isArray(o)?o:[o],{font:a,hoverFont:h}=r,l=(e.active?h:a)||a,u=n.isArray(l)?l.map((t=>n.toFont(t))):[n.toFont(l)],c=function(t,e,n){const i=n.reduce((function(t,e){return t+=e.string}),""),r=e.join()+i+(t._measureText?"-spriting":"");if(!f.has(r)){t.save();const i=e.length;let o=0,s=0;for(let r=0;r<i;r++){const i=n[Math.min(r,n.length-1)];t.font=i.string;const a=e[r];o=Math.max(o,t.measureText(a).width),s+=i.lineHeight}t.restore(),f.set(r,{width:o,height:s})}return f.get(r)}(t,s,u);if(!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);return!0}(0,e,r,c))return;const{color:g,hoverColor:d,align:p}=r,m=(e.active?d:g)||g,x=n.isArray(m)?m:[m],b=function(t,e,n){const{align:i,position:r,padding:o}=e;let s,a;s=M(t,i,o),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,r,c);t.textAlign=p,t.textBaseline="middle";let y=0;s.forEach((function(e,n){const i=x[Math.min(n,x.length-1)],r=u[Math.min(n,u.length-1)],o=r.lineHeight;t.font=r.string,t.fillStyle=i,t.fillText(e,b.x,b.y+o/2+y),y+=o}))}(t,e,i):!h&&w(e,s)&&function(t,e,i,r){const{captions:o,spacing:s,rtl:a}=i,{color:h,hoverColor:l,font:u,hoverFont:c,padding:g,align:d,formatter:f}=o,p=(e.active?l:h)||h,m=d||(a?"right":"left"),x=(e.active?c:u)||u,b=n.toFont(x),y=b.lineHeight/2,v=M(e,m,g);t.fillStyle=p,t.font=b.string,t.textAlign=m,t.textBaseline="middle",t.fillText(f||r.g,v,e.y+g+s+y)}(t,e,i,r),t.restore()}function M(t,e,n){return"left"===e?t.x+n:"right"===e?t.x+t.w-n:t.x+t.w/2}class C extends e.Element{constructor(t){super(),this.options=void 0,this.width=void 0,this.height=void 0,t&&Object.assign(this,t)}draw(t,e,i=0){if(!e)return;const r=this.options,{inner:o,outer:s}=b(this),a=(h=s.radius).topLeft||h.topRight||h.bottomLeft||h.bottomRight?n.addRoundedRectPath:v;var h;t.save(),s.w===o.w&&s.h===o.h||(t.beginPath(),a(t,s),t.clip(),a(t,o),t.fillStyle=r.borderColor,t.fill("evenodd")),t.beginPath(),a(t,o),t.fillStyle=r.backgroundColor,t.fill(),function(t,e,n,i){const r=n.dividers;if(!r.display||!i._data.children.length)return;const{x:o,y:s,w:a,h:h}=e,{lineColor:l,lineCapStyle:u,lineDash:c,lineDashOffset:g,lineWidth:d}=r;if(t.save(),t.strokeStyle=l,t.lineCap=u,t.setLineDash(c),t.lineDashOffset=g,t.lineWidth=d,t.beginPath(),a>h){const e=a/2;t.moveTo(o+e,s),t.lineTo(o+e,s+h)}else{const e=h/2;t.moveTo(o,s+e),t.lineTo(o+a,s+e)}t.stroke(),t.restore()}(t,o,r,e),_(t,o,r,e,i),t.restore()}inRange(t,e,n){return y(this,t,e,n)}inXRange(t,e){return y(this,t,null,e)}inYRange(t,e){return y(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}}function k(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}}C.id="treemap",C.defaults={label:void 0,borderRadius:0,borderWidth:0,captions:{align:void 0,color:"black",display:!0,font:{},formatter:t=>t.raw.g||t.raw._data.label||"",padding:3},dividers:{display:!1,lineCapStyle:"butt",lineColor:"black",lineDash:[],lineDashOffset:0,lineWidth:1},labels:{align:"center",color:"black",display:!1,font:{},formatter:t=>t.raw.g?[t.raw.g,t.raw.v+""]:t.raw._data.label?[t.raw._data.label,t.raw.v+""]:t.raw.v+"",overflow:"cut",position:"middle",padding:3},rtl:!1,spacing:.5},C.descriptors={labels:{_fallback:!0},captions:{_fallback:!0},_scriptable:!0,_indexable:!1},C.defaultRoutes={backgroundColor:"backgroundColor",borderColor:"borderColor"};const R=(t,e)=>t.rtl?t.x+t.iw-e:t.x+t._ix;function O(t,e,n,i){const r={x:R(t,n.w),y:t.y+t._iy,w:n.w,h:n.h,a:e._normalized,v:e.value,s:i,_data:e._data};return e.group&&(r.g=e.group,r.l=e.level,r.gs=e.groupSum),r}class j{constructor(t){t=t||{w:1,h:1},this.rtl=!!t.rtl,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}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,a=r*r,h=[];let l=0,u=0;for(const e of o){const n=k(e,s,a,i);u+=n.d1,l=Math.max(l,n.d2),h.push(O(this,e,n,t.sum)),this[i]+=n.d1}return this["x"===e?"_iy":"_ix"]+=l,this[i]-=u,h}}const T=Math.min,P=Math.max;function S(t,e){const n=+e[t.key],i=n*t.ratio;return e._normalized=i,{min:T(t.min,n),max:P(t.max,n),sum:t.sum+n,nmin:T(t.nmin,i),nmax:P(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 L{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,S(this,t))}pushIf(t,e,...n){const i=S(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 E(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 A(t,e,n,i,r,o){t=t||[];const s=[],h=new j(e),l=new L("value",h.area/g(t,n));let d=h.side;const f=t.length;let p,m;if(!f)return s;const x=t.slice();n=u(x,n),c(x,n);const b=t=>i&&x[t][i];for(p=0;p<f;++p)m={value:(y=p,n?+x[y][n]:+x[y]),groupSum:o,_data:t[x[p]._idx],level:void 0,group:void 0},i&&(m.level=r,m.group=b(p)),m=l.pushIf(m,E,d),m&&(s.push(h.map(l)),d=h.side,l.reset(),l.push(m));var y;return l.length&&s.push(h.map(l)),a(s)}function F(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{x:o+i,y:s+i,width:a-r,height:h-r,hidden:r>a||r>h}}class z extends e.DatasetController{constructor(t,e){super(t,e),this._groups=void 0,this._key=void 0,this._rect=void 0,this._rectChanged=!0}initialize(){this.enableOptionSharing=!0,super.initialize()}getMinMax(t){return{min:0,max:"x"===t.axis?t.right-t.left:t.bottom-t.top}}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={x:0,y:0,w:n,h:i,rtl:!!this.options.rtl};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||(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:i}=this.getMeta(),o=e.groups||(e.groups=[]),a=e.key,h=e.tree=e.tree||e.data||[];"reset"===t&&this.configure(),(this._rectChanged||this._key!==a||function(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;n<i;++n)if(t[n]!==e[n])return!0;return!1}(this._groups,o)||this._prevTree!==h)&&(this._groups=o.slice(),this._key=a,this._prevTree=h,this._rectChanged=!1,e.data=function(t,e,i){const o=e.key||"",a=e.treeLeafKey||"_leaf";n.isObject(t)&&(t=s(o,a,t));const h=e.groups||[],u=h.length,c=n.valueOrDefault(e.spacing,0),g=e.captions||{},d=n.toFont(g.font),f=n.valueOrDefault(g.padding,3);return u?function n(i,s,p,m){const b=r(h[i]),y=i>0&&r(h[i-1]),v=l(t,b,o,a,y,p,h.filter(((t,e)=>e<=i))),_=A(v,s,o,b,i,m),M=_.slice();return i<u-1&&_.forEach((t=>{const r=x(e.borderWidth,t.w/2,t.h/2),o={...s,x:t.x+c+r.l,y:t.y+c+r.t,w:t.w-2*c-r.l-r.r,h:t.h-2*c-r.t-r.b};w(o,g)&&(o.y+=d.lineHeight+2*f,o.h-=d.lineHeight+2*f),M.push(...n(i+1,o,t.g,t.s))})),M}(0,i):A(t,i,o)}(h,e,this._rect),this._dataCheck(),this._resyncElements()),this.updateElements(i,0,i.length,t)}updateElements(t,e,n,i){const r="reset"===i,o=this.getDataset(),s=this._rect.options=this.resolveDataElementOptions(e,i),a=this.getSharedOptions(s),h=this.includeOptions(i,a),{xScale:l,yScale:u}=this.getMeta(this.index);for(let s=e;s<e+n;s++){const e=a||this.resolveDataElementOptions(s,i),n=F(o.data[s],l,u,e.spacing);r&&(n.width=0,n.height=0),h&&(n.options=e),this.updateElement(t[s],s,n,i)}this.updateSharedOptions(a,i,s)}draw(){const{ctx:t,chartArea:e}=this.chart,i=this.getMeta().data||[],r=this.getDataset(),o=(r.groups||[]).length-1,s=r.data;n.clipArea(t,e);for(let e=0,n=i.length;e<n;++e){const n=i[e];n.hidden||n.draw(t,s[e],o)}n.unclipArea(t)}}z.id="treemap",z.version="2.2.0",z.defaults={dataElementType:"treemap",animations:{numbers:{type:"number",properties:["x","y","width","height"]}}},z.descriptors={_scriptable:!0,_indexable:!1},z.overrides={interaction:{mode:"point",includeInvisible:!0,intersect:!0},hover:{},plugins:{tooltip:{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],i=n.g||n._data.label||e.label;return(i?i+": ":"")+n.v}}}},scales:{x:{type:"linear",alignToPixels:!0,bounds:"data",display:!1},y:{type:"linear",alignToPixels:!0,bounds:"data",display:!1,reverse:!0}}},z.beforeRegister=function(){d("chart.js","3.8",e.Chart.version)},z.afterRegister=function(){const t=e.registry.plugins.get("tooltip");t?t.positioners.treemap=function(t){if(!t.length)return!1;return t[t.length-1].element.tooltipPosition()}:console.warn("Unable to register the treemap positioner because tooltip plugin is not registered")},z.afterUnregister=function(){const t=e.registry.plugins.get("tooltip");t&&delete t.positioners.treemap},e.Chart.register(z,C),t.flatten=a,t.getGroupKey=r,t.group=l,t.index=u,t.normalizeTreeToArray=s,t.requireVersion=d,t.sort=c,t.sum=g})); | ||
!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";const i=(t,e)=>e>t||t.length>e.length&&t.slice(0,e.length)===e,r=t=>""+t;function o(t,e,i,s=[],a=0,h=[]){const l=a-1;if(t[0]in i&&a>0){const n=s.reduce((function(t,e,n){return n!==l&&(t[r(n)]=e),t}),{});n[e]=s[l],t.forEach((function(t){n[t]=i[t]})),h.push(n)}else for(const r of Object.keys(i)){const l=i[r];n.isObject(l)&&(s.push(r),o(t,e,l,s,a+1,h))}return s.splice(l,1),h}function s(t,e,n){const i=o(t,e,n);if(!i.length)return i;const s=i.reduce((function(t,e){const n=Object.keys(e).length-2;return t>n?t:n}));return i.forEach((function(t){for(let e=0;e<s;e++){const n=r(e);t[n]||(t[n]="")}})),i}function a(t){const e=[...t],n=[];for(;e.length;){const t=e.pop();Array.isArray(t)?e.push(...t):n.push(t)}return n.reverse()}function h(t,e,n){if(!t.length)return;const i=[];for(const r of t){const t=e[r];if(""===t){i.push(n);break}i.push(t)}return i.length?i.join("."):n}function l(t,e,n,i,r,o,s=[]){const a=n[0],l=n.slice(1),c=Object.create(null),u=Object.create(null),f=[];let d,g,p;for(g=0,p=t.length;g<p;++g){const n=t[g];if(r&&n[r]!==o)continue;if(d=n[e]||n[i]||"",!(d in c)){const t=c[d]={value:0};l.forEach((function(e){t[e]=0})),u[d]=[]}c[d].value+=+n[a],c[d].label=n[e]||"";const f=c[d];l.forEach((function(t){f[t]+=n[t]})),c[d].path=h(s,n,d),u[d].push(n)}return Object.keys(c).forEach((t=>{const n={children:u[t]};n[a]=+c[t].value,l.forEach((function(e){n[e]=+c[t][e]})),n[e]=c[t].label,n.label=t,n.path=c[t].path,r&&(n[r]=o),f.push(n)})),f}function c(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<r;++i)o?t[i]._idx=i:t[i]={v:t[i],_idx:i};return e}function u(t,e){e?t.sort(((t,n)=>+n[e]-+t[e])):t.sort(((t,e)=>+e-+t))}function f(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}function d(t,e,n,r=!0){const o=n.split(".");let s=0;for(const a of e.split(".")){const h=o[s++];if(parseInt(a,10)<parseInt(h,10))break;if(i(h,a)){if(r)throw new Error(`${t} v${n} is not supported. v${e} or newer is required.`);return!1}}return!0}const g=new Map;function p(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 m(t,e,n){return Math.max(Math.min(t,n),e)}function x(t,e,i){const r=n.toTRBL(t);return{t:m(r.top,0,i),r:m(r.right,0,e),b:m(r.bottom,0,i),l:m(r.left,0,e)}}function b(t){const e=p(t),i=e.right-e.left,r=e.bottom-e.top,o=x(t.options.borderWidth,i/2,r/2),s=function(t,e,i){const r=n.toTRBLCorners(t),o=Math.min(e,i);return{topLeft:m(r.topLeft,0,o),topRight:m(r.topRight,0,o),bottomLeft:m(r.bottomLeft,0,o),bottomRight:m(r.bottomRight,0,o)}}(t.options.borderRadius,i/2,r/2),a={x:e.left,y:e.top,w:i,h:r,active:t.active,radius:s};return{outer:a,inner:{x:a.x+o.l,y:a.y+o.t,w:a.w-o.l-o.r,h:a.h-o.t-o.b,active:t.active,radius:{topLeft:Math.max(0,s.topLeft-Math.max(o.t,o.l)),topRight:Math.max(0,s.topRight-Math.max(o.t,o.r)),bottomLeft:Math.max(0,s.bottomLeft-Math.max(o.b,o.l)),bottomRight:Math.max(0,s.bottomRight-Math.max(o.b,o.r))}}}}function y(t,e,n,i){const r=null===e,o=null===n,s=!(!t||r&&o)&&p(t,i);return s&&(r||e>=s.left&&e<=s.right)&&(o||n>=s.top&&n<=s.bottom)}function v(t,e){t.rect(e.x,e.y,e.w,e.h)}function w(t,e){if(!e||!1===e.display)return!1;const{w:i,h:r}=t,o=n.toFont(e.font).lineHeight,s=m(2*n.valueOrDefault(e.padding,3),0,Math.min(i,r));return i-s>o&&r-s>o}function _(t,e,i,r,o){const{captions:s,labels:a}=i;t.save(),t.beginPath(),t.rect(e.x,e.y,e.w,e.h),t.clip();const h=r&&(!n.defined(r.l)||r.l===o);h&&a.display?function(t,e,i){const r=i.labels,o=r.formatter;if(!o)return;const s=n.isArray(o)?o:[o];let a=function(t,e){const{font:i,hoverFont:r}=e,o=(t.active?r:i)||i;return n.isArray(o)?o.map((t=>n.toFont(t))):[n.toFont(o)]}(e,r),h=function(t,e,n){const i=n.reduce((function(t,e){return t+=e.string}),""),r=e.join()+i+(t._measureText?"-spriting":"");if(!g.has(r)){t.save();const i=e.length;let o=0,s=0;for(let r=0;r<i;r++){const i=n[Math.min(r,n.length-1)];t.font=i.string;const a=e[r];o=Math.max(o,t.measureText(a).width),s+=i.lineHeight}t.restore(),g.set(r,{width:o,height:s})}return g.get(r)}(t,s,a);const l=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,r,h);if(!l)return;n.isNumber(l)&&(h={width:h.width*l,height:h.height*l},a=function(t,e){return t.map((function(t){return t.size=Math.floor(t.size*e),t.lineHeight=void 0,n.toFont(t)}))}(a,l));const{color:c,hoverColor:u,align:f}=r,d=(e.active?u:c)||c,p=n.isArray(d)?d:[d],m=function(t,e,n){const{align:i,position:r,padding:o}=e;let s,a;s=M(t,i,o),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,r,h);t.textAlign=f,t.textBaseline="middle";let x=0;s.forEach((function(e,n){const i=p[Math.min(n,p.length-1)],r=a[Math.min(n,a.length-1)],o=r.lineHeight;t.font=r.string,t.fillStyle=i,t.fillText(e,m.x,m.y+o/2+x),x+=o}))}(t,e,i):!h&&w(e,s)&&function(t,e,i,r){const{captions:o,spacing:s,rtl:a}=i,{color:h,hoverColor:l,font:c,hoverFont:u,padding:f,align:d,formatter:g}=o,p=(e.active?l:h)||h,m=d||(a?"right":"left"),x=(e.active?u:c)||c,b=n.toFont(x),y=b.lineHeight/2,v=M(e,m,f);t.fillStyle=p,t.font=b.string,t.textAlign=m,t.textBaseline="middle",t.fillText(g||r.g,v,e.y+f+s+y)}(t,e,i,r),t.restore()}function M(t,e,n){return"left"===e?t.x+n:"right"===e?t.x+t.w-n:t.x+t.w/2}class C extends e.Element{constructor(t){super(),this.options=void 0,this.width=void 0,this.height=void 0,t&&Object.assign(this,t)}draw(t,e,i=0){if(!e)return;const r=this.options,{inner:o,outer:s}=b(this),a=(h=s.radius).topLeft||h.topRight||h.bottomLeft||h.bottomRight?n.addRoundedRectPath:v;var h;t.save(),s.w===o.w&&s.h===o.h||(t.beginPath(),a(t,s),t.clip(),a(t,o),t.fillStyle=r.borderColor,t.fill("evenodd")),t.beginPath(),a(t,o),t.fillStyle=r.backgroundColor,t.fill(),function(t,e,n,i){const r=n.dividers;if(!r.display||!i._data.children.length)return;const{x:o,y:s,w:a,h:h}=e,{lineColor:l,lineCapStyle:c,lineDash:u,lineDashOffset:f,lineWidth:d}=r;if(t.save(),t.strokeStyle=l,t.lineCap=c,t.setLineDash(u),t.lineDashOffset=f,t.lineWidth=d,t.beginPath(),a>h){const e=a/2;t.moveTo(o+e,s),t.lineTo(o+e,s+h)}else{const e=h/2;t.moveTo(o,s+e),t.lineTo(o+a,s+e)}t.stroke(),t.restore()}(t,o,r,e),_(t,o,r,e,i),t.restore()}inRange(t,e,n){return y(this,t,e,n)}inXRange(t,e){return y(this,t,null,e)}inYRange(t,e){return y(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}}function k(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}}C.id="treemap",C.defaults={label:void 0,borderRadius:0,borderWidth:0,captions:{align:void 0,color:"black",display:!0,font:{},formatter:t=>t.raw.g||t.raw._data.label||"",padding:3},dividers:{display:!1,lineCapStyle:"butt",lineColor:"black",lineDash:[],lineDashOffset:0,lineWidth:1},labels:{align:"center",color:"black",display:!1,font:{},formatter:t=>t.raw.g?[t.raw.g,t.raw.v+""]:t.raw._data.label?[t.raw._data.label,t.raw.v+""]:t.raw.v+"",overflow:"cut",position:"middle",padding:3},rtl:!1,spacing:.5},C.descriptors={labels:{_fallback:!0},captions:{_fallback:!0},_scriptable:!0,_indexable:!1},C.defaultRoutes={backgroundColor:"backgroundColor",borderColor:"borderColor"};const R=(t,e)=>t.rtl?t.x+t.iw-e:t.x+t._ix;function O(t,e,n,i){const r={x:R(t,n.w),y:t.y+t._iy,w:n.w,h:n.h,a:e._normalized,v:e.value,vs:e.values,s:i,_data:e._data};return e.group&&(r.g=e.group,r.l=e.level,r.gs=e.groupSum),r}class j{constructor(t){t=t||{w:1,h:1},this.rtl=!!t.rtl,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}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,a=r*r,h=[];let l=0,c=0;for(const e of o){const n=k(e,s,a,i);c+=n.d1,l=Math.max(l,n.d2),h.push(O(this,e,n,t.sum)),this[i]+=n.d1}return this["x"===e?"_iy":"_ix"]+=l,this[i]-=c,h}}const T=Math.min,E=Math.max;function P(t,e){const n=+e[t.key],i=n*t.ratio;return e._normalized=i,{min:T(t.min,n),max:E(t.max,n),sum:t.sum+n,nmin:T(t.nmin,i),nmax:E(t.nmax,i),nsum:t.nsum+i}}function S(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){S(this,t,P(this,t))}pushIf(t,e,...n){const i=P(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;S(this,t,i)}get(){return this._arr}}function L(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 F(t,e,n=[],i,r,o){t=t||[];const s=[],h=new j(e),l=new D("value",h.area/f(t,n[0]));let d=h.side;const g=t.length;let p,m;if(!g)return s;const x=t.slice();let b=c(x,n[0]);u(x,b);const y=t=>i&&x[t][i];for(p=0;p<g;++p){if(m={value:(v=p,b?+x[v][b]:+x[v]),groupSum:o,_data:t[x[p]._idx],level:void 0,group:void 0},i){m.level=r,m.group=y(p);const t=x[p];m.values=n.reduce((function(e,n){return e[n]=+t[n],e}),{})}m=l.pushIf(m,L,d),m&&(s.push(h.map(l)),d=h.side,l.reset(),l.push(m))}var v;return l.length&&s.push(h.map(l)),a(s)}function A(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{x:o+i,y:s+i,width:a-r,height:h-r,hidden:r>a||r>h}}function z(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;n<i;++n)if(t[n]!==e[n])return!0;return!1}class H extends e.DatasetController{constructor(t,e){super(t,e),this._groups=void 0,this._keys=void 0,this._rect=void 0,this._rectChanged=!0}initialize(){this.enableOptionSharing=!0,super.initialize()}getMinMax(t){return{min:0,max:"x"===t.axis?t.right-t.left:t.bottom-t.top}}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={x:0,y:0,w:n,h:i,rtl:!!this.options.rtl};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||(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:i}=this.getMeta(),o=e.groups||[],a=[e.key||""].concat(e.sumKeys||[]),h=e.tree=e.tree||e.data||[];"reset"===t&&this.configure(),(this._rectChanged||z(this._keys,a)||z(this._groups,o)||this._prevTree!==h)&&(this._groups=o.slice(),this._keys=a.slice(),this._prevTree=h,this._rectChanged=!1,e.data=function(t,e,i,o){const a=e.treeLeafKey||"_leaf";n.isObject(t)&&(t=s(i,a,t));const h=e.groups||[],c=h.length,u=n.valueOrDefault(e.spacing,0),f=e.captions||{},d=n.toFont(f.font),g=n.valueOrDefault(f.padding,3);return c?function n(o,s,p,m){const b=r(h[o]),y=o>0&&r(h[o-1]),v=l(t,b,i,a,y,p,h.filter(((t,e)=>e<=o))),_=F(v,s,i,b,o,m),M=_.slice();return o<c-1&&_.forEach((t=>{const i=x(e.borderWidth,t.w/2,t.h/2),r={...s,x:t.x+u+i.l,y:t.y+u+i.t,w:t.w-2*u-i.l-i.r,h:t.h-2*u-i.t-i.b};w(r,f)&&(r.y+=d.lineHeight+2*g,r.h-=d.lineHeight+2*g),M.push(...n(o+1,r,t.g,t.s))})),M}(0,o):F(t,o,i)}(h,e,this._keys,this._rect),this._dataCheck(),this._resyncElements()),this.updateElements(i,0,i.length,t)}updateElements(t,e,n,i){const r="reset"===i,o=this.getDataset(),s=this._rect.options=this.resolveDataElementOptions(e,i),a=this.getSharedOptions(s),h=this.includeOptions(i,a),{xScale:l,yScale:c}=this.getMeta(this.index);for(let s=e;s<e+n;s++){const e=a||this.resolveDataElementOptions(s,i),n=A(o.data[s],l,c,e.spacing);r&&(n.width=0,n.height=0),h&&(n.options=e),this.updateElement(t[s],s,n,i)}this.updateSharedOptions(a,i,s)}draw(){const{ctx:t,chartArea:e}=this.chart,i=this.getMeta().data||[],r=this.getDataset(),o=(r.groups||[]).length-1,s=r.data;n.clipArea(t,e);for(let e=0,n=i.length;e<n;++e){const n=i[e];n.hidden||n.draw(t,s[e],o)}n.unclipArea(t)}}H.id="treemap",H.version="2.3.0",H.defaults={dataElementType:"treemap",animations:{numbers:{type:"number",properties:["x","y","width","height"]}}},H.descriptors={_scriptable:!0,_indexable:!1},H.overrides={interaction:{mode:"point",includeInvisible:!0,intersect:!0},hover:{},plugins:{tooltip:{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],i=n.g||n._data.label||e.label;return(i?i+": ":"")+n.v}}}},scales:{x:{type:"linear",alignToPixels:!0,bounds:"data",display:!1},y:{type:"linear",alignToPixels:!0,bounds:"data",display:!1,reverse:!0}}},H.beforeRegister=function(){d("chart.js","3.8",e.Chart.version)},H.afterRegister=function(){const t=e.registry.plugins.get("tooltip");t?t.positioners.treemap=function(t){if(!t.length)return!1;return t[t.length-1].element.tooltipPosition()}:console.warn("Unable to register the treemap positioner because tooltip plugin is not registered")},H.afterUnregister=function(){const t=e.registry.plugins.get("tooltip");t&&delete t.positioners.treemap},e.Chart.register(H,C),t.flatten=a,t.getGroupKey=r,t.group=l,t.index=c,t.normalizeTreeToArray=s,t.requireVersion=d,t.sort=u,t.sum=f})); | ||
//# sourceMappingURL=chartjs-chart-treemap.min.js.map |
{ | ||
"name": "chartjs-chart-treemap", | ||
"homepage": "https://chartjs-chart-treemap.pages.dev/", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Chart.js module for creating treemap charts", | ||
@@ -6,0 +6,0 @@ "type": "module", |
# chartjs-chart-treemap | ||
[Chart.js](https://www.chartjs.org/) **v3.8.0** module for creating treemap charts. Implementation for Chart.js v2 is in [2.x branch](https://github.com/kurkle/chartjs-chart-treemap/tree/2.x) | ||
[Chart.js](https://www.chartjs.org/) **v3.8+, v4+** module for creating treemap charts. Implementation for Chart.js v2 is in [2.x branch](https://github.com/kurkle/chartjs-chart-treemap/tree/2.x) | ||
@@ -5,0 +5,0 @@ [![npm](https://img.shields.io/npm/v/chartjs-chart-treemap.svg)](https://www.npmjs.com/package/chartjs-chart-treemap) |
@@ -44,3 +44,3 @@ import { | ||
export type LabelOverflow = 'cut' | 'hidden'; | ||
export type LabelOverflow = 'cut' | 'hidden' | 'fit'; | ||
@@ -75,2 +75,3 @@ type TreemapControllerDatasetDividersOptions = { | ||
groups?: Array<keyof DType>; | ||
sumKeys?: Array<keyof DType>; | ||
tree: number[] | DType[] | AnyObject; | ||
@@ -106,2 +107,6 @@ treeLeafKey?: keyof DType; | ||
gs?: number, | ||
/** | ||
* additonal keys sums, only available if grouping | ||
*/ | ||
vs?: AnyObject | ||
} | ||
@@ -108,0 +113,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
139520
8
2278