UNPKG

@suntree/lwc-plugin-rectangle

Version:

282 lines (281 loc) 7.64 kB
var x = Object.defineProperty; var w = (s, e, t) => e in s ? x(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t; var o = (s, e, t) => (w(s, typeof e != "symbol" ? e + "" : e, t), t); import { isBusinessDay as m } from "lightweight-charts"; function c(s, e, t) { const i = Math.round(t * s), r = Math.round(t * e); return { position: Math.min(i, r), length: Math.abs(r - i) + 1 }; } class f { constructor(e, t, i, r) { o(this, "_p1"); o(this, "_p2"); o(this, "_options"); o(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 = 0.5; const r = c( 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 u { constructor(e, t) { o(this, "_source"); o(this, "_p1", null); o(this, "_p2", null); o(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 V extends u { getPoints() { const e = this._source.series, t = e.priceToCoordinate(this._source.p1.price), i = e.priceToCoordinate(this._source.p2.price); return [t, i]; } } class g extends u { 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 d { constructor(e, t) { o(this, "_source"); o(this, "_p"); o(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 l extends d { update() { const e = this._source.chart.timeScale(); this._pos = e.timeToCoordinate(this._p.time); } text() { return this._source.options.timeLabelFormatter(this._p.time); } } class p extends d { update() { const e = this._source.series; this._pos = e.priceToCoordinate(this._p.price); } text() { return this._source.options.priceLabelFormatter(this._p.price); } } const b = { //* Define the default values for all the primitive options. 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: (s) => s.toFixed(2), timeLabelFormatter: (s) => typeof s == "string" ? s : (m(s) ? new Date(s.year, s.month, s.day) : new Date(s * 1e3)).toLocaleDateString() }; class A { constructor(e, t, i) { o(this, "_p1"); o(this, "_p2"); o(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 = c( this._p1.x, this._p2.x, t.horizontalPixelRatio ), n = c( 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, n.position, r.length, n.length )), this._options.fillColor && (i.fillStyle = this._options.fillColor ?? "rgba(0,0,0,0)", i.fillRect( r.position, n.position, r.length, n.length )); }); } } class C { constructor(e) { o(this, "_source"); o(this, "_p1", { x: null, y: null }); o(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(), n = r.timeToCoordinate(this._source.p1.time), a = r.timeToCoordinate(this._source.p2.time); this._p1 = { x: n, y: t }, this._p2 = { x: a, y: i }; } renderer() { return new A(this._p1, this._p2, this._source.options); } } function _(s) { if (s === void 0) throw new Error("Value is undefined"); return s; } class P { constructor() { o(this, "_chart"); o(this, "_series"); o(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 _(this._chart); } get series() { return _(this._series); } _fireDataUpdated(e) { this.dataUpdated && this.dataUpdated(e); } } class v extends P { constructor(t, i, r = {}) { super(); o(this, "_options"); o(this, "_p1"); o(this, "_p2"); o(this, "_paneViews"); o(this, "_timeAxisViews"); o(this, "_priceAxisViews"); o(this, "_priceAxisPaneViews"); o(this, "_timeAxisPaneViews"); this._p1 = t, this._p2 = i, this._options = { ...b, ...r }, this._paneViews = [new C(this)], this._timeAxisViews = [ new l(this, t), new l(this, i) ], this._priceAxisViews = [ new p(this, t), new p(this, i) ], this._priceAxisPaneViews = [new V(this, !0)], this._timeAxisPaneViews = [new g(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 n = this.chart.timeScale(), a = n.timeToCoordinate(t); if (a === null) return !1; const h = n.coordinateToLogical(a); return h === null ? !1 : h <= r && h >= 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; } } export { v as Rectangle };