vue-data-ui
Version:
A user-empowering data visualization Vue 3 components library for eloquent data storytelling
381 lines (380 loc) • 17 kB
JavaScript
import { useCssVars as pe, computed as k, defineAsyncComponent as me, ref as y, toRefs as be, onMounted as U, watch as X, nextTick as j, createElementBlock as s, openBlock as a, normalizeStyle as S, createCommentVNode as c, createBlock as G, createElementVNode as g, createTextVNode as _e, toDisplayString as p, unref as n, createVNode as xe, renderSlot as q, Fragment as d, renderList as h, normalizeClass as Y } from "vue";
import { u as ke, c as Ce, t as Ae, b as we, o as $e, f as P, g as Le, s as m, k as J, i as K, X as ze, x as D } from "./index-q-LPw2IT.js";
import { t as Se, u as Me } from "./useResponsive-DfdjqQps.js";
import { u as Fe, B as Ie } from "./useLoading-D7YHNtLX.js";
import { u as Te } from "./useFitSvgText-2crs6Fv5.js";
import { u as Q } from "./useNestedProp-04aFeUYu.js";
import { u as Ee } from "./useChartAccessibility-9icAAmYg.js";
import { _ as Ne } from "./Shape-DHIaJs9G.js";
import { _ as De } from "./_plugin-vue_export-helper-CHgC5LLL.js";
const We = { key: 0 }, Ve = { key: 1 }, He = ["xmlns", "viewBox"], Oe = ["width", "height"], Re = ["id"], Be = ["stop-color"], Ue = ["stop-color"], Xe = ["id"], je = ["stop-color"], Ge = ["stop-color"], qe = ["id"], Ye = ["stop-color"], Pe = ["stop-color"], Je = ["height", "width", "fill", "x", "stroke", "stroke-width", "rx", "stroke-dasharray"], Ke = { key: 1 }, Qe = ["x", "y", "height", "width", "fill", "stroke", "stroke-width", "rx"], Ze = { key: 2 }, et = ["x", "y", "font-size", "font-weight", "fill"], tt = ["x", "y", "font-size", "fill"], lt = ["x", "y", "font-size", "fill"], at = ["height", "width", "x", "onMouseover", "onMouseleave", "onClick"], st = {
__name: "vue-ui-sparkhistogram",
props: {
config: {
type: Object,
default() {
return {};
}
},
dataset: {
type: Array,
default() {
return [];
}
}
},
emits: ["selectDatapoint"],
setup(Z, { emit: ee }) {
pe((l) => ({
c8e2d1ee: ve.value
}));
const te = me(() => import("./PackageVersion-5ZjKSIei.js")), { vue_ui_sparkhistogram: le } = ke(), b = Z, _ = y(Ce()), W = y(null), V = y(null), M = y(null), C = y(null), w = y(null), e = y(T()), { loading: H, FINAL_DATASET: A } = Fe({
...be(b),
FINAL_CONFIG: e,
prepareConfig: T,
skeletonDataset: [
{ value: 1, intensity: 0.2, color: "#CACACA" },
{ value: 2, intensity: 0.3, color: "#CACACA" },
{ value: 3, intensity: 0.5, color: "#CACACA" },
{ value: 5, intensity: 0.7, color: "#CACACA" },
{ value: 8, intensity: 0.9, color: "#CACACA" },
{ value: 13, intensity: 0.95, color: "#CACACA" },
{ value: 21, intensity: 1, color: "#CACACA" },
{ value: 13, intensity: 0.95, color: "#CACACA" },
{ value: 8, intensity: 0.9, color: "#CACACA" },
{ value: 5, intensity: 0.7, color: "#CACACA" },
{ value: 3, intensity: 0.5, color: "#CACACA" },
{ value: 2, intensity: 0.3, color: "#CACACA" },
{ value: 1, intensity: 0.2, color: "#CACACA" }
],
skeletonConfig: Ae({
defaultConfig: e.value,
userConfig: {
style: {
animation: { show: !1 },
backgroundColor: "#99999930"
}
}
})
}), F = y(e.value.style.layout.width), I = y(e.value.style.layout.height), { svgRef: O } = Ee({ config: e.value.style.title });
function T() {
const l = Q({
userConfig: b.config,
defaultConfig: le
});
return l.theme ? {
...Q({
userConfig: we.vue_ui_sparkhistogram[l.theme] || b.config,
defaultConfig: l
})
} : l;
}
U(() => {
B();
});
const R = k(() => !!e.value.debug);
function B() {
if ($e(b.dataset) ? P({
componentName: "VueUiSparkHistogram",
type: "dataset",
debug: R.value
}) : b.dataset.forEach((l, o) => {
Le({
datasetObject: l,
requiredAttributes: ["value"]
}).forEach((t) => {
P({
componentName: "VueUiSparkHistogram",
type: "datasetSerieAttribute",
property: t,
index: o,
debug: R.value
});
});
}), e.value.responsive) {
const l = Se(() => {
const { width: o, height: t } = Me({
chart: M.value,
title: e.value.style.title.text ? V.value : null,
source: W.value
});
requestAnimationFrame(() => {
F.value = Math.max(10, o), I.value = Math.max(10, t - 12);
});
});
C.value && (w.value && C.value.unobserve(w.value), C.value.disconnect()), C.value = new ResizeObserver(l), w.value = M.value.parentNode, C.value.observe(w.value);
}
}
X(() => b.config, (l) => {
e.value = T(), B();
}, { deep: !0 });
const v = k(() => {
const l = I.value, o = F.value, t = e.value.style.layout.padding.top, i = l - e.value.style.layout.padding.bottom, f = e.value.style.layout.padding.left, $ = o - e.value.style.layout.padding.right, L = t + (l - t - e.value.style.layout.padding.bottom) / 2, E = l - e.value.style.layout.padding.top - e.value.style.layout.padding.bottom, z = o - e.value.style.layout.padding.left - e.value.style.layout.padding.right;
return {
bottom: i,
centerY: L,
drawingHeight: E,
drawingWidth: z,
height: l,
left: f,
right: $,
top: t,
width: o
};
}), ae = k(() => Math.max(...A.value.map((l) => Math.abs(l.value || 0))));
function se(l) {
return Math.abs(l) / ae.value;
}
const u = k(() => A.value.map((l, o) => {
const t = se(l.value || 0), i = v.value.drawingHeight * t, f = v.value.drawingWidth / A.value.length, $ = f * (e.value.style.bars.gap / 100), L = f - $, E = v.value.centerY - i / 2, z = v.value.left + ($ / 2 + o * f), de = v.value.left + o * f, N = typeof l.intensity > "u" ? 100 : Math.round(l.intensity * 100), ye = l.color ? l.color : l.value >= 0 ? m(e.value.style.bars.colors.positive, N) : m(e.value.style.bars.colors.negative, N), he = l.color ? l.color : l.value >= 0 ? e.value.style.bars.colors.positive : e.value.style.bars.colors.negative, fe = l.color ? `url(#gradient_datapoint_${o}_${_.value})` : l.value >= 0 ? `url(#gradient_positive_${o}_${_.value})` : `url(#gradient_negative_${o}_${_.value})`, ge = z + L / 2;
return {
...l,
color: ye,
gradient: fe,
height: i,
intensity: N,
proportion: t,
stroke: he,
textAnchor: ge,
trapX: de,
unitWidth: f,
width: L,
x: z,
y: E
};
}));
function oe(l, o) {
return J(
e.value.style.labels.value.formatter,
l.value,
K({
p: e.value.style.labels.value.prefix,
v: l.value,
s: e.value.style.labels.value.suffix,
r: e.value.style.labels.value.rounding
}),
{ datapoint: l, seriesIndex: o }
);
}
const r = y(null), ie = ee;
function ue(l, o) {
ie("selectDatapoint", { datapoint: l, index: o }), e.value.events.datapointClick && e.value.events.datapointClick({ datapoint: l, seriesIndex: o });
}
function ne(l, o) {
r.value = o, e.value.events.datapointEnter && e.value.events.datapointEnter({ datapoint: l, seriesIndex: o });
}
function re(l, o) {
r.value = null, e.value.events.datapointLeave && e.value.events.datapointLeave({ datapoint: l, seriesIndex: o });
}
const ve = k(() => `${e.value.style.animation.speedMs}ms`), ce = k(() => v.value.drawingWidth / A.value.length * 0.9), { fitText: x } = Te({
svgRef: O,
unitWidth: ce
});
return U(async () => {
await j(), x(".vue-ui-sparkhistogram-top-label", e.value.style.labels.value.minFontSize), x(".vue-ui-sparkhistogram-bottom-label", e.value.style.labels.valueLabel.minFontSize), x(".vue-ui-sparkhistogram-time-label", e.value.style.labels.timeLabel.minFontSize);
}), X([F, I, () => A.value], async () => {
await j(), x(".vue-ui-sparkhistogram-top-label", e.value.style.labels.value.minFontSize), x(".vue-ui-sparkhistogram-bottom-label", e.value.style.labels.valueLabel.minFontSize), x(".vue-ui-sparkhistogram-time-label", e.value.style.labels.timeLabel.minFontSize);
}), (l, o) => (a(), s("div", {
class: "vue-ui-spark-histogram",
ref_key: "histogramChart",
ref: M,
style: S(`width:100%;background:${e.value.style.backgroundColor};font-family:${e.value.style.fontFamily}`),
onMouseleave: o[0] || (o[0] = (t) => r.value = null)
}, [
e.value.style.title.text ? (a(), s("div", {
key: 0,
ref_key: "chartTitle",
ref: V,
style: S(`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}`)
}, [
g("div", {
style: S(`font-size:${e.value.style.title.fontSize}px;color:${e.value.style.title.color};font-weight:${e.value.style.title.bold ? "bold" : "normal"}`)
}, [
_e(p(e.value.style.title.text) + " ", 1),
r.value !== null ? (a(), s("span", We, "- " + p(u.value[r.value].timeLabel || "") + " " + p(n(J)(
e.value.style.labels.value.formatter,
u.value[r.value].value,
n(K)({
p: e.value.style.labels.value.prefix,
v: u.value[r.value].value,
s: e.value.style.labels.value.suffix,
r: e.value.style.labels.value.rounding
}),
{ datapoint: u.value[r.value], seriesIndex: r.value }
)), 1)) : c("", !0),
![void 0, null].includes(r.value) && ![null, void 0].includes(u.value[r.value].valueLabel) ? (a(), s("span", Ve, "(" + p(u.value[r.value].valueLabel || 0) + ")", 1)) : c("", !0)
], 4),
e.value.style.title.subtitle.text ? (a(), s("div", {
key: 0,
style: S(`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"}`)
}, p(e.value.style.title.subtitle.text), 5)) : c("", !0)
], 4)) : c("", !0),
(a(), s("svg", {
ref_key: "svgRef",
ref: O,
xmlns: n(ze),
viewBox: `0 0 ${v.value.width} ${v.value.height}`,
style: { overflow: "visible" }
}, [
xe(n(te)),
l.$slots["chart-background"] ? (a(), s("foreignObject", {
key: 0,
x: 0,
y: 0,
width: v.value.width,
height: v.value.height,
style: {
pointerEvents: "none"
}
}, [
q(l.$slots, "chart-background", {}, void 0, !0)
], 8, Oe)) : c("", !0),
g("defs", null, [
(a(!0), s(d, null, h(u.value, (t, i) => (a(), s("radialGradient", {
id: `gradient_positive_${i}_${_.value}`,
cy: "50%",
cx: "50%",
r: "50%",
fx: "50%",
fy: "50%"
}, [
g("stop", {
offset: "0%",
"stop-color": n(m)(n(D)(e.value.style.bars.colors.positive, 0.05), t.intensity)
}, null, 8, Be),
g("stop", {
offset: "100%",
"stop-color": n(m)(e.value.style.bars.colors.positive, t.intensity)
}, null, 8, Ue)
], 8, Re))), 256)),
(a(!0), s(d, null, h(u.value, (t, i) => (a(), s("radialGradient", {
id: `gradient_negative_${i}_${_.value}`,
cy: "50%",
cx: "50%",
r: "50%",
fx: "50%",
fy: "50%"
}, [
g("stop", {
offset: "0%",
"stop-color": n(m)(n(D)(e.value.style.bars.colors.negative, 0.05), t.intensity)
}, null, 8, je),
g("stop", {
offset: "100%",
"stop-color": n(m)(e.value.style.bars.colors.negative, t.intensity)
}, null, 8, Ge)
], 8, Xe))), 256)),
(a(!0), s(d, null, h(u.value, (t, i) => (a(), s("radialGradient", {
id: `gradient_datapoint_${i}_${_.value}`,
cy: "50%",
cx: "50%",
r: "50%",
fx: "50%",
fy: "50%"
}, [
g("stop", {
offset: "0%",
"stop-color": n(m)(n(D)(t.color, 0.05), t.intensity)
}, null, 8, Ye),
g("stop", {
offset: "100%",
"stop-color": n(m)(t.color, t.intensity)
}, null, 8, Pe)
], 8, qe))), 256))
]),
(a(!0), s(d, null, h(u.value, (t, i) => (a(), s("g", null, [
r.value !== null && r.value === i ? (a(), s("rect", {
key: 0,
height: v.value.height,
width: t.unitWidth,
fill: e.value.style.selector.fill,
x: t.trapX,
y: 0,
stroke: e.value.style.selector.stroke,
"stroke-width": e.value.style.selector.strokeWidth,
rx: e.value.style.selector.borderRadius,
"stroke-dasharray": e.value.style.selector.strokeDasharray
}, null, 8, Je)) : c("", !0)
]))), 256)),
!e.value.style.bars.shape || e.value.style.bars.shape === "square" ? (a(), s("g", Ke, [
(a(!0), s(d, null, h(u.value, (t, i) => (a(), s("rect", {
x: t.x,
y: t.y,
height: t.height,
width: t.width,
fill: e.value.style.bars.colors.gradient.show ? t.gradient : t.color,
stroke: t.stroke,
"stroke-width": e.value.style.bars.strokeWidth,
rx: `${e.value.style.bars.borderRadius * t.proportion / 12}%`,
class: Y({ "vue-ui-sparkhistogram-shape": e.value.style.animation.show })
}, null, 10, Qe))), 256))
])) : (a(), s("g", Ze, [
(a(!0), s(d, null, h(u.value, (t, i) => (a(), G(Ne, {
plot: { x: t.x + t.width / 2, y: t.y + t.height / 2 },
color: e.value.style.bars.colors.gradient.show ? t.gradient : t.color,
shape: e.value.style.bars.shape,
radius: Math.min(t.height * 0.4, t.width * 0.4),
class: Y({ "vue-ui-sparkhistogram-shape": e.value.style.animation.show })
}, null, 8, ["plot", "color", "shape", "radius", "class"]))), 256))
])),
n(H) ? c("", !0) : (a(), s(d, { key: 3 }, [
(a(!0), s(d, null, h(u.value, (t, i) => (a(), s("text", {
class: "vue-ui-sparkhistogram-top-label",
"text-anchor": "middle",
x: t.textAnchor,
y: t.y - e.value.style.labels.value.fontSize / 3 + e.value.style.labels.value.offsetY,
"font-size": e.value.style.labels.value.fontSize,
"font-weight": e.value.style.labels.value.bold ? "bold" : "normal",
fill: e.value.style.labels.value.color
}, p(oe(t, i)), 9, et))), 256)),
(a(!0), s(d, null, h(u.value, (t, i) => (a(), s("g", null, [
t.valueLabel ? (a(), s("text", {
key: 0,
class: "vue-ui-sparkhistogram-bottom-label",
x: t.textAnchor,
y: t.y + t.height + e.value.style.labels.valueLabel.fontSize,
"font-size": e.value.style.labels.valueLabel.fontSize,
"text-anchor": "middle",
fill: e.value.style.labels.valueLabel.color
}, p(t.valueLabel), 9, tt)) : c("", !0)
]))), 256)),
(a(!0), s(d, null, h(u.value, (t, i) => (a(), s("g", null, [
t.timeLabel ? (a(), s("text", {
key: 0,
class: "vue-ui-sparkhistogram-time-label",
x: t.textAnchor,
y: v.value.height - e.value.style.labels.timeLabel.fontSize / 2,
"font-size": e.value.style.labels.timeLabel.fontSize,
fill: e.value.style.labels.timeLabel.color,
"text-anchor": "middle"
}, p(t.timeLabel), 9, lt)) : c("", !0)
]))), 256))
], 64)),
(a(!0), s(d, null, h(u.value, (t, i) => (a(), s("g", null, [
g("rect", {
height: v.value.height,
width: t.unitWidth,
fill: "transparent",
x: t.trapX,
y: 0,
onMouseover: (f) => ne(t, i),
onMouseleave: (f) => re(t, i),
onClick: () => ue(t, i)
}, null, 40, at)
]))), 256))
], 8, He)),
l.$slots.source ? (a(), s("div", {
key: 1,
ref_key: "source",
ref: W,
dir: "auto"
}, [
q(l.$slots, "source", {}, void 0, !0)
], 512)) : c("", !0),
n(H) ? (a(), G(Ie, { key: 2 })) : c("", !0)
], 36));
}
}, ht = /* @__PURE__ */ De(st, [["__scopeId", "data-v-f8e879db"]]);
export {
ht as default
};