lwc-plugin-rectangles
Version:
2 lines (1 loc) • 6.49 kB
JavaScript
(function(n,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("lightweight-charts")):typeof define=="function"&&define.amd?define(["exports","lightweight-charts"],a):(n=typeof globalThis<"u"?globalThis:n||self,a(n.Rectangles={},n.LightweightCharts))})(this,function(n,a){"use strict";var A=Object.defineProperty;var T=(n,a,h)=>a in n?A(n,a,{enumerable:!0,configurable:!0,writable:!0,value:h}):n[a]=h;var s=(n,a,h)=>(T(n,typeof a!="symbol"?a+"":a,h),h);function h(r,t,e){const i=Math.round(e*r),o=Math.round(e*t);return{position:Math.min(i,o),length:Math.abs(o-i)+1}}class w{constructor(t,e,i,o){s(this,"_p1");s(this,"_p2");s(this,"_fillColor");s(this,"_vertical",!1);this._p1=t,this._p2=e,this._fillColor=i,this._vertical=o}draw(t){t.useBitmapCoordinateSpace(e=>{if(this._p1===null||this._p2===null)return;const i=e.context;i.globalAlpha=.5;const o=h(this._p1,this._p2,this._vertical?e.verticalPixelRatio:e.horizontalPixelRatio);i.fillStyle=this._fillColor,this._vertical?i.fillRect(0,o.position,15,o.length):i.fillRect(o.position,0,o.length,15)})}}class u{constructor(t,e){s(this,"_source");s(this,"_p1",null);s(this,"_p2",null);s(this,"_vertical",!1);this._source=t,this._vertical=e}update(){[this._p1,this._p2]=this.getPoints()}renderer(){return new w(this._p1,this._p2,this._source.options.fillColor,this._vertical)}zOrder(){return"bottom"}}class m extends u{getPoints(){const t=this._source.series,e=t.priceToCoordinate(this._source.p1.price),i=t.priceToCoordinate(this._source.p2.price);return[e,i]}}class g extends u{getPoints(){const t=this._source.chart.timeScale(),e=t.timeToCoordinate(this._source.p1.time),i=t.timeToCoordinate(this._source.p2.time);return[e,i]}}class _{constructor(t,e){s(this,"_source");s(this,"_p");s(this,"_pos",null);this._source=t,this._p=e}coordinate(){return this._pos??-1}visible(){return this._source.options.showLabels}tickVisible(){return this._source.options.showLabels}textColor(){return this._source.options.labelTextColor}backColor(){return this._source.options.labelColor}movePoint(t){this._p=t,this.update()}}class d extends _{update(){const t=this._source.chart.timeScale();this._pos=t.timeToCoordinate(this._p.time)}text(){return this._source.options.timeLabelFormatter(this._p.time)}}class f extends _{update(){const t=this._source.series;this._pos=t.priceToCoordinate(this._p.price)}text(){return this._source.options.priceLabelFormatter(this._p.price)}}const y={fillColor:"rgba(200, 50, 100, 0.75)",labelColor:"rgba(200, 50, 100, 1)",labelTextColor:"white",showLabels:!0,borderType:"none",priceLabelFormatter:r=>r.toFixed(2),timeLabelFormatter:r=>typeof r=="string"?r:(a.isBusinessDay(r)?new Date(r.year,r.month,r.day):new Date(r*1e3)).toLocaleDateString()};class C{constructor(t,e,i,o){s(this,"_p1");s(this,"_p2");s(this,"_fillColor");s(this,"_borderType");this._p1=t,this._p2=e,this._fillColor=i,this._borderType=o}draw(t){t.useBitmapCoordinateSpace(e=>{if(this._p1.x===null||this._p1.y===null||this._p2.x===null||this._p2.y===null)return;const i=e.context,o=h(this._p1.x,this._p2.x,e.horizontalPixelRatio),l=h(this._p1.y,this._p2.y,e.verticalPixelRatio);i.fillStyle=this._fillColor,this._borderType==="dashed"?(i.strokeStyle="white",i.lineWidth=2,i.setLineDash([2,4]),i.strokeRect(o.position,l.position,o.length,l.length)):i.fillRect(o.position,l.position,o.length,l.length)})}}class V{constructor(t){s(this,"_source");s(this,"_p1",{x:null,y:null});s(this,"_p2",{x:null,y:null});this._source=t}update(){if(!this._source.series||!this._source.chart){console.warn("⏳ Skipping update: Series or Chart not ready!");return}const t=this._source.series,e=this._source.chart.timeScale();if(!e){console.warn("⏳ Skipping update: Time scale not ready!");return}const i=e.timeToCoordinate(this._source.p1.time)??this._p1.x,o=e.timeToCoordinate(this._source.p2.time)??this._p2.x,l=t.priceToCoordinate(this._source.p1.price)??this._p1.y,c=t.priceToCoordinate(this._source.p2.price)??this._p2.y;(i===null||o===null)&&console.warn("⚠️ timeToCoordinate returned null! p1:",this._source.p1,"p2:",this._source.p2),(l===null||c===null)&&console.warn("⚠️ priceToCoordinate returned null! p1:",this._source.p1,"p2:",this._source.p2),this._p1={x:i,y:l},this._p2={x:o,y:c}}renderer(){return new C(this._p1,this._p2,this._source.options.fillColor,this._source.options.borderType)}}function x(r){if(r===void 0)throw new Error("Value is undefined");return r}class b{constructor(){s(this,"_chart");s(this,"_series");s(this,"_requestUpdate")}requestUpdate(){this._requestUpdate&&this._requestUpdate()}attached({chart:t,series:e,requestUpdate:i}){this._chart=t,this._series=e,this._series.subscribeDataChanged(this._fireDataUpdated),this._requestUpdate=i,this.requestUpdate()}detached(){this._chart=void 0,this._series=void 0,this._requestUpdate=void 0}get chart(){return x(this._chart)}get series(){return x(this._series)}_fireDataUpdated(t){this.dataUpdated&&this.dataUpdated(t)}}class P extends b{constructor(e,i,o={}){super();s(this,"_options");s(this,"_p1");s(this,"_p2");s(this,"_paneViews");s(this,"_timeAxisViews");s(this,"_priceAxisViews");s(this,"_priceAxisPaneViews");s(this,"_timeAxisPaneViews");this._p1=e,this._p2=i;const l={...y,showLabels:!1};this._options={...l,...o},this._paneViews=[new V(this)],this._timeAxisViews=[new d(this,e),new d(this,i)],this._priceAxisViews=[new f(this,e),new f(this,i)],this._priceAxisPaneViews=[new m(this,!0)],this._timeAxisPaneViews=[new g(this,!1)]}updateAllViews(){this._paneViews.forEach(e=>e.update()),this._timeAxisViews.forEach(e=>e.update()),this._priceAxisViews.forEach(e=>e.update()),this._priceAxisPaneViews.forEach(e=>e.update()),this._timeAxisPaneViews.forEach(e=>e.update())}priceAxisViews(){return[]}timeAxisViews(){return[]}paneViews(){return this._paneViews}priceAxisPaneViews(){return[]}timeAxisPaneViews(){return[]}autoscaleInfo(e,i){return this._timeCurrentlyVisible(this.p1.time,e,i)||this._timeCurrentlyVisible(this.p2.time,e,i)?{priceRange:{minValue:Math.min(this.p1.price,this.p2.price),maxValue:Math.max(this.p1.price,this.p2.price)}}:null}dataUpdated(){}_timeCurrentlyVisible(e,i,o){const l=this.chart.timeScale(),c=l.timeToCoordinate(e);if(c===null)return!1;const p=l.coordinateToLogical(c);return p===null?!1:p<=o&&p>=i}get options(){return this._options}applyOptions(e){this._options={...this._options,...e},this.requestUpdate()}get p1(){return this._p1}get p2(){return this._p2}}n.Rectangles=P,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});