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.

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