UNPKG

apexstock

Version:

An extension library for ApexCharts focusing on stock charts

1 lines 168 kB
var ApexStock=function(){"use strict";class t{static truncateNumber(t){return null===t?t:Number(t.toFixed(2))}static is(t,e){return Object.prototype.toString.call(e)==="[object "+t+"]"}static isObject(t){return t&&"object"==typeof t&&!Array.isArray(t)&&null!=t}static extend(t,e){"function"!=typeof Object.assign&&(Object.assign=function(t){if(null==t)throw new TypeError("Cannot convert undefined or null to object");let e=Object(t);for(let t=1;arguments.length>t;t++){let n=arguments[t];if(null!=n)for(let t in n)n.hasOwnProperty(t)&&(e[t]=n[t])}return e});let n=Object.assign({},t);return this.isObject(t)&&this.isObject(e)&&Object.keys(e).forEach((i=>{this.isObject(e[i])?i in t?n[i]=this.extend(t[i],e[i]):Object.assign(n,{[i]:e[i]}):Object.assign(n,{[i]:e[i]})})),n}static generateUniqueId(t){return`${t}-${"10000000-1000-4000-8000-100000000000".replace(/[018]/g,(t=>(t^crypto.getRandomValues(new Uint8Array(1))[0]&15>>t/4).toString(16)))}`}}class e{static calculateMovingAverage(e,n){const i=[];for(let o=0;e.length>o;o++)if(n-1>o)i.push(null);else{let s=0;for(let t=o-n+1;o>=t;t++)s+=e[t].y[3];i.push(t.truncateNumber(s/n))}return i}static calculateRSI(e,n){const i=[];let o=0,s=0;for(let a=0;e.length>a;a++){const r=e[a].y[3];if(0===a)i.push(null);else{const l=r-e[a-1].y[3];if(n>a)if(o+=l>0?l:0,s+=0>l?-l:0,a===n-1){const e=s/n;i.push(t.truncateNumber(0===e?100:100-100/(1+(0===e?0:o/n/e))))}else i.push(null);else{let o=0,s=0;for(let t=a-n+1;a>=t;t++){const n=e[t].y[3]-e[t-1].y[3];o+=n>0?n:0,s+=0>n?-n:0}const r=s/n;i.push(t.truncateNumber(0===r?100:100-100/(1+(0===r?0:o/n/r))))}}}return i}static calculateBollingerBands(n,i,o){const s=e.calculateMovingAverage(n,i),a=[],r=[];for(let e=0;n.length>e;e++)if(i-1>e)a.push(null),r.push(null);else{let l=0;for(let t=e-i+1;e>=t;t++){const i=n[t].y[3]-s[e];l+=i*i}const c=Math.sqrt(l/i);a.push(t.truncateNumber(s[e]+o*c)),r.push(t.truncateNumber(s[e]-o*c))}return{middle:s,upper:a,lower:r}}static calculateMACD(t){let n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:26,i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:9;const o=e.calculateEMA(t,arguments.length>1&&void 0!==arguments[1]?arguments[1]:12),s=e.calculateEMA(t,n),a=[];for(let e=0;t.length>e;e++)a.push(n-1>e?null:o[e]-s[e]);const r=[];let l=0,c=0;for(let t=0;a.length>t;t++)if(n-1+i-1>t)r.push(null);else if(i>c)l+=a[t-(i-1)+c],c++,r.push(c===i?l/i:null);else{const e=2/(i+1);r.push(a[t]*e+r[t-1]*(1-e))}const h=[];for(let e=0;t.length>e;e++)h.push(n-1+i-1>e?null:a[e]-r[e]);return{macd:a,signal:r,histogram:h}}static calculateEMA(e,n){const i=[],o=2/(n+1);let s=0;for(let t=0;n>t;t++)s+=e[t].y[3];const a=s/n;for(let s=0;e.length>s;s++)i.push(n-1>s?null:t.truncateNumber(s===n-1?a:e[s].y[3]*o+i[s-1]*(1-o)));return i}static calculateFibonacciRetracements(t){const e=t.map((t=>t.y[1])),n=t.map((t=>t.y[2])),i=Math.max(...e),o=Math.min(...n),s=i-o;return[0,.236,.382,.5,.618,1].map((t=>o+t*s))}static calculateFibonacciRetracementsForRange(t,e,n){if(!t||0===t.length)return[];const i=t.slice(e=Math.max(0,e),(n=Math.min(t.length-1,n))+1),o=i.map((t=>t.y[1])),s=i.map((t=>t.y[2])),a=Math.max(...o),r=Math.min(...s),l=a-r;return[0,.236,.382,.5,.618,1].map((t=>r+t*l))}static calculatePVT(e){const n=[];let i=0;for(let o=0;e.length>o;o++)if(0===o)n.push({x:e[o].x,y:0});else{const s=e[o-1].y[3];i+=(e[o].y[3]-s)/s*(e[o].v||0),n.push({x:e[o].x,y:t.truncateNumber(i)})}return n}static calculateStochastic(e,n,i){const o=[];for(let i=0;e.length>i;i++)if(n-1>i)o.push({x:e[i].x,y:null});else{const s=e.slice(i-n+1,i+1);s.map((t=>t.y[3]));const a=s.map((t=>t.y[1])),r=s.map((t=>t.y[2])),l=Math.max(...a),c=Math.min(...r);o.push({x:e[i].x,y:t.truncateNumber((e[i].y[3]-c)/(l-c)*100)})}const s=[];for(let e=0;o.length>e;e++)if(i-1>e||null===o[e].y)s.push({x:o[e].x,y:null});else{let n=0;for(let t=e-i+1;e>=t;t++)n+=o[t].y;s.push({x:o[e].x,y:t.truncateNumber(n/i)})}return{k:o,d:s}}static calculateStdDevIndicator(e,n){const i=[];for(let o=0;e.length>o;o++)if(n-1>o)i.push({x:e[o].x,y:null});else{const s=e.slice(o-n+1,o+1).map((t=>t.y[3])),a=s.reduce(((t,e)=>t+e),0)/n;let r=0;s.forEach((t=>{r+=Math.pow(t-a,2)}));i.push({x:e[o].x,y:t.truncateNumber(Math.sqrt(r/n))})}return i}static calculateADX(e,n){if(!e||2*n>e.length)return e.map((t=>({x:t.x,y:null})));const i=e.map((t=>({x:t.x,y:null}))),o=[],s=[],a=[],r=[],l=[],c=[],h=[];for(let t=1;e.length>t;t++){const n=e[t].y,i=e[t-1].y,r=n[1],l=n[2],c=i[3];o.push(Math.max(Math.abs(r-l),Math.abs(r-c),Math.abs(l-c)));const h=r-i[1],d=i[2]-l;let p=0,u=0;h>d&&h>0&&(p=h),d>h&&d>0&&(u=d),s.push(p),a.push(u)}let d=0,p=0,u=0;for(let t=0;n>t;t++)d+=o[t],p+=s[t],u+=a[t];r.push(d),l.push(p),c.push(u);for(let t=1;o.length-n+1>t;t++){const e=r[t-1]-r[t-1]/n+o[t+n-1],i=l[t-1]-l[t-1]/n+s[t+n-1],d=c[t-1]-c[t-1]/n+a[t+n-1];r.push(e),l.push(i),c.push(d);const p=i/e*100,u=d/e*100;h.push(Math.abs(p-u)/(p+u)*100)}let m=0;for(let t=0;n>t;t++)h.length>t&&(m+=h[t]);let g=m/n;const x=2*n;e.length>x&&(i[x].y=t.truncateNumber(g));for(let o=n;h.length>o;o++){g=(g*(n-1)+h[o])/n;const s=o+n+1;e.length>s&&(i[s].y=t.truncateNumber(g))}return i}static calculateChaikinOsc(n,i,o){const s=[];let a=0;for(let t=0;n.length>t;t++){const e=n[t].y[1],i=n[t].y[2],o=n[t].y[3];a+=(e-o-(o-i))/(e-i||1)*(n[t].v||0),s.push(a)}const r=e.calculateEMAFromArray(s,i),l=e.calculateEMAFromArray(s,o),c=[];for(let e=0;s.length>e;e++)c.push(null===r[e]||null===l[e]?{x:n[e].x,y:null}:{x:n[e].x,y:t.truncateNumber(r[e]-l[e])});return c}static calculateEMAFromArray(e,n){const i=[],o=2/(n+1);let s=0;for(let a=0;e.length>a;a++)if(n>a)if(s+=null!==e[a]?e[a]:0,a===n-1){i.push(t.truncateNumber(s/n))}else i.push(null);else i.push(t.truncateNumber(e[a]*o+i[a-1]*(1-o)));return i}static calculateSMAFromArray(e,n){const i=[];for(let o=0;e.length>o;o++)if(n-1>o)i.push(null);else{let s=0;for(let t=o-n+1;o>=t;t++)s+=e[t];i.push(t.truncateNumber(s/n))}return i}static calculateBBPercent(e,n,i){const o=[];for(let s=0;e.length>s;s++){const a=e[s].y[3];if(null===n[s]||null===i[s])o.push({x:e[s].x,y:null});else{o.push({x:e[s].x,y:t.truncateNumber((a-n[s])/(i[s]-n[s]))})}}return o}static calculateBBWidth(e,n,i,o){const s=[];for(let a=0;e.length>a;a++)if(null===n[a]||null===i[a]||null===o[a])s.push({x:e[a].x,y:null});else{s.push({x:e[a].x,y:t.truncateNumber((i[a]-o[a])/n[a])})}return s}static calculateLinearRegression(e,n){const i=[];for(let o=0;e.length>o;o++)if(n-1>o)i.push({x:e[o].x,y:null});else{let s=0,a=0,r=0,l=0;for(let t=o-n+1;o>=t;t++){const n=e[t].y[3];s+=t,a+=n,r+=t*n,l+=t*t}const c=(n*r-s*a)/(n*l-s*s);i.push({x:e[o].x,y:t.truncateNumber(c*o+(a-c*s)/n)})}return i}static calculateIchimoku(e){const n=[],i=[],o=[],s=[],a=[];for(let r=0;e.length>r;r++){if(8>r)n.push({x:e[r].x,y:null});else{const i=e.slice(r-8,r+1),o=Math.max(...i.map((t=>t.y[1]))),s=Math.min(...i.map((t=>t.y[2])));n.push({x:e[r].x,y:t.truncateNumber((o+s)/2)})}if(25>r)i.push({x:e[r].x,y:null});else{const n=e.slice(r-25,r+1),o=Math.max(...n.map((t=>t.y[1]))),s=Math.min(...n.map((t=>t.y[2])));i.push({x:e[r].x,y:t.truncateNumber((o+s)/2)})}if(a.push(26>r?{x:e[r].x,y:null}:{x:e[r-26].x,y:e[r].y[3]}),51>r)s.push({x:e[r].x,y:null});else{const n=e.slice(r-51,r+1),i=Math.max(...n.map((t=>t.y[1]))),o=Math.min(...n.map((t=>t.y[2])));s.push({x:e[r].x,y:t.truncateNumber((i+o)/2)})}o.push(25>r||null===n[r].y||null===i[r].y?{x:e[r].x,y:null}:{x:e[r].x,y:t.truncateNumber((n[r].y+i[r].y)/2)})}return{tenkan:n,kijun:i,senkouA:o,senkouB:s,chikou:a}}static calculateCCI(e){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:20;const i=[];for(let o=0;e.length>o;o++)if(n-1>o)i.push({x:e[o].x,y:null});else{const s=e.slice(o-n+1,o+1).map((t=>(t.y[1]+t.y[2]+t.y[3])/3)),a=s.reduce(((t,e)=>t+e),0)/n;let r=0;s.forEach((t=>{r+=Math.abs(t-a)})),r/=n;i.push({x:e[o].x,y:t.truncateNumber(0===r?0:((e[o].y[1]+e[o].y[2]+e[o].y[3])/3-a)/(.015*r))})}return i}static calculateTSI(n){let i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:25,o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:13,s=arguments.length>3&&void 0!==arguments[3]?arguments[3]:7;const a=[],r=[],l=[];for(let t=0;n.length>t;t++)if(0===t)r.push(0),l.push(0);else{const e=n[t].y[3]-n[t-1].y[3];r.push(e),l.push(Math.abs(e))}const c=e.calculateEMAFromArray(r,i),h=e.calculateEMAFromArray(c,o),d=e.calculateEMAFromArray(l,i),p=e.calculateEMAFromArray(d,o);for(let e=0;n.length>e;e++)if(null===h[e]||null===p[e]||0===p[e])a.push({x:n[e].x,y:null});else{a.push({x:n[e].x,y:t.truncateNumber(h[e]/p[e]*100)})}const u=a.map((t=>t.y)),m=e.calculateEMAFromArray(u,s),g=a.map(((t,e)=>({x:t.x,y:m[e]})));return{tsi:a,signal:g}}static calculateAcceleratorOsc(n){let i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:5;const o=e.calculateAwesomeOscillator(n),s=o.map((t=>t.y)),a=e.calculateSMAFromArray(s,i),r=[];for(let e=0;n.length>e;e++)r.push(null===o[e].y||null===a[e]?{x:n[e].x,y:null}:{x:n[e].x,y:t.truncateNumber(o[e].y-a[e])});return r}static calculateAwesomeOscillator(e){const n=[];for(let i=0;e.length>i;i++)if(33>i)n.push({x:e[i].x,y:null});else{const o=e.slice(i-5+1,i+1),s=e.slice(i-34+1,i+1),a=o.reduce(((t,e)=>t+(e.y[1]+e.y[2])/2),0)/5,r=s.reduce(((t,e)=>t+(e.y[1]+e.y[2])/2),0)/34;n.push({x:e[i].x,y:t.truncateNumber(a-r)})}return n}}class n{constructor(t,e,n,i,o,s,a,r,l,c){this.chart=t,this.chartDiv=e,this.svgOverlay=n,this.syncOverlayPosition=c,this.intervalId=null,this.boundMouseDown=i,this.boundMouseMove=o,this.boundMouseUp=s,this.boundResize=a,this.boundWheelEvent=r,this.redrawElements=l,this.initEventListeners(),this.listenForChartEvents()}initEventListeners(){this.svgOverlay.addEventListener("mousedown",this.boundMouseDown),window.addEventListener("mousemove",this.boundMouseMove),window.addEventListener("mouseup",this.boundMouseUp),window.addEventListener("resize",this.boundResize),this.chartDiv.addEventListener("wheel",this.boundWheelEvent),this.svgOverlay.addEventListener("wheel",this.boundWheelEvent)}listenForChartEvents(){new MutationObserver((()=>{"function"==typeof this.syncOverlayPosition&&this.syncOverlayPosition()})).observe(this.chartDiv,{attributes:!0,childList:!0,subtree:!0}),this.intervalId=setInterval((()=>{"function"==typeof this.syncOverlayPosition&&this.syncOverlayPosition()}),1e3);try{this.chart.addEventListener&&(this.chart.addEventListener("updated",(()=>{setTimeout((()=>{"function"==typeof this.syncOverlayPosition&&this.syncOverlayPosition(),this.redrawElements()}),300)})),this.chart.addEventListener("zoomed",(()=>{"function"==typeof this.coordinateConverter?.refreshBounds&&this.coordinateConverter.refreshBounds(),this.redrawElements()})),this.chart.addEventListener("scrolled",(()=>{"function"==typeof this.coordinateConverter?.refreshBounds&&this.coordinateConverter.refreshBounds(),this.redrawElements()})))}catch(t){console.error("Error setting up chart event listeners:",t)}}destroy(){this.svgOverlay.removeEventListener("mousedown",this.boundMouseDown),window.removeEventListener("mousemove",this.boundMouseMove),window.removeEventListener("mouseup",this.boundMouseUp),window.removeEventListener("resize",this.boundResize),this.chartDiv.removeEventListener("wheel",this.boundWheelEvent),this.svgOverlay.removeEventListener("wheel",this.boundWheelEvent),this.intervalId&&clearInterval(this.intervalId)}}class i{constructor(t){this.chartDiv=t,this.overlayWrapper=null,this.svgOverlay=null,this.defs=null,this.drawingGroup=null,this.setupSVGOverlay()}setupSVGOverlay(){this.overlayWrapper=document.createElement("div"),this.overlayWrapper.className="apexstock-drawing-overlay-wrapper",this.chartDiv.appendChild(this.overlayWrapper),this.svgOverlay=document.createElementNS("http://www.w3.org/2000/svg","svg"),this.svgOverlay.setAttribute("class","apexstock-drawing-overlay"),this.overlayWrapper.appendChild(this.svgOverlay),this.defs=document.createElementNS("http://www.w3.org/2000/svg","defs"),this.svgOverlay.appendChild(this.defs),this.drawingGroup=document.createElementNS("http://www.w3.org/2000/svg","g"),this.svgOverlay.appendChild(this.drawingGroup),setTimeout((()=>{this.syncOverlayPosition()}),100),window.addEventListener("resize",(()=>{this.syncOverlayPosition()}))}syncOverlayPosition(){if(this.chartDiv&&this.overlayWrapper)try{const t=this.chartDiv.getBoundingClientRect(),e=this.chartDiv.parentNode.getBoundingClientRect();this.overlayWrapper.style.width=t.width+"px",this.overlayWrapper.style.height=t.height+"px",this.overlayWrapper.style.left=t.left-e.left+"px"}catch(t){console.error("Error syncing overlay position:",t)}}}class o{constructor(t,e,n,i,o,s,a){this.ctx=t,this.chartDiv=e,this.drawingColor=n,this.drawingWidth=i,this.toolClickHandler=o,this.clearHandler=s,this.toolbarContainer=null,this.onColorChange=null,this.onWidthChange=null,this.availableTools=a||{},this.createDrawingToolbar()}createDrawingToolbar(){const t=document.createElement("div");t.className="apexstock-drawing-toolbar";const e=[{name:"line",icon:"╱",tooltip:"Line"},{name:"brush",icon:"∿",tooltip:"Brush"},{name:"highlighter",icon:"🖌️",tooltip:"Highlighter"},{name:"rectangle",icon:"▢",tooltip:"Rectangle"},{name:"circle",icon:"◯",tooltip:"Circle"},{name:"ellipse",icon:"⬭",tooltip:"Ellipse"},{name:"text",icon:"T",tooltip:"Text Annotation"},{name:"pin",icon:"📌",tooltip:"Pin Tooltips"},{name:"clear",icon:"🗑",tooltip:"Clear All"}].filter((t=>!this.availableTools||!1!==this.availableTools[t.name])),n=document.createElement("input");n.type="color",n.value=this.drawingColor,n.classList.add("apexstock-drawing-color-picker"),n.title="Color",n.addEventListener("input",(t=>{this.drawingColor=t.target.value,"function"==typeof this.onColorChange&&this.onColorChange(t.target.value)})),t.appendChild(n);const i=document.createElement("select");i.style.marginRight="5px",i.style.height="30px",i.title="Line Width",[1,2,3,5,8].forEach((t=>{const e=document.createElement("option");e.value=t,e.textContent=t+"px",t===this.drawingWidth&&(e.selected=!0),i.appendChild(e)})),i.addEventListener("change",(t=>{this.drawingWidth=parseInt(t.target.value),"function"==typeof this.onWidthChange&&this.onWidthChange(parseInt(t.target.value))})),t.appendChild(i),e.forEach((e=>{const n=document.createElement("button");n.className="apexstock-drawing-tool",n.dataset.tool=e.name,n.textContent=e.icon,n.title=e.tooltip,"select"===e.name&&(n.style.backgroundColor="#f0f0f0"),"pin"===e.name&&(n.classList.add("active"),n.style.marginRight="2px"),"clear"===e.name&&(n.style.marginLeft="2px"),n.addEventListener("click",(()=>{"clear"===e.name?"function"==typeof this.clearHandler&&this.clearHandler():"function"==typeof this.toolClickHandler&&this.toolClickHandler(e.name)})),t.appendChild(n)})),this.ctx.primaryToolbarLeft.appendChild(t),this.toolbarContainer=t}setActiveTool(t){this.toolbarContainer&&this.toolbarContainer.querySelectorAll(".apexstock-drawing-tool").forEach((e=>{e.dataset.tool===t?e.classList.add("active"):-1===["clear","pin"].indexOf(e.dataset.tool)&&e.classList.remove("active")}))}setColor(t){this.drawingColor=t;const e=this.toolbarContainer.querySelector(".apexstock-drawing-color-picker");e&&(e.value=t)}setWidth(t){this.drawingWidth=t;const e=this.toolbarContainer.querySelector("select");if(e)for(let n=0;e.options.length>n;n++)if(parseInt(e.options[n].value)===t){e.selectedIndex=n;break}}destroy(){this.toolbarContainer&&this.toolbarContainer.parentNode&&this.toolbarContainer.parentNode.removeChild(this.toolbarContainer)}}class s{constructor(t,e,n){this.startPoint=t,this.color=e,this.width=n,this.fillColor="#ffffff",this.fillOpacity=.5}setFillColor(t){return this.fillColor=t,this}setFillOpacity(t){return this.fillOpacity=t,this}createElement(t){switch(t){case"line":return this.createLine();case"brush":return this.createBrush();case"highlighter":return this.createHighlighter();case"rectangle":return this.createRectangle();case"circle":return this.createCircle();case"ellipse":return this.createEllipse();default:return null}}createLine(){const e=document.createElementNS("http://www.w3.org/2000/svg","line");e.setAttribute("x1",this.startPoint.x),e.setAttribute("y1",this.startPoint.y),e.setAttribute("x2",this.startPoint.x),e.setAttribute("y2",this.startPoint.y),e.setAttribute("stroke",this.color),e.setAttribute("stroke-width",this.width);const n=t.generateUniqueId("line");e.dataset.elementId=n;return{element:e,data:{id:n,type:"line",x1:this.startPoint.dataX,y1:this.startPoint.dataY,x2:this.startPoint.dataX,y2:this.startPoint.dataY,color:this.color,width:this.width}}}createBrush(){const e=document.createElementNS("http://www.w3.org/2000/svg","path");e.setAttribute("d",`M ${this.startPoint.x} ${this.startPoint.y}`),e.setAttribute("fill","none"),e.setAttribute("stroke",this.color),e.setAttribute("stroke-width",this.width),e.setAttribute("stroke-linecap","round"),e.setAttribute("stroke-linejoin","round");const n=t.generateUniqueId("brush");e.dataset.elementId=n;return{element:e,data:{id:n,type:"brush",points:[{x:this.startPoint.dataX,y:this.startPoint.dataY}],color:this.color,width:this.width}}}createHighlighter(){const e=document.createElementNS("http://www.w3.org/2000/svg","path");e.setAttribute("d",`M ${this.startPoint.x} ${this.startPoint.y}`),e.setAttribute("fill","none"),e.setAttribute("stroke",this.color),e.setAttribute("stroke-width",3*this.width),e.setAttribute("stroke-linecap","round"),e.setAttribute("stroke-linejoin","round"),e.setAttribute("stroke-opacity","0.3");const n=t.generateUniqueId("highlighter");e.dataset.elementId=n;return{element:e,data:{id:n,type:"highlighter",points:[{x:this.startPoint.dataX,y:this.startPoint.dataY}],color:this.color,width:3*this.width,opacity:.3}}}createRectangle(){const e=document.createElementNS("http://www.w3.org/2000/svg","rect");e.setAttribute("x",this.startPoint.x),e.setAttribute("y",this.startPoint.y),e.setAttribute("width","0"),e.setAttribute("height","0"),e.setAttribute("stroke",this.color),e.setAttribute("stroke-width",this.width),e.setAttribute("fill",this.fillColor),e.setAttribute("fill-opacity",this.fillOpacity);const n=t.generateUniqueId("rectangle");e.dataset.elementId=n;return{element:e,data:{id:n,type:"rectangle",x:this.startPoint.dataX,y:this.startPoint.dataY,width:0,height:0,color:this.color,strokeWidth:this.width,fill:this.fillColor,fillOpacity:this.fillOpacity}}}createCircle(){const e=document.createElementNS("http://www.w3.org/2000/svg","circle");e.setAttribute("cx",this.startPoint.x),e.setAttribute("cy",this.startPoint.y),e.setAttribute("r","0"),e.setAttribute("stroke",this.color),e.setAttribute("stroke-width",this.width),e.setAttribute("fill",this.fillColor),e.setAttribute("fill-opacity",this.fillOpacity);const n=t.generateUniqueId("circle");e.dataset.elementId=n;return{element:e,data:{id:n,type:"circle",cx:this.startPoint.dataX,cy:this.startPoint.dataY,r:0,color:this.color,strokeWidth:this.width,fill:this.fillColor,fillOpacity:this.fillOpacity}}}createEllipse(){const e=document.createElementNS("http://www.w3.org/2000/svg","ellipse");e.setAttribute("cx",this.startPoint.x),e.setAttribute("cy",this.startPoint.y),e.setAttribute("rx","0"),e.setAttribute("ry","0"),e.setAttribute("stroke",this.color),e.setAttribute("stroke-width",this.width),e.setAttribute("fill",this.fillColor),e.setAttribute("fill-opacity",this.fillOpacity);const n=t.generateUniqueId("ellipse");e.dataset.elementId=n;return{element:e,data:{id:n,type:"ellipse",cx:this.startPoint.dataX,cy:this.startPoint.dataY,rx:0,ry:0,color:this.color,strokeWidth:this.width,fill:this.fillColor,fillOpacity:this.fillOpacity}}}}class a{constructor(t,e){this.chart=t,this.chartDiv=e,this.cachedBounds=null,this.lastUpdateTime=0}getChartBounds(){let t=arguments.length>0&&void 0!==arguments[0]&&arguments[0];const e=Date.now();if(!t&&this.cachedBounds&&100>e-this.lastUpdateTime)return this.cachedBounds;try{const t=this.chartDiv.getBoundingClientRect();let n,i,o,s,a,r,l,c;try{n=this.chart.w.globals.minX,i=this.chart.w.globals.maxX,o=this.chart.w.globals.minY,s=this.chart.w.globals.maxY}catch(n){return console.warn("Could not access chart axis values:",n),this.cachedBounds={xaxis:0,xaxisMax:100,yaxis:0,yaxisMax:100,leftMargin:0,translateY:0,gridWidth:t.width,gridHeight:t.height,chartRect:t},this.lastUpdateTime=e,this.cachedBounds}try{a=this.chart.w.globals.translateX,r=this.chart.w.globals.translateY,l=this.chart.w.globals.gridWidth,c=this.chart.w.globals.gridHeight}catch(e){console.warn("Could not access chart grid values:",e),a=0,r=0,l=t.width,c=t.height}return(void 0===a||isNaN(a))&&(a=60),(void 0===r||isNaN(r))&&(r=20),(void 0===l||isNaN(l))&&(l=t.width-a),(void 0===c||isNaN(c))&&(c=t.height-r),this.cachedBounds={xaxis:n,xaxisMax:i,yaxis:o,yaxisMax:s,leftMargin:a,translateY:r,gridWidth:l,gridHeight:c,chartRect:t},this.lastUpdateTime=e,this.cachedBounds}catch(t){return console.error("Error getting chart bounds:",t),{xaxis:0,xaxisMax:100,yaxis:0,yaxisMax:100,leftMargin:0,translateY:0,gridWidth:100,gridHeight:100,chartRect:{width:100,height:100}}}}screenToData(t,e){try{const n=this.getChartBounds(),i=n.xaxis+(n.xaxisMax-n.xaxis)*((t-n.leftMargin)/n.gridWidth),o=n.yaxisMax-(n.yaxisMax-n.yaxis)*((e-n.translateY)/n.gridHeight);return isNaN(i)||isNaN(o)?(console.warn("Invalid data coordinates calculated:",i,o),{x:t,y:e}):{x:i,y:o}}catch(n){return console.error("Error converting screen to data coordinates:",n),{x:t,y:e}}}dataToScreen(t,e){try{const n=this.getChartBounds(),i=n.leftMargin+n.gridWidth*((t-n.xaxis)/(n.xaxisMax-n.xaxis)),o=n.translateY+n.gridHeight*((n.yaxisMax-e)/(n.yaxisMax-n.yaxis));return isNaN(i)||isNaN(o)?(console.warn("Invalid screen coordinates calculated:",i,o),{x:t,y:e}):{x:i,y:o}}catch(n){return console.error("Error converting data to screen coordinates:",n),{x:t,y:e}}}screenDeltaToDataDelta(t,e){try{const n=this.getChartBounds();return{x:t*((n.xaxisMax-n.xaxis)/n.gridWidth),y:-e*((n.yaxisMax-n.yaxis)/n.gridHeight)}}catch(t){return console.error("Error converting screen delta to data delta:",t),{x:0,y:0}}}refreshBounds(){this.getChartBounds(!0)}getDataDistance(t,e){return Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2))}static getInstance(t,e){return a.instance&&a.instance.chart===t&&a.instance.chartDiv===e||(a.instance=new a(t,e)),a.instance}}class r{constructor(t,e,n,i){this.svgOverlay=e,this.chartDiv=t,this.coordinateConverter=n,this.onTextCreated=i,this.isEditing=!1,this.clickX=0,this.clickY=0,this.currentGroup=null,this.currentData=null,this.editor=null,this.toolbar=null,this.textStyle={fontSize:14,fontFamily:"Arial, sans-serif",fontWeight:"normal",fontStyle:"normal",textDecoration:"none",color:"#0077b6",backgroundColor:"#ffffff",backgroundOpacity:0},this.createToolbar(),this.handleOutsideClick=this.handleOutsideClick.bind(this)}createTextAnnotation(e,n,i,o){this.clickX=e,this.clickY=n;const s=document.createElementNS("http://www.w3.org/2000/svg","g");s.classList.add("apexstock-text-annotation");const a=t.generateUniqueId("text");return s.dataset.elementId=a,this.currentData={id:a,type:"text",x:i.x,y:i.y,clickX:e,clickY:n,text:"",color:o||this.textStyle.color,backgroundColor:this.textStyle.backgroundColor,backgroundOpacity:this.textStyle.backgroundOpacity,...this.textStyle},this.currentGroup=s,this.startEditing(),{element:s,data:this.currentData}}createToolbar(){const t=document.createElement("div");t.className="apexstock-text-toolbar";const e=document.createElement("button");e.innerHTML="<strong>B</strong>",e.addEventListener("click",(()=>{this.toggleStyle("fontWeight","bold","normal")}));const n=document.createElement("button");n.innerHTML="<em>I</em>",n.addEventListener("click",(()=>{this.toggleStyle("fontStyle","italic","normal")}));const i=document.createElement("button");i.innerHTML="<u>U</u>",i.addEventListener("click",(()=>{this.toggleStyle("textDecoration","underline","none")}));const o=document.createElement("div");o.className="color-wrapper";const s=document.createElement("label");s.textContent="Text";const a=document.createElement("input");a.type="color",a.value=this.textStyle.color,a.addEventListener("input",(t=>{this.setStyle("color",t.target.value)})),o.appendChild(s),o.appendChild(a);const r=document.createElement("div");r.className="color-wrapper";const l=document.createElement("label");l.textContent="Background";const c=document.createElement("input");c.type="color",c.value=this.textStyle.backgroundColor,c.addEventListener("input",(t=>{this.setStyle("backgroundColor",t.target.value)})),r.appendChild(l),r.appendChild(c);const h=document.createElement("div");h.className="opacity-wrapper";const d=document.createElement("label");d.textContent="Opacity";const p=document.createElement("input");p.type="range",p.min="0",p.max="100",p.value=100*this.textStyle.backgroundOpacity,p.addEventListener("input",(t=>{this.setStyle("backgroundOpacity",parseInt(t.target.value)/100)})),h.appendChild(d),h.appendChild(p);const u=document.createElement("select");[10,12,14,16,18,20,24].forEach((t=>{const e=document.createElement("option");e.value=t,e.text=t+"px",t===this.textStyle.fontSize&&(e.selected=!0),u.appendChild(e)})),u.addEventListener("change",(()=>{this.setStyle("fontSize",parseInt(u.value))}));const m=document.createElement("select");["Arial, sans-serif","Courier New, monospace","Helvetica, Arial, sans-serif","Verdana, Geneva, sans-serif","Tahoma, Geneva, sans-serif","Trebuchet MS, Helvetica, sans-serif","Segoe UI, Helvetica, sans-serif","Consolas, Monaco, monospace","Menlo, Consolas, monospace","Times New Roman, serif","Georgia, Times New Roman, serif","Cambria, Georgia, serif"].forEach((t=>{const e=document.createElement("option");e.value=t,e.text=t.split(",")[0],t===this.textStyle.fontFamily&&(e.selected=!0),m.appendChild(e)})),m.addEventListener("change",(()=>{this.setStyle("fontFamily",m.value)}));const g=document.createElement("button");g.textContent="✓",g.addEventListener("click",(()=>{this.finishEditing()}));const x=document.createElement("button");x.textContent="✕",x.addEventListener("click",(()=>{this.cancelEditing()}));const y=document.createElement("div");y.className="font-controls",y.appendChild(u),y.appendChild(m),y.appendChild(e),y.appendChild(n),y.appendChild(i);const b=document.createElement("div");b.className="color-controls",b.appendChild(o),b.appendChild(r),b.appendChild(h);const v=document.createElement("div");v.className="action-controls",v.appendChild(x),v.appendChild(g),t.appendChild(y),t.appendChild(b),t.appendChild(v),this.chartDiv.appendChild(t),this.toolbar=t}startEditing(){if(this.isEditing)return;this.isEditing=!0;const t=document.createElement("div");t.className="apexstock-text-editor",t.contentEditable=!0,t.style.left=this.clickX+"px",t.style.top=this.clickY+"px",t.style.minHeight=this.textStyle.fontSize+"px",t.style.background=this.currentData.backgroundColor||"#ffffff",t.style.opacity=this.currentData.backgroundOpacity||.7,t.style.fontFamily=this.currentData.fontFamily,t.style.fontSize=this.currentData.fontSize+"px",t.style.fontWeight=this.currentData.fontWeight,t.style.fontStyle=this.currentData.fontStyle,t.style.textDecoration=this.currentData.textDecoration,t.style.color=this.currentData.color,this.chartDiv.appendChild(t),this.editor=t,setTimeout((()=>{t.focus(),this.showToolbar(this.clickX,this.clickY-150)}),10),t.addEventListener("keydown",(t=>{"Escape"===t.key?(this.cancelEditing(),t.preventDefault()):"Enter"!==t.key||t.shiftKey||(this.finishEditing(),t.preventDefault())})),document.addEventListener("mousedown",this.handleOutsideClick)}showToolbar(t,e){if(!this.toolbar)return;const n=window.innerWidth;this.toolbar.style.display="flex";const i=this.toolbar.offsetWidth;let o=t;o+i>n&&(o=Math.max(5,n-i-5));let s=e;5>s&&(s=this.clickY+30),this.toolbar.style.left=o+"px",this.toolbar.style.top=s+"px"}handleOutsideClick(t){this.isEditing&&this.editor&&!this.editor.contains(t.target)&&this.toolbar&&!this.toolbar.contains(t.target)&&(t.preventDefault(),this.finishEditing())}finishEditing(){if(!this.isEditing||!this.editor)return;const t=this.editor.textContent.trim();t?(this.currentData.text=t,this.createSvgText(),this.cleanup()):this.cancelEditing()}createSvgText(){if(this.currentGroup)for(;this.currentGroup.firstChild;)this.currentGroup.removeChild(this.currentGroup.firstChild);else this.currentGroup=document.createElementNS("http://www.w3.org/2000/svg","g"),this.currentGroup.classList.add("apexstock-text-annotation"),this.currentData.id&&(this.currentGroup.dataset.elementId=this.currentData.id);this.currentGroup.parentNode||this.svgOverlay.appendChild(this.currentGroup);const t=document.createElementNS("http://www.w3.org/2000/svg","rect");t.setAttribute("fill",this.currentData.backgroundColor||"#ffffff"),t.setAttribute("fill-opacity",this.currentData.backgroundOpacity||.7),t.setAttribute("rx","3");const e=document.createElementNS("http://www.w3.org/2000/svg","text");e.setAttribute("x",this.clickX),e.setAttribute("y",this.clickY),e.setAttribute("font-family",this.currentData.fontFamily),e.setAttribute("font-size",this.currentData.fontSize+"px"),e.setAttribute("font-weight",this.currentData.fontWeight),e.setAttribute("font-style",this.currentData.fontStyle),e.setAttribute("text-decoration",this.currentData.textDecoration),e.setAttribute("fill",this.currentData.color),e.setAttribute("dominant-baseline","text-before-edge");this.currentData.text.split("\n").forEach(((t,n)=>{const i=document.createElementNS("http://www.w3.org/2000/svg","tspan");i.textContent=t,i.setAttribute("x",this.clickX),i.setAttribute("dy",0===n?"0":"1.2em"),e.appendChild(i)})),this.currentGroup.appendChild(e);try{const n=e.getBBox(),i=5;t.setAttribute("x",n.x-i),t.setAttribute("y",n.y-i),t.setAttribute("width",n.width+2*i),t.setAttribute("height",n.height+2*i),this.currentGroup.insertBefore(t,e)}catch(n){console.error("Error measuring text:",n),t.setAttribute("x",this.clickX-5),t.setAttribute("y",this.clickY-5),t.setAttribute("width",100),t.setAttribute("height",30),this.currentGroup.insertBefore(t,e)}this.onTextCreated&&this.onTextCreated(this.currentGroup,this.currentData)}cancelEditing(){this.isEditing&&(this.currentGroup&&this.currentGroup.parentNode&&this.currentGroup.parentNode.removeChild(this.currentGroup),this.cleanup())}cleanup(){this.editor&&this.editor.parentNode&&(this.editor.parentNode.removeChild(this.editor),this.editor=null),this.toolbar&&(this.toolbar.style.display="none"),document.removeEventListener("mousedown",this.handleOutsideClick),this.isEditing=!1}toggleStyle(t,e,n){if(!this.currentData||!this.editor)return;this.setStyle(t,this.currentData[t]===e?n:e)}setStyle(t,e){if(this.currentData&&this.editor)switch(this.currentData[t]=e,t){case"fontSize":this.editor.style.fontSize=e+"px";break;case"fontFamily":this.editor.style.fontFamily=e;break;case"fontWeight":this.editor.style.fontWeight=e;break;case"fontStyle":this.editor.style.fontStyle=e;break;case"textDecoration":this.editor.style.textDecoration=e;break;case"color":this.editor.style.color=e;break;case"backgroundColor":this.editor.style.backgroundColor=e;break;case"backgroundOpacity":this.editor.style.opacity=e}}redrawTextElement(e){const n=document.createElementNS("http://www.w3.org/2000/svg","g");if(n.classList.add("apexstock-text-annotation"),e.id)n.dataset.elementId=e.id;else{const i=t.generateUniqueId("text");n.dataset.elementId=i,e.id=i}let i,o;const s=this.coordinateConverter.dataToScreen(e.x,e.y);i=s.x,o=s.y,(isNaN(s.x)||isNaN(s.y))&&(i=e.clickX||0,o=e.clickY||0);const a=document.createElementNS("http://www.w3.org/2000/svg","text");a.setAttribute("x",i),a.setAttribute("y",o),a.setAttribute("font-family",e.fontFamily||this.textStyle.fontFamily),a.setAttribute("font-size",(e.fontSize||this.textStyle.fontSize)+"px"),a.setAttribute("font-weight",e.fontWeight||this.textStyle.fontWeight),a.setAttribute("font-style",e.fontStyle||this.textStyle.fontStyle),a.setAttribute("text-decoration",e.textDecoration||this.textStyle.textDecoration),a.setAttribute("fill",e.color||this.textStyle.color),a.setAttribute("dominant-baseline","text-before-edge");const r=document.createElementNS("http://www.w3.org/2000/svg","rect");r.setAttribute("fill",e.backgroundColor||this.textStyle.backgroundColor),r.setAttribute("fill-opacity",e.backgroundOpacity||this.textStyle.backgroundOpacity),r.setAttribute("rx","3");(e.text||"").split("\n").forEach(((t,e)=>{const n=document.createElementNS("http://www.w3.org/2000/svg","tspan");n.textContent=t,n.setAttribute("x",i),n.setAttribute("dy",0===e?"0":"1.2em"),a.appendChild(n)})),n.appendChild(a),this.svgOverlay.appendChild(n);try{const t=a.getBBox(),e=5;r.setAttribute("x",t.x-e),r.setAttribute("y",t.y-e),r.setAttribute("width",t.width+2*e),r.setAttribute("height",t.height+2*e)}catch(t){console.error("Error measuring text:",t),r.setAttribute("x",i-5),r.setAttribute("y",o-5),r.setAttribute("width",100),r.setAttribute("height",30)}return this.svgOverlay.removeChild(n),n.innerHTML="",n.appendChild(r),n.appendChild(a),e.clickX=i,e.clickY=o,n}destroy(){this.cleanup(),this.toolbar&&this.toolbar.parentNode&&(this.toolbar.parentNode.removeChild(this.toolbar),this.toolbar=null)}}class l{constructor(t,e,n,i){this.chartDiv=t,this.svgOverlay=e,this.coordinateConverter=n,this.onTooltipCreated=i,this.tooltipElements=new Map}createTooltipAnnotation(e,n,i,o){if(!e)return null;if((!o||isNaN(o.x)||isNaN(o.y))&&(!(o=this.coordinateConverter.screenToData(n,i))||isNaN(o.x)||isNaN(o.y)))return console.warn("Could not determine data coordinates for tooltip"),null;const s=document.createElementNS("http://www.w3.org/2000/svg","g");s.classList.add("apexstock-tooltip-annotation");const a=t.generateUniqueId("tooltip");s.dataset.tooltipId=a,s.dataset.elementId=a;const r={id:a,type:"tooltip",x:o.x,y:o.y,clickX:n,clickY:i,tooltipContent:e.innerHTML,tooltipWidth:e.offsetWidth+5,tooltipHeight:e.offsetHeight+5};return this.createSvgTooltip(s,r),this.tooltipElements.set(a,s),{element:s,data:r}}createSvgTooltip(t,e){const n=this.coordinateConverter.dataToScreen(e.x,e.y),i=n.x-e.tooltipWidth/2,o=n.y-e.tooltipHeight-10,s=this.chartDiv.classList.contains("apexstock-theme-dark")||this.chartDiv.parentNode.classList.contains("apexstock-theme-dark"),a=document.createElementNS("http://www.w3.org/2000/svg","rect");a.setAttribute("x",i),a.setAttribute("y",o),a.setAttribute("width",e.tooltipWidth),a.setAttribute("height",e.tooltipHeight),a.setAttribute("rx","3"),s?(a.setAttribute("fill","#343a40"),a.setAttribute("stroke","#495057")):(a.setAttribute("fill","white"),a.setAttribute("stroke","#e9ecef")),a.setAttribute("fill-opacity","0.75"),a.setAttribute("stroke-width","1");const r=document.createElementNS("http://www.w3.org/2000/svg","foreignObject");r.setAttribute("x",i),r.setAttribute("y",o),r.setAttribute("width",e.tooltipWidth),r.setAttribute("height",e.tooltipHeight);const l=document.createElement("div");l.style.width="100%",l.style.height="100%",l.style.padding="6px 8px",l.style.fontFamily="Helvetica, Arial, sans-serif",l.style.fontSize="12px",l.style.color=s?"#f8f9fa":"#373d3f",l.style.pointerEvents="none",l.innerHTML=e.tooltipContent,r.appendChild(l),t.appendChild(a),t.appendChild(r);const c=document.createElementNS("http://www.w3.org/2000/svg","line");c.setAttribute("x1",n.x),c.setAttribute("y1",o+e.tooltipHeight),c.setAttribute("x2",n.x),c.setAttribute("y2",n.y),c.setAttribute("stroke",s?"#6c757d":"#b0bec5"),c.setAttribute("stroke-width","1"),c.setAttribute("stroke-dasharray","2,2"),t.appendChild(c);const h=document.createElementNS("http://www.w3.org/2000/svg","circle");h.setAttribute("cx",n.x),h.setAttribute("cy",n.y),h.setAttribute("r","3"),h.setAttribute("fill",s?"#6c757d":"#b0bec5"),h.setAttribute("stroke",s?"#343a40":"white"),h.setAttribute("stroke-width","1"),t.appendChild(h),e.currentScreenX=n.x,e.currentScreenY=n.y}redrawTooltipElement(e){const n=document.createElementNS("http://www.w3.org/2000/svg","g");if(n.classList.add("apexstock-tooltip-annotation"),e.id)n.dataset.tooltipId=e.id,n.dataset.elementId=e.id;else{const i=t.generateUniqueId("tooltip");n.dataset.tooltipId=i,n.dataset.elementId=i,e.id=i}return this.coordinateConverter.refreshBounds(),this.createSvgTooltip(n,e),this.tooltipElements.set(e.id,n),n}updateTooltipPosition(t){const e=this.tooltipElements.get(t);if(!e)return!1;const n=this.getTooltipDataFromElement(e);if(!n)return!1;const i=this.redrawTooltipElement(n);return!!e.parentNode&&(e.parentNode.replaceChild(i,e),!0)}getTooltipDataFromElement(t){if(!t)return null;const e=t.dataset.tooltipId||t.dataset.elementId;if(!e)return null;for(const t of this.tooltipElements.values())if(t.dataset&&(t.dataset.tooltipId===e||t.dataset.elementId===e)){const n=t.querySelector("foreignObject"),i=t.querySelector("rect"),o=t.querySelector("circle");if(!n||!i||!o)continue;const s=parseFloat(o.getAttribute("cx")),a=parseFloat(o.getAttribute("cy")),r=this.coordinateConverter.screenToData(s,a);return{id:e,type:"tooltip",x:r.x,y:r.y,clickX:s,clickY:a,tooltipContent:n.querySelector("div").innerHTML,tooltipWidth:parseFloat(i.getAttribute("width")),tooltipHeight:parseFloat(i.getAttribute("height"))}}return null}removeTooltip(t,e){if(!t)return;const n=t.dataset.tooltipId||t.dataset.elementId;if(e&&n!==e){console.warn(`Tooltip ID mismatch! Expected: ${e}, Found: ${n}`);const t=document.querySelector(`.apexstock-tooltip-annotation[data-tooltip-id="${e}"], .apexstock-tooltip-annotation[data-element-id="${e}"]`);if(t&&t.parentNode)return t.parentNode.removeChild(t),void this.tooltipElements.delete(e)}t&&t.parentNode&&t.parentNode.removeChild(t),n&&this.tooltipElements.delete(n)}removeTooltipById(t){if(!t)return!1;const e=document.querySelector(`.apexstock-tooltip-annotation[data-tooltip-id="${t}"], .apexstock-tooltip-annotation[data-element-id="${t}"]`);return!(!e||!e.parentNode)&&(e.parentNode.removeChild(e),this.tooltipElements.delete(t),!0)}cleanup(){this.tooltipElements.forEach(((t,e)=>{t&&t.parentNode&&t.parentNode.removeChild(t)})),this.tooltipElements.clear()}destroy(){this.cleanup()}}class c{constructor(t,e,n){this.chartDiv=t,this.deleteCallback=e,this.styleChangeCallback=n,this.popupElement=null,this.currentElement=null,this.currentElementData=null,this.strokeColorPicker=null,this.fillColorPicker=null,this.fillOpacitySlider=null,this.isDarkTheme=!1,this.createPopup()}createPopup(){this.popupElement=document.createElement("div"),this.popupElement.className="apexstock-element-popup",this.isDarkTheme=document.body.classList.contains("dark-mode")||null!==document.querySelector(".apexstock-theme-dark"),this.isDarkTheme&&this.popupElement.classList.add("apexstock-theme-dark");const t=document.createElement("div");t.className="apexstock-style-toolbar";const e=document.createElement("div");e.className="apexstock-color-container";const n=document.createElement("label");n.textContent="Outline",e.appendChild(n),this.strokeColorPicker=document.createElement("input"),this.strokeColorPicker.type="color",this.strokeColorPicker.className="apexstock-stroke-color-picker",this.strokeColorPicker.value="#000000",this.strokeColorPicker.addEventListener("input",(t=>{const e=this.currentElementData;e&&this.styleChangeCallback&&this.styleChangeCallback(this.currentElement,e,{stroke:t.target.value}),t.stopPropagation()})),e.appendChild(this.strokeColorPicker);const i=document.createElement("div");i.className="apexstock-color-container";const o=document.createElement("label");o.textContent="Fill",i.appendChild(o),this.fillColorPicker=document.createElement("input"),this.fillColorPicker.type="color",this.fillColorPicker.className="apexstock-fill-color-picker",this.fillColorPicker.value="#ffffff",this.fillColorPicker.addEventListener("input",(t=>{const e=this.currentElementData;e&&this.styleChangeCallback&&this.styleChangeCallback(this.currentElement,e,{fill:t.target.value}),t.stopPropagation()})),i.appendChild(this.fillColorPicker);const s=document.createElement("div");s.className="apexstock-opacity-container";const a=document.createElement("label");a.textContent="Opacity",s.appendChild(a);const r=document.createElement("div");this.fillOpacitySlider=document.createElement("input"),this.fillOpacitySlider.type="range",this.fillOpacitySlider.min="0",this.fillOpacitySlider.max="100",this.fillOpacitySlider.value="50",this.fillOpacitySlider.className="apexstock-opacity-slider",this.fillOpacitySlider.addEventListener("input",(t=>{const e=this.currentElementData;if(e&&this.styleChangeCallback){this.styleChangeCallback(this.currentElement,e,{fillOpacity:parseInt(t.target.value)/100})}t.stopPropagation()})),r.appendChild(this.fillOpacitySlider),s.appendChild(r),t.appendChild(e),t.appendChild(i),t.appendChild(s);const l=document.createElement("div");l.className="apexstock-divider";const c=document.createElement("button");c.className="apexstock-element-delete-btn",c.innerHTML="🗑 Delete",c.addEventListener("click",(t=>{"function"==typeof this.deleteCallback&&this.deleteCallback(),this.hide(),t.stopPropagation()})),this.popupElement.appendChild(t),this.popupElement.appendChild(l),this.popupElement.appendChild(c),this.chartDiv.appendChild(this.popupElement),document.addEventListener("mousedown",(t=>{this.popupElement&&"block"===this.popupElement.style.display&&!this.popupElement.contains(t.target)&&this.hide()})),this.popupElement.addEventListener("click",(t=>{t.stopPropagation()}))}configureForElement(t){if(t)switch(this.currentElementData=t,t.type){case"line":case"brush":case"highlighter":this.strokeColorPicker.value=t.color||"#000000",this.strokeColorPicker.parentElement.style.display="flex",this.fillColorPicker.parentElement.style.display="none",this.fillOpacitySlider.parentElement.parentElement.style.display="none";break;case"rectangle":case"circle":case"ellipse":this.strokeColorPicker.value=t.color||"#000000",this.fillColorPicker.value=t.fill||"#ffffff",this.fillOpacitySlider.value=Math.round(100*(t.fillOpacity||.5)),this.strokeColorPicker.parentElement.style.display="flex",this.fillColorPicker.parentElement.style.display="flex",this.fillOpacitySlider.parentElement.parentElement.style.display="flex";break;case"text":this.strokeColorPicker.value=t.color||"#000000",this.fillColorPicker.value=t.backgroundColor||"#ffffff",this.fillOpacitySlider.value=Math.round(100*(t.backgroundOpacity||.7)),this.strokeColorPicker.parentElement.style.display="flex",this.fillColorPicker.parentElement.style.display="flex",this.fillOpacitySlider.parentElement.parentElement.style.display="flex",this.strokeColorPicker.parentElement.querySelector("label").textContent="Text",this.fillColorPicker.parentElement.querySelector("label").textContent="Background";break;case"tooltip":this.strokeColorPicker.parentElement.style.display="none",this.fillColorPicker.parentElement.style.display="none",this.fillOpacitySlider.parentElement.parentElement.style.display="none"}}show(t,e,n,i){const o=this.chartDiv.getBoundingClientRect(),s=t-o.left,a=e-o.top;this.currentElement=n,this.currentElementData=i,this.configureForElement(i),this.popupElement.style.left=s+"px",this.popupElement.style.top=a+"px",this.popupElement.style.display="block";const r=this.popupElement.getBoundingClientRect();r.right>o.right&&(this.popupElement.style.left=s-r.width+"px"),r.bottom>o.bottom&&(this.popupElement.style.top=a-r.height+"px")}hide(){this.popupElement&&(this.popupElement.style.display="none",this.currentElement=null,this.currentElementData=null)}destroy(){this.popupElement&&this.popupElement.parentNode&&this.popupElement.parentNode.removeChild(this.popupElement)}}class h{constructor(t,e,n,i,o,s){this.chartEl=t,this.svgOverlay=e,this.drawingGroup=n,this.elements=i,this.redrawElements=o,this.coordinateConverter=s,this.hoveredElement=null,this.hoveredElementId=null,this.selectedElement=null,this.selectedElementId=null,this.isMoving=!1,this.moveStartX=0,this.moveStartY=0,this.elementStartX=0,this.elementStartY=0,this.activeElementForDrag=null,this.hoverOutline=null,this.selectionOutline=null,this.elementPopup=null,this.handleElementMouseOver=this.handleElementMouseOver.bind(this),this.handleElementMouseOut=this.handleElementMouseOut.bind(this),this.handleElementClick=this.handleElementClick.bind(this),this.handleElementMouseDown=this.handleElementMouseDown.bind(this),this.handleMouseDown=this.handleMouseDown.bind(this),this.handleMouseMove=this.handleMouseMove.bind(this),this.handleMouseUp=this.handleMouseUp.bind(this),this.handleKeyDown=this.handleKeyDown.bind(this),this.deleteSelectedElement=this.deleteSelectedElement.bind(this),this.createVisualElements(),this.elementPopup=new c(this.chartEl,this.deleteSelectedElement.bind(this),((t,e,n)=>{if(this.context&&"function"==typeof this.context.handleStyleChange)this.context.handleStyleChange(t,e,n);else{const t=this.elements.findIndex((t=>t.data&&t.data.id===e.id));-1!==t&&(n.stroke&&(this.elements[t].data.color=n.stroke,this.elements[t].element.setAttribute("stroke",n.stroke)),n.fill&&(this.elements[t].data.fill=n.fill,this.elements[t].element.setAttribute("fill",n.fill)),void 0!==n.fillOpacity&&(this.elements[t].data.fillOpacity=n.fillOpacity,this.elements[t].element.setAttribute("fill-opacity",n.fillOpacity)))}})),this.attachEventListeners(),this.activateInteraction(),this.ensureElementIds()}ensureElementIds(){this.elements.forEach((e=>{e.data.id||(e.data.id=t.generateUniqueId(e.data.type||"element"),e.element&&(e.element.dataset.elementId=e.data.id))}))}getElementById(t){return t?this.elements.find((e=>e.data&&e.data.id===t)):null}getElementIndexById(t){return t?this.elements.findIndex((e=>e.data&&e.data.id===t)):-1}createVisualElements(){this.hoverOutline=document.createElementNS("http://www.w3.org/2000/svg","rect"),this.hoverOutline.setAttribute("fill","none"),this.hoverOutline.setAttribute("stroke","#1E90FF"),this.hoverOutline.setAttribute("stroke-width","1"),this.hoverOutline.setAttribute("stroke-dasharray","2,2"),this.hoverOutline.setAttribute("pointer-events","none"),this.hoverOutline.style.display="none",this.drawingGroup.appendChild(this.hoverOutline),this.selectionOutline=document.createElementNS("http://www.w3.org/2000/svg","rect"),this.selectionOutline.setAttribute("fill","none"),this.selectionOutline.setAttribute("stroke","#FF4500"),this.selectionOutline.setAttribute("stroke-width","2"),this.selectionOutline.setAttribute("pointer-events","none"),this.selectionOutline.style.display="none",this.drawingGroup.appendChild(this.selectionOutline)}attachEventListeners(){document.addEventListener("keydown",this.handleKeyDown),window.addEventListener("mousemove",this.handleMouseMove),window.addEventListener("mouseup",this.handleMouseUp)}activateInteraction(){this.ensureElementIds(),window.addEventListener("mousemove",this.handleMouseMove),window.addEventListener("mouseup",this.handleMouseUp);for(const t of this.elements)t.element&&(t.data&&t.data.id&&(t.element.dataset.elementId=t.data.id),t.element.addEventListener("mouseover",this.handleElementMouseOver),t.element.addEventListener("mouseout",this.handleElementMouseOut),t.element.addEventListener("click",this.handleElementClick),t.element.addEventListener("mousedown",this.handleElementMouseDown),t.element.style.pointerEvents="all",t.element.style.cursor="move")}deactivateInteraction(){this.clearHover(),this.clearSelection(),this.elementPopup&&this.elementPopup.hide();for(const t of this.elements)t.element&&(t.element.removeEventListener("mouseover",this.handleElementMouseOver),t.element.removeEventListener("mouseout",this.handleElementMouseOut),t.element.removeEventListener("click",this.handleElementClick),t.element.removeEventListener("mousedown",this.handleElementMouseDown),t.element.style.pointerEvents="none",t.element.style.cursor="")}handleElementMouseOver(t){if(this.isMoving)return;const e=t.currentTarget,n=e.dataset.elementId;n!==this.selectedElementId&&(this.hoveredElement=e,this.hoveredElementId=n,this.updateHoverOutline())}handleElementMouseOut(){this.clearHover()}updateHoverOutline(){if(this.hoveredElement)try{const t=this.hoveredElement.getBBox();this.hoverOutline.setAttribute("x",t.x-2),this.hoverOutline.setAttribute("y",t.y-2),this.hoverOutline.setAttribute("width",t.width+4),this.hoverOutline.setAttribute("height",t.height+4),this.hoverOutline.style.display="block"}catch(t){console.error("Error updating hover outline:",t)}}clearHover(){this.hoveredElement=null,this.hoveredElementId=null,this.hoverOutline.style.display="none"}handleElementMouseDown(t){const e=t.currentTarget,n=e.dataset.elementId;if(!n)return void console.warn("Clicked element has no ID:",e);if(n===this.selectedElementId)return void this.handleMouseDown(t);this.elementPopup&&this.elementPopup.hide(),this.activeElementForDrag=e;const i=this.svgOverlay.getBoundingClientRect();this.moveStartX=t.clientX-i.left,this.moveStartY=t.clientY-i.top;const o=this.getElementById(n);o&&o.data?(this.storeElementStartPosition(o.data),this.isMoving=!0,t.stopPropagation(),t.preventDefault()):console.warn("Could not find element data for ID:",n)}handleElementClick(t){if(this.isMoving)return void t.stopPropagation();const e=t.currentTarget,n=e.dataset.elementId;if(n){if(n!==this.selectedElementId){if(this.selectedElement&&this.clearSelection(),this.selectedElement=e,this.selectedElementId=n,this.updateSelectionOutline(),this.elementPopup){const e=this.getElementById(n)?.data;this.elementPopup.show(t.clientX+10,t.clientY-10,this.selectedElement,e)}t.stopPropagation()}}else console.warn("Clicked element has no ID:",e)}updateSelectionOutline(){if(this.selectedElement)try{const t=this.selectedElement.getBBox();this.selectionOutline.setAttribute("x",t.x-2),this.selectionOutline.setAttribute("y",t.y-2),this.selectionOutline.setAttribute("width",t.width+4),this.selectionOutline.setAttribute("height",t.height+4),this.selectionOutline.style.display="block"}catch(t){console.error("Error updating selection outline:",t)}}clearSelection(){this.selectedElement=null,this.selectedElementId=null,this.selectionOutline.style.display="none",this.elementPopup&&this.elementPopup.hide()}handleMouseDown(t){if(!this.selectedElement||!this.selectedElementId)return;const e=this.svgOverlay.getBoundingClientRect();this.moveStartX=t.clientX-e.le