@tradeglotnpm/charts 0.1.0 → 0.2.0
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/README.md +1 -0
- package/dist/glot-charts.esm.min.js +4 -4
- package/dist/glot-charts.min.js +4 -4
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1119 -4
- package/dist/series/axes.d.ts +46 -0
- package/dist/series/frame.d.ts +40 -0
- package/dist/series/gestures.d.ts +28 -0
- package/dist/series/logo.d.ts +2 -0
- package/dist/series/paint.d.ts +33 -0
- package/dist/series/series.d.ts +67 -0
- package/dist/series/time-chart.d.ts +76 -0
- package/dist/series/timeline.d.ts +15 -0
- package/dist/series/types.d.ts +128 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,6 +38,7 @@ chart.update({
|
|
|
38
38
|
|
|
39
39
|
## Documentation
|
|
40
40
|
|
|
41
|
+
- [Live demo](https://tradeglot.github.io/glot-charts/)
|
|
41
42
|
- [Getting started](https://github.com/tradeglot/glot-charts/blob/main/docs/getting-started.md)
|
|
42
43
|
- [API reference](https://github.com/tradeglot/glot-charts/blob/main/docs/api.md)
|
|
43
44
|
- [Examples: plain HTML, React, Vue](https://github.com/tradeglot/glot-charts/tree/main/examples)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/*! Glot Charts 0.
|
|
2
|
-
var
|
|
1
|
+
/*! Glot Charts 0.2.0 | Apache-2.0 with NOTICE | https://github.com/tradeglot/glot-charts */
|
|
2
|
+
var Fe=Object.defineProperty;var Be=(t,e,i)=>e in t?Fe(t,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[e]=i;var p=(t,e,i)=>Be(t,typeof e!="symbol"?e+"":e,i);function de(t,e,i){let n=Math.ceil(e/i.barWidth)+1,r=Math.min(t-1,Math.floor(i.end));return[Math.max(0,r-n+1),r]}function Q(t,e,i){return e-(i.end-t+.5)*i.barWidth}function q(t,e,i){return Math.round(i.end+.5-(e-t)/i.barWidth)}function K(t,e=.05){let i=1/0,n=-1/0;for(let o of t)o===null||!Number.isFinite(o)||(o<i&&(i=o),o>n&&(n=o));if(i===1/0)return[0,1];if(i===n)return[i-1,n+1];let r=(n-i)*e;return[i-r,n+r]}function R(t,e){let i=t/Math.max(1,e),n=10**Math.floor(Math.log10(i)),r=i/n;return(r<1.5?1:r<3?2:r<7?5:10)*n}function P(t){return Math.max(0,Math.min(8,-Math.floor(Math.log10(t)+1e-9)))}function y(t,e){let i=Math.abs(t).toFixed(e),[n="0",r]=i.split("."),o=n.replace(/\B(?=(\d{3})+(?!\d))/g,",");return(t<0?"\u2212":"")+o+(r?"."+r:"")}function z(t,e){if(!t)return null;switch(t){case"up":return e.up;case"down":return e.down;case"accent":return e.accent;case"neutral":return e.muted;default:return t}}function ee(t,e,i,n,r,o,s,a){t.lineWidth=1.5;for(let l=Math.max(r,1);l<=o;l++){let h=e.values[l-1],m=e.values[l];h==null||m===null||m===void 0||(t.strokeStyle=z(e.colors?.[l],n)??i,t.beginPath(),t.moveTo(s(l-1),a(h)),t.lineTo(s(l),a(m)),t.stroke())}t.lineWidth=1}function pe(t,e,i,n,r,o,s,a){let l=i.find(m=>m.name===e.a)?.values,h=i.find(m=>m.name===e.b)?.values;if(!(!l||!h))for(let m=Math.max(r,1);m<=o;m++){let u=[l[m-1],l[m],h[m],h[m-1]],c=z(e.colors[m],n);if(!c||u.some(b=>b==null))continue;let[g,v,S,A]=u;t.fillStyle=c,t.beginPath(),t.moveTo(s(m-1),a(g)),t.lineTo(s(m),a(v)),t.lineTo(s(m),a(S)),t.lineTo(s(m-1),a(A)),t.closePath(),t.fill()}}function ce(t,e,i,n,r,o,s,a,l){for(let m=r;m<=o;m++){if(!e.values[m])continue;let u=i[m],c=s(m),g=e.at==="below_bar"?a(u.low)+12+2:e.at==="top"?12:e.at==="bottom"?l-12:a(u.high)-12-2,v=z(e.colors?.[m],n)??n.accent;if(t.fillStyle=t.strokeStyle=v,Ve(t,e.shape,c,g,6),e.text){t.font=`11px ${n.font}`,t.textAlign="center";let S=e.at==="below_bar"||e.at==="bottom";t.fillText(e.text,c,S?g+12+4:g-12-4),t.textAlign="start"}}}function Ve(t,e,i,n,r){switch(t.beginPath(),e){case"triangle_up":case"arrow_up":case"label_up":L(t,[i,n-r,i+r,n+r,i-r,n+r]);break;case"triangle_down":case"arrow_down":case"label_down":L(t,[i,n+r,i+r,n-r,i-r,n-r]);break;case"square":t.rect(i-r*.8,n-r*.8,r*1.6,r*1.6);break;case"diamond":L(t,[i,n-r,i+r,n,i,n+r,i-r,n]);break;case"cross":case"x_cross":{let o=e==="cross"?0:Math.PI/4;t.lineWidth=2;for(let s of[o,o+Math.PI/2])t.moveTo(i-r*Math.cos(s),n-r*Math.sin(s)),t.lineTo(i+r*Math.cos(s),n+r*Math.sin(s));t.stroke(),t.lineWidth=1;return}case"flag":L(t,[i-r*.6,n-r,i+r,n-r*.4,i-r*.6,n+r*.2]);break;default:t.arc(i,n,r*.7,0,Math.PI*2)}t.fill()}function L(t,e){t.moveTo(e[0],e[1]);for(let i=2;i<e.length;i+=2)t.lineTo(e[i],e[i+1]);t.closePath()}var x=72,H=24;function be(t,e,i,n,r){let o=r.width-x,s=e.series??[],a=s.filter(d=>!d.overlay),l=(r.height-H)*(a.length?.72:1),h=l,m=r.height-H-l;if(t.clearRect(0,0,r.width,r.height),t.fillStyle=n.bg,t.fillRect(0,0,r.width,r.height),e.bars.length===0)return;let[u,c]=de(e.bars.length,o,i),g=e.bars.slice(u,c+1),v=s.filter(d=>d.overlay),[S,A]=K([...g.flatMap(d=>[d.low,d.high]),...v.flatMap(d=>d.values.slice(u,c+1))]),b=d=>(A-d)/(A-S)*(l-8)+4,w=d=>Q(d,o,i),$=R(A-S,Math.max(2,Math.floor(l/60))),le=P($);t.font=`12px ${n.mono}`,t.textBaseline="middle";for(let d=Math.ceil(S/$)*$;d<=A;d+=$)D(t,n.grid,0,b(d),o,b(d)),t.fillStyle=n.muted,t.fillText(y(d,le),o+6,b(d));r.marked!==null&&r.marked>=u&&r.marked<=c&&(t.fillStyle=n.grid,t.fillRect(w(r.marked)-i.barWidth/2,0,i.barWidth,r.height-H));let he=Math.max(1,i.barWidth*.7);for(let d=u;d<=c;d++){let f=e.bars[d],T=f.close>=f.open;t.strokeStyle=t.fillStyle=T?n.up:n.down;let C=Math.round(w(d))+.5;D(t,t.strokeStyle,C,b(f.high),C,b(f.low));let M=b(Math.max(f.open,f.close));t.fillRect(C-he/2,M,he,Math.max(1,b(Math.min(f.open,f.close))-M))}for(let d of e.fills??[])pe(t,d,s,n,u,c,w,b);v.forEach((d,f)=>{let T=n.series[f%n.series.length];d.colors?.some(C=>C)?ee(t,d,T,n,u,c,w,b):fe(t,T,d.values,u,c,w,b,f>=n.series.length)});for(let d of e.shapes??[])ce(t,d,e.bars,n,u,c,w,b,l);let me=d=>ge(e.bars,d);for(let d of e.trades??[]){let f=me(d.entryTime),T=me(d.exitTime),C=d.side==="buy"?n.up:n.down;f>=u&&f<=c&&Le(t,C,w(f),b(d.entryPrice),d.side==="buy"),T>=u&&T<=c&&ze(t,C,w(T),b(d.exitPrice))}if(a.length){D(t,n.grid,0,h,r.width,h);let[d,f]=K(a.flatMap(M=>M.values.slice(u,c+1))),T=M=>h+4+(f-M)/(f-d)*(m-8);a.forEach((M,Oe)=>{let ue=n.series[Oe%n.series.length];M.colors?.some(We=>We)?ee(t,M,ue,n,u,c,w,T):fe(t,ue,M.values,u,c,w,T,!1)});let C=P(R(f-d,2));t.fillStyle=n.muted,t.fillText(y(f,C),o+6,h+10),t.fillText(y(d,C),o+6,h+m-10)}Ne(t,e,i,n,r,u,c),$e(t,e,n,r,w,b,l,le)}function Ne(t,e,i,n,r,o,s){let a=r.width-x,l=r.height-H;D(t,n.grid,0,l,r.width,l),t.fillStyle=n.muted,t.font=`12px ${n.font}`;let h=Math.max(1,Math.ceil(110/i.barWidth));for(let m=o-o%h;m<=s;m+=h){if(m<o)continue;let u=new Date(e.bars[m].time).toISOString();t.fillText(`${u.slice(5,10)} ${u.slice(11,16)}`,Q(m,a,i)-30,l+12)}t.fillText("UTC",a+6,l+12)}function $e(t,e,i,n,r,o,s,a){if(n.hover===null||!e.bars[n.hover])return;let l=e.bars[n.hover];t.setLineDash([4,4]),D(t,i.muted,Math.round(r(n.hover))+.5,0,Math.round(r(n.hover))+.5,n.height-H),t.setLineDash([]);let h=o(l.close);h>=0&&h<=s&&(t.fillStyle=i.grid,t.fillRect(n.width-x,h-9,x,18),t.fillStyle=i.text,t.font=`12px ${i.mono}`,t.fillText(y(l.close,a+1),n.width-x+6,h))}function fe(t,e,i,n,r,o,s,a){t.strokeStyle=e,t.lineWidth=1.5,t.setLineDash(a?[6,3]:[]),t.beginPath();let l=!1;for(let h=n;h<=r;h++){let m=i[h];if(m==null||!Number.isFinite(m)){l=!1;continue}l?t.lineTo(o(h),s(m)):t.moveTo(o(h),s(m)),l=!0}t.stroke(),t.setLineDash([]),t.lineWidth=1}function D(t,e,i,n,r,o){t.strokeStyle=e,t.beginPath(),t.moveTo(i,n),t.lineTo(r,o),t.stroke()}function Le(t,e,i,n,r){let s=r?n+10:n-10;t.fillStyle=e,t.beginPath(),t.moveTo(i,r?s-6:s+6),t.lineTo(i-6,r?s+6:s-6),t.lineTo(i+6,r?s+6:s-6),t.fill()}function ze(t,e,i,n){t.strokeStyle=e,t.lineWidth=2,t.beginPath(),t.arc(i,n,4,0,Math.PI*2),t.stroke(),t.lineWidth=1}function ge(t,e){let i=0,n=t.length-1;for(;i<n;){let r=i+n+1>>1;t[r].time<=e?i=r:n=r-1}return i}var E="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI5NiIgaGVpZ2h0PSIyMCIgdmlld0JveD0iMCAwIDk2IDIwIiByb2xlPSJpbWciIGFyaWEtbGFiZWw9Ikdsb3QgQ2hhcnRzIj4KPHJlY3QgeD0iMiIgeT0iMiIgd2lkdGg9IjE2IiBoZWlnaHQ9IjE2IiByeD0iNCIgZmlsbD0iIzZENUJGRiIvPgo8cGF0aCBkPSJNNyA2LjVMNC44IDEwIDcgMTMuNU0xMyA2LjVMMTUuMiAxMCAxMyAxMy41IiBmaWxsPSJub25lIiBzdHJva2U9IiNGRkZGRkYiIHN0cm9rZS13aWR0aD0iMS40IiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiLz4KPHJlY3QgeD0iOC42IiB5PSI3IiB3aWR0aD0iMi44IiBoZWlnaHQ9IjYiIHJ4PSIwLjgiIGZpbGw9IiNGRkZGRkYiLz4KPHRleHQgeD0iMjMiIHk9IjE0LjUiIGZvbnQtZmFtaWx5PSJTcGFjZSBHcm90ZXNrLCBBcmlhbCwgc2Fucy1zZXJpZiIgZm9udC1zaXplPSIxMiIgZm9udC13ZWlnaHQ9IjYwMCIgZmlsbD0iIzVBNUY3QSI+R2xvdCBDaGFydHM8L3RleHQ+Cjwvc3ZnPgo=",O="https://tradeglot.com/";var _e=`
|
|
3
3
|
:where(.glot-chart-legend){position:absolute;left:8px;top:6px;right:80px;font:12px var(--glot-chart-mono);color:var(--glot-chart-text);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;pointer-events:none}
|
|
4
4
|
:where(.glot-chart-label){position:absolute;right:80px;bottom:30px;font:600 12px var(--glot-chart-font);color:var(--glot-chart-muted);border:1px solid var(--glot-chart-grid);border-radius:999px;padding:2px 10px;pointer-events:none}
|
|
5
|
-
`;function
|
|
6
|
-
`).filter(a=>a.trim()),n=(t.shift()??"").split(",").map(a=>a.trim().toLowerCase()),i=a=>n.indexOf(a),[r,o,l,u,h]=["time","open","high","low","close"].map(i);if([r,o,l,u,h].some(a=>a===void 0||a<0))return[];let m=[];for(let a of t){let d=a.split(","),c={time:Number(d[r]),open:Number(d[o]),high:Number(d[l]),low:Number(d[u]),close:Number(d[h])};Object.values(c).every(Number.isFinite)&&m.push(c)}return m}export{Z as ATTRIBUTION_HREF,O as ATTRIBUTION_SRC,k as GlotChart,_ as THEMES,ge as VERSION,P as barAt,fe as barsFromCsv,q as bisect,be as createChart,H as decimalsFor,L as extent,I as formatNumber,W as niceStep,G as readTheme,B as resolveColor,U as visibleRange,N as xOf};
|
|
5
|
+
`;function _(t){if(t.querySelector("style[data-glot-charts]"))return;let e=t.createElement("style");e.dataset.glotCharts="",e.textContent=_e,t.head.append(e)}function U(t,e){t.style.setProperty("--glot-chart-text",e.text),t.style.setProperty("--glot-chart-muted",e.muted),t.style.setProperty("--glot-chart-grid",e.grid),t.style.setProperty("--glot-chart-font",e.font),t.style.setProperty("--glot-chart-mono",e.mono)}var ve='"Be Vietnam Pro", "Inter", sans-serif',ye='"JetBrains Mono", Menlo, monospace',te={dark:{bg:"#0E0F1A",text:"#ECEEF8",muted:"#A3A8C3",grid:"#2A2E48",up:"#3D8BFF",down:"#FF7A3D",accent:"#A99FFF",font:ve,mono:ye,series:["#A99FFF","#ECEEF8","#A3A8C3"]},light:{bg:"#F6F6FB",text:"#14152B",muted:"#5A5F7A",grid:"#DCDDEA",up:"#2266DE",down:"#BA481A",accent:"#5B4BE0",font:ve,mono:ye,series:["#5B4BE0","#14152B","#5A5F7A"]}};function W(t,e="dark"){let i=typeof e=="string"?te[e]:{...te.dark,...e},n=getComputedStyle(t),r=(h,m)=>n.getPropertyValue(h).trim()||m,o=r("--tg-text-accent",i.accent),s=r("--tg-text",i.text),a=r("--tg-muted",i.muted),l=n.getPropertyValue("--tg-text-accent").trim()!=="";return{bg:r("--tg-bg",i.bg),text:s,muted:a,grid:r("--tg-border",i.grid),up:r("--tg-chart-candle-up",i.up),down:r("--tg-chart-candle-down",i.down),accent:o,font:r("--tg-font-ui",i.font),mono:r("--tg-font-mono",i.mono),series:l?[o,s,a]:i.series}}var Ue=2,Ge=40,G=class{constructor(e,i={}){this.opts=i;p(this,"el");p(this,"canvas");p(this,"legend");p(this,"labelEl");p(this,"data",{bars:[]});p(this,"view",{end:0,barWidth:8});p(this,"frame",{width:0,height:0,hover:null,marked:null});p(this,"drag",null);p(this,"moved",!1);p(this,"raf",0);p(this,"resize");if(this.el=document.createElement("div"),this.el.className="glot-chart",this.el.style.cssText="position:relative;width:100%;height:100%;overflow:hidden;touch-action:none",this.canvas=document.createElement("canvas"),this.canvas.style.cssText="position:absolute;inset:0;width:100%;height:100%;cursor:crosshair",this.canvas.setAttribute("role","img"),this.legend=document.createElement("div"),this.legend.className="glot-chart-legend",this.labelEl=document.createElement("span"),this.labelEl.className="glot-chart-label",this.el.append(this.canvas,this.legend,this.labelEl),i.attributionLogo!==!1){let n=document.createElement("a");n.className="glot-chart-attribution",n.href=i.attributionHref??O,n.target="_blank",n.rel="noopener",n.title="Charts by Glot Charts (TradeGlot)",n.style.cssText="position:absolute;left:8px;bottom:28px;height:18px;opacity:0.7";let r=document.createElement("img");r.src=i.attributionSrc??E,r.alt="Glot Charts",r.height=18,r.style.cssText="height:18px;display:block",n.append(r),this.el.append(n)}_(e.ownerDocument),e.append(this.el),this.bind(),this.resize=new ResizeObserver(()=>this.schedule()),this.resize.observe(this.el)}setData(e,i=!1){let n=this.view.end>=this.data.bars.length-1;this.data=e,(!i||n)&&(this.view.end=Math.max(0,e.bars.length-1)),this.labelEl.textContent=e.label??"",this.labelEl.hidden=!e.label,this.canvas.setAttribute("aria-label",this.ariaLabel()),this.schedule()}update(e){let i=this.data.bars,n=i[i.length-1],r=this.view.end>=i.length-1;if(n&&e.time<n.time)throw new RangeError(`update: bar time ${e.time} is older than the last bar ${n.time}`);n&&e.time===n.time?i[i.length-1]=e:(i.push(e),r&&(this.view.end=i.length-1)),this.canvas.setAttribute("aria-label",this.ariaLabel()),this.schedule()}setTheme(e){this.opts={...this.opts,theme:e},this.schedule()}getData(){return this.data}mark(e){if(this.frame.marked=e,e!==null){let i=(this.frame.width-x)/this.view.barWidth;(e>this.view.end||e<this.view.end-i+1)&&(this.view.end=Math.min(this.data.bars.length-1,e+Math.floor(i/2)))}this.schedule()}refresh(){this.schedule()}dispose(){this.resize.disconnect(),cancelAnimationFrame(this.raf),this.el.remove()}ariaLabel(){let e=this.data.label;return`${this.data.bars.length} bars${e?`, ${e}`:""}`}bind(){let e=this.canvas;e.addEventListener("wheel",i=>{i.preventDefault();let n=i.deltaY>0?.85:1.15;this.view.barWidth=Math.min(Ge,Math.max(Ue,this.view.barWidth*n)),this.schedule()},{passive:!1}),e.addEventListener("pointerdown",i=>{this.drag={x:i.clientX,end:this.view.end},this.moved=!1,e.setPointerCapture(i.pointerId)}),e.addEventListener("pointermove",i=>{let n=e.getBoundingClientRect();if(this.drag){let r=i.clientX-this.drag.x;Math.abs(r)>3&&(this.moved=!0);let o=this.data.bars.length;this.view.end=Math.min(o-1+20,Math.max(0,this.drag.end-r/this.view.barWidth))}this.hover(q(i.clientX-n.left,n.width-x,this.view))}),e.addEventListener("pointerup",i=>{let n=e.getBoundingClientRect();if(this.drag&&!this.moved){let r=q(i.clientX-n.left,n.width-x,this.view);r>=0&&r<this.data.bars.length&&this.opts.onBarClick?.(r)}this.drag=null}),e.addEventListener("pointerleave",()=>this.hover(null))}hover(e){let i=e!==null&&e>=0&&e<this.data.bars.length?e:null;this.frame.hover=i,this.renderLegend(i??this.frame.marked??this.data.bars.length-1),this.schedule()}renderLegend(e){let i=this.data.bars[e];if(!i){this.legend.textContent="";return}let n=o=>y(o,2),r=[`O ${n(i.open)} H ${n(i.high)} L ${n(i.low)} C ${n(i.close)}`];for(let o of this.data.series??[]){let s=o.values[e];r.push(`${o.name} ${s==null?"na":n(s)}`)}this.legend.textContent=r.join(" ")}schedule(){cancelAnimationFrame(this.raf),this.raf=requestAnimationFrame(()=>this.draw())}draw(){let e=window.devicePixelRatio||1,i=this.el.clientWidth,n=this.el.clientHeight;if(i===0||n===0)return;(this.canvas.width!==Math.round(i*e)||this.canvas.height!==Math.round(n*e))&&(this.canvas.width=Math.round(i*e),this.canvas.height=Math.round(n*e));let r=this.canvas.getContext("2d");if(!r)return;r.setTransform(e,0,0,e,0,0),this.frame.width=i,this.frame.height=n,this.frame.hover===null&&this.renderLegend(this.frame.marked??this.data.bars.length-1);let o=W(this.el,this.opts.theme);U(this.el,o),be(r,this.data,this.view,o,this.frame)}};function Ze(t,e={}){return new G(t,e)}var je="0.2.0";function Ye(t){let e=t.split(`
|
|
6
|
+
`).filter(m=>m.trim()),i=(e.shift()??"").split(",").map(m=>m.trim().toLowerCase()),n=m=>i.indexOf(m),[r,o,s,a,l]=["time","open","high","low","close"].map(n);if([r,o,s,a,l].some(m=>m===void 0||m<0))return[];let h=[];for(let m of e){let u=m.split(","),c={time:Number(u[r]),open:Number(u[o]),high:Number(u[s]),low:Number(u[a]),close:Number(u[l])};Object.values(c).every(Number.isFinite)&&h.push(c)}return h}function we(t){let e=document.createElement("a");e.className="glot-chart-attribution",e.href=t.attributionHref??O,e.target="_blank",e.rel="noopener",e.title="Charts by Glot Charts (TradeGlot)";let i=t.timeAxisHeight??24;e.style.cssText=`position:absolute;left:8px;bottom:${i+6}px;height:18px;opacity:0.7;z-index:3`;let n=document.createElement("img");return n.src=t.attributionSrc??E,n.alt="Glot Charts",n.height=18,n.style.cssText="height:18px;display:block",e.append(n),e}function Te(t,e){switch(t){case"dotted":return[e,e];case"dashed":return[2*e,2*e];case"large-dashed":return[6*e,6*e];case"sparse-dotted":return[e,4*e];default:return[]}}function*ie(t,e){for(let i of t){let n=e.at(i.time);n===void 0||n<e.first-1||n>e.last+1||(yield[i,n])}}function Ce(t,e,i){e.visible&&(e.kind==="candles"?Xe(t,e,i):e.kind==="line"?Je(t,e,i):Qe(t,e,i))}function Xe(t,e,i){let n=e.opts,r=n.upColor??i.theme.up,o=n.downColor??i.theme.down,s=Math.max(1,Math.min(i.bw*.7,i.bw-1));t.lineWidth=1,t.setLineDash([]);for(let[a,l]of ie(e.data(),i)){let h=a.close>=a.open,m=Math.round(i.x(l))+.5;t.strokeStyle=h?n.wickUpColor??r:n.wickDownColor??o,t.beginPath(),t.moveTo(m,i.y(a.high)),t.lineTo(m,i.y(a.low)),t.stroke(),t.fillStyle=h?r:o;let u=i.y(Math.max(a.open,a.close)),c=Math.max(1,i.y(Math.min(a.open,a.close))-u);t.fillRect(Math.round(m-s/2),u,Math.max(1,Math.round(s)),c)}}function Je(t,e,i){let n=e.opts.width??1.5;t.strokeStyle=e.opts.color??i.theme.series[0],t.lineWidth=n,t.setLineDash(Te(e.opts.style,n)),t.beginPath();let r=!1,o=-2;for(let[s,a]of ie(e.data(),i)){if(!Number.isFinite(s.value)){r=!1;continue}r&&a>o?t.lineTo(i.x(a),i.y(s.value)):t.moveTo(i.x(a),i.y(s.value)),r=!0,o=a}t.stroke(),t.setLineDash([]),t.lineWidth=1}function Qe(t,e,i){let n=i.y(e.opts.base??0),r=Math.max(1,Math.round(i.bw*.8)),o=e.opts.color??i.theme.series[0];for(let[s,a]of ie(e.data(),i)){if(!Number.isFinite(s.value))continue;let l=i.y(s.value);t.fillStyle=s.color??o,t.fillRect(Math.round(i.x(a)-r/2),Math.min(l,n),r,Math.max(1,Math.abs(n-l)))}}function Me(t,e,i){if(!e.visible)return;t.font=`11px ${i.theme.font}`,t.textBaseline="bottom",t.textAlign="right";for(let r of e.lines){let o=r.opts,s=Math.round(i.y(o.price))+.5;if(s<0||s>i.plotH)continue;let a=o.width??1;t.strokeStyle=o.color??i.theme.muted,t.lineWidth=a,t.setLineDash(Te(o.style,a)),t.beginPath(),t.moveTo(0,s),t.lineTo(i.plotW,s),t.stroke(),o.title&&o.axisLabel===!1&&(t.setLineDash([]),t.fillStyle=o.color??i.theme.muted,t.fillText(o.title,i.plotW-6,s-2))}let n=re(e,i.theme);if(n&&e.opts.lastPriceLine===!0){let r=Math.round(i.y(n.value))+.5;t.strokeStyle=n.color,t.lineWidth=1,t.setLineDash([1,2]),t.beginPath(),t.moveTo(0,r),t.lineTo(i.plotW,r),t.stroke()}t.setLineDash([]),t.lineWidth=1,t.textAlign="left"}function ne(t){let e=0,i=0,n=0,r=/^#([0-9a-f]{3}|[0-9a-f]{6})/i.exec(t),o=/rgba?\(([^)]+)\)/i.exec(t);if(r){let s=r[1].length===3?[...r[1]].map(a=>a+a).join(""):r[1];[e,i,n]=[0,2,4].map(a=>parseInt(s.slice(a,a+2),16))}else if(o)[e,i,n]=o[1].split(",").map(s=>parseFloat(s));else return"#ffffff";return .299*e+.587*i+.114*n>150?"#000000":"#ffffff"}function re(t,e){let i=t.data(),n=i[i.length-1];if(!n)return null;if(t.kind==="candles"){let s=n,a=s.close>=s.open;return{value:s.close,color:(a?t.opts.upColor:t.opts.downColor)??(a?e.up:e.down)}}let r=n,o=r.color??t.opts.color??e.series[0];return{value:r.value,color:o}}function Se(t,e,i){if(!e.visible||e.markers.length===0)return;let n=e.data(),r=new Map(n.map(a=>[a.time,a])),o=new Map,s=Math.max(7,Math.min(12,i.bw*.9));t.font=`11px ${i.theme.font}`,t.textAlign="center";for(let a of e.markers){let l=i.at(a.time),h=r.get(a.time);if(l===void 0||!h||l<i.first||l>i.last)continue;let m="high"in h?[h.high,h.low]:[h.value,h.value],u=`${a.time}:${a.position}`,c=o.get(u)??0,g=s*(a.size??1),v=qe(t,a,i.x(l),m,i,g,c);o.set(u,c+v)}t.textAlign="left"}function qe(t,e,i,[n,r],o,s,a){let l=e.color??o.theme.accent,h=e.position==="above",m=e.position==="inside"?o.y(n):h?o.y(n)-4:o.y(r)+4,u=h?-1:1,c=m+u*a,g=e.position==="inside"?m:c+u*s/2;t.fillStyle=l,s>=3&&Ke(t,e.shape,i,g,s);let v=s+2;if(e.text){t.textBaseline=h?"bottom":"top";let S=e.position==="inside"?g-s:c+u*(s+2);t.fillText(e.text,i,S),v+=13}return v}function Ke(t,e,i,n,r){let o=r/2;if(t.beginPath(),e==="circle")t.arc(i,n,o*.8,0,Math.PI*2);else if(e==="square")t.rect(i-o*.8,n-o*.8,o*1.6,o*1.6);else{let s=e==="arrow_up"?-1:1;t.moveTo(i,n+s*o),t.lineTo(i-o,n),t.lineTo(i-o*.35,n),t.lineTo(i-o*.35,n-s*o),t.lineTo(i+o*.35,n-s*o),t.lineTo(i+o*.35,n),t.lineTo(i+o,n),t.closePath()}t.fill()}var xe="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),Z=864e5;function ke(t,e,i,n){let r=R(e-t,Math.max(2,Math.floor(i/50))),o=[];for(let s=Math.ceil(t/r)*r;s<=e;s+=r)o.push(s);return{ticks:o,dec:n??P(r)}}var oe=[1,5,10,15,30,60,120,300,600,900,1800,3600,7200,10800,14400,21600,43200,86400,172800,604800,2592e3,7776e3,31536e3].map(t=>t*1e3);function Ie(t,e){let i=90/t.bw*e.step(),n=oe.find(s=>s>=i)??oe[oe.length-1],r=[],o=null;for(let s=Math.max(0,t.first-1);s<=t.last;s++){let a=e.times[s];if(a===void 0)continue;let l=Math.floor(a/n);o!==null&&l!==o&&s>=t.first&&r.push(s),o=l}return r}var I=t=>String(t).padStart(2,"0");function et(t){return t.getUTCMonth()===0&&t.getUTCDate()===1?String(t.getUTCFullYear()):`${t.getUTCDate()} ${xe[t.getUTCMonth()]}`}function tt(t,e,i,n){let r=new Date(t),o=e===null||Math.floor(e/Z)!==Math.floor(t/Z);if(!n.timeVisible||i>=Z||o&&t%Z===0)return et(r);let s=`${I(r.getUTCHours())}:${I(r.getUTCMinutes())}`;return n.secondsVisible?`${s}:${I(r.getUTCSeconds())}`:s}function it(t,e){let i=new Date(t),n=`${I(i.getUTCDate())} ${xe[i.getUTCMonth()]} ${i.getUTCFullYear()}`;if(!e.timeVisible)return n;let r=e.secondsVisible?`:${I(i.getUTCSeconds())}`:"";return`${n} ${I(i.getUTCHours())}:${I(i.getUTCMinutes())}${r}`}function Ae(t,e,i,n,r){if(r.grid!=="none"){t.strokeStyle=e.theme.grid,t.lineWidth=1,t.setLineDash(r.grid==="dotted"?[1,3]:[]),t.beginPath();for(let o of i){let s=Math.round(e.y(o))+.5;t.moveTo(0,s),t.lineTo(e.plotW,s)}for(let o of n){let s=Math.round(e.x(o))+.5;t.moveTo(s,0),t.lineTo(s,e.plotH)}t.stroke(),t.setLineDash([])}}var k=18;function nt(t,e=1/0,i=k){let n=t.map((a,l)=>({y:a,i:l})).sort((a,l)=>a.y-l.y),r=n.map(a=>a.y);for(let a=1;a<r.length;a++)r[a]=Math.max(r[a],r[a-1]+i);let o=e;for(let a=r.length-1;a>=0;a--)r[a]=Math.min(r[a],o),o=r[a]-i;let s=new Array(t.length);return n.forEach((a,l)=>s[a.i]=r[l]),s}function Re(t,e,i,n,r,o=ne(r)){i<-k/2||i>e.plotH+k/2||(t.fillStyle=r,t.fillRect(e.plotW,Math.round(i)-9,e.width-e.plotW,k),t.fillStyle=o,t.textBaseline="middle",t.fillText(n,e.plotW+6,Math.round(i)))}function rt(t,e,i,n,r){let o=t.measureText(n).width+10;t.fillStyle=r,t.fillRect(e.plotW-o-2,Math.round(i)-9,o,k),t.fillStyle=ne(r),t.textBaseline="middle",t.fillText(n,e.plotW-o+3,Math.round(i))}function ot(t,e,i){let n=[];for(let r of t){if(!r.visible)continue;let o=r.opts.priceDecimals??i;for(let a of r.lines){if(a.opts.axisLabel===!1)continue;let l=a.opts.color??e.theme.muted;n.push({y:e.y(a.opts.price),text:y(a.opts.price,o),bg:l,title:a.opts.title})}let s=r.opts.lastValueLabel!==!1?re(r,e.theme):null;s&&n.push({y:e.y(s.value),text:y(s.value,o),bg:s.color})}return n.filter(r=>r.y>=-k&&r.y<=e.plotH+k)}function Pe(t,e,i,n,r){let o=e.theme;t.fillStyle=o.bg,t.fillRect(e.plotW,0,e.width-e.plotW,e.height),t.strokeStyle=o.grid,t.beginPath(),t.moveTo(e.plotW+.5,0),t.lineTo(e.plotW+.5,e.plotH),t.stroke(),t.font=`11px ${o.mono}`,t.textBaseline="middle",t.fillStyle=o.muted;for(let l of n)t.fillText(y(l,r),e.plotW+6,e.y(l));let s=ot(i,e,r),a=nt(s.map(l=>l.y),e.plotH-k/2);s.forEach((l,h)=>{l.title&&rt(t,e,a[h],l.title,l.bg),Re(t,e,a[h],l.text,l.bg)})}function He(t,e,i,n,r){let o=e.theme;t.fillStyle=o.bg,t.fillRect(0,e.plotH,e.width,e.height-e.plotH),t.strokeStyle=o.grid,t.beginPath(),t.moveTo(0,e.plotH+.5),t.lineTo(e.width,e.plotH+.5),t.stroke(),t.font=`11px ${o.font}`,t.textBaseline="middle",t.textAlign="center",t.fillStyle=o.muted;let s=i.step(),a=n[0]!==void 0?i.toTime(n[0]-1):null,l=e.plotH+(e.height-e.plotH)/2;for(let h of n){let m=i.toTime(h);if(m===null)continue;let u=e.x(h);u>20&&u<e.plotW-20&&t.fillText(tt(m,a,s,r),u,l),a=m}t.textAlign="left",r.timeLabel&&t.fillText(r.timeLabel,e.plotW+6,l)}function De(t,e,i,n,r,o,s){let a=e.theme,l=Math.round(e.x(n.index))+.5;t.strokeStyle=a.muted,t.lineWidth=1,t.setLineDash([4,4]),t.beginPath(),t.moveTo(l,0),t.lineTo(l,e.plotH),n.y>=0&&n.y<=e.plotH&&(t.moveTo(0,Math.round(n.y)+.5),t.lineTo(e.plotW,Math.round(n.y)+.5)),t.stroke(),t.setLineDash([]),t.font=`11px ${a.mono}`,Re(t,e,n.y,y(r,o),a.grid,a.text);let h=i.toTime(n.index);if(h===null)return;let m=it(h,s),u=t.measureText(m).width+12,c=Math.max(0,Math.min(e.plotW-u,l-u/2));t.fillStyle=a.grid,t.fillRect(c,e.plotH+1,u,e.height-e.plotH-1),t.fillStyle=a.text,t.textBaseline="middle",t.fillText(m,c+6,e.plotH+(e.height-e.plotH)/2)}function st(t,e,i,n){let r=1/0,o=-1/0;for(let s of t){if(!s.visible)continue;let a=1/0,l=-1/0;for(let u of s.data()){let c=e.exact(u.time);if(c===void 0||c<i||c>n)continue;let[g,v]=s.span(u);Number.isFinite(g)&&g<a&&(a=g),Number.isFinite(v)&&v>l&&(l=v)}let h=a<=l?[a,l]:null,m=s.opts.autoscale?s.opts.autoscale(h):h;m&&(r=Math.min(r,m[0]),o=Math.max(o,m[1]))}return r===1/0?null:r===o?[r-1,o+1]:[r,o]}function at(t,e){let i=Math.max(0,t.width-t.axisW),n=Math.max(0,t.height-t.axisH),{end:r,bw:o}=t.view,s=t.timeline.length,a=Math.max(0,Math.floor(r-i/o)),l=Math.min(s-1,Math.ceil(r));return{width:t.width,height:t.height,plotW:i,plotH:n,bw:o,first:a,last:l,x:h=>i-(r-h+.5)*o,y:h=>e.top+(e.hi-h)/(e.hi-e.lo)*e.inner,at:h=>t.timeline.exact(h),theme:t.theme}}function se(t,e){let i=Math.max(0,t.width-t.axisW),n=Math.max(0,t.height-t.axisH),r=Math.max(0,Math.floor(t.view.end-i/t.view.bw)),o=Math.min(t.timeline.length-1,Math.ceil(t.view.end)),s=st(t.series,t.timeline,r,o),[a,l]=s??(e?[e.lo,e.hi]:[0,1]),h=n*.2;return{lo:a,hi:l,top:h,inner:Math.max(1,n-h-n*.1)}}function Ee(t,e,i){let n=at(e,i);t.clearRect(0,0,e.width,e.height),t.fillStyle=e.theme.bg,t.fillRect(0,0,e.width,e.height);let r=e.axis.priceDecimals??e.series.find(u=>u.visible)?.opts.priceDecimals,{ticks:o,dec:s}=ke(i.lo,i.hi,n.plotH,r),a=Ie(n,e.timeline);Ae(t,n,o,a,e.axis),t.save(),t.beginPath(),t.rect(0,0,n.plotW,n.plotH),t.clip();let l={width:n.plotW,height:n.plotH},h=u=>{t.save(),u.draw(t,l),t.restore()};for(let u of e.overlays)u.zOrder==="bottom"&&h(u);let m=["histogram","candles","line"];for(let u of m)for(let c of e.series)c.kind===u&&Ce(t,c,n);for(let u of e.series)Se(t,u,n);for(let u of e.series)Me(t,u,n);for(let u of e.overlays)u.zOrder!=="bottom"&&h(u);if(t.restore(),Pe(t,n,e.series,o,s),He(t,n,e.timeline,a,e.axis),e.hover){let u=i.hi-(e.hover.y-i.top)/i.inner*(i.hi-i.lo);De(t,n,e.timeline,e.hover,u,s,e.axis)}return n}var j=class{constructor(e,i){this.el=e;this.t=i;p(this,"pointers",new Map);p(this,"drag",null);p(this,"pinch",0);p(this,"off",[]);this.on("wheel",n=>this.wheel(n),{passive:!1}),this.on("pointerdown",n=>this.down(n)),this.on("pointermove",n=>this.move(n)),this.on("pointerup",n=>this.up(n)),this.on("pointercancel",n=>this.end(n)),this.on("pointerleave",n=>{n.pointerType==="mouse"&&this.t.hover(null,0)})}on(e,i,n){this.el.addEventListener(e,i,n),this.off.push(()=>this.el.removeEventListener(e,i,n))}pos(e){let i=this.el.getBoundingClientRect();return{x:e.clientX-i.left,y:e.clientY-i.top}}wheel(e){if(!this.t.enabled())return;e.preventDefault();let{x:i}=this.pos(e);Math.abs(e.deltaX)>Math.abs(e.deltaY)?this.t.pan(this.t.viewEnd(),-e.deltaX):this.t.zoom(Math.exp(-e.deltaY*.002),i)}down(e){let i=this.pos(e);this.pointers.set(e.pointerId,i),this.el.setPointerCapture?.(e.pointerId),this.pointers.size===2?(this.drag=null,this.pinch=this.spread()):this.drag={x:i.x,end:this.t.viewEnd(),moved:!1}}move(e){let i=this.pos(e);if(this.pointers.has(e.pointerId)&&this.pointers.set(e.pointerId,i),this.pointers.size===2&&this.pinch>0){let n=this.spread();if(this.t.enabled()&&n>0){let r=[...this.pointers.values()].reduce((o,s)=>o+s.x,0)/2;this.t.zoom(n/this.pinch,r)}this.pinch=n;return}if(this.drag){let n=i.x-this.drag.x;Math.abs(n)>3&&(this.drag.moved=!0),this.drag.moved&&this.t.enabled()&&this.t.pan(this.drag.end,n)}this.t.hover(i.x,i.y)}up(e){let i=this.pos(e);this.drag&&!this.drag.moved&&this.pointers.size===1&&this.t.click(i.x,i.y),this.end(e)}end(e){this.pointers.delete(e.pointerId),this.drag=null,this.pointers.size<2&&(this.pinch=0)}spread(){let[e,i]=[...this.pointers.values()];return e&&i?Math.hypot(e.x-i.x,e.y-i.y):0}dispose(){for(let e of this.off)e()}};var Y=class{constructor(e,i,n){this.host=e;this.opts=i;this.owner=n}update(e){this.opts={...this.opts,...e},this.host.redraw()}remove(){this.owner.removePriceLine(this)}};function lt(t,e){let i=t[t.length-1];if(!i||e.time>i.time){t.push(e);return}let n=0,r=t.length;for(;n<r;){let o=n+r>>1;t[o].time<e.time?n=o+1:r=o}t[n]?.time===e.time?t[n]=e:t.splice(n,0,e)}var F=class{constructor(e,i){this.host=e;this.opts=i;p(this,"points",[]);p(this,"lines",[]);p(this,"markers",[])}setData(e){this.points=[...e].sort((i,n)=>i.time-n.time),this.host.dataChanged()}update(e){lt(this.points,e),this.host.dataChanged()}data(){return this.points}times(){return this.points.map(e=>e.time)}setOptions(e){this.opts={...this.opts,...e},this.host.redraw()}get visible(){return this.opts.visible!==!1}addPriceLine(e){let i=new Y(this.host,e,this);return this.lines.push(i),this.host.redraw(),i}removePriceLine(e){let i=this.lines.indexOf(e);i>=0&&this.lines.splice(i,1),this.host.redraw()}setMarkers(e){this.markers=[...e].sort((i,n)=>i.time-n.time),this.host.redraw()}priceToY(e){return this.host.priceToY(e)}yToPrice(e){return this.host.yToPrice(e)}},B=class extends F{constructor(){super(...arguments);p(this,"kind","candles")}span(i){return[i.low,i.high]}value(i){return i.close}},V=class extends F{constructor(){super(...arguments);p(this,"kind","line")}span(i){return[i.value,i.value]}value(i){return i.value}},N=class extends F{constructor(){super(...arguments);p(this,"kind","histogram")}span(i){let n=this.opts.base??0;return[Math.min(n,i.value),Math.max(n,i.value)]}value(i){return i.value}};var X=class{constructor(){p(this,"times",[]);p(this,"index",new Map)}rebuild(e){let i=new Set;for(let n of e)for(let r of n)i.add(r);this.times=[...i].sort((n,r)=>n-r),this.index=new Map(this.times.map((n,r)=>[n,r]))}get length(){return this.times.length}exact(e){return this.index.get(e)}step(){let e=this.times.length;return e>=2?this.times[e-1]-this.times[e-2]:6e4}toIndex(e){let i=this.times,n=i.length;if(n===0)return null;let r=this.index.get(e);if(r!==void 0)return r;if(e<i[0])return n>=2?(e-i[0])/(i[1]-i[0]):-1;if(e>i[n-1])return n-1+(e-i[n-1])/this.step();let o=0,s=n-1;for(;s-o>1;){let a=o+s>>1;i[a]<=e?o=a:s=a}return o+(e-i[o])/(i[s]-i[o])}toTime(e){let i=this.times,n=i.length;if(n===0)return null;let r=Math.round(e);return r>=0&&r<n?i[r]:r<0?i[0]+r*(n>=2?i[1]-i[0]:6e4):i[n-1]+(r-n+1)*this.step()}};var ht=1,mt=60,J=class{constructor(e,i={}){this.opts=i;p(this,"el");p(this,"canvas");p(this,"legendEl");p(this,"series",[]);p(this,"overlays",[]);p(this,"timeline",new X);p(this,"view");p(this,"scale",null);p(this,"hover",null);p(this,"clicks",new Set);p(this,"rangeSubs",new Set);p(this,"lastRange","");p(this,"raf",0);p(this,"resize");p(this,"host");p(this,"gestures");this.view={end:0,bw:i.barSpacing??8},this.el=document.createElement("div"),this.el.className="glot-chart",this.el.style.cssText="position:relative;width:100%;height:100%;overflow:hidden;touch-action:none",this.canvas=document.createElement("canvas"),this.canvas.style.cssText="position:absolute;inset:0;width:100%;height:100%;cursor:crosshair",this.canvas.setAttribute("role","img"),this.legendEl=document.createElement("div"),this.legendEl.className="glot-chart-legend",this.legendEl.hidden=i.legend===!1,this.el.append(this.canvas,this.legendEl),i.attributionLogo!==!1&&this.el.append(we(i)),_(e.ownerDocument),e.append(this.el),this.host={dataChanged:()=>this.dataChanged(),redraw:()=>this.invalidate(),priceToY:n=>this.priceToY(n),yToPrice:n=>this.yToPrice(n)},this.gestures=new j(this.canvas,{enabled:()=>this.opts.interactive!==!1,pan:(n,r)=>this.panTo(n-r/this.view.bw),viewEnd:()=>this.view.end,zoom:(n,r)=>this.zoomAt(n,r),hover:(n,r)=>this.setHover(n,r),click:(n,r)=>this.emitClick(n,r)}),this.resize=new ResizeObserver(()=>this.invalidate()),this.resize.observe(this.el)}addCandles(e={}){return this.add(new B(this.host,{lastPriceLine:!0,...e}))}addLine(e={}){return this.add(new V(this.host,e))}addHistogram(e={}){return this.add(new N(this.host,e))}add(e){return this.series.push(e),this.dataChanged(),e}removeSeries(e){this.series=this.series.filter(i=>i!==e),this.dataChanged()}addOverlay(e){return this.overlays.push(e),this.invalidate(),()=>{this.overlays=this.overlays.filter(i=>i!==e),this.invalidate()}}setOptions(e){let i=this.timeline.length;e.rightOffset!==void 0&&e.rightOffset!==this.opts.rightOffset&&i&&(this.view.end=i-1+e.rightOffset),e.barSpacing!==void 0&&(this.view.bw=ae(e.barSpacing)),e.legend!==void 0&&(this.legendEl.hidden=e.legend===!1),this.opts={...this.opts,...e},this.invalidate()}options(){return{...this.opts,barSpacing:this.view.bw,rightOffset:this.opts.rightOffset??6}}plotSize(){let e=this.el.clientWidth-(this.opts.priceAxisWidth??72),i=this.el.clientHeight-(this.opts.timeAxisHeight??24);return{width:Math.max(0,e),height:Math.max(0,i)}}indexToX(e){return this.plotSize().width-(this.view.end-e+.5)*this.view.bw}xToIndex(e){return this.view.end+.5-(this.plotSize().width-e)/this.view.bw}timeToX(e){let i=this.timeline.toIndex(e);return i===null?null:this.indexToX(i)}xToTime(e){return this.timeline.toTime(this.xToIndex(e))}timeToIndex(e){return this.timeline.exact(e)??null}priceToY(e){let i=this.currentScale();return i?i.top+(i.hi-e)/(i.hi-i.lo)*i.inner:null}yToPrice(e){let i=this.currentScale();return i?i.hi-(e-i.top)/i.inner*(i.hi-i.lo):null}getVisibleIndexRange(){if(!this.timeline.length)return null;let e=this.plotSize().width;return{from:this.view.end-e/this.view.bw+1,to:this.view.end}}setVisibleIndexRange(e){let i=this.plotSize().width,n=Math.max(1,e.to-e.from+1);i>0&&(this.view.bw=ae(i/n)),this.view.end=e.to,this.invalidate()}getVisibleRange(){let e=this.getVisibleIndexRange(),i=this.timeline.length;if(!e||!i)return null;let n=Math.min(i-1,Math.max(0,Math.ceil(e.from))),r=Math.min(i-1,Math.max(0,Math.floor(e.to)));return{from:this.timeline.times[n],to:this.timeline.times[r]}}setVisibleRange(e){let i=this.timeline.toIndex(e.from),n=this.timeline.toIndex(e.to);i===null||n===null||this.setVisibleIndexRange({from:i,to:n})}scrollToEnd(){this.view.end=Math.max(0,this.timeline.length-1)+(this.opts.rightOffset??6),this.invalidate()}fitContent(){let e=this.timeline.length;e&&this.setVisibleIndexRange({from:0,to:e-1})}onVisibleRangeChange(e){return this.rangeSubs.add(e),()=>this.rangeSubs.delete(e)}onClick(e){return this.clicks.add(e),()=>this.clicks.delete(e)}invalidate(){this.scale=null,cancelAnimationFrame(this.raf),this.raf=requestAnimationFrame(()=>this.draw())}dispose(){this.resize.disconnect(),this.gestures.dispose(),cancelAnimationFrame(this.raf),this.el.remove()}dataChanged(){let e=this.timeline.length,i=this.view.end>=e-1;this.timeline.rebuild(this.series.map(r=>r.times()));let n=this.timeline.length;e===0&&n>0?this.view.end=n-1+(this.opts.rightOffset??6):i&&n>e&&(this.view.end+=n-e),this.canvas.setAttribute("aria-label",`${n} bars`),this.invalidate()}panTo(e){let i=this.timeline.length,n=this.plotSize().width/this.view.bw;this.view.end=Math.min(i-1+n*.8,Math.max(Math.min(i-1,2),e)),this.invalidate()}zoomAt(e,i){let n=this.plotSize().width,r=this.xToIndex(Math.min(i,n)),o=ae(this.view.bw*e);this.view.end=r-.5+(n-Math.min(i,n))/o,this.view.bw=o,this.invalidate()}setHover(e,i){let n=this.timeline.length;e===null||!n||e>this.plotSize().width?this.hover=null:this.hover={index:Math.round(this.xToIndex(e)-.5),y:i},this.invalidate()}emitClick(e,i){let n=Math.round(this.xToIndex(e)-.5),r=n>=0&&n<this.timeline.length,o={x:e,y:i,index:r?n:null,time:r?this.timeline.times[n]:null,price:this.yToPrice(i)};for(let s of this.clicks)s(o)}state(){let e=this.opts;return{width:this.el.clientWidth,height:this.el.clientHeight,axisW:e.priceAxisWidth??72,axisH:e.timeAxisHeight??24,view:this.view,timeline:this.timeline,series:this.series,overlays:this.overlays,theme:W(this.el,e.theme),axis:{grid:e.grid??"solid",timeVisible:e.timeVisible!==!1,secondsVisible:e.secondsVisible===!0,timeLabel:e.timeLabel??"UTC",priceDecimals:e.priceDecimals},hover:e.crosshair===!1?null:this.hover}}currentScale(){return this.timeline.length?(this.scale??(this.scale=se(this.state(),null)),this.scale):null}draw(){let e=window.devicePixelRatio||1,i=this.el.clientWidth,n=this.el.clientHeight;if(i===0||n===0)return;(this.canvas.width!==Math.round(i*e)||this.canvas.height!==Math.round(n*e))&&(this.canvas.width=Math.round(i*e),this.canvas.height=Math.round(n*e));let r=this.canvas.getContext("2d");if(!r)return;r.setTransform(e,0,0,e,0,0);let o=this.state();U(this.el,o.theme),this.scale=se(o,this.scale),Ee(r,o,this.scale),this.renderLegend(),this.emitRange()}renderLegend(){if(this.legendEl.hidden)return;let e=this.timeline.length,i=this.hover&&this.hover.index<e?this.hover.index:e-1,n=this.timeline.times[i];if(n===void 0){this.legendEl.textContent="";return}let r=[];for(let o of this.series){if(!o.visible)continue;let s=o.data().find(h=>h.time===n);if(!s)continue;let a=o.opts.priceDecimals??this.opts.priceDecimals??2,l=h=>y(h,a);if(o.kind==="candles"){let h=s;r.push(`O ${l(h.open)} H ${l(h.high)} L ${l(h.low)} C ${l(h.close)}`)}else o.opts.title&&r.push(`${o.opts.title} ${l(s.value)}`)}this.legendEl.textContent=r.join(" ")}emitRange(){let e=this.getVisibleRange(),i=e?`${e.from}:${e.to}`:"";if(i!==this.lastRange){this.lastRange=i;for(let n of this.rangeSubs)n(e)}}};function ae(t){return Math.min(mt,Math.max(ht,t))}function ut(t,e={}){return new J(t,e)}export{O as ATTRIBUTION_HREF,E as ATTRIBUTION_SRC,B as CandleSeries,G as GlotChart,N as HistogramSeries,V as LineSeriesApi,Y as PriceLine,te as THEMES,J as TimeChart,je as VERSION,q as barAt,Ye as barsFromCsv,ge as bisect,Ze as createChart,ut as createTimeChart,P as decimalsFor,K as extent,y as formatNumber,R as niceStep,W as readTheme,z as resolveColor,de as visibleRange,Q as xOf};
|
package/dist/glot-charts.min.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/*! Glot Charts 0.
|
|
2
|
-
"use strict";var GlotCharts=(()=>{var
|
|
1
|
+
/*! Glot Charts 0.2.0 | Apache-2.0 with NOTICE | https://github.com/tradeglot/glot-charts */
|
|
2
|
+
"use strict";var GlotCharts=(()=>{var G=Object.defineProperty;var Le=Object.getOwnPropertyDescriptor;var ze=Object.getOwnPropertyNames;var _e=Object.prototype.hasOwnProperty;var Ue=(t,e,i)=>e in t?G(t,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[e]=i;var Ge=(t,e)=>{for(var i in e)G(t,i,{get:e[i],enumerable:!0})},Ze=(t,e,i,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of ze(e))!_e.call(t,r)&&r!==i&&G(t,r,{get:()=>e[r],enumerable:!(n=Le(e,r))||n.enumerable});return t};var je=t=>Ze(G({},"__esModule",{value:!0}),t);var p=(t,e,i)=>Ue(t,typeof e!="symbol"?e+"":e,i);var vt={};Ge(vt,{ATTRIBUTION_HREF:()=>D,ATTRIBUTION_SRC:()=>H,CandleSeries:()=>O,GlotChart:()=>$,HistogramSeries:()=>F,LineSeriesApi:()=>W,PriceLine:()=>L,THEMES:()=>q,TimeChart:()=>_,VERSION:()=>Ce,barAt:()=>j,barsFromCsv:()=>Me,bisect:()=>re,createChart:()=>Te,createTimeChart:()=>Ve,decimalsFor:()=>P,extent:()=>Y,formatNumber:()=>y,niceStep:()=>R,readTheme:()=>E,resolveColor:()=>B,visibleRange:()=>ie,xOf:()=>Z});function ie(t,e,i){let n=Math.ceil(e/i.barWidth)+1,r=Math.min(t-1,Math.floor(i.end));return[Math.max(0,r-n+1),r]}function Z(t,e,i){return e-(i.end-t+.5)*i.barWidth}function j(t,e,i){return Math.round(i.end+.5-(e-t)/i.barWidth)}function Y(t,e=.05){let i=1/0,n=-1/0;for(let o of t)o===null||!Number.isFinite(o)||(o<i&&(i=o),o>n&&(n=o));if(i===1/0)return[0,1];if(i===n)return[i-1,n+1];let r=(n-i)*e;return[i-r,n+r]}function R(t,e){let i=t/Math.max(1,e),n=10**Math.floor(Math.log10(i)),r=i/n;return(r<1.5?1:r<3?2:r<7?5:10)*n}function P(t){return Math.max(0,Math.min(8,-Math.floor(Math.log10(t)+1e-9)))}function y(t,e){let i=Math.abs(t).toFixed(e),[n="0",r]=i.split("."),o=n.replace(/\B(?=(\d{3})+(?!\d))/g,",");return(t<0?"\u2212":"")+o+(r?"."+r:"")}function B(t,e){if(!t)return null;switch(t){case"up":return e.up;case"down":return e.down;case"accent":return e.accent;case"neutral":return e.muted;default:return t}}function ne(t,e,i,n,r,o,s,a){t.lineWidth=1.5;for(let l=Math.max(r,1);l<=o;l++){let h=e.values[l-1],m=e.values[l];h==null||m===null||m===void 0||(t.strokeStyle=B(e.colors?.[l],n)??i,t.beginPath(),t.moveTo(s(l-1),a(h)),t.lineTo(s(l),a(m)),t.stroke())}t.lineWidth=1}function fe(t,e,i,n,r,o,s,a){let l=i.find(m=>m.name===e.a)?.values,h=i.find(m=>m.name===e.b)?.values;if(!(!l||!h))for(let m=Math.max(r,1);m<=o;m++){let u=[l[m-1],l[m],h[m],h[m-1]],c=B(e.colors[m],n);if(!c||u.some(b=>b==null))continue;let[g,v,S,A]=u;t.fillStyle=c,t.beginPath(),t.moveTo(s(m-1),a(g)),t.lineTo(s(m),a(v)),t.lineTo(s(m),a(S)),t.lineTo(s(m-1),a(A)),t.closePath(),t.fill()}}function be(t,e,i,n,r,o,s,a,l){for(let m=r;m<=o;m++){if(!e.values[m])continue;let u=i[m],c=s(m),g=e.at==="below_bar"?a(u.low)+12+2:e.at==="top"?12:e.at==="bottom"?l-12:a(u.high)-12-2,v=B(e.colors?.[m],n)??n.accent;if(t.fillStyle=t.strokeStyle=v,Ye(t,e.shape,c,g,6),e.text){t.font=`11px ${n.font}`,t.textAlign="center";let S=e.at==="below_bar"||e.at==="bottom";t.fillText(e.text,c,S?g+12+4:g-12-4),t.textAlign="start"}}}function Ye(t,e,i,n,r){switch(t.beginPath(),e){case"triangle_up":case"arrow_up":case"label_up":X(t,[i,n-r,i+r,n+r,i-r,n+r]);break;case"triangle_down":case"arrow_down":case"label_down":X(t,[i,n+r,i+r,n-r,i-r,n-r]);break;case"square":t.rect(i-r*.8,n-r*.8,r*1.6,r*1.6);break;case"diamond":X(t,[i,n-r,i+r,n,i,n+r,i-r,n]);break;case"cross":case"x_cross":{let o=e==="cross"?0:Math.PI/4;t.lineWidth=2;for(let s of[o,o+Math.PI/2])t.moveTo(i-r*Math.cos(s),n-r*Math.sin(s)),t.lineTo(i+r*Math.cos(s),n+r*Math.sin(s));t.stroke(),t.lineWidth=1;return}case"flag":X(t,[i-r*.6,n-r,i+r,n-r*.4,i-r*.6,n+r*.2]);break;default:t.arc(i,n,r*.7,0,Math.PI*2)}t.fill()}function X(t,e){t.moveTo(e[0],e[1]);for(let i=2;i<e.length;i+=2)t.lineTo(e[i],e[i+1]);t.closePath()}var x=72,V=24;function ve(t,e,i,n,r){let o=r.width-x,s=e.series??[],a=s.filter(d=>!d.overlay),l=(r.height-V)*(a.length?.72:1),h=l,m=r.height-V-l;if(t.clearRect(0,0,r.width,r.height),t.fillStyle=n.bg,t.fillRect(0,0,r.width,r.height),e.bars.length===0)return;let[u,c]=ie(e.bars.length,o,i),g=e.bars.slice(u,c+1),v=s.filter(d=>d.overlay),[S,A]=Y([...g.flatMap(d=>[d.low,d.high]),...v.flatMap(d=>d.values.slice(u,c+1))]),b=d=>(A-d)/(A-S)*(l-8)+4,w=d=>Z(d,o,i),U=R(A-S,Math.max(2,Math.floor(l/60))),ue=P(U);t.font=`12px ${n.mono}`,t.textBaseline="middle";for(let d=Math.ceil(S/U)*U;d<=A;d+=U)N(t,n.grid,0,b(d),o,b(d)),t.fillStyle=n.muted,t.fillText(y(d,ue),o+6,b(d));r.marked!==null&&r.marked>=u&&r.marked<=c&&(t.fillStyle=n.grid,t.fillRect(w(r.marked)-i.barWidth/2,0,i.barWidth,r.height-V));let de=Math.max(1,i.barWidth*.7);for(let d=u;d<=c;d++){let f=e.bars[d],T=f.close>=f.open;t.strokeStyle=t.fillStyle=T?n.up:n.down;let C=Math.round(w(d))+.5;N(t,t.strokeStyle,C,b(f.high),C,b(f.low));let M=b(Math.max(f.open,f.close));t.fillRect(C-de/2,M,de,Math.max(1,b(Math.min(f.open,f.close))-M))}for(let d of e.fills??[])fe(t,d,s,n,u,c,w,b);v.forEach((d,f)=>{let T=n.series[f%n.series.length];d.colors?.some(C=>C)?ne(t,d,T,n,u,c,w,b):ge(t,T,d.values,u,c,w,b,f>=n.series.length)});for(let d of e.shapes??[])be(t,d,e.bars,n,u,c,w,b,l);let pe=d=>re(e.bars,d);for(let d of e.trades??[]){let f=pe(d.entryTime),T=pe(d.exitTime),C=d.side==="buy"?n.up:n.down;f>=u&&f<=c&&Qe(t,C,w(f),b(d.entryPrice),d.side==="buy"),T>=u&&T<=c&&qe(t,C,w(T),b(d.exitPrice))}if(a.length){N(t,n.grid,0,h,r.width,h);let[d,f]=Y(a.flatMap(M=>M.values.slice(u,c+1))),T=M=>h+4+(f-M)/(f-d)*(m-8);a.forEach((M,Ne)=>{let ce=n.series[Ne%n.series.length];M.colors?.some($e=>$e)?ne(t,M,ce,n,u,c,w,T):ge(t,ce,M.values,u,c,w,T,!1)});let C=P(R(f-d,2));t.fillStyle=n.muted,t.fillText(y(f,C),o+6,h+10),t.fillText(y(d,C),o+6,h+m-10)}Xe(t,e,i,n,r,u,c),Je(t,e,n,r,w,b,l,ue)}function Xe(t,e,i,n,r,o,s){let a=r.width-x,l=r.height-V;N(t,n.grid,0,l,r.width,l),t.fillStyle=n.muted,t.font=`12px ${n.font}`;let h=Math.max(1,Math.ceil(110/i.barWidth));for(let m=o-o%h;m<=s;m+=h){if(m<o)continue;let u=new Date(e.bars[m].time).toISOString();t.fillText(`${u.slice(5,10)} ${u.slice(11,16)}`,Z(m,a,i)-30,l+12)}t.fillText("UTC",a+6,l+12)}function Je(t,e,i,n,r,o,s,a){if(n.hover===null||!e.bars[n.hover])return;let l=e.bars[n.hover];t.setLineDash([4,4]),N(t,i.muted,Math.round(r(n.hover))+.5,0,Math.round(r(n.hover))+.5,n.height-V),t.setLineDash([]);let h=o(l.close);h>=0&&h<=s&&(t.fillStyle=i.grid,t.fillRect(n.width-x,h-9,x,18),t.fillStyle=i.text,t.font=`12px ${i.mono}`,t.fillText(y(l.close,a+1),n.width-x+6,h))}function ge(t,e,i,n,r,o,s,a){t.strokeStyle=e,t.lineWidth=1.5,t.setLineDash(a?[6,3]:[]),t.beginPath();let l=!1;for(let h=n;h<=r;h++){let m=i[h];if(m==null||!Number.isFinite(m)){l=!1;continue}l?t.lineTo(o(h),s(m)):t.moveTo(o(h),s(m)),l=!0}t.stroke(),t.setLineDash([]),t.lineWidth=1}function N(t,e,i,n,r,o){t.strokeStyle=e,t.beginPath(),t.moveTo(i,n),t.lineTo(r,o),t.stroke()}function Qe(t,e,i,n,r){let s=r?n+10:n-10;t.fillStyle=e,t.beginPath(),t.moveTo(i,r?s-6:s+6),t.lineTo(i-6,r?s+6:s-6),t.lineTo(i+6,r?s+6:s-6),t.fill()}function qe(t,e,i,n){t.strokeStyle=e,t.lineWidth=2,t.beginPath(),t.arc(i,n,4,0,Math.PI*2),t.stroke(),t.lineWidth=1}function re(t,e){let i=0,n=t.length-1;for(;i<n;){let r=i+n+1>>1;t[r].time<=e?i=r:n=r-1}return i}var H="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI5NiIgaGVpZ2h0PSIyMCIgdmlld0JveD0iMCAwIDk2IDIwIiByb2xlPSJpbWciIGFyaWEtbGFiZWw9Ikdsb3QgQ2hhcnRzIj4KPHJlY3QgeD0iMiIgeT0iMiIgd2lkdGg9IjE2IiBoZWlnaHQ9IjE2IiByeD0iNCIgZmlsbD0iIzZENUJGRiIvPgo8cGF0aCBkPSJNNyA2LjVMNC44IDEwIDcgMTMuNU0xMyA2LjVMMTUuMiAxMCAxMyAxMy41IiBmaWxsPSJub25lIiBzdHJva2U9IiNGRkZGRkYiIHN0cm9rZS13aWR0aD0iMS40IiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiLz4KPHJlY3QgeD0iOC42IiB5PSI3IiB3aWR0aD0iMi44IiBoZWlnaHQ9IjYiIHJ4PSIwLjgiIGZpbGw9IiNGRkZGRkYiLz4KPHRleHQgeD0iMjMiIHk9IjE0LjUiIGZvbnQtZmFtaWx5PSJTcGFjZSBHcm90ZXNrLCBBcmlhbCwgc2Fucy1zZXJpZiIgZm9udC1zaXplPSIxMiIgZm9udC13ZWlnaHQ9IjYwMCIgZmlsbD0iIzVBNUY3QSI+R2xvdCBDaGFydHM8L3RleHQ+Cjwvc3ZnPgo=",D="https://tradeglot.com/";var Ke=`
|
|
3
3
|
:where(.glot-chart-legend){position:absolute;left:8px;top:6px;right:80px;font:12px var(--glot-chart-mono);color:var(--glot-chart-text);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;pointer-events:none}
|
|
4
4
|
:where(.glot-chart-label){position:absolute;right:80px;bottom:30px;font:600 12px var(--glot-chart-font);color:var(--glot-chart-muted);border:1px solid var(--glot-chart-grid);border-radius:999px;padding:2px 10px;pointer-events:none}
|
|
5
|
-
`;function
|
|
6
|
-
`).filter(a=>a.trim()),n=(t.shift()??"").split(",").map(a=>a.trim().toLowerCase()),i=a=>n.indexOf(a),[r,o,l,u,h]=["time","open","high","low","close"].map(i);if([r,o,l,u,h].some(a=>a===void 0||a<0))return[];let m=[];for(let a of t){let d=a.split(","),c={time:Number(d[r]),open:Number(d[o]),high:Number(d[l]),low:Number(d[u]),close:Number(d[h])};Object.values(c).every(Number.isFinite)&&m.push(c)}return m}return be(Ie);})();
|
|
5
|
+
`;function J(t){if(t.querySelector("style[data-glot-charts]"))return;let e=t.createElement("style");e.dataset.glotCharts="",e.textContent=Ke,t.head.append(e)}function Q(t,e){t.style.setProperty("--glot-chart-text",e.text),t.style.setProperty("--glot-chart-muted",e.muted),t.style.setProperty("--glot-chart-grid",e.grid),t.style.setProperty("--glot-chart-font",e.font),t.style.setProperty("--glot-chart-mono",e.mono)}var ye='"Be Vietnam Pro", "Inter", sans-serif',we='"JetBrains Mono", Menlo, monospace',q={dark:{bg:"#0E0F1A",text:"#ECEEF8",muted:"#A3A8C3",grid:"#2A2E48",up:"#3D8BFF",down:"#FF7A3D",accent:"#A99FFF",font:ye,mono:we,series:["#A99FFF","#ECEEF8","#A3A8C3"]},light:{bg:"#F6F6FB",text:"#14152B",muted:"#5A5F7A",grid:"#DCDDEA",up:"#2266DE",down:"#BA481A",accent:"#5B4BE0",font:ye,mono:we,series:["#5B4BE0","#14152B","#5A5F7A"]}};function E(t,e="dark"){let i=typeof e=="string"?q[e]:{...q.dark,...e},n=getComputedStyle(t),r=(h,m)=>n.getPropertyValue(h).trim()||m,o=r("--tg-text-accent",i.accent),s=r("--tg-text",i.text),a=r("--tg-muted",i.muted),l=n.getPropertyValue("--tg-text-accent").trim()!=="";return{bg:r("--tg-bg",i.bg),text:s,muted:a,grid:r("--tg-border",i.grid),up:r("--tg-chart-candle-up",i.up),down:r("--tg-chart-candle-down",i.down),accent:o,font:r("--tg-font-ui",i.font),mono:r("--tg-font-mono",i.mono),series:l?[o,s,a]:i.series}}var et=2,tt=40,$=class{constructor(e,i={}){this.opts=i;p(this,"el");p(this,"canvas");p(this,"legend");p(this,"labelEl");p(this,"data",{bars:[]});p(this,"view",{end:0,barWidth:8});p(this,"frame",{width:0,height:0,hover:null,marked:null});p(this,"drag",null);p(this,"moved",!1);p(this,"raf",0);p(this,"resize");if(this.el=document.createElement("div"),this.el.className="glot-chart",this.el.style.cssText="position:relative;width:100%;height:100%;overflow:hidden;touch-action:none",this.canvas=document.createElement("canvas"),this.canvas.style.cssText="position:absolute;inset:0;width:100%;height:100%;cursor:crosshair",this.canvas.setAttribute("role","img"),this.legend=document.createElement("div"),this.legend.className="glot-chart-legend",this.labelEl=document.createElement("span"),this.labelEl.className="glot-chart-label",this.el.append(this.canvas,this.legend,this.labelEl),i.attributionLogo!==!1){let n=document.createElement("a");n.className="glot-chart-attribution",n.href=i.attributionHref??D,n.target="_blank",n.rel="noopener",n.title="Charts by Glot Charts (TradeGlot)",n.style.cssText="position:absolute;left:8px;bottom:28px;height:18px;opacity:0.7";let r=document.createElement("img");r.src=i.attributionSrc??H,r.alt="Glot Charts",r.height=18,r.style.cssText="height:18px;display:block",n.append(r),this.el.append(n)}J(e.ownerDocument),e.append(this.el),this.bind(),this.resize=new ResizeObserver(()=>this.schedule()),this.resize.observe(this.el)}setData(e,i=!1){let n=this.view.end>=this.data.bars.length-1;this.data=e,(!i||n)&&(this.view.end=Math.max(0,e.bars.length-1)),this.labelEl.textContent=e.label??"",this.labelEl.hidden=!e.label,this.canvas.setAttribute("aria-label",this.ariaLabel()),this.schedule()}update(e){let i=this.data.bars,n=i[i.length-1],r=this.view.end>=i.length-1;if(n&&e.time<n.time)throw new RangeError(`update: bar time ${e.time} is older than the last bar ${n.time}`);n&&e.time===n.time?i[i.length-1]=e:(i.push(e),r&&(this.view.end=i.length-1)),this.canvas.setAttribute("aria-label",this.ariaLabel()),this.schedule()}setTheme(e){this.opts={...this.opts,theme:e},this.schedule()}getData(){return this.data}mark(e){if(this.frame.marked=e,e!==null){let i=(this.frame.width-x)/this.view.barWidth;(e>this.view.end||e<this.view.end-i+1)&&(this.view.end=Math.min(this.data.bars.length-1,e+Math.floor(i/2)))}this.schedule()}refresh(){this.schedule()}dispose(){this.resize.disconnect(),cancelAnimationFrame(this.raf),this.el.remove()}ariaLabel(){let e=this.data.label;return`${this.data.bars.length} bars${e?`, ${e}`:""}`}bind(){let e=this.canvas;e.addEventListener("wheel",i=>{i.preventDefault();let n=i.deltaY>0?.85:1.15;this.view.barWidth=Math.min(tt,Math.max(et,this.view.barWidth*n)),this.schedule()},{passive:!1}),e.addEventListener("pointerdown",i=>{this.drag={x:i.clientX,end:this.view.end},this.moved=!1,e.setPointerCapture(i.pointerId)}),e.addEventListener("pointermove",i=>{let n=e.getBoundingClientRect();if(this.drag){let r=i.clientX-this.drag.x;Math.abs(r)>3&&(this.moved=!0);let o=this.data.bars.length;this.view.end=Math.min(o-1+20,Math.max(0,this.drag.end-r/this.view.barWidth))}this.hover(j(i.clientX-n.left,n.width-x,this.view))}),e.addEventListener("pointerup",i=>{let n=e.getBoundingClientRect();if(this.drag&&!this.moved){let r=j(i.clientX-n.left,n.width-x,this.view);r>=0&&r<this.data.bars.length&&this.opts.onBarClick?.(r)}this.drag=null}),e.addEventListener("pointerleave",()=>this.hover(null))}hover(e){let i=e!==null&&e>=0&&e<this.data.bars.length?e:null;this.frame.hover=i,this.renderLegend(i??this.frame.marked??this.data.bars.length-1),this.schedule()}renderLegend(e){let i=this.data.bars[e];if(!i){this.legend.textContent="";return}let n=o=>y(o,2),r=[`O ${n(i.open)} H ${n(i.high)} L ${n(i.low)} C ${n(i.close)}`];for(let o of this.data.series??[]){let s=o.values[e];r.push(`${o.name} ${s==null?"na":n(s)}`)}this.legend.textContent=r.join(" ")}schedule(){cancelAnimationFrame(this.raf),this.raf=requestAnimationFrame(()=>this.draw())}draw(){let e=window.devicePixelRatio||1,i=this.el.clientWidth,n=this.el.clientHeight;if(i===0||n===0)return;(this.canvas.width!==Math.round(i*e)||this.canvas.height!==Math.round(n*e))&&(this.canvas.width=Math.round(i*e),this.canvas.height=Math.round(n*e));let r=this.canvas.getContext("2d");if(!r)return;r.setTransform(e,0,0,e,0,0),this.frame.width=i,this.frame.height=n,this.frame.hover===null&&this.renderLegend(this.frame.marked??this.data.bars.length-1);let o=E(this.el,this.opts.theme);Q(this.el,o),ve(r,this.data,this.view,o,this.frame)}};function Te(t,e={}){return new $(t,e)}var Ce="0.2.0";function Me(t){let e=t.split(`
|
|
6
|
+
`).filter(m=>m.trim()),i=(e.shift()??"").split(",").map(m=>m.trim().toLowerCase()),n=m=>i.indexOf(m),[r,o,s,a,l]=["time","open","high","low","close"].map(n);if([r,o,s,a,l].some(m=>m===void 0||m<0))return[];let h=[];for(let m of e){let u=m.split(","),c={time:Number(u[r]),open:Number(u[o]),high:Number(u[s]),low:Number(u[a]),close:Number(u[l])};Object.values(c).every(Number.isFinite)&&h.push(c)}return h}function Se(t){let e=document.createElement("a");e.className="glot-chart-attribution",e.href=t.attributionHref??D,e.target="_blank",e.rel="noopener",e.title="Charts by Glot Charts (TradeGlot)";let i=t.timeAxisHeight??24;e.style.cssText=`position:absolute;left:8px;bottom:${i+6}px;height:18px;opacity:0.7;z-index:3`;let n=document.createElement("img");return n.src=t.attributionSrc??H,n.alt="Glot Charts",n.height=18,n.style.cssText="height:18px;display:block",e.append(n),e}function xe(t,e){switch(t){case"dotted":return[e,e];case"dashed":return[2*e,2*e];case"large-dashed":return[6*e,6*e];case"sparse-dotted":return[e,4*e];default:return[]}}function*oe(t,e){for(let i of t){let n=e.at(i.time);n===void 0||n<e.first-1||n>e.last+1||(yield[i,n])}}function ke(t,e,i){e.visible&&(e.kind==="candles"?it(t,e,i):e.kind==="line"?nt(t,e,i):rt(t,e,i))}function it(t,e,i){let n=e.opts,r=n.upColor??i.theme.up,o=n.downColor??i.theme.down,s=Math.max(1,Math.min(i.bw*.7,i.bw-1));t.lineWidth=1,t.setLineDash([]);for(let[a,l]of oe(e.data(),i)){let h=a.close>=a.open,m=Math.round(i.x(l))+.5;t.strokeStyle=h?n.wickUpColor??r:n.wickDownColor??o,t.beginPath(),t.moveTo(m,i.y(a.high)),t.lineTo(m,i.y(a.low)),t.stroke(),t.fillStyle=h?r:o;let u=i.y(Math.max(a.open,a.close)),c=Math.max(1,i.y(Math.min(a.open,a.close))-u);t.fillRect(Math.round(m-s/2),u,Math.max(1,Math.round(s)),c)}}function nt(t,e,i){let n=e.opts.width??1.5;t.strokeStyle=e.opts.color??i.theme.series[0],t.lineWidth=n,t.setLineDash(xe(e.opts.style,n)),t.beginPath();let r=!1,o=-2;for(let[s,a]of oe(e.data(),i)){if(!Number.isFinite(s.value)){r=!1;continue}r&&a>o?t.lineTo(i.x(a),i.y(s.value)):t.moveTo(i.x(a),i.y(s.value)),r=!0,o=a}t.stroke(),t.setLineDash([]),t.lineWidth=1}function rt(t,e,i){let n=i.y(e.opts.base??0),r=Math.max(1,Math.round(i.bw*.8)),o=e.opts.color??i.theme.series[0];for(let[s,a]of oe(e.data(),i)){if(!Number.isFinite(s.value))continue;let l=i.y(s.value);t.fillStyle=s.color??o,t.fillRect(Math.round(i.x(a)-r/2),Math.min(l,n),r,Math.max(1,Math.abs(n-l)))}}function Ie(t,e,i){if(!e.visible)return;t.font=`11px ${i.theme.font}`,t.textBaseline="bottom",t.textAlign="right";for(let r of e.lines){let o=r.opts,s=Math.round(i.y(o.price))+.5;if(s<0||s>i.plotH)continue;let a=o.width??1;t.strokeStyle=o.color??i.theme.muted,t.lineWidth=a,t.setLineDash(xe(o.style,a)),t.beginPath(),t.moveTo(0,s),t.lineTo(i.plotW,s),t.stroke(),o.title&&o.axisLabel===!1&&(t.setLineDash([]),t.fillStyle=o.color??i.theme.muted,t.fillText(o.title,i.plotW-6,s-2))}let n=ae(e,i.theme);if(n&&e.opts.lastPriceLine===!0){let r=Math.round(i.y(n.value))+.5;t.strokeStyle=n.color,t.lineWidth=1,t.setLineDash([1,2]),t.beginPath(),t.moveTo(0,r),t.lineTo(i.plotW,r),t.stroke()}t.setLineDash([]),t.lineWidth=1,t.textAlign="left"}function se(t){let e=0,i=0,n=0,r=/^#([0-9a-f]{3}|[0-9a-f]{6})/i.exec(t),o=/rgba?\(([^)]+)\)/i.exec(t);if(r){let s=r[1].length===3?[...r[1]].map(a=>a+a).join(""):r[1];[e,i,n]=[0,2,4].map(a=>parseInt(s.slice(a,a+2),16))}else if(o)[e,i,n]=o[1].split(",").map(s=>parseFloat(s));else return"#ffffff";return .299*e+.587*i+.114*n>150?"#000000":"#ffffff"}function ae(t,e){let i=t.data(),n=i[i.length-1];if(!n)return null;if(t.kind==="candles"){let s=n,a=s.close>=s.open;return{value:s.close,color:(a?t.opts.upColor:t.opts.downColor)??(a?e.up:e.down)}}let r=n,o=r.color??t.opts.color??e.series[0];return{value:r.value,color:o}}function Ae(t,e,i){if(!e.visible||e.markers.length===0)return;let n=e.data(),r=new Map(n.map(a=>[a.time,a])),o=new Map,s=Math.max(7,Math.min(12,i.bw*.9));t.font=`11px ${i.theme.font}`,t.textAlign="center";for(let a of e.markers){let l=i.at(a.time),h=r.get(a.time);if(l===void 0||!h||l<i.first||l>i.last)continue;let m="high"in h?[h.high,h.low]:[h.value,h.value],u=`${a.time}:${a.position}`,c=o.get(u)??0,g=s*(a.size??1),v=ot(t,a,i.x(l),m,i,g,c);o.set(u,c+v)}t.textAlign="left"}function ot(t,e,i,[n,r],o,s,a){let l=e.color??o.theme.accent,h=e.position==="above",m=e.position==="inside"?o.y(n):h?o.y(n)-4:o.y(r)+4,u=h?-1:1,c=m+u*a,g=e.position==="inside"?m:c+u*s/2;t.fillStyle=l,s>=3&&st(t,e.shape,i,g,s);let v=s+2;if(e.text){t.textBaseline=h?"bottom":"top";let S=e.position==="inside"?g-s:c+u*(s+2);t.fillText(e.text,i,S),v+=13}return v}function st(t,e,i,n,r){let o=r/2;if(t.beginPath(),e==="circle")t.arc(i,n,o*.8,0,Math.PI*2);else if(e==="square")t.rect(i-o*.8,n-o*.8,o*1.6,o*1.6);else{let s=e==="arrow_up"?-1:1;t.moveTo(i,n+s*o),t.lineTo(i-o,n),t.lineTo(i-o*.35,n),t.lineTo(i-o*.35,n-s*o),t.lineTo(i+o*.35,n-s*o),t.lineTo(i+o*.35,n),t.lineTo(i+o,n),t.closePath()}t.fill()}var Re="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),K=864e5;function Pe(t,e,i,n){let r=R(e-t,Math.max(2,Math.floor(i/50))),o=[];for(let s=Math.ceil(t/r)*r;s<=e;s+=r)o.push(s);return{ticks:o,dec:n??P(r)}}var le=[1,5,10,15,30,60,120,300,600,900,1800,3600,7200,10800,14400,21600,43200,86400,172800,604800,2592e3,7776e3,31536e3].map(t=>t*1e3);function He(t,e){let i=90/t.bw*e.step(),n=le.find(s=>s>=i)??le[le.length-1],r=[],o=null;for(let s=Math.max(0,t.first-1);s<=t.last;s++){let a=e.times[s];if(a===void 0)continue;let l=Math.floor(a/n);o!==null&&l!==o&&s>=t.first&&r.push(s),o=l}return r}var I=t=>String(t).padStart(2,"0");function at(t){return t.getUTCMonth()===0&&t.getUTCDate()===1?String(t.getUTCFullYear()):`${t.getUTCDate()} ${Re[t.getUTCMonth()]}`}function lt(t,e,i,n){let r=new Date(t),o=e===null||Math.floor(e/K)!==Math.floor(t/K);if(!n.timeVisible||i>=K||o&&t%K===0)return at(r);let s=`${I(r.getUTCHours())}:${I(r.getUTCMinutes())}`;return n.secondsVisible?`${s}:${I(r.getUTCSeconds())}`:s}function ht(t,e){let i=new Date(t),n=`${I(i.getUTCDate())} ${Re[i.getUTCMonth()]} ${i.getUTCFullYear()}`;if(!e.timeVisible)return n;let r=e.secondsVisible?`:${I(i.getUTCSeconds())}`:"";return`${n} ${I(i.getUTCHours())}:${I(i.getUTCMinutes())}${r}`}function De(t,e,i,n,r){if(r.grid!=="none"){t.strokeStyle=e.theme.grid,t.lineWidth=1,t.setLineDash(r.grid==="dotted"?[1,3]:[]),t.beginPath();for(let o of i){let s=Math.round(e.y(o))+.5;t.moveTo(0,s),t.lineTo(e.plotW,s)}for(let o of n){let s=Math.round(e.x(o))+.5;t.moveTo(s,0),t.lineTo(s,e.plotH)}t.stroke(),t.setLineDash([])}}var k=18;function mt(t,e=1/0,i=k){let n=t.map((a,l)=>({y:a,i:l})).sort((a,l)=>a.y-l.y),r=n.map(a=>a.y);for(let a=1;a<r.length;a++)r[a]=Math.max(r[a],r[a-1]+i);let o=e;for(let a=r.length-1;a>=0;a--)r[a]=Math.min(r[a],o),o=r[a]-i;let s=new Array(t.length);return n.forEach((a,l)=>s[a.i]=r[l]),s}function Ee(t,e,i,n,r,o=se(r)){i<-k/2||i>e.plotH+k/2||(t.fillStyle=r,t.fillRect(e.plotW,Math.round(i)-9,e.width-e.plotW,k),t.fillStyle=o,t.textBaseline="middle",t.fillText(n,e.plotW+6,Math.round(i)))}function ut(t,e,i,n,r){let o=t.measureText(n).width+10;t.fillStyle=r,t.fillRect(e.plotW-o-2,Math.round(i)-9,o,k),t.fillStyle=se(r),t.textBaseline="middle",t.fillText(n,e.plotW-o+3,Math.round(i))}function dt(t,e,i){let n=[];for(let r of t){if(!r.visible)continue;let o=r.opts.priceDecimals??i;for(let a of r.lines){if(a.opts.axisLabel===!1)continue;let l=a.opts.color??e.theme.muted;n.push({y:e.y(a.opts.price),text:y(a.opts.price,o),bg:l,title:a.opts.title})}let s=r.opts.lastValueLabel!==!1?ae(r,e.theme):null;s&&n.push({y:e.y(s.value),text:y(s.value,o),bg:s.color})}return n.filter(r=>r.y>=-k&&r.y<=e.plotH+k)}function Oe(t,e,i,n,r){let o=e.theme;t.fillStyle=o.bg,t.fillRect(e.plotW,0,e.width-e.plotW,e.height),t.strokeStyle=o.grid,t.beginPath(),t.moveTo(e.plotW+.5,0),t.lineTo(e.plotW+.5,e.plotH),t.stroke(),t.font=`11px ${o.mono}`,t.textBaseline="middle",t.fillStyle=o.muted;for(let l of n)t.fillText(y(l,r),e.plotW+6,e.y(l));let s=dt(i,e,r),a=mt(s.map(l=>l.y),e.plotH-k/2);s.forEach((l,h)=>{l.title&&ut(t,e,a[h],l.title,l.bg),Ee(t,e,a[h],l.text,l.bg)})}function We(t,e,i,n,r){let o=e.theme;t.fillStyle=o.bg,t.fillRect(0,e.plotH,e.width,e.height-e.plotH),t.strokeStyle=o.grid,t.beginPath(),t.moveTo(0,e.plotH+.5),t.lineTo(e.width,e.plotH+.5),t.stroke(),t.font=`11px ${o.font}`,t.textBaseline="middle",t.textAlign="center",t.fillStyle=o.muted;let s=i.step(),a=n[0]!==void 0?i.toTime(n[0]-1):null,l=e.plotH+(e.height-e.plotH)/2;for(let h of n){let m=i.toTime(h);if(m===null)continue;let u=e.x(h);u>20&&u<e.plotW-20&&t.fillText(lt(m,a,s,r),u,l),a=m}t.textAlign="left",r.timeLabel&&t.fillText(r.timeLabel,e.plotW+6,l)}function Fe(t,e,i,n,r,o,s){let a=e.theme,l=Math.round(e.x(n.index))+.5;t.strokeStyle=a.muted,t.lineWidth=1,t.setLineDash([4,4]),t.beginPath(),t.moveTo(l,0),t.lineTo(l,e.plotH),n.y>=0&&n.y<=e.plotH&&(t.moveTo(0,Math.round(n.y)+.5),t.lineTo(e.plotW,Math.round(n.y)+.5)),t.stroke(),t.setLineDash([]),t.font=`11px ${a.mono}`,Ee(t,e,n.y,y(r,o),a.grid,a.text);let h=i.toTime(n.index);if(h===null)return;let m=ht(h,s),u=t.measureText(m).width+12,c=Math.max(0,Math.min(e.plotW-u,l-u/2));t.fillStyle=a.grid,t.fillRect(c,e.plotH+1,u,e.height-e.plotH-1),t.fillStyle=a.text,t.textBaseline="middle",t.fillText(m,c+6,e.plotH+(e.height-e.plotH)/2)}function pt(t,e,i,n){let r=1/0,o=-1/0;for(let s of t){if(!s.visible)continue;let a=1/0,l=-1/0;for(let u of s.data()){let c=e.exact(u.time);if(c===void 0||c<i||c>n)continue;let[g,v]=s.span(u);Number.isFinite(g)&&g<a&&(a=g),Number.isFinite(v)&&v>l&&(l=v)}let h=a<=l?[a,l]:null,m=s.opts.autoscale?s.opts.autoscale(h):h;m&&(r=Math.min(r,m[0]),o=Math.max(o,m[1]))}return r===1/0?null:r===o?[r-1,o+1]:[r,o]}function ct(t,e){let i=Math.max(0,t.width-t.axisW),n=Math.max(0,t.height-t.axisH),{end:r,bw:o}=t.view,s=t.timeline.length,a=Math.max(0,Math.floor(r-i/o)),l=Math.min(s-1,Math.ceil(r));return{width:t.width,height:t.height,plotW:i,plotH:n,bw:o,first:a,last:l,x:h=>i-(r-h+.5)*o,y:h=>e.top+(e.hi-h)/(e.hi-e.lo)*e.inner,at:h=>t.timeline.exact(h),theme:t.theme}}function he(t,e){let i=Math.max(0,t.width-t.axisW),n=Math.max(0,t.height-t.axisH),r=Math.max(0,Math.floor(t.view.end-i/t.view.bw)),o=Math.min(t.timeline.length-1,Math.ceil(t.view.end)),s=pt(t.series,t.timeline,r,o),[a,l]=s??(e?[e.lo,e.hi]:[0,1]),h=n*.2;return{lo:a,hi:l,top:h,inner:Math.max(1,n-h-n*.1)}}function Be(t,e,i){let n=ct(e,i);t.clearRect(0,0,e.width,e.height),t.fillStyle=e.theme.bg,t.fillRect(0,0,e.width,e.height);let r=e.axis.priceDecimals??e.series.find(u=>u.visible)?.opts.priceDecimals,{ticks:o,dec:s}=Pe(i.lo,i.hi,n.plotH,r),a=He(n,e.timeline);De(t,n,o,a,e.axis),t.save(),t.beginPath(),t.rect(0,0,n.plotW,n.plotH),t.clip();let l={width:n.plotW,height:n.plotH},h=u=>{t.save(),u.draw(t,l),t.restore()};for(let u of e.overlays)u.zOrder==="bottom"&&h(u);let m=["histogram","candles","line"];for(let u of m)for(let c of e.series)c.kind===u&&ke(t,c,n);for(let u of e.series)Ae(t,u,n);for(let u of e.series)Ie(t,u,n);for(let u of e.overlays)u.zOrder!=="bottom"&&h(u);if(t.restore(),Oe(t,n,e.series,o,s),We(t,n,e.timeline,a,e.axis),e.hover){let u=i.hi-(e.hover.y-i.top)/i.inner*(i.hi-i.lo);Fe(t,n,e.timeline,e.hover,u,s,e.axis)}return n}var ee=class{constructor(e,i){this.el=e;this.t=i;p(this,"pointers",new Map);p(this,"drag",null);p(this,"pinch",0);p(this,"off",[]);this.on("wheel",n=>this.wheel(n),{passive:!1}),this.on("pointerdown",n=>this.down(n)),this.on("pointermove",n=>this.move(n)),this.on("pointerup",n=>this.up(n)),this.on("pointercancel",n=>this.end(n)),this.on("pointerleave",n=>{n.pointerType==="mouse"&&this.t.hover(null,0)})}on(e,i,n){this.el.addEventListener(e,i,n),this.off.push(()=>this.el.removeEventListener(e,i,n))}pos(e){let i=this.el.getBoundingClientRect();return{x:e.clientX-i.left,y:e.clientY-i.top}}wheel(e){if(!this.t.enabled())return;e.preventDefault();let{x:i}=this.pos(e);Math.abs(e.deltaX)>Math.abs(e.deltaY)?this.t.pan(this.t.viewEnd(),-e.deltaX):this.t.zoom(Math.exp(-e.deltaY*.002),i)}down(e){let i=this.pos(e);this.pointers.set(e.pointerId,i),this.el.setPointerCapture?.(e.pointerId),this.pointers.size===2?(this.drag=null,this.pinch=this.spread()):this.drag={x:i.x,end:this.t.viewEnd(),moved:!1}}move(e){let i=this.pos(e);if(this.pointers.has(e.pointerId)&&this.pointers.set(e.pointerId,i),this.pointers.size===2&&this.pinch>0){let n=this.spread();if(this.t.enabled()&&n>0){let r=[...this.pointers.values()].reduce((o,s)=>o+s.x,0)/2;this.t.zoom(n/this.pinch,r)}this.pinch=n;return}if(this.drag){let n=i.x-this.drag.x;Math.abs(n)>3&&(this.drag.moved=!0),this.drag.moved&&this.t.enabled()&&this.t.pan(this.drag.end,n)}this.t.hover(i.x,i.y)}up(e){let i=this.pos(e);this.drag&&!this.drag.moved&&this.pointers.size===1&&this.t.click(i.x,i.y),this.end(e)}end(e){this.pointers.delete(e.pointerId),this.drag=null,this.pointers.size<2&&(this.pinch=0)}spread(){let[e,i]=[...this.pointers.values()];return e&&i?Math.hypot(e.x-i.x,e.y-i.y):0}dispose(){for(let e of this.off)e()}};var L=class{constructor(e,i,n){this.host=e;this.opts=i;this.owner=n}update(e){this.opts={...this.opts,...e},this.host.redraw()}remove(){this.owner.removePriceLine(this)}};function ft(t,e){let i=t[t.length-1];if(!i||e.time>i.time){t.push(e);return}let n=0,r=t.length;for(;n<r;){let o=n+r>>1;t[o].time<e.time?n=o+1:r=o}t[n]?.time===e.time?t[n]=e:t.splice(n,0,e)}var z=class{constructor(e,i){this.host=e;this.opts=i;p(this,"points",[]);p(this,"lines",[]);p(this,"markers",[])}setData(e){this.points=[...e].sort((i,n)=>i.time-n.time),this.host.dataChanged()}update(e){ft(this.points,e),this.host.dataChanged()}data(){return this.points}times(){return this.points.map(e=>e.time)}setOptions(e){this.opts={...this.opts,...e},this.host.redraw()}get visible(){return this.opts.visible!==!1}addPriceLine(e){let i=new L(this.host,e,this);return this.lines.push(i),this.host.redraw(),i}removePriceLine(e){let i=this.lines.indexOf(e);i>=0&&this.lines.splice(i,1),this.host.redraw()}setMarkers(e){this.markers=[...e].sort((i,n)=>i.time-n.time),this.host.redraw()}priceToY(e){return this.host.priceToY(e)}yToPrice(e){return this.host.yToPrice(e)}},O=class extends z{constructor(){super(...arguments);p(this,"kind","candles")}span(i){return[i.low,i.high]}value(i){return i.close}},W=class extends z{constructor(){super(...arguments);p(this,"kind","line")}span(i){return[i.value,i.value]}value(i){return i.value}},F=class extends z{constructor(){super(...arguments);p(this,"kind","histogram")}span(i){let n=this.opts.base??0;return[Math.min(n,i.value),Math.max(n,i.value)]}value(i){return i.value}};var te=class{constructor(){p(this,"times",[]);p(this,"index",new Map)}rebuild(e){let i=new Set;for(let n of e)for(let r of n)i.add(r);this.times=[...i].sort((n,r)=>n-r),this.index=new Map(this.times.map((n,r)=>[n,r]))}get length(){return this.times.length}exact(e){return this.index.get(e)}step(){let e=this.times.length;return e>=2?this.times[e-1]-this.times[e-2]:6e4}toIndex(e){let i=this.times,n=i.length;if(n===0)return null;let r=this.index.get(e);if(r!==void 0)return r;if(e<i[0])return n>=2?(e-i[0])/(i[1]-i[0]):-1;if(e>i[n-1])return n-1+(e-i[n-1])/this.step();let o=0,s=n-1;for(;s-o>1;){let a=o+s>>1;i[a]<=e?o=a:s=a}return o+(e-i[o])/(i[s]-i[o])}toTime(e){let i=this.times,n=i.length;if(n===0)return null;let r=Math.round(e);return r>=0&&r<n?i[r]:r<0?i[0]+r*(n>=2?i[1]-i[0]:6e4):i[n-1]+(r-n+1)*this.step()}};var bt=1,gt=60,_=class{constructor(e,i={}){this.opts=i;p(this,"el");p(this,"canvas");p(this,"legendEl");p(this,"series",[]);p(this,"overlays",[]);p(this,"timeline",new te);p(this,"view");p(this,"scale",null);p(this,"hover",null);p(this,"clicks",new Set);p(this,"rangeSubs",new Set);p(this,"lastRange","");p(this,"raf",0);p(this,"resize");p(this,"host");p(this,"gestures");this.view={end:0,bw:i.barSpacing??8},this.el=document.createElement("div"),this.el.className="glot-chart",this.el.style.cssText="position:relative;width:100%;height:100%;overflow:hidden;touch-action:none",this.canvas=document.createElement("canvas"),this.canvas.style.cssText="position:absolute;inset:0;width:100%;height:100%;cursor:crosshair",this.canvas.setAttribute("role","img"),this.legendEl=document.createElement("div"),this.legendEl.className="glot-chart-legend",this.legendEl.hidden=i.legend===!1,this.el.append(this.canvas,this.legendEl),i.attributionLogo!==!1&&this.el.append(Se(i)),J(e.ownerDocument),e.append(this.el),this.host={dataChanged:()=>this.dataChanged(),redraw:()=>this.invalidate(),priceToY:n=>this.priceToY(n),yToPrice:n=>this.yToPrice(n)},this.gestures=new ee(this.canvas,{enabled:()=>this.opts.interactive!==!1,pan:(n,r)=>this.panTo(n-r/this.view.bw),viewEnd:()=>this.view.end,zoom:(n,r)=>this.zoomAt(n,r),hover:(n,r)=>this.setHover(n,r),click:(n,r)=>this.emitClick(n,r)}),this.resize=new ResizeObserver(()=>this.invalidate()),this.resize.observe(this.el)}addCandles(e={}){return this.add(new O(this.host,{lastPriceLine:!0,...e}))}addLine(e={}){return this.add(new W(this.host,e))}addHistogram(e={}){return this.add(new F(this.host,e))}add(e){return this.series.push(e),this.dataChanged(),e}removeSeries(e){this.series=this.series.filter(i=>i!==e),this.dataChanged()}addOverlay(e){return this.overlays.push(e),this.invalidate(),()=>{this.overlays=this.overlays.filter(i=>i!==e),this.invalidate()}}setOptions(e){let i=this.timeline.length;e.rightOffset!==void 0&&e.rightOffset!==this.opts.rightOffset&&i&&(this.view.end=i-1+e.rightOffset),e.barSpacing!==void 0&&(this.view.bw=me(e.barSpacing)),e.legend!==void 0&&(this.legendEl.hidden=e.legend===!1),this.opts={...this.opts,...e},this.invalidate()}options(){return{...this.opts,barSpacing:this.view.bw,rightOffset:this.opts.rightOffset??6}}plotSize(){let e=this.el.clientWidth-(this.opts.priceAxisWidth??72),i=this.el.clientHeight-(this.opts.timeAxisHeight??24);return{width:Math.max(0,e),height:Math.max(0,i)}}indexToX(e){return this.plotSize().width-(this.view.end-e+.5)*this.view.bw}xToIndex(e){return this.view.end+.5-(this.plotSize().width-e)/this.view.bw}timeToX(e){let i=this.timeline.toIndex(e);return i===null?null:this.indexToX(i)}xToTime(e){return this.timeline.toTime(this.xToIndex(e))}timeToIndex(e){return this.timeline.exact(e)??null}priceToY(e){let i=this.currentScale();return i?i.top+(i.hi-e)/(i.hi-i.lo)*i.inner:null}yToPrice(e){let i=this.currentScale();return i?i.hi-(e-i.top)/i.inner*(i.hi-i.lo):null}getVisibleIndexRange(){if(!this.timeline.length)return null;let e=this.plotSize().width;return{from:this.view.end-e/this.view.bw+1,to:this.view.end}}setVisibleIndexRange(e){let i=this.plotSize().width,n=Math.max(1,e.to-e.from+1);i>0&&(this.view.bw=me(i/n)),this.view.end=e.to,this.invalidate()}getVisibleRange(){let e=this.getVisibleIndexRange(),i=this.timeline.length;if(!e||!i)return null;let n=Math.min(i-1,Math.max(0,Math.ceil(e.from))),r=Math.min(i-1,Math.max(0,Math.floor(e.to)));return{from:this.timeline.times[n],to:this.timeline.times[r]}}setVisibleRange(e){let i=this.timeline.toIndex(e.from),n=this.timeline.toIndex(e.to);i===null||n===null||this.setVisibleIndexRange({from:i,to:n})}scrollToEnd(){this.view.end=Math.max(0,this.timeline.length-1)+(this.opts.rightOffset??6),this.invalidate()}fitContent(){let e=this.timeline.length;e&&this.setVisibleIndexRange({from:0,to:e-1})}onVisibleRangeChange(e){return this.rangeSubs.add(e),()=>this.rangeSubs.delete(e)}onClick(e){return this.clicks.add(e),()=>this.clicks.delete(e)}invalidate(){this.scale=null,cancelAnimationFrame(this.raf),this.raf=requestAnimationFrame(()=>this.draw())}dispose(){this.resize.disconnect(),this.gestures.dispose(),cancelAnimationFrame(this.raf),this.el.remove()}dataChanged(){let e=this.timeline.length,i=this.view.end>=e-1;this.timeline.rebuild(this.series.map(r=>r.times()));let n=this.timeline.length;e===0&&n>0?this.view.end=n-1+(this.opts.rightOffset??6):i&&n>e&&(this.view.end+=n-e),this.canvas.setAttribute("aria-label",`${n} bars`),this.invalidate()}panTo(e){let i=this.timeline.length,n=this.plotSize().width/this.view.bw;this.view.end=Math.min(i-1+n*.8,Math.max(Math.min(i-1,2),e)),this.invalidate()}zoomAt(e,i){let n=this.plotSize().width,r=this.xToIndex(Math.min(i,n)),o=me(this.view.bw*e);this.view.end=r-.5+(n-Math.min(i,n))/o,this.view.bw=o,this.invalidate()}setHover(e,i){let n=this.timeline.length;e===null||!n||e>this.plotSize().width?this.hover=null:this.hover={index:Math.round(this.xToIndex(e)-.5),y:i},this.invalidate()}emitClick(e,i){let n=Math.round(this.xToIndex(e)-.5),r=n>=0&&n<this.timeline.length,o={x:e,y:i,index:r?n:null,time:r?this.timeline.times[n]:null,price:this.yToPrice(i)};for(let s of this.clicks)s(o)}state(){let e=this.opts;return{width:this.el.clientWidth,height:this.el.clientHeight,axisW:e.priceAxisWidth??72,axisH:e.timeAxisHeight??24,view:this.view,timeline:this.timeline,series:this.series,overlays:this.overlays,theme:E(this.el,e.theme),axis:{grid:e.grid??"solid",timeVisible:e.timeVisible!==!1,secondsVisible:e.secondsVisible===!0,timeLabel:e.timeLabel??"UTC",priceDecimals:e.priceDecimals},hover:e.crosshair===!1?null:this.hover}}currentScale(){return this.timeline.length?(this.scale??(this.scale=he(this.state(),null)),this.scale):null}draw(){let e=window.devicePixelRatio||1,i=this.el.clientWidth,n=this.el.clientHeight;if(i===0||n===0)return;(this.canvas.width!==Math.round(i*e)||this.canvas.height!==Math.round(n*e))&&(this.canvas.width=Math.round(i*e),this.canvas.height=Math.round(n*e));let r=this.canvas.getContext("2d");if(!r)return;r.setTransform(e,0,0,e,0,0);let o=this.state();Q(this.el,o.theme),this.scale=he(o,this.scale),Be(r,o,this.scale),this.renderLegend(),this.emitRange()}renderLegend(){if(this.legendEl.hidden)return;let e=this.timeline.length,i=this.hover&&this.hover.index<e?this.hover.index:e-1,n=this.timeline.times[i];if(n===void 0){this.legendEl.textContent="";return}let r=[];for(let o of this.series){if(!o.visible)continue;let s=o.data().find(h=>h.time===n);if(!s)continue;let a=o.opts.priceDecimals??this.opts.priceDecimals??2,l=h=>y(h,a);if(o.kind==="candles"){let h=s;r.push(`O ${l(h.open)} H ${l(h.high)} L ${l(h.low)} C ${l(h.close)}`)}else o.opts.title&&r.push(`${o.opts.title} ${l(s.value)}`)}this.legendEl.textContent=r.join(" ")}emitRange(){let e=this.getVisibleRange(),i=e?`${e.from}:${e.to}`:"";if(i!==this.lastRange){this.lastRange=i;for(let n of this.rangeSubs)n(e)}}};function me(t){return Math.min(gt,Math.max(bt,t))}function Ve(t,e={}){return new _(t,e)}return je(vt);})();
|
package/dist/index.d.ts
CHANGED
|
@@ -7,3 +7,6 @@ export { bisect } from "./draw.js";
|
|
|
7
7
|
export * from "./scale.js";
|
|
8
8
|
export { resolveColor } from "./marks.js";
|
|
9
9
|
export type { Bar, ChartData, ChartOptions, FillSeries, LineSeries, ProgramColor, ShapeSeries, TradeMark, } from "./types.js";
|
|
10
|
+
export { TimeChart, createTimeChart } from "./series/time-chart";
|
|
11
|
+
export { PriceLine, CandleSeries, LineSeriesApi, HistogramSeries } from "./series/series";
|
|
12
|
+
export type { AutoscaleFn, CandleOptions, ClickInfo, HistogramOptions, LineOptions, LineStyleName, Marker, Overlay, PriceLineOptions, Range, SeriesOptions, TimeChartOptions, TimeValue, } from "./series/types";
|