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