tiny-spark 0.4.1 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/tiny-spark.es.js +82 -82
- package/dist/tiny-spark.umd.js +4 -4
- package/package.json +1 -1
package/dist/tiny-spark.es.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
const I = "http://www.w3.org/2000/svg";
|
|
2
2
|
var l = /* @__PURE__ */ ((t) => (t.ANIMATION = "animation", t.AREA_COLOR = "areaColor", t.CURVE = "curve", t.DATES = "dates", t.ID = "id", t.INDICATOR_COLOR = "indicatorColor", t.INDICATOR_WIDTH = "indicatorWidth", t.LINE_COLOR = "lineColor", t.LINE_THICKNESS = "lineThickness", t.NUMBER_LOCALE = "numberLocale", t.NUMBER_ROUNDING = "numberRounding", t.NUMBER_SHOW_ON = "numberShowOn", t.PLOT_COLOR = "plotColor", t.PLOT_RADIUS = "plotRadius", t.SET = "set", t.HIDE_PLOTS_ABOVE = "hidePlotsAbove", t.SHOW_LAST_VALUE = "showLastValue", t.LAST_VALUE_FONT_SIZE = "lastValueFontSize", t.LAST_VALUE_COLOR = "lastValueColor", t))(l || {}), z = /* @__PURE__ */ ((t) => (t.ANIMATION = "data-animation", t.AREA_COLOR = "data-area-color", t.CURVE = "data-curve", t.DATES = "data-dates", t.ID = "data-id", t.INDICATOR_COLOR = "data-indicator-color", t.INDICATOR_WIDTH = "data-indicator-width", t.LINE_COLOR = "data-line-color", t.LINE_THICKNESS = "data-line-thickness", t.NUMBER_LOCALE = "data-number-locale", t.NUMBER_ROUNDING = "data-number-rounding", t.NUMBER_SHOW_ON = "data-number-show-on", t.PLOT_COLOR = "data-plot-color", t.PLOT_RADIUS = "data-plot-radius", t.SET = "data-set", t.HIDE_PLOTS_ABOVE = "data-hide-plots-above", t.SHOW_LAST_VALUE = "data-show-last-value", t.LAST_VALUE_FONT_SIZE = "data-last-value-font-size", t.LAST_VALUE_COLOR = "data-last-value-color", t))(z || {});
|
|
3
3
|
function J(t) {
|
|
4
|
-
const { width: r, height: n } = t.parentElement.getBoundingClientRect(),
|
|
4
|
+
const { width: r, height: n } = t.parentElement.getBoundingClientRect(), i = { width: 300, height: 100 }, s = String(a(t, l.SHOW_LAST_VALUE, "false")) === "true", o = j(t), d = o && o.length ? o.at(-1) : null;
|
|
5
5
|
let e = 0;
|
|
6
6
|
if (s && ![null, void 0].includes(d)) {
|
|
7
7
|
const u = Number(String(a(t, l.NUMBER_ROUNDING, 0)));
|
|
8
8
|
e = 6 + d.toFixed(u).length * (Number(a(t, l.LAST_VALUE_FONT_SIZE, 12)) / 2);
|
|
9
9
|
}
|
|
10
|
-
const
|
|
11
|
-
return O.id = L, O.setAttribute("viewBox",
|
|
10
|
+
const h = `0 0 ${(r || i.width) + e} ${n || i.height}`, O = document.createElementNS(I, "svg"), L = t.dataset.id;
|
|
11
|
+
return O.id = L, O.setAttribute("viewBox", h), O.style.width = "100%", O.style.height = "100%", {
|
|
12
12
|
svg: O,
|
|
13
13
|
svgId: L,
|
|
14
|
-
width: r ||
|
|
15
|
-
height: n ||
|
|
14
|
+
width: r || i.width,
|
|
15
|
+
height: n || i.height
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
18
|
function N(t, r = 0) {
|
|
@@ -26,39 +26,39 @@ function W(t) {
|
|
|
26
26
|
}
|
|
27
27
|
function B(t) {
|
|
28
28
|
if (t.length < 1) return "0,0";
|
|
29
|
-
const r = t.length - 1, n = [`${N(t[0].x)},${N(t[0].y)}`],
|
|
29
|
+
const r = t.length - 1, n = [`${N(t[0].x)},${N(t[0].y)}`], i = [], s = [], o = [], d = [];
|
|
30
30
|
for (let e = 0; e < r; e += 1)
|
|
31
|
-
|
|
32
|
-
d[0] =
|
|
31
|
+
i[e] = t[e + 1].x - t[e].x, s[e] = t[e + 1].y - t[e].y, o[e] = s[e] / i[e];
|
|
32
|
+
d[0] = o[0], d[r] = o[r - 1];
|
|
33
33
|
for (let e = 1; e < r; e += 1)
|
|
34
|
-
if (
|
|
34
|
+
if (o[e - 1] * o[e] <= 0)
|
|
35
35
|
d[e] = 0;
|
|
36
36
|
else {
|
|
37
|
-
const
|
|
38
|
-
d[e] =
|
|
37
|
+
const p = 2 * o[e - 1] * o[e] / (o[e - 1] + o[e]);
|
|
38
|
+
d[e] = p;
|
|
39
39
|
}
|
|
40
40
|
for (let e = 0; e < r; e += 1) {
|
|
41
|
-
const
|
|
42
|
-
n.push(`C ${N(
|
|
41
|
+
const p = t[e].x, h = t[e].y, O = t[e + 1].x, L = t[e + 1].y, u = d[e], _ = d[e + 1], $ = p + (O - p) / 3, A = h + u * (O - p) / 3, k = O - (O - p) / 3, C = L - _ * (O - p) / 3;
|
|
42
|
+
n.push(`C ${N($)},${N(A)} ${N(k)},${N(C)} ${N(O)},${N(L)}`);
|
|
43
43
|
}
|
|
44
44
|
return n.join(" ");
|
|
45
45
|
}
|
|
46
46
|
function Y(t, r = 1e3, n) {
|
|
47
47
|
t.style.opacity = "1";
|
|
48
|
-
const
|
|
49
|
-
t.style.strokeDasharray = String(
|
|
48
|
+
const i = t.getTotalLength();
|
|
49
|
+
t.style.strokeDasharray = String(i), t.style.strokeDashoffset = String(i), t.getBoundingClientRect(), t.style.transition = `stroke-dashoffset ${r}ms ease-in-out`, t.style.strokeDashoffset = "0", t.addEventListener("transitionend", function s() {
|
|
50
50
|
t.style.transition = "", t.removeEventListener("transitionend", s), n && n();
|
|
51
51
|
});
|
|
52
52
|
}
|
|
53
53
|
function Q(t, r, n = 1e3) {
|
|
54
54
|
r.style.opacity = "1";
|
|
55
|
-
const
|
|
56
|
-
|
|
55
|
+
const i = r.getBBox(), s = i.width, o = document.createElementNS("http://www.w3.org/2000/svg", "clipPath"), d = "clip-" + Math.random().toString(36).substr(2, 9);
|
|
56
|
+
o.setAttribute("id", d);
|
|
57
57
|
const e = document.createElementNS("http://www.w3.org/2000/svg", "rect");
|
|
58
|
-
e.setAttribute("x",
|
|
59
|
-
let
|
|
60
|
-
|
|
61
|
-
r.removeAttribute("clip-path"),
|
|
58
|
+
e.setAttribute("x", i.x.toString()), e.setAttribute("y", i.y.toString()), e.setAttribute("width", "0"), e.setAttribute("height", i.height.toString()), o.appendChild(e);
|
|
59
|
+
let p = t.querySelector("defs");
|
|
60
|
+
p || (p = document.createElementNS("http://www.w3.org/2000/svg", "defs"), t.insertBefore(p, t.firstChild)), p.appendChild(o), r.setAttribute("clip-path", `url(#${d})`), e.style.transition = `width ${n}ms ease-out`, e.getBoundingClientRect(), e.setAttribute("width", s.toString()), e.addEventListener("transitionend", function h() {
|
|
61
|
+
r.removeAttribute("clip-path"), o.parentNode && o.parentNode.removeChild(o), e.removeEventListener("transitionend", h);
|
|
62
62
|
});
|
|
63
63
|
}
|
|
64
64
|
function T() {
|
|
@@ -75,8 +75,8 @@ function tt(t) {
|
|
|
75
75
|
color: "#1A1A1A",
|
|
76
76
|
backgroundColor: "#FFFFFF"
|
|
77
77
|
};
|
|
78
|
-
const r = window.getComputedStyle(t), n = r.getPropertyValue("color") || "#1A1A1A",
|
|
79
|
-
return { color: n, backgroundColor:
|
|
78
|
+
const r = window.getComputedStyle(t), n = r.getPropertyValue("color") || "#1A1A1A", i = r.getPropertyValue("background-color"), s = r.getPropertyValue("background");
|
|
79
|
+
return { color: n, backgroundColor: i || s || "#FFFFFF" };
|
|
80
80
|
}
|
|
81
81
|
function K() {
|
|
82
82
|
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(t) {
|
|
@@ -89,7 +89,7 @@ function j(t) {
|
|
|
89
89
|
if (!r) return [];
|
|
90
90
|
try {
|
|
91
91
|
const n = JSON.parse(r);
|
|
92
|
-
return Array.isArray(n) && n.every((
|
|
92
|
+
return Array.isArray(n) && n.every((i) => typeof i == "number" || [null, void 0].includes(i)) ? n : (console.warn("data-set is not an array of numbers."), []);
|
|
93
93
|
} catch (n) {
|
|
94
94
|
return console.error("Error parsing data-set:", n), [];
|
|
95
95
|
}
|
|
@@ -99,7 +99,7 @@ function et(t) {
|
|
|
99
99
|
if (!r) return [];
|
|
100
100
|
try {
|
|
101
101
|
const n = JSON.parse(r);
|
|
102
|
-
return Array.isArray(n) && n.every((
|
|
102
|
+
return Array.isArray(n) && n.every((i) => typeof i == "string") ? n : (console.warn("data-dates is not an array of strings"), []);
|
|
103
103
|
} catch (n) {
|
|
104
104
|
return console.error("Error parsing data-dates", n), [];
|
|
105
105
|
}
|
|
@@ -114,32 +114,32 @@ function F() {
|
|
|
114
114
|
return new Promise((t) => setTimeout(t, 0));
|
|
115
115
|
}
|
|
116
116
|
function E(t, r) {
|
|
117
|
-
const n = String(a(t, l.NUMBER_LOCALE, navigator.language || "en-US")),
|
|
117
|
+
const n = String(a(t, l.NUMBER_LOCALE, navigator.language || "en-US")), i = Number(String(a(t, l.NUMBER_ROUNDING, 0)));
|
|
118
118
|
return r.toLocaleString(n, {
|
|
119
119
|
useGrouping: !0,
|
|
120
|
-
minimumFractionDigits:
|
|
121
|
-
maximumFractionDigits:
|
|
120
|
+
minimumFractionDigits: i,
|
|
121
|
+
maximumFractionDigits: i
|
|
122
122
|
});
|
|
123
123
|
}
|
|
124
124
|
function nt(t, r, n) {
|
|
125
125
|
if (!t.createSVGPoint || !t.getScreenCTM)
|
|
126
126
|
throw new Error("Your browser does not support SVG coordinate transformation.");
|
|
127
|
-
const
|
|
128
|
-
if (!
|
|
127
|
+
const i = t.getScreenCTM();
|
|
128
|
+
if (!i)
|
|
129
129
|
throw new Error("Cannot obtain the screen CTM.");
|
|
130
130
|
const s = t.createSVGPoint();
|
|
131
131
|
s.x = r, s.y = n;
|
|
132
|
-
const
|
|
133
|
-
return { x:
|
|
132
|
+
const o = s.matrixTransform(i);
|
|
133
|
+
return { x: o.x, y: o.y };
|
|
134
134
|
}
|
|
135
|
-
function G(t, r, n,
|
|
136
|
-
if (rt(
|
|
137
|
-
const { x:
|
|
138
|
-
e.style.opacity = "0", e.classList.add("tiny-spark-tooltip"), e.setAttribute("id", `tooltip_${
|
|
135
|
+
function G(t, r, n, i, s) {
|
|
136
|
+
if (rt(i), !s) return;
|
|
137
|
+
const { x: o, y: d } = nt(t, n.x, n.y), e = document.createElement("div");
|
|
138
|
+
e.style.opacity = "0", e.classList.add("tiny-spark-tooltip"), e.setAttribute("id", `tooltip_${i}`), e.setAttribute("role", "tooltip"), e.setAttribute("aria-live", "polite"), e.setAttribute("aria-hidden", String(!s)), e.style.pointerEvents = "none", e.style.position = "fixed", e.style.top = d + "px", e.style.left = o + "px", e.style.width = "fit-content", e.innerHTML = `
|
|
139
139
|
<div class="tiny-spark-tooltip-content">${n.d ? `${n.d}: ` : ""}${[null, void 0].includes(n.v) ? "-" : E(r, Number(n.v))}</div>
|
|
140
140
|
`, document.body.appendChild(e), F().then(() => {
|
|
141
|
-
const { width:
|
|
142
|
-
e.style.left = `${
|
|
141
|
+
const { width: p, height: h } = e.getBoundingClientRect();
|
|
142
|
+
e.style.left = `${o - p / 2}px`, e.style.top = `${d - h - Number(String(Number(a(r, l.PLOT_RADIUS, 3)) * 1.5))}px`;
|
|
143
143
|
}).then(() => {
|
|
144
144
|
e.style.opacity = "1";
|
|
145
145
|
});
|
|
@@ -148,94 +148,94 @@ function rt(t) {
|
|
|
148
148
|
const r = document.getElementById(`tooltip_${t}`);
|
|
149
149
|
r == null || r.remove();
|
|
150
150
|
}
|
|
151
|
-
function
|
|
151
|
+
function it(t) {
|
|
152
152
|
t.innerHTML = "";
|
|
153
153
|
}
|
|
154
|
-
function
|
|
154
|
+
function ot(t, r) {
|
|
155
155
|
let n = r;
|
|
156
|
-
|
|
157
|
-
const { svg:
|
|
158
|
-
left:
|
|
159
|
-
top:
|
|
160
|
-
width:
|
|
161
|
-
height: d -
|
|
162
|
-
bottom: d -
|
|
163
|
-
}, _ = j(t), { min:
|
|
156
|
+
it(t);
|
|
157
|
+
const { svg: i, svgId: s, width: o, height: d } = J(t), { color: e, backgroundColor: p } = tt(t), h = { T: 12, R: 12, B: 12, L: 12 }, O = K(), L = String(a(t, l.SHOW_LAST_VALUE, "false")) === "true", u = {
|
|
158
|
+
left: h.L,
|
|
159
|
+
top: h.T,
|
|
160
|
+
width: o - h.L - h.R,
|
|
161
|
+
height: d - h.T - h.B,
|
|
162
|
+
bottom: d - h.B
|
|
163
|
+
}, _ = j(t), { min: $ } = D(_), A = _.map((g) => [null, void 0].includes(g) ? g : g + ($ < 0 ? Math.abs($) : 0)), { max: k } = D(A), C = u.width / (_.length - 1) === 1 / 0 ? u.width : u.width / (_.length - 1), X = et(t), b = A.map((g, m) => {
|
|
164
164
|
const f = {
|
|
165
165
|
w: A.length === 1 ? C / 2 : 0,
|
|
166
166
|
h: A.length === 1 ? u.height / 2 : 0
|
|
167
167
|
};
|
|
168
168
|
return {
|
|
169
|
-
y: (1 - (g || 0) / k) * u.height +
|
|
170
|
-
x: u.left + C *
|
|
169
|
+
y: (1 - (g || 0) / k) * u.height + h.T + f.h,
|
|
170
|
+
x: u.left + C * m + f.w,
|
|
171
171
|
v: g,
|
|
172
|
-
d: X[
|
|
172
|
+
d: X[m] || null
|
|
173
173
|
};
|
|
174
|
-
}), v = [...
|
|
174
|
+
}), v = [...b].filter(({ v: g }) => ![null, void 0].includes(g)), x = document.createElementNS(I, "path");
|
|
175
175
|
x.classList.add("tiny-spark-line-path"), !t.dataset.curve || t.dataset.curve === "true" ? x.setAttribute("d", `M ${B(v)}`) : x.setAttribute("d", `M ${W(v)}`), x.setAttribute("fill", "none"), x.setAttribute("stroke", String(a(t, l.LINE_COLOR, e))), x.setAttribute("stroke-width", String(a(t, l.LINE_THICKNESS, 2))), x.setAttribute("stroke-linecap", "round");
|
|
176
176
|
const w = document.createElementNS(I, "path");
|
|
177
177
|
w.classList.add("tiny-spark-line-area");
|
|
178
178
|
const H = t.getAttribute("data-animation");
|
|
179
|
-
H === "true" && n && (x.style.opacity = "0", w.style.opacity = "0"),
|
|
179
|
+
H === "true" && n && (x.style.opacity = "0", w.style.opacity = "0"), b.length && (!t.dataset.curve || t.dataset.curve === "true" ? w.setAttribute("d", `M ${v[0].x},${u.bottom} ${B(v)} L ${v.at(-1).x},${u.bottom} Z`) : w.setAttribute("d", `M ${v[0].x},${u.bottom} ${W(v)} L ${v.at(-1).x},${u.bottom} Z`)), w.setAttribute("fill", String(a(t, l.AREA_COLOR, "transparent"))), b.length > 1 && (i.appendChild(w), i.appendChild(x));
|
|
180
180
|
const P = [];
|
|
181
|
-
|
|
181
|
+
b.forEach((g, m) => {
|
|
182
182
|
const f = document.createElementNS(I, "line");
|
|
183
|
-
f.classList.add("tiny-spark-indicator"), f.setAttribute("id", `indicator_${s}_${
|
|
183
|
+
f.classList.add("tiny-spark-indicator"), f.setAttribute("id", `indicator_${s}_${m}`), f.setAttribute("x1", String(u.left + (b.length === 1 ? u.width / 2 : m * C))), f.setAttribute("x2", String(u.left + (b.length === 1 ? u.width / 2 : m * C))), f.setAttribute("y1", String(u.top)), f.setAttribute("y2", String(u.bottom)), f.setAttribute("stroke", String(a(t, l.INDICATOR_COLOR, "#1A1A1A"))), f.setAttribute("stroke-width", String(a(t, l.INDICATOR_WIDTH, "1"))), f.setAttribute("stroke-linecap", "round"), f.style.pointerEvents = "none", f.style.opacity = "0", P.push(f), i.appendChild(f);
|
|
184
184
|
});
|
|
185
|
-
let
|
|
186
|
-
const M = Number(String(a(t, l.PLOT_RADIUS, 0))) > 0, q = !String(a(t, l.HIDE_PLOTS_ABOVE, "")) ||
|
|
187
|
-
M &&
|
|
185
|
+
let R = [];
|
|
186
|
+
const M = Number(String(a(t, l.PLOT_RADIUS, 0))) > 0, q = !String(a(t, l.HIDE_PLOTS_ABOVE, "")) || b.length <= Number(String(a(t, l.HIDE_PLOTS_ABOVE, 0))), V = M && q;
|
|
187
|
+
M && b.forEach(({ x: g, y: m, v: f }, S) => {
|
|
188
188
|
if (![null, void 0].includes(f)) {
|
|
189
189
|
const c = document.createElementNS(I, "circle");
|
|
190
|
-
c.classList.add("tiny-spark-datapoint-circle"), c.classList.add(`circle-${s}`), c.setAttribute("id", `circle_${s}_${S}`), c.setAttribute("cx", String(g || 0)), c.setAttribute("cy", String(
|
|
190
|
+
c.classList.add("tiny-spark-datapoint-circle"), c.classList.add(`circle-${s}`), c.setAttribute("id", `circle_${s}_${S}`), c.setAttribute("cx", String(g || 0)), c.setAttribute("cy", String(m || 0)), c.setAttribute("r", String(a(t, l.PLOT_RADIUS, 3))), c.setAttribute("fill", String(a(t, l.PLOT_COLOR, String(a(t, "lineColor", e))))), c.setAttribute("stroke", p), c.style.opacity = b.length === 1 ? "1" : "0", c.style.transition = `opacity ${S * (1e3 * 2 / b.length)}ms ease-in`, c.style.pointerEvents = "none", R.push(c), V && i.appendChild(c);
|
|
191
191
|
}
|
|
192
192
|
});
|
|
193
193
|
let y = null;
|
|
194
|
-
if (L &&
|
|
194
|
+
if (L && b.length && b.at(-1)) {
|
|
195
195
|
const g = Number(a(t, l.LAST_VALUE_FONT_SIZE, 12));
|
|
196
|
-
y = document.createElementNS(I, "text"), y.classList.add("tiny-spark-last-value"), y.setAttribute("id", O), y.setAttribute("x", String(
|
|
196
|
+
y = document.createElementNS(I, "text"), y.classList.add("tiny-spark-last-value"), y.setAttribute("id", O), y.setAttribute("x", String(b.at(-1).x + 6 + Number(a(t, l.LINE_THICKNESS, 2)))), y.setAttribute("y", String(b.at(-1).y + g / 3)), y.setAttribute("text-anchor", "start"), y.setAttribute("font-size", String(g) + "px"), y.setAttribute("fill", String(a(t, l.LAST_VALUE_COLOR, String(a(t, l.INDICATOR_COLOR, "#1A1A1A"))))), y.innerHTML = E(t, Number(b.at(-1).v)), y.style.opacity = b.length === 1 ? "1" : "0", i.appendChild(y);
|
|
197
197
|
}
|
|
198
|
-
|
|
199
|
-
const f =
|
|
200
|
-
S.classList.add("tiny-spark-tooltip-trap"), S.setAttribute("x", `${
|
|
201
|
-
if (G(
|
|
202
|
-
const c = document.getElementById(`circle_${s}_${
|
|
198
|
+
b.forEach((g, m) => {
|
|
199
|
+
const f = R[m], S = document.createElementNS(I, "rect");
|
|
200
|
+
S.classList.add("tiny-spark-tooltip-trap"), S.setAttribute("x", `${b.length === 1 ? 0 : u.left + m * C - C / 2}`), S.setAttribute("y", `${u.top}`), S.setAttribute("height", `${u.height}`), S.setAttribute("width", `${C}`), S.setAttribute("fill", "transparent"), S.setAttribute("aria-describedby", `tooltip_${s}`), S.addEventListener("mouseenter", () => {
|
|
201
|
+
if (G(i, t, g, s, !0), V) {
|
|
202
|
+
const c = document.getElementById(`circle_${s}_${m}`);
|
|
203
203
|
c == null || c.setAttribute("r", String(Number(a(t, l.PLOT_RADIUS, 3)) * 1.5));
|
|
204
204
|
} else
|
|
205
|
-
|
|
206
|
-
P[
|
|
205
|
+
i.appendChild(f);
|
|
206
|
+
P[m].style.opacity = "1", L && y && (m === b.length - 1 ? y.style.opacity = "0" : y.style.opacity = "1");
|
|
207
207
|
}), S.addEventListener("mouseout", () => {
|
|
208
|
-
if (G(
|
|
209
|
-
const c = document.getElementById(`circle_${s}_${
|
|
208
|
+
if (G(i, t, g, s, !1), V) {
|
|
209
|
+
const c = document.getElementById(`circle_${s}_${m}`);
|
|
210
210
|
c == null || c.setAttribute("r", String(Number(a(t, l.PLOT_RADIUS, 3))));
|
|
211
211
|
} else
|
|
212
212
|
f.remove();
|
|
213
213
|
P.forEach((c) => c.style.opacity = "0"), L && y && (y.style.opacity = "1");
|
|
214
|
-
}),
|
|
214
|
+
}), i.appendChild(S);
|
|
215
215
|
}), H === "true" && n ? F().then(() => {
|
|
216
|
-
|
|
216
|
+
R.forEach((g) => {
|
|
217
217
|
g.style.opacity = "1";
|
|
218
218
|
}), Y(x, 1e3, () => {
|
|
219
219
|
y && (y.style.opacity = "1");
|
|
220
|
-
}), Q(
|
|
221
|
-
}) : (
|
|
220
|
+
}), Q(i, w);
|
|
221
|
+
}) : (R.forEach((g) => {
|
|
222
222
|
g.style.opacity = "1";
|
|
223
|
-
}), y && (y.style.opacity = "1")), t.appendChild(
|
|
223
|
+
}), y && (y.style.opacity = "1")), t.appendChild(i);
|
|
224
224
|
}
|
|
225
225
|
function at() {
|
|
226
226
|
const t = T();
|
|
227
227
|
t.length && Array.from(t).forEach((r) => {
|
|
228
228
|
if (!r.dataset.id) {
|
|
229
|
-
const
|
|
230
|
-
r.setAttribute("data-id",
|
|
229
|
+
const i = K();
|
|
230
|
+
r.setAttribute("data-id", i);
|
|
231
231
|
}
|
|
232
232
|
const n = r;
|
|
233
233
|
st(n), n.__renderCount = 0, U(n), F().then(() => {
|
|
234
|
-
const
|
|
235
|
-
|
|
234
|
+
const i = new ResizeObserver((o) => {
|
|
235
|
+
o.forEach(() => U(n));
|
|
236
236
|
});
|
|
237
|
-
n.parentElement &&
|
|
238
|
-
for (const d of
|
|
237
|
+
n.parentElement && i.observe(n.parentElement), new MutationObserver((o) => {
|
|
238
|
+
for (const d of o)
|
|
239
239
|
if (d.type === "attributes" && d.attributeName && Object.values(z).includes(d.attributeName)) {
|
|
240
240
|
U(n);
|
|
241
241
|
break;
|
|
@@ -245,7 +245,7 @@ function at() {
|
|
|
245
245
|
});
|
|
246
246
|
}
|
|
247
247
|
function U(t) {
|
|
248
|
-
Z(t, "set") &&
|
|
248
|
+
Z(t, "set") && ot(t, t.__renderCount < 2), t.__renderCount += 1;
|
|
249
249
|
}
|
|
250
250
|
function st(t) {
|
|
251
251
|
t.dataset.set || console.error(
|
package/dist/tiny-spark.umd.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
(function(I,x){typeof exports=="object"&&typeof module<"u"?x(exports):typeof define=="function"&&define.amd?define(["exports"],x):(I=typeof globalThis<"u"?globalThis:I||self,x(I.TinySpark={}))})(this,function(I){"use strict";const x="http://www.w3.org/2000/svg";var a=(t=>(t.ANIMATION="animation",t.AREA_COLOR="areaColor",t.CURVE="curve",t.DATES="dates",t.ID="id",t.INDICATOR_COLOR="indicatorColor",t.INDICATOR_WIDTH="indicatorWidth",t.LINE_COLOR="lineColor",t.LINE_THICKNESS="lineThickness",t.NUMBER_LOCALE="numberLocale",t.NUMBER_ROUNDING="numberRounding",t.NUMBER_SHOW_ON="numberShowOn",t.PLOT_COLOR="plotColor",t.PLOT_RADIUS="plotRadius",t.SET="set",t.HIDE_PLOTS_ABOVE="hidePlotsAbove",t.SHOW_LAST_VALUE="showLastValue",t.LAST_VALUE_FONT_SIZE="lastValueFontSize",t.LAST_VALUE_COLOR="lastValueColor",t))(a||{}),M=(t=>(t.ANIMATION="data-animation",t.AREA_COLOR="data-area-color",t.CURVE="data-curve",t.DATES="data-dates",t.ID="data-id",t.INDICATOR_COLOR="data-indicator-color",t.INDICATOR_WIDTH="data-indicator-width",t.LINE_COLOR="data-line-color",t.LINE_THICKNESS="data-line-thickness",t.NUMBER_LOCALE="data-number-locale",t.NUMBER_ROUNDING="data-number-rounding",t.NUMBER_SHOW_ON="data-number-show-on",t.PLOT_COLOR="data-plot-color",t.PLOT_RADIUS="data-plot-radius",t.SET="data-set",t.HIDE_PLOTS_ABOVE="data-hide-plots-above",t.SHOW_LAST_VALUE="data-show-last-value",t.LAST_VALUE_FONT_SIZE="data-last-value-font-size",t.LAST_VALUE_COLOR="data-last-value-color",t))(M||{});function q(t){const{width:
|
|
2
|
-
<div class="tiny-spark-tooltip-content">${n.d?`${n.d}: `:""}${[null,void 0].includes(n.v)?"-":Z(
|
|
3
|
-
`,document.body.appendChild(e),P().then(()=>{const{width:
|
|
1
|
+
(function(I,x){typeof exports=="object"&&typeof module<"u"?x(exports):typeof define=="function"&&define.amd?define(["exports"],x):(I=typeof globalThis<"u"?globalThis:I||self,x(I.TinySpark={}))})(this,function(I){"use strict";const x="http://www.w3.org/2000/svg";var a=(t=>(t.ANIMATION="animation",t.AREA_COLOR="areaColor",t.CURVE="curve",t.DATES="dates",t.ID="id",t.INDICATOR_COLOR="indicatorColor",t.INDICATOR_WIDTH="indicatorWidth",t.LINE_COLOR="lineColor",t.LINE_THICKNESS="lineThickness",t.NUMBER_LOCALE="numberLocale",t.NUMBER_ROUNDING="numberRounding",t.NUMBER_SHOW_ON="numberShowOn",t.PLOT_COLOR="plotColor",t.PLOT_RADIUS="plotRadius",t.SET="set",t.HIDE_PLOTS_ABOVE="hidePlotsAbove",t.SHOW_LAST_VALUE="showLastValue",t.LAST_VALUE_FONT_SIZE="lastValueFontSize",t.LAST_VALUE_COLOR="lastValueColor",t))(a||{}),M=(t=>(t.ANIMATION="data-animation",t.AREA_COLOR="data-area-color",t.CURVE="data-curve",t.DATES="data-dates",t.ID="data-id",t.INDICATOR_COLOR="data-indicator-color",t.INDICATOR_WIDTH="data-indicator-width",t.LINE_COLOR="data-line-color",t.LINE_THICKNESS="data-line-thickness",t.NUMBER_LOCALE="data-number-locale",t.NUMBER_ROUNDING="data-number-rounding",t.NUMBER_SHOW_ON="data-number-show-on",t.PLOT_COLOR="data-plot-color",t.PLOT_RADIUS="data-plot-radius",t.SET="data-set",t.HIDE_PLOTS_ABOVE="data-hide-plots-above",t.SHOW_LAST_VALUE="data-show-last-value",t.LAST_VALUE_FONT_SIZE="data-last-value-font-size",t.LAST_VALUE_COLOR="data-last-value-color",t))(M||{});function q(t){const{width:i,height:n}=t.parentElement.getBoundingClientRect(),r={width:300,height:100},s=String(l(t,a.SHOW_LAST_VALUE,"false"))==="true",o=j(t),c=o&&o.length?o.at(-1):null;let e=0;if(s&&![null,void 0].includes(c)){const u=Number(String(l(t,a.NUMBER_ROUNDING,0)));e=6+c.toFixed(u).length*(Number(l(t,a.LAST_VALUE_FONT_SIZE,12))/2)}const h=`0 0 ${(i||r.width)+e} ${n||r.height}`,S=document.createElementNS(x,"svg"),C=t.dataset.id;return S.id=C,S.setAttribute("viewBox",h),S.style.width="100%",S.style.height="100%",{svg:S,svgId:C,width:i||r.width,height:n||r.height}}function N(t,i=0){return isNaN(t)?i:t}function W(t){let i=[];for(let n=0;n<t.length;n+=1)i.push(`${N(t[n].x)},${N(t[n].y)} `);return i.join(" ").trim()}function B(t){if(t.length<1)return"0,0";const i=t.length-1,n=[`${N(t[0].x)},${N(t[0].y)}`],r=[],s=[],o=[],c=[];for(let e=0;e<i;e+=1)r[e]=t[e+1].x-t[e].x,s[e]=t[e+1].y-t[e].y,o[e]=s[e]/r[e];c[0]=o[0],c[i]=o[i-1];for(let e=1;e<i;e+=1)if(o[e-1]*o[e]<=0)c[e]=0;else{const b=2*o[e-1]*o[e]/(o[e-1]+o[e]);c[e]=b}for(let e=0;e<i;e+=1){const b=t[e].x,h=t[e].y,S=t[e+1].x,C=t[e+1].y,u=c[e],A=c[e+1],R=b+(S-b)/3,$=h+u*(S-b)/3,F=S-(S-b)/3,v=C-A*(S-b)/3;n.push(`C ${N(R)},${N($)} ${N(F)},${N(v)} ${N(S)},${N(C)}`)}return n.join(" ")}function J(t,i=1e3,n){t.style.opacity="1";const r=t.getTotalLength();t.style.strokeDasharray=String(r),t.style.strokeDashoffset=String(r),t.getBoundingClientRect(),t.style.transition=`stroke-dashoffset ${i}ms ease-in-out`,t.style.strokeDashoffset="0",t.addEventListener("transitionend",function s(){t.style.transition="",t.removeEventListener("transitionend",s),n&&n()})}function Y(t,i,n=1e3){i.style.opacity="1";const r=i.getBBox(),s=r.width,o=document.createElementNS("http://www.w3.org/2000/svg","clipPath"),c="clip-"+Math.random().toString(36).substr(2,9);o.setAttribute("id",c);const e=document.createElementNS("http://www.w3.org/2000/svg","rect");e.setAttribute("x",r.x.toString()),e.setAttribute("y",r.y.toString()),e.setAttribute("width","0"),e.setAttribute("height",r.height.toString()),o.appendChild(e);let b=t.querySelector("defs");b||(b=document.createElementNS("http://www.w3.org/2000/svg","defs"),t.insertBefore(b,t.firstChild)),b.appendChild(o),i.setAttribute("clip-path",`url(#${c})`),e.style.transition=`width ${n}ms ease-out`,e.getBoundingClientRect(),e.setAttribute("width",s.toString()),e.addEventListener("transitionend",function h(){i.removeAttribute("clip-path"),o.parentNode&&o.parentNode.removeChild(o),e.removeEventListener("transitionend",h)})}function Q(){return document.querySelectorAll(".tiny-spark")}function D(t,i){return Object.keys(t.dataset).includes(i)}function l(t,i,n){return D(t,i)?t.dataset[i]:n}function T(t){if(!t)return{color:"#1A1A1A",backgroundColor:"#FFFFFF"};const i=window.getComputedStyle(t),n=i.getPropertyValue("color")||"#1A1A1A",r=i.getPropertyValue("background-color"),s=i.getPropertyValue("background");return{color:n,backgroundColor:r||s||"#FFFFFF"}}function G(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){const i=Math.random()*16|0;return(t=="x"?i:i&3|8).toString(16)})}function j(t){const i=t.getAttribute("data-set");if(!i)return[];try{const n=JSON.parse(i);return Array.isArray(n)&&n.every(r=>typeof r=="number"||[null,void 0].includes(r))?n:(console.warn("data-set is not an array of numbers."),[])}catch(n){return console.error("Error parsing data-set:",n),[]}}function tt(t){const i=t.getAttribute("data-dates");if(!i)return[];try{const n=JSON.parse(i);return Array.isArray(n)&&n.every(r=>typeof r=="string")?n:(console.warn("data-dates is not an array of strings"),[])}catch(n){return console.error("Error parsing data-dates",n),[]}}function z(t){return{min:Math.min(...t),max:Math.max(...t)}}function P(){return new Promise(t=>setTimeout(t,0))}function Z(t,i){const n=String(l(t,a.NUMBER_LOCALE,navigator.language||"en-US")),r=Number(String(l(t,a.NUMBER_ROUNDING,0)));return i.toLocaleString(n,{useGrouping:!0,minimumFractionDigits:r,maximumFractionDigits:r})}function et(t,i,n){if(!t.createSVGPoint||!t.getScreenCTM)throw new Error("Your browser does not support SVG coordinate transformation.");const r=t.getScreenCTM();if(!r)throw new Error("Cannot obtain the screen CTM.");const s=t.createSVGPoint();s.x=i,s.y=n;const o=s.matrixTransform(r);return{x:o.x,y:o.y}}function K(t,i,n,r,s){if(nt(r),!s)return;const{x:o,y:c}=et(t,n.x,n.y),e=document.createElement("div");e.style.opacity="0",e.classList.add("tiny-spark-tooltip"),e.setAttribute("id",`tooltip_${r}`),e.setAttribute("role","tooltip"),e.setAttribute("aria-live","polite"),e.setAttribute("aria-hidden",String(!s)),e.style.pointerEvents="none",e.style.position="fixed",e.style.top=c+"px",e.style.left=o+"px",e.style.width="fit-content",e.innerHTML=`
|
|
2
|
+
<div class="tiny-spark-tooltip-content">${n.d?`${n.d}: `:""}${[null,void 0].includes(n.v)?"-":Z(i,Number(n.v))}</div>
|
|
3
|
+
`,document.body.appendChild(e),P().then(()=>{const{width:b,height:h}=e.getBoundingClientRect();e.style.left=`${o-b/2}px`,e.style.top=`${c-h-Number(String(Number(l(i,a.PLOT_RADIUS,3))*1.5))}px`}).then(()=>{e.style.opacity="1"})}function nt(t){const i=document.getElementById(`tooltip_${t}`);i==null||i.remove()}function it(t){t.innerHTML=""}function rt(t,i){let n=i;it(t);const{svg:r,svgId:s,width:o,height:c}=q(t),{color:e,backgroundColor:b}=T(t),h={T:12,R:12,B:12,L:12},S=G(),C=String(l(t,a.SHOW_LAST_VALUE,"false"))==="true",u={left:h.L,top:h.T,width:o-h.L-h.R,height:c-h.T-h.B,bottom:c-h.B},A=j(t),{min:R}=z(A),$=A.map(g=>[null,void 0].includes(g)?g:g+(R<0?Math.abs(R):0)),{max:F}=z($),v=u.width/(A.length-1)===1/0?u.width:u.width/(A.length-1),lt=tt(t),p=$.map((g,m)=>{const f={w:$.length===1?v/2:0,h:$.length===1?u.height/2:0};return{y:(1-(g||0)/F)*u.height+h.T+f.h,x:u.left+v*m+f.w,v:g,d:lt[m]||null}}),w=[...p].filter(({v:g})=>![null,void 0].includes(g)),L=document.createElementNS(x,"path");L.classList.add("tiny-spark-line-path"),!t.dataset.curve||t.dataset.curve==="true"?L.setAttribute("d",`M ${B(w)}`):L.setAttribute("d",`M ${W(w)}`),L.setAttribute("fill","none"),L.setAttribute("stroke",String(l(t,a.LINE_COLOR,e))),L.setAttribute("stroke-width",String(l(t,a.LINE_THICKNESS,2))),L.setAttribute("stroke-linecap","round");const _=document.createElementNS(x,"path");_.classList.add("tiny-spark-line-area");const E=t.getAttribute("data-animation");E==="true"&&n&&(L.style.opacity="0",_.style.opacity="0"),p.length&&(!t.dataset.curve||t.dataset.curve==="true"?_.setAttribute("d",`M ${w[0].x},${u.bottom} ${B(w)} L ${w.at(-1).x},${u.bottom} Z`):_.setAttribute("d",`M ${w[0].x},${u.bottom} ${W(w)} L ${w.at(-1).x},${u.bottom} Z`)),_.setAttribute("fill",String(l(t,a.AREA_COLOR,"transparent"))),p.length>1&&(r.appendChild(_),r.appendChild(L));const U=[];p.forEach((g,m)=>{const f=document.createElementNS(x,"line");f.classList.add("tiny-spark-indicator"),f.setAttribute("id",`indicator_${s}_${m}`),f.setAttribute("x1",String(u.left+(p.length===1?u.width/2:m*v))),f.setAttribute("x2",String(u.left+(p.length===1?u.width/2:m*v))),f.setAttribute("y1",String(u.top)),f.setAttribute("y2",String(u.bottom)),f.setAttribute("stroke",String(l(t,a.INDICATOR_COLOR,"#1A1A1A"))),f.setAttribute("stroke-width",String(l(t,a.INDICATOR_WIDTH,"1"))),f.setAttribute("stroke-linecap","round"),f.style.pointerEvents="none",f.style.opacity="0",U.push(f),r.appendChild(f)});let k=[];const X=Number(String(l(t,a.PLOT_RADIUS,0)))>0,ut=!String(l(t,a.HIDE_PLOTS_ABOVE,""))||p.length<=Number(String(l(t,a.HIDE_PLOTS_ABOVE,0))),H=X&&ut;X&&p.forEach(({x:g,y:m,v:f},O)=>{if(![null,void 0].includes(f)){const d=document.createElementNS(x,"circle");d.classList.add("tiny-spark-datapoint-circle"),d.classList.add(`circle-${s}`),d.setAttribute("id",`circle_${s}_${O}`),d.setAttribute("cx",String(g||0)),d.setAttribute("cy",String(m||0)),d.setAttribute("r",String(l(t,a.PLOT_RADIUS,3))),d.setAttribute("fill",String(l(t,a.PLOT_COLOR,String(l(t,"lineColor",e))))),d.setAttribute("stroke",b),d.style.opacity=p.length===1?"1":"0",d.style.transition=`opacity ${O*(1e3*2/p.length)}ms ease-in`,d.style.pointerEvents="none",k.push(d),H&&r.appendChild(d)}});let y=null;if(C&&p.length&&p.at(-1)){const g=Number(l(t,a.LAST_VALUE_FONT_SIZE,12));y=document.createElementNS(x,"text"),y.classList.add("tiny-spark-last-value"),y.setAttribute("id",S),y.setAttribute("x",String(p.at(-1).x+6+Number(l(t,a.LINE_THICKNESS,2)))),y.setAttribute("y",String(p.at(-1).y+g/3)),y.setAttribute("text-anchor","start"),y.setAttribute("font-size",String(g)+"px"),y.setAttribute("fill",String(l(t,a.LAST_VALUE_COLOR,String(l(t,a.INDICATOR_COLOR,"#1A1A1A"))))),y.innerHTML=Z(t,Number(p.at(-1).v)),y.style.opacity=p.length===1?"1":"0",r.appendChild(y)}p.forEach((g,m)=>{const f=k[m],O=document.createElementNS(x,"rect");O.classList.add("tiny-spark-tooltip-trap"),O.setAttribute("x",`${p.length===1?0:u.left+m*v-v/2}`),O.setAttribute("y",`${u.top}`),O.setAttribute("height",`${u.height}`),O.setAttribute("width",`${v}`),O.setAttribute("fill","transparent"),O.setAttribute("aria-describedby",`tooltip_${s}`),O.addEventListener("mouseenter",()=>{if(K(r,t,g,s,!0),H){const d=document.getElementById(`circle_${s}_${m}`);d==null||d.setAttribute("r",String(Number(l(t,a.PLOT_RADIUS,3))*1.5))}else r.appendChild(f);U[m].style.opacity="1",C&&y&&(m===p.length-1?y.style.opacity="0":y.style.opacity="1")}),O.addEventListener("mouseout",()=>{if(K(r,t,g,s,!1),H){const d=document.getElementById(`circle_${s}_${m}`);d==null||d.setAttribute("r",String(Number(l(t,a.PLOT_RADIUS,3))))}else f.remove();U.forEach(d=>d.style.opacity="0"),C&&y&&(y.style.opacity="1")}),r.appendChild(O)}),E==="true"&&n?P().then(()=>{k.forEach(g=>{g.style.opacity="1"}),J(L,1e3,()=>{y&&(y.style.opacity="1")}),Y(r,_)}):(k.forEach(g=>{g.style.opacity="1"}),y&&(y.style.opacity="1")),t.appendChild(r)}function ot(){const t=Q();t.length&&Array.from(t).forEach(i=>{if(!i.dataset.id){const r=G();i.setAttribute("data-id",r)}const n=i;st(n),n.__renderCount=0,V(n),P().then(()=>{const r=new ResizeObserver(o=>{o.forEach(()=>V(n))});n.parentElement&&r.observe(n.parentElement),new MutationObserver(o=>{for(const c of o)if(c.type==="attributes"&&c.attributeName&&Object.values(M).includes(c.attributeName)){V(n);break}}).observe(n,{attributes:!0})})})}function V(t){D(t,"set")&&rt(t,t.__renderCount<2),t.__renderCount+=1}function st(t){t.dataset.set||console.error(`Tiny-spark exception:
|
|
4
4
|
|
|
5
5
|
[data-set] data attribute is missing.
|
|
6
6
|
Provide an array of numbers, for example:
|
|
7
7
|
|
|
8
|
-
data-set="[1, 2, 3]"`)}function at(t){return JSON.stringify(t)}I.render=
|
|
8
|
+
data-set="[1, 2, 3]"`)}function at(t){return JSON.stringify(t)}I.render=ot,I.tinyFormat=at,Object.defineProperty(I,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED