UNPKG

vue-data-ui

Version:

A user-empowering data visualization Vue 3 components library for eloquent data storytelling

382 lines (381 loc) • 15.5 kB
import { defineAsyncComponent as V, useSlots as pe, onMounted as G, computed as b, ref as d, watch as q, createElementBlock as r, openBlock as s, normalizeStyle as v, createCommentVNode as y, createBlock as ge, createVNode as H, createElementVNode as p, toDisplayString as C, unref as m, Fragment as x, renderList as $, normalizeClass as D, withCtx as X, renderSlot as T, normalizeProps as J, guardReactiveProps as K } from "vue"; import { u as fe, c as Q, t as he, p as i, a as ye, d as F, b as me, o as be, e as Z, g as ke, f as U, X as _e, s as we, w as Ce, i as ee, x as xe } from "./index-BLtEpj8j.js"; import { u as te } from "./useNestedProp-Bw1KcAmj.js"; import { u as $e } from "./useChartAccessibility-9icAAmYg.js"; import { _ as Se } from "./_plugin-vue_export-helper-CHgC5LLL.js"; const Pe = ["xmlns", "viewBox"], ze = ["id"], Ae = ["stop-color"], De = ["stop-color"], Fe = ["stop-color"], Ne = { id: "stackPill", clipPathUnits: "objectBoundingBox" }, Le = ["fill"], Be = { key: 0, "clip-path": "url(#stackPill)" }, Oe = ["x", "width", "height", "fill"], Ve = ["x", "width", "height", "fill", "stroke"], Te = ["onClick", "x", "width", "height", "onMouseenter"], Ue = ["width", "height", "rx"], je = ["onClick"], Ee = { style: { display: "flex", "flex-direction": "row", "align-items": "center", gap: "4px", "justify-content": "center" } }, Me = ["height", "width"], Re = ["fill"], Ie = { key: 4, ref: "source", dir: "auto" }, We = { __name: "vue-ui-sparkstackbar", props: { config: { type: Object, default() { return {}; } }, dataset: { type: Array, default() { return []; } } }, emits: ["selectDatapoint"], setup(le, { emit: oe }) { const ae = V(() => import("./PackageVersion-CnRyWKP-.js")), se = V(() => import("./vue-ui-skeleton-CsMU8DwQ.js")), re = V(() => import("./Tooltip-CuHsd1IQ.js")), { vue_ui_sparkstackbar: ne } = fe(), u = le, ue = pe(); G(() => { ue["chart-background"] && console.warn("VueUiSparkStackbar does not support the #chart-background slot."); }); const j = b(() => !!u.dataset && u.dataset.length), E = d(null), M = d(Q()), N = d(!1), L = d(""), e = b({ get: () => R(), set: (t) => t }), { svgRef: ie } = $e({ config: e.value.style.title }); function R() { const t = te({ userConfig: u.config, defaultConfig: ne }); return t.theme ? { ...te({ userConfig: ye.vue_ui_sparkstackbar[t.theme] || u.config, defaultConfig: t }), customPalette: he[t.theme] || i } : t; } q(() => u.config, (t) => { e.value = R(), I(); }, { deep: !0 }), q(() => u.dataset, (t) => { k.value = u.dataset.map((a, l) => ({ ...a, color: a.color ? F(a.color) : S.value[l] || i[l] || i[l % i.length] })); }, { deep: !0 }); const S = b(() => me(e.value.customPalette)), k = d(u.dataset.map((t, a) => ({ ...t, value: e.value.style.animation.show ? 0 : t.value || 0, color: t.color ? F(t.color) : S.value[a] || i[a] || i[a % i.length] }))), w = d(!0); G(() => { if (I(), e.value.style.animation.show) { let h = function() { o += l / t, o < l ? (k.value = k.value.map((n, c) => ({ ...n, value: n.value += a[c], color: n.color ? F(n.color) : S.value[c] || i[c] || i[c % i.length] })), requestAnimationFrame(h)) : (w.value = !1, k.value = u.dataset.map((n, c) => ({ ...n, value: n.value || 0, color: n.color ? F(n.color) : S.value[c] || i[c] || i[c % i.length], id: Q() }))); }; const t = e.value.style.animation.animationFrames, a = u.dataset.map((n, c) => n.value / t), l = u.dataset.map((n) => n.value || 0).reduce((n, c) => n + c, 0); let o = 0; w.value = !0, h(); } }); function I() { be(u.dataset) ? Z({ componentName: "VueUiSparkStackbar", type: "dataset" }) : u.dataset.forEach((t, a) => { ke({ datasetObject: t, requiredAttributes: ["name", "value"] }).forEach((l) => { Z({ componentName: "VueUiSparkStackbar", type: "datasetSerieAttribute", property: l, index: a }); }); }); } const g = d({ width: 500, height: 16 }), f = d([]), W = b(() => u.dataset.map((t) => t.value || 0).filter((t, a) => !f.value.includes(a)).reduce((t, a) => t + a, 0)), P = b(() => k.value.map((t, a) => { const l = t.value || 0, o = l / W.value, h = isNaN(o) ? 0 : o, n = h * g.value.width; return { ...t, value: l, proportion: h, width: n, proportionLabel: U({ v: h * 100, s: "%", r: e.value.style.legend.percentage.rounding }) }; })), B = b(() => P.value.filter((t, a) => !f.value.includes(a))); function ce(t) { f.value.includes(t) ? f.value = f.value.filter((a) => a !== t) : f.value.length < k.value.length - 1 && f.value.push(t); } const z = b(() => { let t = 0; const a = []; for (let l = 0; l < B.value.length; l += 1) a.push({ ...B.value[l], start: t }), t += B.value[l].width; return a; }), de = oe; function Y(t, a) { de("selectDatapoint", { datapoint: t, index: a }); } const O = d(null), A = d(!1), _ = d(null); function ve({ datapoint: t, seriesIndex: a }) { if (!e.value.style.tooltip.show) return; O.value = { datapoint: t, seriesIndex: a, config: e.value, series: P.value }, N.value = !0, _.value = a; const l = e.value.style.tooltip.customFormat; if (xe(l)) try { const o = l({ seriesIndex: a, datapoint: t, series: P.value, config: e.value }); typeof o == "string" && (L.value = o, A.value = !0); } catch { console.warn("Custom format cannot be applied."), A.value = !1; } if (!A.value) { let o = ""; o += `<div style="width:100%;text-align:center;border-bottom:1px solid ${e.value.style.tooltip.borderColor};padding-bottom:6px;margin-bottom:3px;">${t.name}</div>`, o += `<div style="display:flex;flex-direction:row;gap:6px;align-items:center;"><svg viewBox="0 0 12 12" height="14" width="14"><circle cx="6" cy="6" r="6" stroke="none" fill="${t.color}"/></svg>`, o += `<b>${t.proportionLabel}</b>`, o += `<span>(${ee( e.value.style.legend.value.formatter, t.value, U({ p: e.value.style.legend.value.prefix, v: t.value, s: e.value.style.legend.value.suffix, r: e.value.style.legend.value.rounding }), { datapoint: t, seriesIndex: a } )})</span>`, L.value = `<div>${o}</div>`; } } return (t, a) => (s(), r("div", { class: "vue-ui-spark-stackbar", ref_key: "sparkstackbarChart", ref: E, style: v(`width:100%; background:${e.value.style.backgroundColor}`) }, [ e.value.style.title.text ? (s(), r("div", { key: 0, style: v(`width:calc(100% - 12px);background:transparent;margin:0 auto;margin:${e.value.style.title.margin};padding: 0 6px;text-align:${e.value.style.title.textAlign}`) }, [ p("div", { class: "atom-title", style: v(`font-size:${e.value.style.title.fontSize}px;color:${e.value.style.title.color};font-weight:${e.value.style.title.bold ? "bold" : "normal"}`) }, C(e.value.style.title.text), 5), e.value.style.title.subtitle.text ? (s(), r("div", { key: 0, class: "atom-subtitle", style: v(`font-size:${e.value.style.title.subtitle.fontSize}px;color:${e.value.style.title.subtitle.color};font-weight:${e.value.style.title.subtitle.bold ? "bold" : "normal"}`) }, C(e.value.style.title.subtitle.text), 5)) : y("", !0) ], 4)) : y("", !0), j.value ? (s(), r("svg", { key: 1, ref_key: "svgRef", ref: ie, xmlns: m(_e), width: "100%", viewBox: `0 0 ${g.value.width} ${g.value.height}` }, [ H(m(ae)), p("defs", null, [ (s(!0), r(x, null, $(z.value, (l, o) => (s(), r("linearGradient", { key: `stack_gradient_${o}`, gradientTransform: "rotate(90)", id: `stack_gradient_${o}_${M.value}` }, [ p("stop", { offset: "0%", "stop-color": l.color }, null, 8, Ae), p("stop", { offset: "50%", "stop-color": m(we)(m(Ce)(l.color, 0.05), 100 - e.value.style.bar.gradient.intensity) }, null, 8, De), p("stop", { offset: "100%", "stop-color": l.color }, null, 8, Fe) ], 8, ze))), 128)), p("clipPath", Ne, [ p("rect", { x: "0.005", y: "-2", width: "0.99", height: "5", rx: "3", ry: "3", fill: e.value.style.backgroundColor }, null, 8, Le) ]) ]), W.value > 0 ? (s(), r("g", Be, [ (s(!0), r(x, null, $(z.value, (l, o) => (s(), r("rect", { key: `stack_underlayer_${o}`, x: l.start, y: 0, width: l.width, height: g.value.height, fill: e.value.style.bar.gradient.underlayerColor, class: D({ animated: !w.value }), style: v({ opacity: _.value !== null && e.value.style.tooltip.show ? _.value === o ? 1 : 0.5 : 1 }) }, null, 14, Oe))), 128)), (s(!0), r(x, null, $(z.value, (l, o) => (s(), r("rect", { key: `stack_${o}`, x: l.start, y: 0, width: l.width, height: g.value.height, fill: e.value.style.bar.gradient.show ? `url(#stack_gradient_${o}_${M.value})` : l.color, stroke: e.value.style.backgroundColor, "stroke-linecap": "round", class: D({ animated: !w.value }), style: v({ opacity: _.value !== null && e.value.style.tooltip.show ? _.value === o ? 1 : 0.5 : 1 }) }, null, 14, Ve))), 128)), (s(!0), r(x, null, $(z.value, (l, o) => (s(), r("rect", { key: `stack_trap_${o}`, onClick: () => Y(l, o), x: l.start, y: 0, width: l.width, height: g.value.height, fill: "transparent", stroke: "none", class: D({ animated: !w.value }), onMouseenter: () => ve({ datapoint: l, seriesIndex: o }), onMouseleave: a[0] || (a[0] = (h) => { N.value = !1, _.value = null; }) }, null, 42, Te))), 128)) ])) : (s(), r("rect", { key: 1, x: 2, y: 1, width: g.value.width - 4, height: g.value.height - 2, stroke: "#CCCCCC", "stroke-width": "2", fill: "transparent", rx: (g.value.height - 4) / 2 }, null, 8, Ue)) ], 8, Pe)) : y("", !0), j.value ? y("", !0) : (s(), ge(m(se), { key: 2, config: { type: "sparkStackbar", style: { backgroundColor: e.value.style.backgroundColor, sparkStackbar: { color: "#CCCCCC" } } } }, null, 8, ["config"])), e.value.style.legend.show ? (s(), r("div", { key: 3, style: v(`background:transparent;margin:0 auto;margin:${e.value.style.legend.margin};justify-content:${e.value.style.legend.textAlign === "left" ? "flex-start" : e.value.style.legend.textAlign === "right" ? "flex-end" : "center"}`), class: "vue-ui-sparkstackbar-legend" }, [ (s(!0), r(x, null, $(P.value, (l, o) => (s(), r("div", { style: v(`font-size:${e.value.style.legend.fontSize}px;`), class: D({ "vue-ui-sparkstackbar-legend-item": !0, "vue-ui-sparkstackbar-legend-item-unselected": f.value.includes(o) }), onClick: (h) => { ce(o), Y(l, o); } }, [ p("div", Ee, [ (s(), r("svg", { height: `${e.value.style.legend.fontSize}px`, width: `${e.value.style.legend.fontSize}px`, viewBox: "0 0 10 10" }, [ p("circle", { cx: 5, cy: 5, r: 5, fill: l.color }, null, 8, Re) ], 8, Me)), p("span", { style: v(`color:${e.value.style.legend.name.color}; font-weight:${e.value.style.legend.name.bold ? "bold" : "normal"}`) }, C(l.name), 5), e.value.style.legend.percentage.show ? (s(), r("span", { key: 0, style: v(`font-weight:${e.value.style.legend.percentage.bold ? "bold" : "normal"};color:${e.value.style.legend.percentage.color}`) }, C(f.value.includes(o) ? " - " : l.proportionLabel), 5)) : y("", !0), e.value.style.legend.value.show ? (s(), r("span", { key: 1, style: v(`font-weight:${e.value.style.legend.value.bold ? "bold" : "normal"};color:${e.value.style.legend.value.color}`) }, " (" + C(m(ee)( e.value.style.legend.value.formatter, l.value, m(U)({ p: e.value.style.legend.value.prefix, v: l.value, s: e.value.style.legend.value.suffix, r: e.value.style.legend.value.rounding }), { datapoint: l, seriesIndex: o } )) + ") ", 5)) : y("", !0) ]) ], 14, je))), 256)) ], 4)) : y("", !0), H(m(re), { show: N.value && e.value.style.tooltip.show, parent: E.value, backgroundColor: e.value.style.tooltip.backgroundColor, color: e.value.style.tooltip.color, fontSize: e.value.style.tooltip.fontSize, borderRadius: e.value.style.tooltip.borderRadius, borderColor: e.value.style.tooltip.borderColor, borderWidth: e.value.style.tooltip.borderWidth, backgroundOpacity: e.value.style.tooltip.backgroundOpacity, position: e.value.style.tooltip.position, content: L.value, isCustom: A.value, offsetY: -124 + e.value.style.tooltip.offsetY, blockShiftY: !0 }, { "tooltip-before": X(() => [ T(t.$slots, "tooltip-before", J(K({ ...O.value })), void 0, !0) ]), "tooltip-after": X(() => [ T(t.$slots, "tooltip-after", J(K({ ...O.value })), void 0, !0) ]), _: 3 }, 8, ["show", "parent", "backgroundColor", "color", "fontSize", "borderRadius", "borderColor", "borderWidth", "backgroundOpacity", "position", "content", "isCustom", "offsetY"]), t.$slots.source ? (s(), r("div", Ie, [ T(t.$slots, "source", {}, void 0, !0) ], 512)) : y("", !0) ], 4)); } }, Je = /* @__PURE__ */ Se(We, [["__scopeId", "data-v-bd91b3ec"]]); export { Je as default };