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