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.

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