tiny-spark 0.2.4-beta.0 → 0.2.4-beta.2
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 +18 -14
- package/dist/tiny-spark.umd.js +3 -3
- package/package.json +1 -1
package/dist/tiny-spark.es.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const L = "http://www.w3.org/2000/svg";
|
|
2
2
|
var V = /* @__PURE__ */ ((t) => (t.BAR = "data-bar", t.LINE = "data-line", t.SET = "data-set", t.RESPONSIVE = "data-responsive", t))(V || {}), y = /* @__PURE__ */ ((t) => (t.ANIMATION = "animation", t.AREA_COLOR = "areaColor", t.CURVE = "curve", t.DATES = "dates", 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))(y || {});
|
|
3
3
|
function G(t) {
|
|
4
|
-
const { width: r, height: e } = t.parentElement.getBoundingClientRect(), o = { width: 300, height: 100 }, a = `0 0 ${r || o.width} ${e || o.height}`, i = document.createElementNS(L, "svg"), u =
|
|
4
|
+
const { width: r, height: e } = t.parentElement.getBoundingClientRect(), o = { width: 300, height: 100 }, a = `0 0 ${r || o.width} ${e || o.height}`, i = document.createElementNS(L, "svg"), u = K();
|
|
5
5
|
return i.id = u, i.setAttribute("viewBox", a), i.style.width = "100%", i.style.height = "100%", {
|
|
6
6
|
svg: i,
|
|
7
7
|
svgId: u,
|
|
@@ -74,7 +74,7 @@ function q(t, r) {
|
|
|
74
74
|
});
|
|
75
75
|
return e.observe(t, { attributes: !0 }), e;
|
|
76
76
|
}
|
|
77
|
-
function
|
|
77
|
+
function J(t) {
|
|
78
78
|
if (!t) return {
|
|
79
79
|
color: "#1A1A1A",
|
|
80
80
|
backgroundColor: "#FFFFFF"
|
|
@@ -82,13 +82,13 @@ function K(t) {
|
|
|
82
82
|
const r = window.getComputedStyle(t), e = r.getPropertyValue("color") || "#1A1A1A", o = r.getPropertyValue("background-color"), a = r.getPropertyValue("background");
|
|
83
83
|
return { color: e, backgroundColor: o || a || "#FFFFFF" };
|
|
84
84
|
}
|
|
85
|
-
function
|
|
85
|
+
function K() {
|
|
86
86
|
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(t) {
|
|
87
87
|
const r = Math.random() * 16 | 0;
|
|
88
88
|
return (t == "x" ? r : r & 3 | 8).toString(16);
|
|
89
89
|
});
|
|
90
90
|
}
|
|
91
|
-
function
|
|
91
|
+
function X(t) {
|
|
92
92
|
const r = t.getAttribute("data-set");
|
|
93
93
|
if (!r) return [];
|
|
94
94
|
try {
|
|
@@ -98,7 +98,7 @@ function Y(t) {
|
|
|
98
98
|
return console.error("Error parsing data-set:", e), [];
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
|
-
function
|
|
101
|
+
function Y(t) {
|
|
102
102
|
const r = t.getAttribute("data-dates");
|
|
103
103
|
if (!r) return [];
|
|
104
104
|
try {
|
|
@@ -108,7 +108,7 @@ function z(t) {
|
|
|
108
108
|
return console.error("Error parsing data-dates", e), [];
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
|
-
function
|
|
111
|
+
function F(t) {
|
|
112
112
|
return {
|
|
113
113
|
min: Math.min(...t),
|
|
114
114
|
max: Math.max(...t)
|
|
@@ -117,7 +117,7 @@ function D(t) {
|
|
|
117
117
|
function H() {
|
|
118
118
|
return new Promise((t) => setTimeout(t, 0));
|
|
119
119
|
}
|
|
120
|
-
function
|
|
120
|
+
function z(t, r) {
|
|
121
121
|
const e = String(h(t, y.NUMBER_LOCALE, navigator.language || "en-US")), o = Number(String(h(t, y.NUMBER_ROUNDING, 0)));
|
|
122
122
|
return r.toLocaleString(e, {
|
|
123
123
|
useGrouping: !0,
|
|
@@ -136,11 +136,11 @@ function Z(t, r, e) {
|
|
|
136
136
|
const i = a.matrixTransform(o);
|
|
137
137
|
return { x: i.x, y: i.y };
|
|
138
138
|
}
|
|
139
|
-
function
|
|
139
|
+
function D(t, r, e, o, a) {
|
|
140
140
|
if (Q(o), !a) return;
|
|
141
141
|
const { x: i, y: u } = Z(t, e.x, e.y), n = document.createElement("div");
|
|
142
142
|
n.style.opacity = "0", n.classList.add("tiny-spark-tooltip"), n.setAttribute("id", `tooltip_${o}`), n.style.pointerEvents = "none", n.style.position = "fixed", n.style.top = u + "px", n.style.left = i + "px", n.style.width = "fit-content", n.innerHTML = `
|
|
143
|
-
<div class="tiny-spark-tooltip-content">${e.d ? `${e.d}: ` : ""}${[null, void 0].includes(e.v) ? "-" :
|
|
143
|
+
<div class="tiny-spark-tooltip-content">${e.d ? `${e.d}: ` : ""}${[null, void 0].includes(e.v) ? "-" : z(r, Number(e.v))}</div>
|
|
144
144
|
`, document.body.appendChild(n), H().then(() => {
|
|
145
145
|
const { width: d, height: g } = n.getBoundingClientRect();
|
|
146
146
|
n.style.left = `${i - d / 2}px`, n.style.top = `${u - g - Number(String(Number(h(r, y.PLOT_RADIUS, 3)) * 1.5))}px`;
|
|
@@ -158,13 +158,13 @@ function T(t) {
|
|
|
158
158
|
function tt(t, r) {
|
|
159
159
|
let e = r;
|
|
160
160
|
T(t);
|
|
161
|
-
const { svg: o, svgId: a, width: i, height: u } = G(t), { color: n, backgroundColor: d } =
|
|
161
|
+
const { svg: o, svgId: a, width: i, height: u } = G(t), { color: n, backgroundColor: d } = J(t), g = { T: 12, R: 12, B: 12, L: 12 }, c = {
|
|
162
162
|
left: g.L,
|
|
163
163
|
top: g.T,
|
|
164
164
|
width: i - g.L - g.R,
|
|
165
165
|
height: u - g.T - g.B,
|
|
166
166
|
bottom: u - g.B
|
|
167
|
-
}, C =
|
|
167
|
+
}, C = X(t), { min: $ } = F(C), O = C.map((l) => [null, void 0].includes(l) ? l : l + ($ < 0 ? Math.abs($) : 0)), { max: E } = F(O), N = c.width / (C.length - 1) === 1 / 0 ? c.width : c.width / (C.length - 1), k = Y(t), m = O.map((l, b) => {
|
|
168
168
|
const s = {
|
|
169
169
|
w: O.length === 1 ? N / 2 : 0,
|
|
170
170
|
h: O.length === 1 ? c.height / 2 : 0
|
|
@@ -195,11 +195,11 @@ function tt(t, r) {
|
|
|
195
195
|
}), m.forEach((l, b) => {
|
|
196
196
|
const s = document.createElementNS(L, "rect");
|
|
197
197
|
s.classList.add("tiny-spark-tooltip-trap"), s.setAttribute("x", `${m.length === 1 ? 0 : c.left + b * N - N / 2}`), s.setAttribute("y", `${c.top}`), s.setAttribute("height", `${c.height}`), s.setAttribute("width", `${N}`), s.setAttribute("fill", "transparent"), s.addEventListener("mouseenter", () => {
|
|
198
|
-
|
|
198
|
+
D(o, t, l, a, !0);
|
|
199
199
|
const x = document.getElementById(`circle_${a}_${b}`);
|
|
200
200
|
x == null || x.setAttribute("r", String(Number(h(t, y.PLOT_RADIUS, 3)) * 1.5)), I[b].style.opacity = "1";
|
|
201
201
|
}), s.addEventListener("mouseout", () => {
|
|
202
|
-
|
|
202
|
+
D(o, t, l, a, !1);
|
|
203
203
|
const x = document.getElementById(`circle_${a}_${b}`);
|
|
204
204
|
x == null || x.setAttribute("r", String(Number(h(t, y.PLOT_RADIUS, 3)))), I.forEach((f) => f.style.opacity = "0");
|
|
205
205
|
}), o.appendChild(s);
|
|
@@ -239,6 +239,10 @@ Provide an array of numbers, for example:
|
|
|
239
239
|
data-set="[1, 2, 3]"`
|
|
240
240
|
);
|
|
241
241
|
}
|
|
242
|
+
function rt(t) {
|
|
243
|
+
return JSON.stringify(t);
|
|
244
|
+
}
|
|
242
245
|
export {
|
|
243
|
-
nt as render
|
|
246
|
+
nt as render,
|
|
247
|
+
rt as tinyFormat
|
|
244
248
|
};
|
package/dist/tiny-spark.umd.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(O,m){typeof exports=="object"&&typeof module<"u"?m(exports):typeof define=="function"&&define.amd?define(["exports"],m):(O=typeof globalThis<"u"?globalThis:O||self,m(O.TinySpark={}))})(this,function(O){"use strict";const m="http://www.w3.org/2000/svg";var _=(t=>(t.BAR="data-bar",t.LINE="data-line",t.SET="data-set",t.RESPONSIVE="data-responsive",t))(_||{}),g=(t=>(t.ANIMATION="animation",t.AREA_COLOR="areaColor",t.CURVE="curve",t.DATES="dates",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))(g||{});function U(t){const{width:r,height:e}=t.parentElement.getBoundingClientRect(),o={width:300,height:100},a=`0 0 ${r||o.width} ${e||o.height}`,i=document.createElementNS(m,"svg"),u=X();return i.id=u,i.setAttribute("viewBox",a),i.style.width="100%",i.style.height="100%",{svg:i,svgId:u,width:r||o.width,height:e||o.height}}function S(t,r=0){return isNaN(t)?r:t}function P(t){let r=[];for(let e=0;e<t.length;e+=1)r.push(`${S(t[e].x)},${S(t[e].y)} `);return r.join(" ").trim()}function F(t){if(t.length<1)return"0,0";const r=t.length-1,e=[`${S(t[0].x)},${S(t[0].y)}`],o=[],a=[],i=[],u=[];for(let n=0;n<r;n+=1)o[n]=t[n+1].x-t[n].x,a[n]=t[n+1].y-t[n].y,i[n]=a[n]/o[n];u[0]=i[0],u[r]=i[r-1];for(let n=1;n<r;n+=1)if(i[n-1]*i[n]<=0)u[n]=0;else{const d=2*i[n-1]*i[n]/(i[n-1]+i[n]);u[n]=d}for(let n=0;n<r;n+=1){const d=t[n].x,y=t[n].y,c=t[n+1].x,w=t[n+1].y,E=u[n],$=u[n+1],k=d+(c-d)/3,v=y+E*(c-d)/3,I=c-(c-d)/3,p=w-$*(c-d)/3;e.push(`C ${S(k)},${S(v)} ${S(I)},${S(p)} ${S(c)},${S(w)}`)}return e.join(" ")}function j(t,r=1e3){t.style.opacity="1";const e=t.getTotalLength();t.style.strokeDasharray=String(e),t.style.strokeDashoffset=String(e),t.getBoundingClientRect(),t.style.transition=`stroke-dashoffset ${r}ms ease-in-out`,t.style.strokeDashoffset="0",t.addEventListener("transitionend",function o(){t.style.transition="",t.removeEventListener("transitionend",o)})}function W(t,r,e=1e3){r.style.opacity="1";const o=r.getBBox(),a=o.width,i=document.createElementNS("http://www.w3.org/2000/svg","clipPath"),u="clip-"+Math.random().toString(36).substr(2,9);i.setAttribute("id",u);const n=document.createElementNS("http://www.w3.org/2000/svg","rect");n.setAttribute("x",o.x.toString()),n.setAttribute("y",o.y.toString()),n.setAttribute("width","0"),n.setAttribute("height",o.height.toString()),i.appendChild(n);let d=t.querySelector("defs");d||(d=document.createElementNS("http://www.w3.org/2000/svg","defs"),t.insertBefore(d,t.firstChild)),d.appendChild(i),r.setAttribute("clip-path",`url(#${u})`),n.style.transition=`width ${e}ms ease-out`,n.getBoundingClientRect(),n.setAttribute("width",a.toString()),n.addEventListener("transitionend",function y(){r.removeAttribute("clip-path"),i.parentNode&&i.parentNode.removeChild(i),n.removeEventListener("transitionend",y)})}function q(){return document.querySelectorAll(".tiny-spark")}function D(t,r){return Object.keys(t.dataset).includes(r)}function f(t,r,e){return D(t,r)?t.dataset[r]:e}function J(t,r){const e=new MutationObserver(o=>{for(const a of o)if(a.type==="attributes"&&a.attributeName&&Object.values(_).includes(a.attributeName)){r();break}});return e.observe(t,{attributes:!0}),e}function K(t){if(!t)return{color:"#1A1A1A",backgroundColor:"#FFFFFF"};const r=window.getComputedStyle(t),e=r.getPropertyValue("color")||"#1A1A1A",o=r.getPropertyValue("background-color"),a=r.getPropertyValue("background");return{color:e,backgroundColor:o||a||"#FFFFFF"}}function X(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){const r=Math.random()*16|0;return(t=="x"?r:r&3|8).toString(16)})}function Y(t){const r=t.getAttribute("data-set");if(!r)return[];try{const e=JSON.parse(r);return Array.isArray(e)&&e.every(o=>typeof o=="number"||[null,void 0].includes(o))?e:(console.warn("data-set is not an array of numbers."),[])}catch(e){return console.error("Error parsing data-set:",e),[]}}function z(t){const r=t.getAttribute("data-dates");if(!r)return[];try{const e=JSON.parse(r);return Array.isArray(e)&&e.every(o=>typeof o=="string")?e:(console.warn("data-dates is not an array of strings"),[])}catch(e){return console.error("Error parsing data-dates",e),[]}}function V(t){return{min:Math.min(...t),max:Math.max(...t)}}function B(){return new Promise(t=>setTimeout(t,0))}function Z(t,r){const e=String(f(t,g.NUMBER_LOCALE,navigator.language||"en-US")),o=Number(String(f(t,g.NUMBER_ROUNDING,0)));return r.toLocaleString(e,{useGrouping:!0,minimumFractionDigits:o,maximumFractionDigits:o})}function Q(t,r,e){if(!t.createSVGPoint||!t.getScreenCTM)throw new Error("Your browser does not support SVG coordinate transformation.");const o=t.getScreenCTM();if(!o)throw new Error("Cannot obtain the screen CTM.");const a=t.createSVGPoint();a.x=r,a.y=e;const i=a.matrixTransform(o);return{x:i.x,y:i.y}}function H(t,r,e,o,a){if(T(o),!a)return;const{x:i,y:u}=Q(t,e.x,e.y),n=document.createElement("div");n.style.opacity="0",n.classList.add("tiny-spark-tooltip"),n.setAttribute("id",`tooltip_${o}`),n.style.pointerEvents="none",n.style.position="fixed",n.style.top=u+"px",n.style.left=i+"px",n.style.width="fit-content",n.innerHTML=`
|
|
2
2
|
<div class="tiny-spark-tooltip-content">${e.d?`${e.d}: `:""}${[null,void 0].includes(e.v)?"-":Z(r,Number(e.v))}</div>
|
|
3
|
-
`,document.body.appendChild(n),B().then(()=>{const{width:d,height:
|
|
3
|
+
`,document.body.appendChild(n),B().then(()=>{const{width:d,height:y}=n.getBoundingClientRect();n.style.left=`${i-d/2}px`,n.style.top=`${u-y-Number(String(Number(f(r,g.PLOT_RADIUS,3))*1.5))}px`}).then(()=>{n.style.opacity="1"})}function T(t){const r=document.getElementById(`tooltip_${t}`);r==null||r.remove()}function tt(t){t.innerHTML=""}function et(t,r){let e=r;tt(t);const{svg:o,svgId:a,width:i,height:u}=U(t),{color:n,backgroundColor:d}=K(t),y={T:12,R:12,B:12,L:12},c={left:y.L,top:y.T,width:i-y.L-y.R,height:u-y.T-y.B,bottom:u-y.B},w=Y(t),{min:E}=V(w),$=w.map(l=>[null,void 0].includes(l)?l:l+(E<0?Math.abs(E):0)),{max:k}=V($),v=c.width/(w.length-1)===1/0?c.width:c.width/(w.length-1),I=z(t),p=$.map((l,b)=>{const s={w:$.length===1?v/2:0,h:$.length===1?c.height/2:0};return{y:(1-(l||0)/k)*c.height+y.T+s.h,x:c.left+v*b+s.w,v:l,d:I[b]||null}}),C=[...p].filter(({v:l})=>![null,void 0].includes(l)),x=document.createElementNS(m,"path");x.classList.add("tiny-spark-line-path"),!t.dataset.curve||t.dataset.curve==="true"?x.setAttribute("d",`M ${F(C)}`):x.setAttribute("d",`M ${P(C)}`),x.setAttribute("fill","none"),x.setAttribute("stroke",String(f(t,g.LINE_COLOR,n))),x.setAttribute("stroke-width",String(f(t,g.LINE_THICKNESS,2))),x.setAttribute("stroke-linecap","round");const L=document.createElementNS(m,"path");L.classList.add("tiny-spark-line-area");const G=t.getAttribute("data-animation");G==="true"&&e&&(x.style.opacity="0",L.style.opacity="0"),p.length&&(!t.dataset.curve||t.dataset.curve==="true"?L.setAttribute("d",`M ${C[0].x},${c.bottom} ${F(C)} L ${C.at(-1).x},${c.bottom} Z`):L.setAttribute("d",`M ${C[0].x},${c.bottom} ${P(C)} L ${C.at(-1).x},${c.bottom} Z`)),L.setAttribute("fill",String(f(t,g.AREA_COLOR,"transparent"))),p.length>1&&(o.appendChild(L),o.appendChild(x));const M=[];p.forEach((l,b)=>{const s=document.createElementNS(m,"line");s.classList.add("tiny-spark-indicator"),s.setAttribute("id",`indicator_${a}_${b}`),s.setAttribute("x1",String(c.left+(p.length===1?c.width/2:b*v))),s.setAttribute("x2",String(c.left+(p.length===1?c.width/2:b*v))),s.setAttribute("y1",String(c.top)),s.setAttribute("y2",String(c.bottom)),s.setAttribute("stroke",String(f(t,g.INDICATOR_COLOR,"#1A1A1A"))),s.setAttribute("stroke-width",String(f(t,g.INDICATOR_WIDTH,"1"))),s.setAttribute("stroke-linecap","round"),s.style.pointerEvents="none",s.style.opacity="0",M.push(s),o.appendChild(s)});let R=[];Number(String(f(t,g.PLOT_RADIUS,0)))>0&&p.forEach(({x:l,y:b,v:s},N)=>{if(![null,void 0].includes(s)){const h=document.createElementNS(m,"circle");h.classList.add("tiny-spark-datapoint-circle"),h.classList.add(`circle-${a}`),h.setAttribute("id",`circle_${a}_${N}`),h.setAttribute("cx",String(l||0)),h.setAttribute("cy",String(b||0)),h.setAttribute("r",String(f(t,g.PLOT_RADIUS,3))),h.setAttribute("fill",String(f(t,g.PLOT_COLOR,String(f(t,"lineColor",n))))),h.setAttribute("stroke",d),h.style.opacity="0",h.style.transition=`opacity ${N*(1e3*2/p.length)}ms ease-in`,o.appendChild(h),R.push(h)}}),p.forEach((l,b)=>{const s=document.createElementNS(m,"rect");s.classList.add("tiny-spark-tooltip-trap"),s.setAttribute("x",`${p.length===1?0:c.left+b*v-v/2}`),s.setAttribute("y",`${c.top}`),s.setAttribute("height",`${c.height}`),s.setAttribute("width",`${v}`),s.setAttribute("fill","transparent"),s.addEventListener("mouseenter",()=>{H(o,t,l,a,!0);const N=document.getElementById(`circle_${a}_${b}`);N==null||N.setAttribute("r",String(Number(f(t,g.PLOT_RADIUS,3))*1.5)),M[b].style.opacity="1"}),s.addEventListener("mouseout",()=>{H(o,t,l,a,!1);const N=document.getElementById(`circle_${a}_${b}`);N==null||N.setAttribute("r",String(Number(f(t,g.PLOT_RADIUS,3)))),M.forEach(h=>h.style.opacity="0")}),o.appendChild(s)}),G==="true"&&e?B().then(()=>{R.forEach(l=>{l.style.opacity="1"}),j(x),W(o,L)}):R.forEach(l=>{l.style.opacity="1"}),t.appendChild(o)}function nt(){const t=q();t.length&&Array.from(t).forEach(r=>{const e=r;rt(e),e.__renderCount=0,A(e),J(e,()=>A(e));const o=new ResizeObserver(i=>{i.forEach(()=>A(e))});e.parentElement&&o.observe(e.parentElement),new MutationObserver(i=>{i.forEach(u=>{u.type==="attributes"&&u.attributeName&&u.attributeName.startsWith("data-")&&A(e)})}).observe(e,{attributes:!0})})}function A(t){D(t,"set")&&et(t,t.__renderCount<2),t.__renderCount+=1}function rt(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]"`)}
|
|
8
|
+
data-set="[1, 2, 3]"`)}function ot(t){return JSON.stringify(t)}O.render=nt,O.tinyFormat=ot,Object.defineProperty(O,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED