@suntree/lwc-plugin-rectangle
Version:
2 lines (1 loc) • 6.13 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.Rectangle={},n.LightweightCharts))})(this,function(n,a){"use strict";var y=Object.defineProperty;var R=(n,a,h)=>a in n?y(n,a,{enumerable:!0,configurable:!0,writable:!0,value:h}):n[a]=h;var s=(n,a,h)=>(R(n,typeof a!="symbol"?a+"":a,h),h);function h(o,e,t){const i=Math.round(t*o),r=Math.round(t*e);return{position:Math.min(i,r),length:Math.abs(r-i)+1}}class f{constructor(e,t,i,r){s(this,"_p1");s(this,"_p2");s(this,"_options");s(this,"_vertical",!1);this._p1=e,this._p2=t,this._options=r,this._vertical=i}draw(e){e.useBitmapCoordinateSpace(t=>{if(this._p1===null||this._p2===null||!this._options.showAxis)return;const i=t.context;i.globalAlpha=.5;const r=h(this._p1,this._p2,this._vertical?t.verticalPixelRatio:t.horizontalPixelRatio);i.fillStyle=this._options.fillColor??"rgba(0,0,0,0.1)",this._vertical?i.fillRect(0,r.position,15,r.length):i.fillRect(r.position,0,r.length,15)})}}class _{constructor(e,t){s(this,"_source");s(this,"_p1",null);s(this,"_p2",null);s(this,"_vertical",!1);this._source=e,this._vertical=t}update(){[this._p1,this._p2]=this.getPoints()}renderer(){return new f(this._p1,this._p2,this._vertical,this._source.options)}zOrder(){return"bottom"}}class g extends _{getPoints(){const e=this._source.series,t=e.priceToCoordinate(this._source.p1.price),i=e.priceToCoordinate(this._source.p2.price);return[t,i]}}class m extends _{getPoints(){const e=this._source.chart.timeScale(),t=e.timeToCoordinate(this._source.p1.time),i=e.timeToCoordinate(this._source.p2.time);return[t,i]}}class u{constructor(e,t){s(this,"_source");s(this,"_p");s(this,"_pos",null);this._source=e,this._p=t}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(e){this._p=e,this.update()}}class d extends u{update(){const e=this._source.chart.timeScale();this._pos=e.timeToCoordinate(this._p.time)}text(){return this._source.options.timeLabelFormatter(this._p.time)}}class x extends u{update(){const e=this._source.series;this._pos=e.priceToCoordinate(this._p.price)}text(){return this._source.options.priceLabelFormatter(this._p.price)}}const V={borderColor:"rgba(200, 50, 100, 0.75)",showAxis:!1,borderWidth:1,fillColor:"rgba(200, 50, 100, 0.25)",labelColor:"rgba(200, 50, 100, 1)",labelTextColor:"white",showLabels:!1,priceLabelFormatter:o=>o.toFixed(2),timeLabelFormatter:o=>typeof o=="string"?o:(a.isBusinessDay(o)?new Date(o.year,o.month,o.day):new Date(o*1e3)).toLocaleDateString()};class b{constructor(e,t,i){s(this,"_p1");s(this,"_p2");s(this,"_options");this._p1=e,this._p2=t,this._options=i}draw(e){e.useBitmapCoordinateSpace(t=>{if(this._p1.x===null||this._p1.y===null||this._p2.x===null||this._p2.y===null)return;const i=t.context,r=h(this._p1.x,this._p2.x,t.horizontalPixelRatio),c=h(this._p1.y,this._p2.y,t.verticalPixelRatio);this._options.borderColor&&(i.strokeStyle=this._options.borderColor,i.lineWidth=this._options.borderWidth??2,i.strokeRect(r.position,c.position,r.length,c.length)),this._options.fillColor&&(i.fillStyle=this._options.fillColor??"rgba(0,0,0,0)",i.fillRect(r.position,c.position,r.length,c.length))})}}class A{constructor(e){s(this,"_source");s(this,"_p1",{x:null,y:null});s(this,"_p2",{x:null,y:null});this._source=e}update(){const e=this._source.series,t=e.priceToCoordinate(this._source.p1.price),i=e.priceToCoordinate(this._source.p2.price),r=this._source.chart.timeScale(),c=r.timeToCoordinate(this._source.p1.time),l=r.timeToCoordinate(this._source.p2.time);this._p1={x:c,y:t},this._p2={x:l,y:i}}renderer(){return new b(this._p1,this._p2,this._source.options)}}function w(o){if(o===void 0)throw new Error("Value is undefined");return o}class C{constructor(){s(this,"_chart");s(this,"_series");s(this,"_requestUpdate")}requestUpdate(){this._requestUpdate&&this._requestUpdate()}attached({chart:e,series:t,requestUpdate:i}){this._chart=e,this._series=t,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 w(this._chart)}get series(){return w(this._series)}_fireDataUpdated(e){this.dataUpdated&&this.dataUpdated(e)}}class P extends C{constructor(t,i,r={}){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=t,this._p2=i,this._options={...V,...r},this._paneViews=[new A(this)],this._timeAxisViews=[new d(this,t),new d(this,i)],this._priceAxisViews=[new x(this,t),new x(this,i)],this._priceAxisPaneViews=[new g(this,!0)],this._timeAxisPaneViews=[new m(this,!1)]}updateAllViews(){this._paneViews.forEach(t=>t.update()),this._timeAxisViews.forEach(t=>t.update()),this._priceAxisViews.forEach(t=>t.update()),this._priceAxisPaneViews.forEach(t=>t.update()),this._timeAxisPaneViews.forEach(t=>t.update())}priceAxisViews(){return this._priceAxisViews}timeAxisViews(){return this._timeAxisViews}paneViews(){return this._paneViews}priceAxisPaneViews(){return this._priceAxisPaneViews}timeAxisPaneViews(){return this._timeAxisPaneViews}autoscaleInfo(t,i){return this._timeCurrentlyVisible(this.p1.time,t,i)||this._timeCurrentlyVisible(this.p2.time,t,i)?{priceRange:{minValue:Math.min(this.p1.price,this.p2.price),maxValue:Math.max(this.p1.price,this.p2.price)}}:null}dataUpdated(t){}_timeCurrentlyVisible(t,i,r){const c=this.chart.timeScale(),l=c.timeToCoordinate(t);if(l===null)return!1;const p=c.coordinateToLogical(l);return p===null?!1:p<=r&&p>=i}get options(){return this._options}applyOptions(t){this._options={...this._options,...t},this.requestUpdate()}get p1(){return this._p1}get p2(){return this._p2}}n.Rectangle=P,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});