vue-data-ui
Version:
A user-empowering data visualization Vue 3 components library for eloquent data storytelling
304 lines (303 loc) • 11.2 kB
JavaScript
import { useSlots as J, onMounted as L, computed as g, ref as k, watch as O, nextTick as K, createElementBlock as c, openBlock as r, normalizeStyle as f, unref as s, renderSlot as C, createCommentVNode as v, createBlock as Q, normalizeProps as R, mergeProps as P, createElementVNode as d, toDisplayString as p, Fragment as T, renderList as Y, createVNode as Z } from "vue";
import { u as ee, c as te, t as le, p as x, a as ae, b as oe, o as se, e as V, g as ue, d as re, s as $, i as b, f as w, X as ne, w as ie } from "./index-CHWA6Lnw.js";
import { u as E } from "./useNestedProp-ByBiJC9_.js";
import ce from "./vue-ui-skeleton-BSUFPx4a.js";
import { _ as ve } from "./PackageVersion-DcMafJMi.js";
const de = ["onClick"], fe = { key: 1 }, pe = ["xmlns", "viewBox"], ye = ["id"], me = ["stop-color"], ge = ["stop-color"], he = ["height", "width", "fill", "rx"], ke = ["height", "width", "fill", "rx"], xe = ["height", "width", "fill", "rx"], be = {
key: 2,
ref: "source",
dir: "auto"
}, Ae = {
__name: "vue-ui-sparkbar",
props: {
config: {
type: Object,
default() {
return {};
}
},
dataset: {
type: Array,
default() {
return [];
}
},
/**
* Used in VueUiRadar's tooltip exclusively
*/
backgroundOpacity: {
type: Number,
default: null
}
},
emits: ["selectDatapoint"],
setup(_, { emit: B }) {
const { vue_ui_sparkbar: M } = ee(), a = _, U = J();
L(() => {
U["chart-background"] && console.warn("VueUiSparkbar does not support the #chart-background slot.");
});
const S = g(() => !!a.dataset && a.dataset.length), A = k(te()), e = g({
get: () => N(),
set: (t) => t
});
function N() {
const t = E({
userConfig: a.config,
defaultConfig: M
});
return t.theme ? {
...E({
userConfig: ae.vue_ui_sparkbar[t.theme] || a.config,
defaultConfig: t
}),
customPalette: le[t.theme] || x
} : t;
}
O(() => a.config, (t) => {
e.value = N();
}, { deep: !0 });
const j = g(() => oe(e.value.customPalette)), y = k(a.dataset.map((t) => ({
...t,
value: e.value.style.animation.show ? 0 : t.value || 0,
formatter: t.formatter || null
}))), D = k(null);
L(async () => {
se(a.dataset) && V({
componentName: "VueUiSparkbar",
type: "dataset"
}), F();
});
function F() {
if (e.value.style.animation.show) {
let z = function() {
i += l / t, i < l ? (y.value = y.value.map((u, h) => ({
...u,
value: u.value += o[h]
})), D.value = requestAnimationFrame(z)) : y.value = a.dataset.map((u) => ({
...u,
value: u.value || 0,
formatter: u.formatter || null
}));
};
const t = e.value.style.animation.animationFrames, o = a.dataset.map((u, h) => u.value / t), l = a.dataset.map((u) => u.value || 0).reduce((u, h) => u + h, 0);
let i = 0;
z();
}
}
O(() => a.dataset, async (t) => {
cancelAnimationFrame(D.value), y.value = a.dataset.map((o) => ({
...o,
value: e.value.style.animation.show ? 0 : o.value || 0,
formatter: o.formatter || null
})), K(F);
}, { deep: !0 });
const n = k({
width: 500,
height: 16
}), G = g(() => Math.max(...a.dataset.map((t) => t.value))), W = g(() => (a.dataset.forEach((t, o) => {
ue({
datasetObject: t,
requiredAttributes: ["name", "value"]
}).forEach((l) => {
V({
componentName: "VueUiSparkbar",
type: "datasetSerieAttribute",
property: l,
index: o
});
});
}), y.value.map((t, o) => ({
...t,
value: t.value || 0,
color: re(t.color) || j.value[o] || x[o] || x[o % x.length]
}))));
function q(t) {
return t / G.value;
}
function I(t) {
return e.value.style.layout.independant ? t.target ? t.value / t.target : e.value.style.layout.percentage ? t.value > 100 ? 1 : t.value / 100 : e.value.style.layout.target === 0 ? 1 : t.value / e.value.style.layout.target : q(t.value);
}
function m(t) {
return e.value.style.layout.independant && t.target || e.value.style.layout.target;
}
const H = B;
function X(t, o) {
H("selectDatapoint", { datapoint: t, index: o });
}
return (t, o) => (r(), c("div", {
class: "vue-ui-sparkbar",
style: f({
width: "100%",
fontFamily: e.value.style.fontFamily,
background: a.backgroundOpacity !== null ? s($)(e.value.style.backgroundColor, a.backgroundOpacity) : e.value.style.backgroundColor
})
}, [
t.$slots.title ? C(t.$slots, "title", R(P({ key: 0 }, { title: { ...t.title, title: e.value.style.title.text, subtitle: e.value.style.title.subtitle.text } }))) : v("", !0),
!t.$slots.title && e.value.style.title.text ? (r(), c("div", {
key: 1,
class: "vue-ui-sparkbar-title-container",
style: f({
background: e.value.style.title.backgroundColor,
margin: e.value.style.title.margin,
textAlign: e.value.style.title.textAlign
})
}, [
d("div", {
class: "vue-ui-sparkbar-title",
style: f({
fontSize: e.value.style.title.fontSize + "px",
color: e.value.style.title.color,
fontWeight: e.value.style.title.bold ? "bold" : "normal"
})
}, p(e.value.style.title.text), 5),
e.value.style.title.subtitle.text ? (r(), c("div", {
key: 0,
class: "vue-ui-sparkbar-subtitle",
style: f({
fontSize: e.value.style.title.subtitle.fontSize + "px",
color: e.value.style.title.subtitle.color,
fontWeight: e.value.style.title.subtitle.bold ? "bold" : "normal"
})
}, p(e.value.style.title.subtitle.text), 5)) : v("", !0)
], 4)) : v("", !0),
(r(!0), c(T, null, Y(W.value, (l, i) => (r(), c(T, null, [
S.value ? (r(), c("div", {
key: 0,
style: f(`display:flex !important;${["left", "right"].includes(e.value.style.labels.name.position) ? `flex-direction: ${e.value.style.labels.name.position === "right" ? "row-reverse" : "row"} !important` : "flex-direction:column !important"};gap:${e.value.style.gap}px !important;align-items:center;${_.dataset.length > 0 && i !== _.dataset.length - 1 ? "margin-bottom:6px" : ""}`),
onClick: () => X(l, i)
}, [
C(t.$slots, "data-label", P({ ref_for: !0 }, { bar: {
...l,
target: m(l),
valueLabel: s(b)(
l.formatter,
l.value,
s(w)({
p: l.prefix || "",
v: l.value,
s: l.suffix || "",
r: l.rounding || 0
}),
{ datapoint: l, seriesIndex: i }
),
targetLabel: s(b)(
l.formatter,
m(l),
s(w)({
p: l.prefix || "",
v: m(l),
s: l.suffix || "",
r: l.rounding || 0
}),
{ datapoint: l, seriesIndex: i }
)
} })),
t.$slots["data-label"] ? v("", !0) : (r(), c("div", {
key: 0,
style: f({
width: e.value.style.labels.name.width,
color: e.value.style.labels.name.color,
fontSize: e.value.style.labels.fontSize + "px",
fontWeight: e.value.style.labels.name.bold ? "bold" : "normal",
textAlign: ["left", "right"].includes(e.value.style.labels.name.position) || ["top", "top-left"].includes(e.value.style.labels.name.position) ? "left" : e.value.style.labels.name.position === "top-center" ? "center" : "right"
})
}, [
d("span", null, p(l.name), 1),
e.value.style.labels.value.show ? (r(), c("span", {
key: 0,
style: f(`font-weight:${e.value.style.labels.value.bold ? "bold" : "normal"}`)
}, ": " + p(s(b)(
l.formatter,
l.value,
s(w)({
p: l.prefix || "",
v: l.value,
s: l.suffix || "",
r: l.rounding || 0
}),
{ datapoint: l, seriesIndex: i }
)), 5)) : v("", !0),
e.value.style.layout.showTargetValue ? (r(), c("span", fe, p(" " + e.value.style.layout.targetValueText) + " " + p(s(b)(
l.formatter,
m(l),
s(w)({
p: l.prefix || "",
v: m(l),
s: l.suffix || "",
r: l.rounding || 0
}),
{ datapoint: l, seriesIndex: i }
)), 1)) : v("", !0)
], 4)),
(r(), c("svg", {
xmlns: s(ne),
viewBox: `0 0 ${n.value.width} ${n.value.height}`,
width: "100%"
}, [
Z(ve),
d("defs", null, [
d("linearGradient", {
x1: "0%",
y1: "0%",
x2: "100%",
y2: "0%",
id: `sparkbar_gradient_${i}_${A.value}`
}, [
d("stop", {
offset: "0%",
"stop-color": s($)(s(ie)(l.color, 0.03), 100 - e.value.style.bar.gradient.intensity)
}, null, 8, me),
d("stop", {
offset: "100%",
"stop-color": l.color
}, null, 8, ge)
], 8, ye)
]),
d("rect", {
height: n.value.height,
width: n.value.width,
x: 0,
y: 0,
fill: s($)(e.value.style.gutter.backgroundColor, e.value.style.gutter.opacity),
rx: n.value.height / 2
}, null, 8, he),
d("rect", {
height: n.value.height,
width: n.value.width * I(l),
x: 0,
y: 0,
fill: e.value.style.bar.gradient.underlayerColor,
rx: n.value.height / 2
}, null, 8, ke),
d("rect", {
height: n.value.height,
width: n.value.width * I(l),
x: 0,
y: 0,
fill: e.value.style.bar.gradient.show ? `url(#sparkbar_gradient_${i}_${A.value})` : l.color,
rx: n.value.height / 2
}, null, 8, xe)
], 8, pe))
], 12, de)) : v("", !0)
], 64))), 256)),
t.$slots.source ? (r(), c("div", be, [
C(t.$slots, "source")
], 512)) : v("", !0),
S.value ? v("", !0) : (r(), Q(ce, {
key: 3,
config: {
type: "sparkbar",
style: {
backgroundColor: e.value.style.backgroundColor,
sparkbar: {
color: "#CCCCCC"
}
}
}
}, null, 8, ["config"]))
], 4));
}
};
export {
Ae as default
};