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.

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