waffle-chart
Version:
28 lines (24 loc) • 18 kB
JavaScript
(function(F,Y){typeof exports=="object"&&typeof module<"u"?Y(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],Y):(F=typeof globalThis<"u"?globalThis:F||self,Y(F.WaffleChart={},F.React))})(this,function(F,Y){"use strict";const Oe=(t=>t&&typeof t=="object"&&"default"in t?t:{default:t})(Y),Ar="";var le={exports:{}},q={};/**
* @license React
* react-jsx-runtime.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/var Se;function Ie(){if(Se)return q;Se=1;var t=Oe.default,i=Symbol.for("react.element"),o=Symbol.for("react.fragment"),d=Object.prototype.hasOwnProperty,g=t.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,b={key:!0,ref:!0,__self:!0,__source:!0};function _(h,l,m){var y,s={},v=null,C=null;m!==void 0&&(v=""+m),l.key!==void 0&&(v=""+l.key),l.ref!==void 0&&(C=l.ref);for(y in l)d.call(l,y)&&!b.hasOwnProperty(y)&&(s[y]=l[y]);if(h&&h.defaultProps)for(y in l=h.defaultProps,l)s[y]===void 0&&(s[y]=l[y]);return{$$typeof:i,type:h,key:v,ref:C,props:s,_owner:g.current}}return q.Fragment=o,q.jsx=_,q.jsxs=_,q}var V={};/**
* @license React
* react-jsx-runtime.development.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/var we;function Je(){return we||(we=1,process.env.NODE_ENV!=="production"&&function(){var t=Oe.default,i=Symbol.for("react.element"),o=Symbol.for("react.portal"),d=Symbol.for("react.fragment"),g=Symbol.for("react.strict_mode"),b=Symbol.for("react.profiler"),_=Symbol.for("react.provider"),h=Symbol.for("react.context"),l=Symbol.for("react.forward_ref"),m=Symbol.for("react.suspense"),y=Symbol.for("react.suspense_list"),s=Symbol.for("react.memo"),v=Symbol.for("react.lazy"),C=Symbol.for("react.offscreen"),j=Symbol.iterator,$="@@iterator";function U(e){if(e===null||typeof e!="object")return null;var r=j&&e[j]||e[$];return typeof r=="function"?r:null}var x=t.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function T(e){{for(var r=arguments.length,n=new Array(r>1?r-1:0),a=1;a<r;a++)n[a-1]=arguments[a];B("error",e,n)}}function B(e,r,n){{var a=x.ReactDebugCurrentFrame,f=a.getStackAddendum();f!==""&&(r+="%s",n=n.concat([f]));var p=n.map(function(u){return String(u)});p.unshift("Warning: "+r),Function.prototype.apply.call(console[e],console,p)}}var fe=!1,de=!1,ve=!1,pe=!1,Q=!1,z;z=Symbol.for("react.module.reference");function ee(e){return!!(typeof e=="string"||typeof e=="function"||e===d||e===b||Q||e===g||e===m||e===y||pe||e===C||fe||de||ve||typeof e=="object"&&e!==null&&(e.$$typeof===v||e.$$typeof===s||e.$$typeof===_||e.$$typeof===h||e.$$typeof===l||e.$$typeof===z||e.getModuleId!==void 0))}function re(e,r,n){var a=e.displayName;if(a)return a;var f=r.displayName||r.name||"";return f!==""?n+"("+f+")":n}function te(e){return e.displayName||"Context"}function w(e){if(e==null)return null;if(typeof e.tag=="number"&&T("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),typeof e=="function")return e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case d:return"Fragment";case o:return"Portal";case b:return"Profiler";case g:return"StrictMode";case m:return"Suspense";case y:return"SuspenseList"}if(typeof e=="object")switch(e.$$typeof){case h:var r=e;return te(r)+".Consumer";case _:var n=e;return te(n._context)+".Provider";case l:return re(e,e.render,"ForwardRef");case s:var a=e.displayName||null;return a!==null?a:w(e.type)||"Memo";case v:{var f=e,p=f._payload,u=f._init;try{return w(u(p))}catch{return null}}}return null}var k=Object.assign,N=0,G,H,I,he,M,J,ne;function ae(){}ae.__reactDisabledLog=!0;function ye(){{if(N===0){G=console.log,H=console.info,I=console.warn,he=console.error,M=console.group,J=console.groupCollapsed,ne=console.groupEnd;var e={configurable:!0,enumerable:!0,value:ae,writable:!0};Object.defineProperties(console,{info:e,log:e,warn:e,error:e,group:e,groupCollapsed:e,groupEnd:e})}N++}}function ge(){{if(N--,N===0){var e={configurable:!0,enumerable:!0,writable:!0};Object.defineProperties(console,{log:k({},e,{value:G}),info:k({},e,{value:H}),warn:k({},e,{value:I}),error:k({},e,{value:he}),group:k({},e,{value:M}),groupCollapsed:k({},e,{value:J}),groupEnd:k({},e,{value:ne})})}N<0&&T("disabledDepth fell below zero. This is a bug in React. Please file an issue.")}}var P=x.ReactCurrentDispatcher,be;function oe(e,r,n){{if(be===void 0)try{throw Error()}catch(f){var a=f.stack.trim().match(/\n( *(at )?)/);be=a&&a[1]||""}return`
`+be+e}}var Ee=!1,ie;{var ur=typeof WeakMap=="function"?WeakMap:Map;ie=new ur}function Fe(e,r){if(!e||Ee)return"";{var n=ie.get(e);if(n!==void 0)return n}var a;Ee=!0;var f=Error.prepareStackTrace;Error.prepareStackTrace=void 0;var p;p=P.current,P.current=null,ye();try{if(r){var u=function(){throw Error()};if(Object.defineProperty(u.prototype,"props",{set:function(){throw Error()}}),typeof Reflect=="object"&&Reflect.construct){try{Reflect.construct(u,[])}catch(D){a=D}Reflect.construct(e,[],u)}else{try{u.call()}catch(D){a=D}e.call(u.prototype)}}else{try{throw Error()}catch(D){a=D}e()}}catch(D){if(D&&a&&typeof D.stack=="string"){for(var c=D.stack.split(`
`),O=a.stack.split(`
`),E=c.length-1,R=O.length-1;E>=1&&R>=0&&c[E]!==O[R];)R--;for(;E>=1&&R>=0;E--,R--)if(c[E]!==O[R]){if(E!==1||R!==1)do if(E--,R--,R<0||c[E]!==O[R]){var S=`
`+c[E].replace(" at new "," at ");return e.displayName&&S.includes("<anonymous>")&&(S=S.replace("<anonymous>",e.displayName)),typeof e=="function"&&ie.set(e,S),S}while(E>=1&&R>=0);break}}}finally{Ee=!1,P.current=p,ge(),Error.prepareStackTrace=f}var L=e?e.displayName||e.name:"",He=L?oe(L):"";return typeof e=="function"&&ie.set(e,He),He}function lr(e,r,n){return Fe(e,!1)}function fr(e){var r=e.prototype;return!!(r&&r.isReactComponent)}function se(e,r,n){if(e==null)return"";if(typeof e=="function")return Fe(e,fr(e));if(typeof e=="string")return oe(e);switch(e){case m:return oe("Suspense");case y:return oe("SuspenseList")}if(typeof e=="object")switch(e.$$typeof){case l:return lr(e.render);case s:return se(e.type,r,n);case v:{var a=e,f=a._payload,p=a._init;try{return se(p(f),r,n)}catch{}}}return""}var ce=Object.prototype.hasOwnProperty,Ne={},Me=x.ReactDebugCurrentFrame;function ue(e){if(e){var r=e._owner,n=se(e.type,e._source,r?r.type:null);Me.setExtraStackFrame(n)}else Me.setExtraStackFrame(null)}function dr(e,r,n,a,f){{var p=Function.call.bind(ce);for(var u in e)if(p(e,u)){var c=void 0;try{if(typeof e[u]!="function"){var O=Error((a||"React class")+": "+n+" type `"+u+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+typeof e[u]+"`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.");throw O.name="Invariant Violation",O}c=e[u](r,u,a,n,null,"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED")}catch(E){c=E}c&&!(c instanceof Error)&&(ue(f),T("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).",a||"React class",n,u,typeof c),ue(null)),c instanceof Error&&!(c.message in Ne)&&(Ne[c.message]=!0,ue(f),T("Failed %s type: %s",n,c.message),ue(null))}}}var vr=Array.isArray;function _e(e){return vr(e)}function pr(e){{var r=typeof Symbol=="function"&&Symbol.toStringTag,n=r&&e[Symbol.toStringTag]||e.constructor.name||"Object";return n}}function hr(e){try{return $e(e),!1}catch{return!0}}function $e(e){return""+e}function We(e){if(hr(e))return T("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.",pr(e)),$e(e)}var K=x.ReactCurrentOwner,yr={key:!0,ref:!0,__self:!0,__source:!0},Le,Ye,Re;Re={};function gr(e){if(ce.call(e,"ref")){var r=Object.getOwnPropertyDescriptor(e,"ref").get;if(r&&r.isReactWarning)return!1}return e.ref!==void 0}function br(e){if(ce.call(e,"key")){var r=Object.getOwnPropertyDescriptor(e,"key").get;if(r&&r.isReactWarning)return!1}return e.key!==void 0}function Er(e,r){if(typeof e.ref=="string"&&K.current&&r&&K.current.stateNode!==r){var n=w(K.current.type);Re[n]||(T('Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. This case cannot be automatically converted to an arrow function. We ask you to manually fix this case by using useRef() or createRef() instead. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-string-ref',w(K.current.type),e.ref),Re[n]=!0)}}function _r(e,r){{var n=function(){Le||(Le=!0,T("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};n.isReactWarning=!0,Object.defineProperty(e,"key",{get:n,configurable:!0})}}function Rr(e,r){{var n=function(){Ye||(Ye=!0,T("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};n.isReactWarning=!0,Object.defineProperty(e,"ref",{get:n,configurable:!0})}}var mr=function(e,r,n,a,f,p,u){var c={$$typeof:i,type:e,key:r,ref:n,props:u,_owner:p};return c._store={},Object.defineProperty(c._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:!1}),Object.defineProperty(c,"_self",{configurable:!1,enumerable:!1,writable:!1,value:a}),Object.defineProperty(c,"_source",{configurable:!1,enumerable:!1,writable:!1,value:f}),Object.freeze&&(Object.freeze(c.props),Object.freeze(c)),c};function Tr(e,r,n,a,f){{var p,u={},c=null,O=null;n!==void 0&&(We(n),c=""+n),br(r)&&(We(r.key),c=""+r.key),gr(r)&&(O=r.ref,Er(r,f));for(p in r)ce.call(r,p)&&!yr.hasOwnProperty(p)&&(u[p]=r[p]);if(e&&e.defaultProps){var E=e.defaultProps;for(p in E)u[p]===void 0&&(u[p]=E[p])}if(c||O){var R=typeof e=="function"?e.displayName||e.name||"Unknown":e;c&&_r(u,R),O&&Rr(u,R)}return mr(e,c,O,f,a,K.current,u)}}var me=x.ReactCurrentOwner,qe=x.ReactDebugCurrentFrame;function W(e){if(e){var r=e._owner,n=se(e.type,e._source,r?r.type:null);qe.setExtraStackFrame(n)}else qe.setExtraStackFrame(null)}var Te;Te=!1;function Ce(e){return typeof e=="object"&&e!==null&&e.$$typeof===i}function Ve(){{if(me.current){var e=w(me.current.type);if(e)return`
Check the render method of \``+e+"`."}return""}}function Cr(e){{if(e!==void 0){var r=e.fileName.replace(/^.*[\\\/]/,""),n=e.lineNumber;return`
Check your code at `+r+":"+n+"."}return""}}var Ue={};function Or(e){{var r=Ve();if(!r){var n=typeof e=="string"?e:e.displayName||e.name;n&&(r=`
Check the top-level render call using <`+n+">.")}return r}}function Be(e,r){{if(!e._store||e._store.validated||e.key!=null)return;e._store.validated=!0;var n=Or(r);if(Ue[n])return;Ue[n]=!0;var a="";e&&e._owner&&e._owner!==me.current&&(a=" It was passed a child from "+w(e._owner.type)+"."),W(e),T('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.',n,a),W(null)}}function ze(e,r){{if(typeof e!="object")return;if(_e(e))for(var n=0;n<e.length;n++){var a=e[n];Ce(a)&&Be(a,r)}else if(Ce(e))e._store&&(e._store.validated=!0);else if(e){var f=U(e);if(typeof f=="function"&&f!==e.entries)for(var p=f.call(e),u;!(u=p.next()).done;)Ce(u.value)&&Be(u.value,r)}}}function Sr(e){{var r=e.type;if(r==null||typeof r=="string")return;var n;if(typeof r=="function")n=r.propTypes;else if(typeof r=="object"&&(r.$$typeof===l||r.$$typeof===s))n=r.propTypes;else return;if(n){var a=w(r);dr(n,e.props,"prop",a,e)}else if(r.PropTypes!==void 0&&!Te){Te=!0;var f=w(r);T("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?",f||"Unknown")}typeof r.getDefaultProps=="function"&&!r.getDefaultProps.isReactClassApproved&&T("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.")}}function wr(e){{for(var r=Object.keys(e.props),n=0;n<r.length;n++){var a=r[n];if(a!=="children"&&a!=="key"){W(e),T("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.",a),W(null);break}}e.ref!==null&&(W(e),T("Invalid attribute `ref` supplied to `React.Fragment`."),W(null))}}function Ge(e,r,n,a,f,p){{var u=ee(e);if(!u){var c="";(e===void 0||typeof e=="object"&&e!==null&&Object.keys(e).length===0)&&(c+=" You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.");var O=Cr(f);O?c+=O:c+=Ve();var E;e===null?E="null":_e(e)?E="array":e!==void 0&&e.$$typeof===i?(E="<"+(w(e.type)||"Unknown")+" />",c=" Did you accidentally export a JSX literal instead of a component?"):E=typeof e,T("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s",E,c)}var R=Tr(e,r,n,f,p);if(R==null)return R;if(u){var S=r.children;if(S!==void 0)if(a)if(_e(S)){for(var L=0;L<S.length;L++)ze(S[L],e);Object.freeze&&Object.freeze(S)}else T("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else ze(S,e)}return e===d?wr(R):Sr(R),R}}function xr(e,r,n){return Ge(e,r,n,!0)}function jr(e,r,n){return Ge(e,r,n,!1)}var kr=jr,Pr=xr;V.Fragment=d,V.jsx=kr,V.jsxs=Pr}()),V}(function(t){process.env.NODE_ENV==="production"?t.exports=Ie():t.exports=Je()})(le);const A=le.exports.jsx,X=le.exports.jsxs,xe="waffle-chart-square",Ke=({gridItemProps:t,isAnimatedFill:i,isZeros:o,clickHandler:d})=>{const g=[xe];g.push(t==null?void 0:t.identifier),i&&g.push("animate-fill"),o&&g.push("zeroed");const b={background:`var(--bg-fallback-${t==null?void 0:t.identifier})`};return A("div",{"data-testid":xe,className:g.join(" "),style:(t==null?void 0:t.style)||b,title:`square${t==null?void 0:t.index}`,onClick:()=>d(t)},t==null?void 0:t.index)},je="chart-container",Xe=({itemProps:t=[],verticalFill:i="bottom",horizontalFill:o="right",isAnimatedFill:d=!0,isZeros:g=!1,clickHandler:b})=>{const _=t==null?void 0:t.map((h,l)=>A(Ke,{gridItemProps:h,isAnimatedFill:d,isZeros:g,clickHandler:b},`${h.identifier}${l}`));return A("div",{className:`waffle-chart ${je} ${i} ${o}`,"data-testid":je,children:_})},ke="display-item",Ze=({data1:t,data2:i,data3:o,verticalFill:d,precision:g})=>{const b=["waffle-chart-display-container",d],_=({id:l,value:m,percentage:y,label:s,style:v})=>X("div",{className:[ke,l].join(" "),style:{color:v==null?void 0:v.color},"data-testid":ke,children:[X("h3",{className:"display-item-title","data-testid":"display-percentage",children:[y.toFixed(g),"%"]}),X("p",{className:"display-item-description","data-testid":"display-count",children:[m,"\xA0",s]})]},`${s}${m}`),h=[t,i,o].map(l=>_(l));return A("div",{className:b.join(" "),"data-testid":"display-container",children:h})},Pe="total",Qe=({style:t=void 0,children:i})=>A("h3",{className:Pe,style:t,"data-testid":Pe,children:i}),De="waffle-chart-container",er=(t,i,o)=>t+i+o,rr=(t,i,o)=>[t,i,o].map(d=>d<=0?0:d),tr=(t,i)=>t/i*100||0,nr=t=>t.sort((i,o)=>o.value-i.value),Z=t=>t?{color:t,background:t}:void 0,ar=(t,i)=>{const o={...t};switch(t.percentage>100&&(o.value=100),i){case"nearest":o.percentage=Math.round(t.percentage);break;case"up":o.percentage=Math.ceil(t.percentage);break;case"down":o.percentage=Math.floor(t.percentage);break;default:o.percentage=Math.round(t.percentage)}return o},or=([t,i,o])=>{const d=o.percentage,g=i.percentage,b=d+g,_=[];for(let h=100;h>0;h--){const l=h<=d,m=h>d&&h<=b,y=!m&&!l,s=l?o.id:m?i.id:t.id,v=l?o.style:m?i.style:y?t.style:void 0,C={index:h,identifier:s,isSmall:l,isMedium:m,isLarge:y,style:v};_.push(C)}return t.percentage===50?_.reverse():_},Ae=t=>{const i=Math.sqrt(t),o=Math.ceil(i),d=Math.floor(t/o)||0,g=t-o*d||0;return{c:o,r:d,e:g}},ir=([t,i,o])=>{const d=[],g=o.percentage?o.percentage+i.percentage:i.percentage,b=Ae(o.percentage),_=Ae(g),h=s=>Math.ceil(s/10)*10/10,l=(s,v)=>s*10-10+v,m=(s,v,C)=>v<s.r&&C<=l(v,s.c)||v===s.r&&C<=l(v,s.c),y=(s,v,C)=>v===s.r+1&&C<=l(v,s.e);for(let s=100;s>0;s--){const v=h(s),C=b.r?m(b,v,s)||y(b,v,s):!1,j=m(_,v,s)||y(_,v,s),$=!C&&!j,U=C?o.id:j?i.id:t.id,x=C?o.style:j?i.style:$?t.style:void 0,T={index:s,identifier:U,isSmall:C,isMedium:j,isLarge:$,style:x};d.push(T)}return d},sr=([t,i,o],d)=>{const b=o.percentage>2,_=b||i.percentage>2;return!(t.percentage===50&&!o.percentage)&&d&&(b||_)?ir([t,i,o]):or([t,i,o])},cr=({partA:t=0,partB:i=0,partC:o=0,displayPrecision:d=0,partAlabel:g="A",partBlabel:b="B",partClabel:_="C",rounding:h="nearest",isFilledFromTop:l=!1,isFilledFromLeft:m=!1,isSquareFill:y=!0,isAnimatedFill:s=!0,showDataDisplay:v=!0,showTotal:C=!0,partAcolor:j=void 0,partBcolor:$=void 0,partCcolor:U=void 0,totalColor:x=void 0,clickHandler:T=B=>B})=>{const B=P=>T(P),fe=Z(j),de=Z($),ve=Z(U),pe=Z(x),[Q,z,ee]=rr(t,i,o),re=er(Q,z,ee),te=re.toFixed(d),[w,k,N]=[Q,z,ee].map(P=>tr(P,re)),G={id:"a",value:t,percentage:w,label:g,style:fe},H={id:"b",value:i,percentage:k,label:b,style:de},I={id:"c",value:o,percentage:N,label:_,style:ve},M=nr([G,H,I]).map(P=>ar(P,h)),J=l?"top":"bottom",ne=m?"left":"right",ae=C?A(Qe,{style:pe,children:te}):null,ye=v&&G&&H&&I?A(Ze,{data1:M[2],data2:M[1],data3:M[0],verticalFill:J,precision:d}):null,ge=sr(M,y);return X("div",{className:De,"data-testid":De,children:[ae,A(Xe,{itemProps:ge,verticalFill:J,horizontalFill:ne,isAnimatedFill:s,isZeros:!t&&!i,clickHandler:B}),ye]})};F.WaffleChart=cr,Object.defineProperties(F,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});