UNPKG

@devgateway/dvz-ui-react

Version:

A modular, embeddable React component library for data visualization and UI, built with TypeScript. Provides reusable components for charts, maps, dashboards, and more, with built-in support for internationalization and Redux integration.

2 lines (1 loc) 6.08 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const L=require("react/jsx-runtime"),q=require("react"),c=require("./Utils.js"),C=a=>{const{data:t,measures:v,swap:w,dimensions:g,locale:E,customLabels:S}=a;let n={};const A=g.filter(M=>M!=""),_=new Set;if(A.length==0&&t){const M=c.measuresMap(t),N=new Set,T=new Set;let u=[],r;if(t.metadata&&t.metadata.measures){const y=t.metadata.measures.filter(o=>v.includes(o.value)).sort((o,p)=>o.position!=null&&p.position!=null&&o.position!=p.position?o.position-p.position:0);u=[],r="measure",N.add("measure");const B={};Object.keys(t).forEach(o=>{B[o]=t[o]}),y.forEach(o=>{const p={},m=S[o.value]||c.getTranslatedValue(M[o.value],E);p.type="measure",p.measureFieldName=o.value,p.measure=m,p[m]=t[o.value],p.variables=B,u.push(p),T.add(m),_.add(M[o.value])}),n={categories:N,indexBy:r,keys:Array.from(T),measuresMetadata:_,data:u}}}return n},K=a=>{const{data:t,measures:v,dimensions:w,overallLabel:g}=a;if(w.length==1&&t.children){if(!(t.children.filter(S=>S.value==g).length>0)){const S={};S.type=w[0],S.value=g,S.label=g,Object.keys(t).forEach(n=>{["children","metadata","type","value"].includes(n)||(S[n]=t[n])}),t.children=[S,...t.children]}}else w.length==2&&t.children&&t.children.forEach(E=>{if(!(E.children.filter(n=>n.value==g).length>0)){const n={};n.type=w[1],n.value=g,n.label=g,Object.keys(E).forEach(A=>{["children","metadata","type","value"].includes(A)||(n[A]=E[A])}),E.children=[n,...E.children]}});return t},U=a=>{var u;let t={};const{data:v,measures:w,swap:g,dimensions:E,includeOverall:S,locale:n,customLabels:A,colorBy:_,hiddenBars:M}=a,N=E.filter(r=>r!=""),T=(u=v==null?void 0:v.metadata)==null?void 0:u.measures.filter(r=>w.includes(r.value)).sort((r,y)=>r.position!=null&&y.position!=null&&r.position!=y.position?r.position-y.position:0);if(S&&w.length==1&&K(a),N.length==0&&v)t=C(a);else if(v&&v.children&&N.length>0){const r=c.measuresMap(v),y=c.typesMap(v),B=new Set,o=new Set,p=new Set,m=[];let R;g&&N.length==1&&w.length>0?(R="measure",T.forEach(s=>{const d={};d.measure=A[s.value]||c.getTranslatedValue(r[s.value],n),o.add(r[s.value]),v.children.forEach(h=>{const f=c.getTranslatedValue(y[h.type].items.filter(i=>i.value===h.value)[0],n)||h.value,e={};Object.keys(h).forEach(i=>{e[i]=h[i]}),e[h.type]=h.value.toString(),d.variables=e,B.add(y[h.type]),d[f]=h[s.value],p.add(f)}),m.push({...d})})):(v.children[0]&&(R=v.children[0].type),v.children.forEach(s=>{const d={},h={};h[s.type]=c.getTranslatedValue(y[s.type]&&y[s.type].items?y[s.type].items.filter(f=>f.value===s.value)[0]:s.value,n)||s.value,Object.keys(s).forEach(f=>{d[f]=s[f]}),B.add(y[s.type]),d[s.type]=s.value.toString(),T.map(f=>{const e=A[f.value]||c.getTranslatedValue(r[f.value],n);h[e]=s[f.value],o.add(r[f.value]),p.add(e)}),m.push({...h,variables:d,parent_variables:d})}));const D=Array.from(p);let O=M&&m?m.filter(s=>M.indexOf(s[R])==-1):m;a.sort=="alphabetically"&&(O=O.sort((s,d)=>c.alphaSort(a.sortReverse,n,s[R],d[R]))),a.sort=="date"&&(O=O.sort((s,d)=>c.dateSort(a.sortReverse,s[R],d[R]))),a.sort=="values"&&(O=O.sort((s,d)=>{const h=Math.max(...D.map(e=>s[e])),f=Math.max(...D.map(e=>d[e]));return c.numericSort(a.sortReverse,h,f)})),t={metadata:v.metadata,indexBy:R,dimensionsMetadata:B,measuresMetadata:o,keys:D,data:O}}return q.Children.map(a.children,r=>q.cloneElement(r,{options:t}))},P=a=>{var N,T;const{data:t,measures:v,includeOverall:w,dimensions:g,hiddenBars:E,colorBy:S,locale:n,customLabels:A}=a,_=g.filter(u=>u!="");let M={};if(w&&K(a),_.length==0&&t)M=C(a);else if(t&&t.children&&t.children.length>0&&_.length>0){c.measuresMap(t);const u=c.typesMap(t),r=v[0],y=new Set,B=new Set,o=[],p=[],m=t.children[0].type;let R=0,D,O;t.children.forEach(e=>{const i={variables:{}};if(O=c.getTranslatedValue(u[e.type]&&u[e.type].items?u[e.type].items.filter(l=>l.value===e.value)[0]:e.value,n)||e.value,i[e.type]=O,i[O]=e[r],D=new Object,D[e.type]=O,i.parent_variables=D,Object.keys(e).forEach(l=>{D[l]=e[l]}),y.add(u[e.type]),e.children||B.add(O),e.children)e.children.forEach(l=>{D=new Object,y.add(u[l.type]);const j=c.getTranslatedValue(u[l.type]&&u[l.type].items?u[l.type].items.filter(b=>b.value===l.value)[0]:l.value,n)||l.value;D[e.type]=O,D[l.type]=j,Object.keys(l).forEach(b=>{D[b]=l[b]}),i.variables[j]=D,B.add(j),R+=l[r],p.push(l[r]),i[j]=l[r]});else{const l=new Object;l[e.type]=O,y.add(u[e.type]),Object.keys(t).forEach(j=>{l[j]=e[j]}),i.variables=l}o.push(i)});const s=n.toUpperCase(),d=S=="id"?o:o.filter(e=>E.indexOf(e[m])==-1),h=Array.from(B);a.sort=="alphabetically"?d.sort((e,i)=>c.alphaSort(a.sortReverse,n,e[m],i[m])):a.sort=="date"?d.sort((e,i)=>c.dateSort(a.sortReverse,e[m],i[m])):a.sort=="values"&&d.sort((e,i)=>{if(a.sortSecondDimension=="_total"){const l=Math.max(...h.map(b=>e[b])),j=Math.max(...h.map(b=>i[b]));return c.numericSort(a.sortReverse,l,j)}else{const l=Object.keys(e).reduce((b,x)=>x!==m&&x!=="variables"&&x!=="parent_variables"&&!isNaN(e[x])?b+(e[x]||0):b,0),j=Object.keys(i).reduce((b,x)=>x!==m&&x!=="variables"&&x!=="parent_variables"&&!isNaN(i[x])?b+(i[x]||0):b,0);return c.numericSort(a.sortReverse,l,j)}});const f=[...B];((T=(N=t==null?void 0:t.metadata)==null?void 0:N.types)==null?void 0:T.length)>1&&f.sort((e,i)=>{var l,j;if(a.sortSecondDimension=="date")return c.dateSort(a.sortReverseSecondDimension,e,i);if(a.sortSecondDimension=="alphabetically")return c.alphaSort(a.sortReverseSecondDimension,n,e,i);{const b=t.metadata.types[1].items.filter(V=>V.value==e||V.labels&&V.labels[s]==e),x=t.metadata.types[1].items.filter(V=>V.value==i||V.labels&&V.labels[s]==i),F=(l=b[0])==null?void 0:l.position,J=(j=x[0])==null?void 0:j.position;return F-J}}),M={metadata:t.metadata,dimensionsMetadata:y,indexBy:m,keys:S=="index"?f:f.filter(e=>E.indexOf(e)==-1),data:d}}return L.jsx(L.Fragment,{children:q.Children.map(a.children,u=>q.cloneElement(u,{options:M}))})},z=a=>{const{data:t,measures:v,dimensions:w}=a,g=JSON.parse(JSON.stringify(t));return w.length===1?L.jsx(U,{...a,data:g}):L.jsx(P,{...a,data:g})};exports.default=z;