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