| 1 |
|
−// Drawing manager: tool state machine (click-click or drag to create), selection, handles, move/resize, |
| 2 |
|
−// undo/redo (JSON snapshots), rendering on the main pane overlay. |
| 3 |
|
− |
| 4 |
|
−import { TOOLS, POINT_COUNT, FIB_LEVELS, newDrawing, serialize, deserialize } from './model.js' |
| 5 |
|
−import { hitDrawing, extendLine, HANDLE_R } from './geometry.js' |
| 6 |
|
−import { crisp, withAlpha, roundRect, alignFor, hair } from '../render/canvas.js' |
| 7 |
|
−import { font, pill, measure, inkFor } from '../render/text.js' |
|
1 |
+// Drawing manager: tool state machine (click-click, drag, or multi-click creation), selection (multi with Shift), |
|
2 |
+// handles, move/resize, Alt+drag duplication, keyboard nudging, per-pane drawings, styles, text editing events, |
|
3 |
+// undo/redo (JSON snapshots), copy/paste, rendering on each pane's overlay. |
|
4 |
+ |
|
5 |
+import { TOOLS, POINT_COUNT, CREATE_CLICKS, MULTI_MODE, TEXT_TOOLS, PANE_TOOLS, newDrawing, serialize, deserialize, newId } from './model.js' |
|
6 |
+import { hitDrawing, primitives, extendLine, HANDLE_R, fmtRatio } from './geometry.js' |
|
7 |
+import { crisp, withAlpha, roundRect, alignFor, hair, snap } from '../render/canvas.js' |
|
8 |
+import { font, pill, measure, inkFor, textY } from '../render/text.js' |
| 8 |
9 |
import { formatPercent } from '../format/number.js' |
| 9 |
10 |
import { fmtDuration, fmtFull } from '../format/time.js' |
|
11 |
+import { rollingLinReg } from '../../indicators/util.js' |
| 10 |
12 |
|
| 11 |
13 |
const MAX_HISTORY = 100 |
| 12 |
14 |
|
| 15 |
17 |
this.chart = chart |
| 16 |
18 |
this.list = [] |
| 17 |
19 |
this.tool = null |
| 18 |
|
− this.creating = null // { type, points: [{t, price}], preview: {t, price} | null } |
| 19 |
|
− this.selectedId = null |
|
20 |
+ this.creating = null // { type, points: [{t, price}], preview, pane, downX, downY, dragging } |
|
21 |
+ this.selected = new Set() |
| 20 |
22 |
this.hoverId = null |
| 21 |
|
− this.drag = null // { id, part, index, startX, startY, orig } |
|
23 |
+ this.drag = null // { ids, part, index, hitId, startX, startY, orig: Map<id, points>, pane, snapshot, moved } |
| 22 |
24 |
this.undoStack = [] |
| 23 |
25 |
this.redoStack = [] |
| 24 |
26 |
this._textW = 40 |
|
27 |
+ this.clipboard = null |
| 25 |
28 |
} |
| 26 |
29 |
|
| 27 |
|
− /* ─── coordinates ─── */ |
|
30 |
+ /** Back-compat: first selected id (or null). */ |
|
31 |
+ get selectedId() { return this.selected.size ? this.selected.values().next().value : null } |
|
32 |
+ set selectedId(v) { this.selected = new Set(v ? [v] : []) } |
|
33 |
+ |
|
34 |
+ /* ─── panes & coordinates ─── */ |
|
35 |
+ |
|
36 |
+ paneKey(pane) { return !pane || pane.kind === 'main' ? 'main' : pane.id } |
|
37 |
+ paneOf(d) { const c = this.chart; if (!d.pane) return c.mainPane; return c.panes.find(p => p.id === d.pane) || null } |
|
38 |
+ inPane(d, pane) { return (d.pane || 'main') === this.paneKey(pane) } |
| 28 |
39 |
|
| 29 |
|
− toPixel(p) { |
|
40 |
+ toPixel(p, pane) { |
| 30 |
41 |
const c = this.chart |
| 31 |
42 |
const i = c.store.indexOfTime(p.t) |
| 32 |
|
− return { x: c.ts.x(i), y: c.mainPane.scale.y(p.price) } |
|
43 |
+ return { x: c.ts.x(i), y: (pane || c.mainPane).scale.y(p.price) } |
| 33 |
44 |
} |
| 34 |
45 |
|
| 35 |
|
− fromPixel(x, y, snap = true) { |
|
46 |
+ fromPixel(x, y, pane, snapOn = true) { |
| 36 |
47 |
const c = this.chart |
| 37 |
48 |
const idx = c.ts.indexAt(x) |
| 38 |
49 |
const t = c.store.timeAtIndex(idx) |
| 39 |
|
− const price = snap ? c.snapPrice(x, y) : c.mainPane.scale.priceAt(y) |
|
50 |
+ const main = !pane || pane.kind === 'main' |
|
51 |
+ const price = main && snapOn ? c.snapPrice(x, y) : (pane || c.mainPane).scale.priceAt(y) |
| 40 |
52 |
return { t: t == null ? 0 : t, price } |
| 41 |
53 |
} |
| 42 |
54 |
|
|
55 |
+ isVisible(d) { |
|
56 |
+ if (d.visible === false) return false |
|
57 |
+ if (d.timeframes && d.timeframes.length && !d.timeframes.includes(this.chart.opts.timeframe)) return false |
|
58 |
+ return true |
|
59 |
+ } |
|
60 |
+ |
| 43 |
61 |
/* ─── public API ─── */ |
| 44 |
62 |
|
| 45 |
63 |
setTool(tool) { |
| 46 |
64 |
if (tool != null && !TOOLS.includes(tool)) throw new Error(`Unknown drawing tool: ${tool}`) |
| 47 |
65 |
this.tool = tool |
| 48 |
66 |
this.creating = null |
| 49 |
|
− if (tool) this.selectedId = null |
|
67 |
+ if (tool) this._select([]) |
| 50 |
68 |
this.chart.emitter.emit('toolChange', tool) |
| 51 |
69 |
this.chart.invalidate('overlay') |
| 52 |
70 |
} |
| 53 |
71 |
|
| 54 |
72 |
toJSON() { return serialize(this.list) } |
|
73 |
+ getById(id) { return this.list.find(d => d.id === id) || null } |
|
74 |
+ getDrawing(id) { const d = this.getById(id); return d ? serialize([d])[0] : null } |
| 55 |
75 |
|
| 56 |
76 |
fromJSON(list) { |
| 57 |
77 |
this._push() |
| 58 |
78 |
this.list = deserialize(list) |
| 59 |
|
− this.selectedId = null |
|
79 |
+ this._select([]) |
| 60 |
80 |
this._changed() |
| 61 |
81 |
} |
| 62 |
82 |
|
| 64 |
84 |
if (!this.list.length) return |
| 65 |
85 |
this._push() |
| 66 |
86 |
this.list = [] |
| 67 |
|
− this.selectedId = null |
|
87 |
+ this._select([]) |
| 68 |
88 |
this._changed() |
| 69 |
89 |
} |
| 70 |
90 |
|
| 71 |
91 |
deleteSelected() { |
| 72 |
|
− if (!this.selectedId) return |
| 73 |
|
− const d = this.list.find(x => x.id === this.selectedId) |
| 74 |
|
− if (!d || d.locked) return |
|
92 |
+ const ids = Array.from(this.selected).filter(id => { const d = this.getById(id); return d && !d.locked }) |
|
93 |
+ if (!ids.length) return |
|
94 |
+ this._push() |
|
95 |
+ this.list = this.list.filter(d => !ids.includes(d.id)) |
|
96 |
+ this._select([]) |
|
97 |
+ this._changed() |
|
98 |
+ } |
|
99 |
+ |
|
100 |
+ removeDrawing(id) { |
|
101 |
+ if (!this.getById(id)) return |
| 75 |
102 |
this._push() |
| 76 |
|
− this.list = this.list.filter(x => x.id !== this.selectedId) |
| 77 |
|
− this.selectedId = null |
|
103 |
+ this.list = this.list.filter(d => d.id !== id) |
|
104 |
+ this.selected.delete(id) |
| 78 |
105 |
this._changed() |
| 79 |
106 |
} |
| 80 |
107 |
|
| 82 |
109 |
if (!this.undoStack.length) return |
| 83 |
110 |
this.redoStack.push(JSON.stringify(serialize(this.list))) |
| 84 |
111 |
this.list = deserialize(JSON.parse(this.undoStack.pop())) |
| 85 |
|
− this.selectedId = null |
|
112 |
+ this._select([]) |
| 86 |
113 |
this._changed() |
| 87 |
114 |
} |
| 88 |
115 |
|
| 90 |
117 |
if (!this.redoStack.length) return |
| 91 |
118 |
this.undoStack.push(JSON.stringify(serialize(this.list))) |
| 92 |
119 |
this.list = deserialize(JSON.parse(this.redoStack.pop())) |
| 93 |
|
− this.selectedId = null |
|
120 |
+ this._select([]) |
| 94 |
121 |
this._changed() |
| 95 |
122 |
} |
| 96 |
123 |
|
| 97 |
124 |
escape() { |
| 98 |
125 |
if (this.creating) { this.creating = null; this.chart.invalidate('overlay'); return } |
| 99 |
126 |
if (this.tool) { this.setTool(null); return } |
| 100 |
|
− if (this.selectedId) { this.selectedId = null; this.chart.invalidate('overlay') } |
|
127 |
+ if (this.selected.size) this._select([]) |
|
128 |
+ } |
|
129 |
+ |
|
130 |
+ /** Finish a multi-click drawing (path) — also triggered by double-click and Enter. */ |
|
131 |
+ finish() { |
|
132 |
+ const cr = this.creating |
|
133 |
+ if (!cr || MULTI_MODE[cr.type] !== 'click') return false |
|
134 |
+ if (cr.points.length >= 2) this._commitCreating(); else this.creating = null |
|
135 |
+ this.chart.invalidate('overlay') |
|
136 |
+ return true |
|
137 |
+ } |
|
138 |
+ |
|
139 |
+ select(ids) { |
|
140 |
+ const arr = ids == null ? [] : Array.isArray(ids) ? ids : [ids] |
|
141 |
+ this._select(arr.filter(id => this.getById(id))) |
|
142 |
+ } |
|
143 |
+ getSelected() { return Array.from(this.selected) } |
|
144 |
+ |
|
145 |
+ getStyle(id) { const d = this.getById(id); return d ? { ...d.style, text: d.text, locked: d.locked, visible: d.visible !== false, timeframes: d.timeframes ? d.timeframes.slice() : null, pane: d.pane || 'main' } : null } |
|
146 |
+ |
|
147 |
+ setStyle(id, style = {}) { |
|
148 |
+ const d = this.getById(id) |
|
149 |
+ if (!d) return |
|
150 |
+ this._push() |
|
151 |
+ const { text, locked, visible, timeframes, ...rest } = style |
|
152 |
+ d.style = { ...d.style, ...rest } |
|
153 |
+ if (d.style.dash != null && !Array.isArray(d.style.dash)) d.style.dash = d.style.dash === 'dashed' ? [6, 4] : d.style.dash === 'dotted' ? [2, 3] : null |
|
154 |
+ if (!(d.style.width > 0)) d.style.width = 1 |
|
155 |
+ if (text !== undefined) d.text = text || undefined |
|
156 |
+ if (locked !== undefined) d.locked = !!locked |
|
157 |
+ if (visible !== undefined) d.visible = visible !== false |
|
158 |
+ if (timeframes !== undefined) d.timeframes = Array.isArray(timeframes) && timeframes.length ? timeframes.slice() : undefined |
|
159 |
+ this._changed() |
|
160 |
+ } |
|
161 |
+ |
|
162 |
+ setText(id, text) { const d = this.getById(id); if (!d) return; this._push(); d.text = text || undefined; this._changed() } |
|
163 |
+ setLocked(id, locked) { this.setStyle(id, { locked }) } |
|
164 |
+ setVisible(id, visible) { this.setStyle(id, { visible }) } |
|
165 |
+ setTimeframes(id, tfs) { this.setStyle(id, { timeframes: tfs }) } |
|
166 |
+ |
|
167 |
+ /** Duplicate a drawing (offset by `bars` bars). Returns the new id. */ |
|
168 |
+ duplicate(id, bars = 5) { |
|
169 |
+ const d = this.getById(id) |
|
170 |
+ if (!d) return null |
|
171 |
+ this._push() |
|
172 |
+ const copy = this._clone(d, bars) |
|
173 |
+ this.list.push(copy) |
|
174 |
+ this._select([copy.id]) |
|
175 |
+ this._changed() |
|
176 |
+ return copy.id |
|
177 |
+ } |
|
178 |
+ |
|
179 |
+ _clone(d, bars = 0) { |
|
180 |
+ const c = this.chart |
|
181 |
+ const copy = deserialize([serialize([d])[0]])[0] |
|
182 |
+ copy.id = newId() |
|
183 |
+ if (bars) copy.points = copy.points.map(p => { const t = c.store.timeAtIndex(c.store.indexOfTime(p.t) + bars); return { t: t == null ? p.t : t, price: p.price } }) |
|
184 |
+ return copy |
|
185 |
+ } |
|
186 |
+ |
|
187 |
+ /** JSON of the selected drawings (also kept in an internal clipboard). */ |
|
188 |
+ copy() { |
|
189 |
+ const ids = Array.from(this.selected) |
|
190 |
+ if (!ids.length) return null |
|
191 |
+ this.clipboard = serialize(this.list.filter(d => ids.includes(d.id))) |
|
192 |
+ return JSON.parse(JSON.stringify(this.clipboard)) |
|
193 |
+ } |
|
194 |
+ |
|
195 |
+ /** Paste drawings from JSON (or the internal clipboard); new ids, shifted by `bars`. Returns the new ids. */ |
|
196 |
+ paste(json, bars = 5) { |
|
197 |
+ const src = deserialize(json || this.clipboard || []) |
|
198 |
+ if (!src.length) return [] |
|
199 |
+ this._push() |
|
200 |
+ const added = src.map(d => this._clone(d, bars)) |
|
201 |
+ this.list.push(...added) |
|
202 |
+ this._select(added.map(d => d.id)) |
|
203 |
+ this._changed() |
|
204 |
+ return added.map(d => d.id) |
|
205 |
+ } |
|
206 |
+ |
|
207 |
+ /** Move the selection by whole bars (dx) and price ticks (dy, one tick = 10^-decimals or minMove). */ |
|
208 |
+ nudge(dxBars, dyTicks) { |
|
209 |
+ const ids = Array.from(this.selected).filter(id => { const d = this.getById(id); return d && !d.locked }) |
|
210 |
+ if (!ids.length) return false |
|
211 |
+ const c = this.chart |
|
212 |
+ this._push() |
|
213 |
+ for (const id of ids) { |
|
214 |
+ const d = this.getById(id) |
|
215 |
+ const pane = this.paneOf(d) || c.mainPane |
|
216 |
+ const tick = pane.kind === 'main' ? (c.opts.priceFormat.minMove || Math.pow(10, -c.decimals)) : Math.pow(10, -(pane.decimals || 2)) |
|
217 |
+ d.points = d.points.map(p => { const t = dxBars ? c.store.timeAtIndex(c.store.indexOfTime(p.t) + dxBars) : p.t; return { t: t == null ? p.t : t, price: p.price + dyTicks * tick } }) |
|
218 |
+ } |
|
219 |
+ this._changed() |
|
220 |
+ return true |
| 101 |
221 |
} |
| 102 |
222 |
|
| 103 |
223 |
onDataChange() { this.chart.invalidate('overlay') } |
| 104 |
|
− destroy() { this.list = []; this.undoStack = []; this.redoStack = [] } |
|
224 |
+ destroy() { this.list = []; this.undoStack = []; this.redoStack = []; this.selected.clear() } |
| 105 |
225 |
|
| 106 |
226 |
_push() { |
| 107 |
227 |
this.undoStack.push(JSON.stringify(serialize(this.list))) |
| 114 |
234 |
this.chart.invalidate('overlay') |
| 115 |
235 |
} |
| 116 |
236 |
|
|
237 |
+ _select(ids) { |
|
238 |
+ const next = new Set(ids) |
|
239 |
+ const same = next.size === this.selected.size && Array.from(next).every(id => this.selected.has(id)) |
|
240 |
+ this.selected = next |
|
241 |
+ this.chart.invalidate('overlay') |
|
242 |
+ if (!same) this.chart.emitter.emit('drawingSelect', this.selectedId, Array.from(next)) |
|
243 |
+ } |
|
244 |
+ |
| 117 |
245 |
/* ─── hit testing ─── */ |
| 118 |
246 |
|
| 119 |
|
− _pixels(d) { return d.points.map(p => this.toPixel(p)) } |
|
247 |
+ _pixels(d, pane) { return d.points.map(p => this.toPixel(p, pane)) } |
| 120 |
248 |
|
| 121 |
|
− hitTest(x, y) { |
| 122 |
|
− const box = { w: this.chart.plotWidth, h: this.chart.mainPane.height, textW: this._textW } |
| 123 |
|
− // Selected drawing first (its handles take priority), then top-most. |
| 124 |
|
− const order = this.list.slice().sort((a, b) => (a.id === this.selectedId ? -1 : b.id === this.selectedId ? 1 : 0)) |
|
249 |
+ _opts(d, pane) { |
|
250 |
+ const o = { style: d.style, text: d.text } |
|
251 |
+ if (d.type === 'regression') o.regression = this._regression(d, pane) |
|
252 |
+ return o |
|
253 |
+ } |
|
254 |
+ |
|
255 |
+ hitTest(x, y, pane) { |
|
256 |
+ const c = this.chart |
|
257 |
+ pane = pane || c.mainPane |
|
258 |
+ const box = { w: c.plotWidth, h: pane.height, textW: this._textW } |
|
259 |
+ // Selected drawings first (their handles take priority), then top-most. |
|
260 |
+ const order = this.list.filter(d => this.inPane(d, pane) && this.isVisible(d)).sort((a, b) => (this.selected.has(a.id) ? -1 : this.selected.has(b.id) ? 1 : 0)) |
| 125 |
261 |
for (const d of order) { |
| 126 |
|
− const hit = hitDrawing(d.type, this._pixels(d), x, y, box) |
| 127 |
|
− if (hit) { if (hit.part === 'handle' && d.id !== this.selectedId) return { id: d.id, part: 'body' }; return { id: d.id, ...hit } } |
|
262 |
+ const hit = hitDrawing(d.type, this._pixels(d, pane), x, y, box, undefined, this._opts(d, pane)) |
|
263 |
+ if (hit) { if (hit.part === 'handle' && !this.selected.has(d.id)) return { id: d.id, part: 'body' }; return { id: d.id, ...hit } } |
| 128 |
264 |
} |
| 129 |
265 |
return null |
| 130 |
266 |
} |
| 131 |
267 |
|
| 132 |
268 |
cursorAt(x, y, pane) { |
| 133 |
|
− if (pane && pane.kind !== 'main') return null |
| 134 |
269 |
if (this.drag) return this.drag.part === 'handle' ? 'grabbing' : 'move' |
| 135 |
|
− if (this.tool) return 'crosshair' |
| 136 |
|
− const h = this.hitTest(x, y) |
|
270 |
+ if (this.tool) return pane && pane.kind !== 'main' && !PANE_TOOLS.has(this.tool) ? 'not-allowed' : 'crosshair' |
|
271 |
+ const h = this.hitTest(x, y, pane) |
| 137 |
272 |
if (!h) return null |
| 138 |
273 |
return h.part === 'handle' ? 'grab' : 'pointer' |
| 139 |
274 |
} |
| 140 |
275 |
|
| 141 |
|
− /* ─── pointer state machine (coordinates relative to the main pane plot) ─── */ |
| 142 |
|
− |
| 143 |
|
− onDoubleClick(x, y, pane) { return (!pane || pane.kind === 'main') && !!this.hitTest(x, y) } |
|
276 |
+ /* ─── pointer state machine (coordinates relative to the pane's plot) ─── */ |
| 144 |
277 |
|
| 145 |
278 |
pointerDown(x, y, ev, pane) { |
| 146 |
|
− if (pane && pane.kind !== 'main') return false |
|
279 |
+ const c = this.chart |
|
280 |
+ pane = pane || c.mainPane |
| 147 |
281 |
if (this.tool) { |
| 148 |
|
− const p = this.fromPixel(x, y) |
|
282 |
+ if (pane.kind !== 'main' && !PANE_TOOLS.has(this.tool)) return false |
|
283 |
+ const p = this.fromPixel(x, y, pane) |
| 149 |
284 |
if (!this.creating) { |
| 150 |
|
− this.creating = { type: this.tool, points: [p], preview: null, downX: x, downY: y, dragging: true } |
| 151 |
|
− if (POINT_COUNT[this.tool] === 1) { this._commitCreating(); return true } |
|
285 |
+ this.creating = { type: this.tool, points: [p], preview: null, pane, downX: x, downY: y, dragging: true } |
|
286 |
+ const need = CREATE_CLICKS[this.tool] || POINT_COUNT[this.tool] |
|
287 |
+ if (need === 1) { this._commitCreating(); return true } |
|
288 |
+ return true |
|
289 |
+ } |
|
290 |
+ const cr = this.creating |
|
291 |
+ if (cr.pane !== pane) return true // a drawing lives in one pane |
|
292 |
+ if (MULTI_MODE[cr.type] === 'click') { |
|
293 |
+ // Clicking the last point again finishes the path. |
|
294 |
+ const last = this.toPixel(cr.points[cr.points.length - 1], pane) |
|
295 |
+ if (Math.hypot(last.x - x, last.y - y) < 6) { this.finish(); return true } |
|
296 |
+ cr.points.push(p); cr.preview = null |
|
297 |
+ c.invalidate('overlay') |
| 152 |
298 |
return true |
| 153 |
299 |
} |
| 154 |
|
− // Second / third click. |
| 155 |
|
− this.creating.points.push(p) |
| 156 |
|
− this.creating.dragging = true |
| 157 |
|
− this.creating.downX = x; this.creating.downY = y |
| 158 |
|
− if (this.creating.points.length >= POINT_COUNT[this.creating.type]) this._commitCreating() |
|
300 |
+ cr.points.push(p) |
|
301 |
+ cr.dragging = true |
|
302 |
+ cr.downX = x; cr.downY = y |
|
303 |
+ if (cr.points.length >= (CREATE_CLICKS[cr.type] || POINT_COUNT[cr.type])) this._commitCreating() |
| 159 |
304 |
return true |
| 160 |
305 |
} |
| 161 |
|
− const hit = this.hitTest(x, y) |
|
306 |
+ const hit = this.hitTest(x, y, pane) |
| 162 |
307 |
if (!hit) { |
| 163 |
|
− if (this.selectedId) { this.selectedId = null; this.chart.invalidate('overlay') } |
|
308 |
+ if (this.selected.size && !(ev && ev.shiftKey)) this._select([]) |
| 164 |
309 |
return false |
| 165 |
310 |
} |
| 166 |
|
− const d = this.list.find(z => z.id === hit.id) |
| 167 |
|
− this.selectedId = hit.id |
| 168 |
|
− if (!d.locked) this.drag = { id: hit.id, part: hit.part, index: hit.index, startX: x, startY: y, orig: d.points.map(p => ({ ...p })), moved: false, snapshot: JSON.stringify(serialize(this.list)) } |
| 169 |
|
− this.chart.invalidate('overlay') |
| 170 |
|
− void ev |
|
311 |
+ const d = this.getById(hit.id) |
|
312 |
+ const shift = !!(ev && ev.shiftKey), alt = !!(ev && ev.altKey) |
|
313 |
+ let ids |
|
314 |
+ if (shift && hit.part === 'body') { |
|
315 |
+ ids = Array.from(this.selected) |
|
316 |
+ if (ids.includes(hit.id)) ids = ids.filter(i => i !== hit.id); else ids.push(hit.id) |
|
317 |
+ } else if (this.selected.has(hit.id) && hit.part === 'body' && this.selected.size > 1) ids = Array.from(this.selected) |
|
318 |
+ else ids = [hit.id] |
|
319 |
+ const snapshot = JSON.stringify(serialize(this.list)) |
|
320 |
+ let hitId = hit.id |
|
321 |
+ // Alt + drag = duplicate the hit drawing and drag the copy. |
|
322 |
+ if (alt && !d.locked) { |
|
323 |
+ const copy = this._clone(d, 0) |
|
324 |
+ this.list.push(copy) |
|
325 |
+ ids = [copy.id]; hitId = copy.id |
|
326 |
+ } |
|
327 |
+ const dragIds = ids.filter(id => { const z = this.getById(id); return z && !z.locked }) |
|
328 |
+ this._select(ids) |
|
329 |
+ if (dragIds.length) { |
|
330 |
+ const orig = new Map() |
|
331 |
+ for (const id of dragIds) orig.set(id, this.getById(id).points.map(p => ({ ...p }))) |
|
332 |
+ this.drag = { ids: dragIds, part: hit.part, index: hit.index, hitId, dup: alt, origId: hit.id, startX: x, startY: y, orig, pane, moved: false, snapshot } |
|
333 |
+ } |
|
334 |
+ c.invalidate('overlay') |
| 171 |
335 |
return true |
| 172 |
336 |
} |
| 173 |
337 |
|
| 174 |
338 |
pointerMove(x, y, ev, pane) { |
| 175 |
|
− if (pane && pane.kind !== 'main') return false |
|
339 |
+ const c = this.chart |
|
340 |
+ pane = pane || c.mainPane |
| 176 |
341 |
if (this.creating) { |
| 177 |
|
− const p = this.fromPixel(x, y) |
| 178 |
342 |
const cr = this.creating |
| 179 |
|
− if (cr.type === 'brush') { if (cr.dragging) cr.points.push(p) } |
| 180 |
|
− else cr.preview = p |
| 181 |
|
− this.chart.invalidate('overlay') |
|
343 |
+ if (cr.pane !== pane) return true |
|
344 |
+ const p = this.fromPixel(x, y, pane) |
|
345 |
+ if (cr.type === 'brush') { if (cr.dragging) cr.points.push(p) } else cr.preview = p |
|
346 |
+ c.invalidate('overlay') |
| 182 |
347 |
return true |
| 183 |
348 |
} |
| 184 |
349 |
if (this.drag) { |
| 185 |
350 |
const dg = this.drag |
| 186 |
|
− const d = this.list.find(z => z.id === dg.id) |
| 187 |
|
− if (!d) { this.drag = null; return false } |
| 188 |
351 |
if (!dg.moved && Math.hypot(x - dg.startX, y - dg.startY) < 2) return true |
| 189 |
352 |
dg.moved = true |
|
353 |
+ const ps = dg.pane.scale |
| 190 |
354 |
if (dg.part === 'handle') { |
| 191 |
|
− const p = this.fromPixel(x, y) |
| 192 |
|
− d.points[dg.index] = p |
|
355 |
+ const d = this.getById(dg.hitId) |
|
356 |
+ if (!d) { this.drag = null; return false } |
|
357 |
+ d.points[dg.index] = this.fromPixel(x, y, dg.pane) |
|
358 |
+ this._normalize(d, dg.index) |
| 193 |
359 |
} else { |
| 194 |
|
− // Move: translate every point by the pixel delta (in index/price space). |
| 195 |
|
− const c = this.chart |
|
360 |
+ // Move: translate every point of every dragged drawing by the pixel delta (index/price space). |
| 196 |
361 |
const di = (x - dg.startX) / c.ts.barSpacing |
| 197 |
|
− const ps = c.mainPane.scale |
| 198 |
362 |
const dPix = y - dg.startY |
| 199 |
|
− d.points = dg.orig.map(o => { |
| 200 |
|
− const i0 = c.store.indexOfTime(o.t) |
| 201 |
|
− const t = c.store.timeAtIndex(i0 + di) |
| 202 |
|
− const yy = ps.y(o.price) + dPix |
| 203 |
|
− return { t: t == null ? o.t : t, price: ps.priceAt(yy) } |
| 204 |
|
− }) |
|
363 |
+ for (const id of dg.ids) { |
|
364 |
+ const d = this.getById(id) |
|
365 |
+ if (!d) continue |
|
366 |
+ d.points = dg.orig.get(id).map(o => { |
|
367 |
+ const t = c.store.timeAtIndex(c.store.indexOfTime(o.t) + di) |
|
368 |
+ return { t: t == null ? o.t : t, price: ps.priceAt(ps.y(o.price) + dPix) } |
|
369 |
+ }) |
|
370 |
+ } |
| 205 |
371 |
} |
| 206 |
|
− this.chart.invalidate('overlay') |
|
372 |
+ c.invalidate('overlay') |
| 207 |
373 |
return true |
| 208 |
374 |
} |
| 209 |
|
− const h = this.hitTest(x, y) |
|
375 |
+ const h = this.hitTest(x, y, pane) |
| 210 |
376 |
const id = h ? h.id : null |
| 211 |
|
− if (id !== this.hoverId) { this.hoverId = id; this.chart.invalidate('overlay') } |
|
377 |
+ if (id !== this.hoverId) { this.hoverId = id; c.invalidate('overlay') } |
| 212 |
378 |
void ev |
| 213 |
379 |
return false |
| 214 |
380 |
} |
| 215 |
381 |
|
| 216 |
382 |
pointerUp(x, y, ev, pane) { |
| 217 |
|
− if (pane && pane.kind !== 'main') return false |
|
383 |
+ pane = pane || this.chart.mainPane |
| 218 |
384 |
if (this.creating) { |
| 219 |
385 |
const cr = this.creating |
| 220 |
386 |
if (cr.type === 'brush') { cr.dragging = false; if (cr.points.length >= 2) this._commitCreating(); else this.creating = null; return true } |
|
387 |
+ if (MULTI_MODE[cr.type] === 'click') return true |
| 221 |
388 |
const moved = Math.hypot(x - cr.downX, y - cr.downY) > 4 |
| 222 |
389 |
if (moved && cr.dragging) { |
| 223 |
|
− cr.points.push(this.fromPixel(x, y)) |
| 224 |
|
− if (cr.points.length >= POINT_COUNT[cr.type]) this._commitCreating() |
|
390 |
+ cr.points.push(this.fromPixel(x, y, cr.pane)) |
|
391 |
+ if (cr.points.length >= (CREATE_CLICKS[cr.type] || POINT_COUNT[cr.type])) this._commitCreating() |
| 225 |
392 |
else cr.preview = null |
| 226 |
393 |
} |
| 227 |
|
− cr.dragging = false |
|
394 |
+ if (this.creating) this.creating.dragging = false |
| 228 |
395 |
return true |
| 229 |
396 |
} |
| 230 |
397 |
if (this.drag) { |
| 235 |
402 |
if (this.undoStack.length > MAX_HISTORY) this.undoStack.shift() |
| 236 |
403 |
this.redoStack = [] |
| 237 |
404 |
this._changed() |
|
405 |
+ } else if (dg.dup) { |
|
406 |
+ // Alt+click without a move: drop the duplicate. |
|
407 |
+ this.list = deserialize(JSON.parse(dg.snapshot)); this._select([dg.origId]); this.chart.invalidate('overlay') |
| 238 |
408 |
} |
| 239 |
409 |
return true |
| 240 |
410 |
} |
| 241 |
411 |
return false |
| 242 |
412 |
} |
| 243 |
413 |
|
|
414 |
+ /** Double-click: finishes a path, opens text editing on text-capable drawings. Returns true when handled. */ |
|
415 |
+ onDoubleClick(x, y, pane) { |
|
416 |
+ pane = pane || this.chart.mainPane |
|
417 |
+ if (this.creating && MULTI_MODE[this.creating.type] === 'click') return this.finish() |
|
418 |
+ const hit = this.hitTest(x, y, pane) |
|
419 |
+ if (!hit) return false |
|
420 |
+ const d = this.getById(hit.id) |
|
421 |
+ if (d && TEXT_TOOLS.has(d.type) && !d.locked) this._emitTextEdit(d, pane) |
|
422 |
+ return true |
|
423 |
+ } |
|
424 |
+ |
|
425 |
+ _emitTextEdit(d, pane) { |
|
426 |
+ const c = this.chart |
|
427 |
+ const anchor = d.type === 'callout' ? d.points[1] : d.points[0] |
|
428 |
+ const px = this.toPixel(anchor, pane) |
|
429 |
+ c.emitter.emit('textEdit', { id: d.id, text: d.text || '', x: px.x + c.plotX0, y: px.y + pane.top, pane: this.paneKey(pane) }) |
|
430 |
+ } |
|
431 |
+ |
|
432 |
+ /** Keep tool-specific invariants after a handle drag (position tools share t between target and stop). */ |
|
433 |
+ _normalize(d, index) { |
|
434 |
+ if (d.type === 'long' || d.type === 'short') { |
|
435 |
+ if (index === 1) d.points[2].t = d.points[1].t |
|
436 |
+ else if (index === 2) d.points[1].t = d.points[2].t |
|
437 |
+ const e = d.points[0].price |
|
438 |
+ const isLong = d.type === 'long' |
|
439 |
+ if (isLong) { if (d.points[1].price < e) d.points[1].price = e; if (d.points[2].price > e) d.points[2].price = e } |
|
440 |
+ else { if (d.points[1].price > e) d.points[1].price = e; if (d.points[2].price < e) d.points[2].price = e } |
|
441 |
+ } |
|
442 |
+ } |
|
443 |
+ |
| 244 |
444 |
_commitCreating() { |
| 245 |
445 |
const cr = this.creating |
| 246 |
446 |
this.creating = null |
| 247 |
447 |
if (!cr) return |
| 248 |
|
− const d = newDrawing(cr.type, cr.points, { text: cr.type === 'text' ? 'Text' : undefined }) |
|
448 |
+ const c = this.chart |
|
449 |
+ let points = cr.points |
|
450 |
+ if ((cr.type === 'long' || cr.type === 'short') && points.length === 1) { |
|
451 |
+ // One click spawns entry + target (2 %) + stop (1 %) over ~20 bars. |
|
452 |
+ const e = points[0] |
|
453 |
+ const i0 = c.store.indexOfTime(e.t) |
|
454 |
+ const t1 = c.store.timeAtIndex(i0 + 20) ?? e.t |
|
455 |
+ const sgn = cr.type === 'long' ? 1 : -1 |
|
456 |
+ points = [e, { t: t1, price: e.price * (1 + sgn * 0.02) }, { t: t1, price: e.price * (1 - sgn * 0.01) }] |
|
457 |
+ } |
|
458 |
+ const d = newDrawing(cr.type, points, { text: cr.type === 'text' ? 'Text' : cr.type === 'callout' ? 'Note' : undefined, pane: this.paneKey(cr.pane) }) |
| 249 |
459 |
this._push() |
| 250 |
460 |
this.list.push(d) |
| 251 |
|
− this.selectedId = cr.type === 'measure' ? null : d.id |
| 252 |
|
− // Measure is transient: it is removed as soon as the tool is used again or deselected — keep it as a |
| 253 |
|
− // normal drawing here (callers may filter type === 'measure' before persisting). |
|
461 |
+ this._select(cr.type === 'measure' ? [] : [d.id]) |
| 254 |
462 |
this.setToolAfterCreate() |
| 255 |
463 |
this._changed() |
|
464 |
+ if (cr.type === 'text' || cr.type === 'callout') this._emitTextEdit(d, cr.pane) |
| 256 |
465 |
} |
| 257 |
466 |
|
| 258 |
467 |
setToolAfterCreate() { |
| 260 |
469 |
if (this.tool !== 'brush') { this.tool = null; this.chart.emitter.emit('toolChange', null) } |
| 261 |
470 |
} |
| 262 |
471 |
|
|
472 |
+ /* ─── regression helper (pixel-space channel from the bars between the two anchors) ─── */ |
|
473 |
+ |
|
474 |
+ _regression(d, pane) { |
|
475 |
+ const c = this.chart |
|
476 |
+ const st = c.store |
|
477 |
+ if (!st.length) return null |
|
478 |
+ const i0 = Math.max(0, Math.min(st.length - 1, Math.round(st.indexOfTime(d.points[0].t)))) |
|
479 |
+ const i1 = Math.max(0, Math.min(st.length - 1, Math.round(st.indexOfTime(d.points[1].t)))) |
|
480 |
+ const a = Math.min(i0, i1), b = Math.max(i0, i1) |
|
481 |
+ const n = b - a + 1 |
|
482 |
+ if (n < 2) return null |
|
483 |
+ const src = st.c.subarray(a, b + 1) |
|
484 |
+ const { slope, intercept } = rollingLinReg(src, n) |
|
485 |
+ const m = slope[n - 1], k = intercept[n - 1] |
|
486 |
+ let sq = 0 |
|
487 |
+ for (let j = 0; j < n; j++) { const r = src[j] - (k + m * j); sq += r * r } |
|
488 |
+ const sd = Math.sqrt(sq / n) |
|
489 |
+ const mult = d.style && d.style.mult > 0 ? d.style.mult : 2 |
|
490 |
+ const ps = (pane || c.mainPane).scale |
|
491 |
+ const x0 = c.ts.x(a), xb = c.ts.x(b) |
|
492 |
+ const x1 = d.style && d.style.extendRight ? c.plotWidth : xb |
|
493 |
+ const y0 = ps.y(k), yb = ps.y(k + m * (n - 1)) |
|
494 |
+ const slopePx = (yb - y0) / Math.max(1e-9, xb - x0) |
|
495 |
+ const run = (x1 - x0) * slopePx |
|
496 |
+ return { x0, x1, y0, y1: y0 + run, up0: ps.y(k + mult * sd), up1: ps.y(k + mult * sd) + run, lo0: ps.y(k - mult * sd), lo1: ps.y(k - mult * sd) + run, sd, slope: m } |
|
497 |
+ } |
|
498 |
+ |
| 263 |
499 |
/* ─── rendering ─── */ |
| 264 |
500 |
|
| 265 |
501 |
draw(ctx, g) { |
| 266 |
502 |
const { theme } = g |
| 267 |
|
− if (g.pane && g.pane.kind !== 'main') return |
| 268 |
|
− for (const d of this.list) this._drawOne(ctx, g, d, d.id === this.selectedId, d.id === this.hoverId) |
| 269 |
|
− if (this.creating) { |
|
503 |
+ const pane = g.pane || this.chart.mainPane |
|
504 |
+ for (const d of this.list) { |
|
505 |
+ if (!this.inPane(d, pane) || !this.isVisible(d)) continue |
|
506 |
+ this._drawOne(ctx, g, d, this.selected.has(d.id), d.id === this.hoverId, false, pane) |
|
507 |
+ } |
|
508 |
+ if (this.creating && this.creating.pane === pane) { |
| 270 |
509 |
const cr = this.creating |
| 271 |
510 |
const pts = cr.points.slice() |
| 272 |
511 |
if (cr.preview && cr.type !== 'brush') pts.push(cr.preview) |
| 273 |
512 |
if (pts.length >= 1) { |
| 274 |
513 |
const need = POINT_COUNT[cr.type] |
| 275 |
514 |
const tmp = { type: cr.type, points: pts, style: { width: 1 }, text: cr.type === 'text' ? 'Text' : undefined } |
| 276 |
|
− if (need === Infinity || pts.length >= Math.min(need, 2) || need === 1) this._drawOne(ctx, g, tmp, true, false, true) |
| 277 |
|
− else this._drawHandles(ctx, pts.map(p => this.toPixel(p)), theme) |
|
515 |
+ if (need === Infinity || pts.length >= Math.min(need, 2) || need === 1) this._drawOne(ctx, g, tmp, true, false, true, pane) |
|
516 |
+ else this._drawHandles(ctx, pts.map(p => this.toPixel(p, pane)), theme) |
| 278 |
517 |
} |
| 279 |
518 |
} |
| 280 |
519 |
} |
| 281 |
520 |
|
| 282 |
|
− _drawOne(ctx, g, d, selected, hovered, preview = false) { |
|
521 |
+ _drawOne(ctx, g, d, selected, hovered, preview, pane) { |
| 283 |
522 |
const { theme, width: W, height: H } = g |
| 284 |
|
− const pts = this._pixels(d) |
|
523 |
+ const c = this.chart |
|
524 |
+ const pts = this._pixels(d, pane) |
| 285 |
525 |
const color = (d.style && d.style.color) || theme.drawing |
| 286 |
|
− const lw = (d.style && d.style.width) || 1 |
|
526 |
+ const lwCss = (d.style && d.style.width) || 1 |
|
527 |
+ const box = { w: W, h: H, textW: this._textW } |
|
528 |
+ const o = this._opts(d, pane) |
| 287 |
529 |
ctx.save() |
| 288 |
530 |
ctx.beginPath(); ctx.rect(0, 0, W, H); ctx.clip() |
| 289 |
531 |
ctx.strokeStyle = color |
| 290 |
532 |
ctx.fillStyle = color |
| 291 |
|
− ctx.lineWidth = hovered && !selected ? lw + 1 : lw |
|
533 |
+ ctx.lineWidth = hovered && !selected ? lwCss + 1 : lwCss |
| 292 |
534 |
ctx.lineJoin = 'round'; ctx.lineCap = 'round' |
| 293 |
|
− if (d.style && d.style.dash) ctx.setLineDash(d.style.dash) |
|
535 |
+ const baseDash = (d.style && d.style.dash) || [] |
|
536 |
+ ctx.setLineDash(baseDash) |
| 294 |
537 |
if (preview) ctx.globalAlpha = 0.85 |
| 295 |
|
− const [a, b, c] = pts |
| 296 |
|
− switch (d.type) { |
| 297 |
|
− case 'trendline': case 'ray': case 'extended': { |
| 298 |
|
− const mode = d.type === 'trendline' ? 'segment' : d.type |
| 299 |
|
− const [x1, y1, x2, y2] = extendLine(a.x, a.y, b.x, b.y, W, H, mode) |
| 300 |
|
− line(ctx, x1, y1, x2, y2) |
| 301 |
|
− break |
| 302 |
|
− } |
| 303 |
|
− case 'arrow': { |
| 304 |
|
− line(ctx, a.x, a.y, b.x, b.y) |
| 305 |
|
− arrowHead(ctx, a.x, a.y, b.x, b.y, 9 + lw * 2) |
| 306 |
|
− break |
|
538 |
+ const baseAlpha = ctx.globalAlpha |
|
539 |
+ const prims = primitives(d.type, pts, box, o) |
|
540 |
+ const fmt = v => c._formatPane(pane, v) |
|
541 |
+ // Level colors for fib-like tools: one palette color per level. |
|
542 |
+ const perLevel = d.type === 'fib' || d.type === 'fib-extension' || d.type === 'fib-fan' || d.type === 'fib-arcs' || d.type === 'fib-timezones' |
|
543 |
+ for (const pr of prims) { |
|
544 |
+ if (pr.k === 'label' || pr.k === 'marker') continue |
|
545 |
+ const col = perLevel && pr.index != null ? theme.series[pr.index % theme.series.length] : pr.zone ? (pr.zone === 'profit' ? theme.up : theme.down) : color |
|
546 |
+ ctx.strokeStyle = col; ctx.fillStyle = col |
|
547 |
+ ctx.globalAlpha = baseAlpha * (pr.alpha == null ? 1 : pr.alpha) |
|
548 |
+ if (pr.dash) ctx.setLineDash(pr.dash) |
|
549 |
+ switch (pr.k) { |
|
550 |
+ case 'seg': { |
|
551 |
+ if (pr.y1 === pr.y2) { const y = alignFor(lwCss, pr.y1); strokeLine(ctx, pr.x1, y, pr.x2, y) } else if (pr.x1 === pr.x2) { const x = alignFor(lwCss, pr.x1); strokeLine(ctx, x, pr.y1, x, pr.y2) } else strokeLine(ctx, pr.x1, pr.y1, pr.x2, pr.y2) |
|
552 |
+ break |
|
553 |
+ } |
|
554 |
+ case 'ray': { const [x1, y1, x2, y2] = extendLine(pr.x1, pr.y1, pr.x2, pr.y2, W, H, 'ray'); if (y1 === y2) { const y = alignFor(lwCss, y1); strokeLine(ctx, x1, y, x2, y) } else strokeLine(ctx, x1, y1, x2, y2); break } |
|
555 |
+ case 'line': { |
|
556 |
+ if (pr.y1 === pr.y2) { const y = alignFor(lwCss, pr.y1); strokeLine(ctx, 0, y, W, y) } else if (pr.x1 === pr.x2) { const x = alignFor(lwCss, pr.x1); strokeLine(ctx, x, 0, x, H) } else { const [x1, y1, x2, y2] = extendLine(pr.x1, pr.y1, pr.x2, pr.y2, W, H, 'extended'); strokeLine(ctx, x1, y1, x2, y2) } |
|
557 |
+ break |
|
558 |
+ } |
|
559 |
+ case 'rect': { |
|
560 |
+ if (pr.fill) { ctx.globalAlpha = baseAlpha * pr.fill; ctx.fillRect(pr.x, pr.y, pr.w, pr.h); ctx.globalAlpha = baseAlpha * (pr.alpha == null ? 1 : pr.alpha) } |
|
561 |
+ if (pr.stroke) { const x = alignFor(lwCss, pr.x), y = alignFor(lwCss, pr.y); ctx.strokeRect(x, y, snap(pr.w), snap(pr.h)) } |
|
562 |
+ break |
|
563 |
+ } |
|
564 |
+ case 'poly': { |
|
565 |
+ ctx.beginPath(); pr.pts.forEach(([x, y], i) => (i ? ctx.lineTo(x, y) : ctx.moveTo(x, y))) |
|
566 |
+ if (pr.closed) ctx.closePath() |
|
567 |
+ if (pr.fill) { ctx.globalAlpha = baseAlpha * pr.fill; ctx.fill(); ctx.globalAlpha = baseAlpha } |
|
568 |
+ if (pr.stroke) { if (d.type === 'brush') ctx.lineWidth = Math.max(1.5, lwCss); ctx.stroke() } |
|
569 |
+ break |
|
570 |
+ } |
|
571 |
+ case 'ellipse': { |
|
572 |
+ ctx.beginPath(); ctx.ellipse(pr.cx, pr.cy, Math.max(0.5, pr.rx), Math.max(0.5, pr.ry), 0, 0, Math.PI * 2) |
|
573 |
+ if (pr.fill) { ctx.globalAlpha = baseAlpha * pr.fill; ctx.fill(); ctx.globalAlpha = baseAlpha } |
|
574 |
+ ctx.stroke(); break |
|
575 |
+ } |
|
576 |
+ case 'arc': { ctx.beginPath(); ctx.arc(pr.cx, pr.cy, Math.max(0.5, pr.r), pr.a0, pr.a1); ctx.stroke(); break } |
|
577 |
+ default: break |
| 307 |
578 |
} |
| 308 |
|
− case 'hline': { |
| 309 |
|
− ctx.setLineDash(d.style && d.style.dash ? d.style.dash : []) |
| 310 |
|
− line(ctx, 0, crisp(a.y), W, crisp(a.y)) |
| 311 |
|
− ctx.restore(); ctx.save() // axis label lives outside the plot clip |
| 312 |
|
− ctx.globalAlpha = preview ? 0.85 : 1 |
| 313 |
|
− pill(ctx, this.chart._formatMain(d.points[0].price), W + 4, a.y, { bg: color, color: inkFor(color), fontStr: font(theme, { mono: true, size: 11 }), h: 18, padX: 4, clampTo: { x0: W + 2, x1: W + this.chart.axisWidth - 1, y0: 0, y1: H } }) |
|
579 |
+ if (pr.dash) ctx.setLineDash(baseDash) |
|
580 |
+ } |
|
581 |
+ ctx.globalAlpha = baseAlpha |
|
582 |
+ ctx.setLineDash([]) |
|
583 |
+ ctx.strokeStyle = color; ctx.fillStyle = color; ctx.lineWidth = lwCss |
|
584 |
+ this._decorate(ctx, g, d, pts, prims, color, pane, fmt) |
|
585 |
+ ctx.restore() |
|
586 |
+ if (selected) { |
|
587 |
+ if (d.type === 'brush') { ctx.save(); ctx.strokeStyle = theme.selection; ctx.lineWidth = 6; ctx.lineJoin = 'round'; ctx.lineCap = 'round'; ctx.beginPath(); pts.forEach((p, i) => (i ? ctx.lineTo(p.x, p.y) : ctx.moveTo(p.x, p.y))); ctx.stroke(); ctx.restore() } |
|
588 |
+ else this._drawHandles(ctx, pts, theme, d.locked) |
|
589 |
+ } |
|
590 |
+ } |
|
591 |
+ |
|
592 |
+ _drawHandles(ctx, pts, theme, locked = false) { |
|
593 |
+ for (const p of pts) { |
|
594 |
+ ctx.beginPath(); ctx.arc(p.x, p.y, HANDLE_R + 4, 0, Math.PI * 2) |
|
595 |
+ ctx.fillStyle = theme.selection; ctx.fill() |
|
596 |
+ ctx.beginPath(); ctx.arc(p.x, p.y, HANDLE_R / 2 + 0.5, 0, Math.PI * 2) |
|
597 |
+ ctx.fillStyle = locked ? theme.textMuted : theme.drawingHandle; ctx.fill() |
|
598 |
+ ctx.lineWidth = 1.5; ctx.strokeStyle = theme.drawing; ctx.stroke() |
|
599 |
+ } |
|
600 |
+ } |
|
601 |
+ |
|
602 |
+ /* ─── decorations: labels, boxes, markers ─── */ |
|
603 |
+ |
|
604 |
+ _decorate(ctx, g, d, pts, prims, color, pane, fmt) { |
|
605 |
+ const { theme, width: W, height: H } = g |
|
606 |
+ const c = this.chart |
|
607 |
+ const [a, b] = pts |
|
608 |
+ const st = d.style || {} |
|
609 |
+ const showLabels = st.showLabels !== false |
|
610 |
+ const small = font(theme, { mono: true, size: 10 }) |
|
611 |
+ const tag = (text, x, y, col, align = 'left') => { |
|
612 |
+ ctx.font = small; ctx.textBaseline = 'alphabetic'; ctx.textAlign = 'left' |
|
613 |
+ const w = measure(ctx, text, small) |
|
614 |
+ let lx = align === 'right' ? x - w : align === 'center' ? x - w / 2 : x |
|
615 |
+ lx = Math.max(2, Math.min(W - w - 2, lx)) |
|
616 |
+ ctx.fillStyle = withAlpha(theme.bg, 0.8); ctx.fillRect(snap(lx) - 3, snap(y) - 7, Math.ceil(w) + 6, 14) |
|
617 |
+ ctx.fillStyle = col; ctx.fillText(text, snap(lx), textY(y, 10)) |
|
618 |
+ } |
|
619 |
+ const axisPill = (price, col) => { ctx.save(); ctx.beginPath(); ctx.rect(0, 0, W + c.axisWidth, H); ctx.clip(); pill(ctx, fmt(price), W + 4, pane.scale.y(price), { bg: col, color: inkFor(col), fontStr: font(theme, { mono: true, size: 11 }), h: 18, padX: 4, clampTo: { x0: W + 2, x1: W + c.axisWidth - 1, y0: 0, y1: H } }); ctx.restore() } |
|
620 |
+ const timeTag = (t, x) => { const txt = fmtFull(t, c.opts.timeframe, c.opts.sessionLabel); pill(ctx, txt, x + 6, 12, { bg: withAlpha(color, 0.9), color: inkFor(color), fontStr: small, h: 16, padX: 4, clampTo: { x0: 0, x1: W, y0: 0, y1: H } }) } |
|
621 |
+ switch (d.type) { |
|
622 |
+ case 'hline': case 'hray': { if (a.y >= -9 && a.y <= H + 9) axisPill(d.points[0].price, color); if (d.text) tag(d.text, d.type === 'hray' ? a.x + 6 : 6, a.y - 10, color); break } |
|
623 |
+ case 'cross': { axisPill(d.points[0].price, color); timeTag(d.points[0].t, a.x); break } |
|
624 |
+ case 'vline': timeTag(d.points[0].t, a.x); break |
|
625 |
+ case 'vrange': { |
|
626 |
+ const i0 = c.store.indexOfTime(d.points[0].t), i1 = c.store.indexOfTime(d.points[1].t) |
|
627 |
+ tag(`${Math.abs(Math.round(i1 - i0))} bars · ${fmtDuration(d.points[1].t - d.points[0].t)}`, (a.x + b.x) / 2, 14, color, 'center') |
|
628 |
+ if (d.text) tag(d.text, (a.x + b.x) / 2, 30, color, 'center') |
| 314 |
629 |
break |
| 315 |
630 |
} |
| 316 |
|
− case 'vline': { |
| 317 |
|
− line(ctx, crisp(a.x), 0, crisp(a.x), H) |
| 318 |
|
− const t = d.points[0].t |
| 319 |
|
− pill(ctx, fmtFull(t, this.chart.opts.timeframe, this.chart.opts.sessionLabel), a.x + 6, 12, { bg: withAlpha(color, 0.9), color: inkFor(color), fontStr: font(theme, { mono: true, size: 10 }), h: 16, padX: 4, clampTo: { x0: 0, x1: W, y0: 0, y1: H } }) |
|
631 |
+ case 'trendline': case 'ray': case 'extended': case 'arrow': case 'channel': case 'path': case 'rect': case 'ellipse': case 'triangle': |
|
632 |
+ if (d.type === 'arrow') arrowHead(ctx, a.x, a.y, b.x, b.y, 9 + ((st.width || 1) * 2)) |
|
633 |
+ if (d.text) tag(d.text, (a.x + (b ? b.x : a.x)) / 2, Math.min(a.y, b ? b.y : a.y) - 10, color, 'center') |
| 320 |
634 |
break |
| 321 |
|
− } |
| 322 |
|
− case 'rect': { |
| 323 |
|
− const l = Math.min(a.x, b.x), t = Math.min(a.y, b.y), w = Math.abs(b.x - a.x), h = Math.abs(b.y - a.y) |
| 324 |
|
− ctx.fillStyle = withAlpha(color, 0.12); ctx.fillRect(l, t, w, h) |
| 325 |
|
− ctx.strokeRect(crisp(l), crisp(t), Math.round(w), Math.round(h)) |
|
635 |
+ case 'fib': case 'fib-extension': { |
|
636 |
+ const levels = prims.filter(p => p.k === 'seg' && !p.guide) |
|
637 |
+ const sorted = levels.slice().sort((p, q) => p.y1 - q.y1) |
|
638 |
+ for (let i = 1; i < sorted.length; i++) { const col = theme.series[sorted[i].index % theme.series.length]; ctx.fillStyle = withAlpha(col, 0.06); ctx.fillRect(sorted[i].x1, sorted[i - 1].y1, sorted[i].x2 - sorted[i].x1, sorted[i].y1 - sorted[i - 1].y1) } |
|
639 |
+ if (!showLabels) break |
|
640 |
+ for (const pr of levels) { |
|
641 |
+ const col = theme.series[pr.index % theme.series.length] |
|
642 |
+ const price = pane.scale.priceAt(pr.y1) |
|
643 |
+ tag(`${fmtRatio(pr.level)} ${fmt(price)}`, Math.min(pr.x2 + 6, W - 120), pr.y1, col) |
|
644 |
+ } |
| 326 |
645 |
break |
| 327 |
646 |
} |
| 328 |
|
− case 'channel': { |
| 329 |
|
− line(ctx, a.x, a.y, b.x, b.y) |
| 330 |
|
− if (c) { |
| 331 |
|
− const ox = c.x - a.x, oy = c.y - a.y |
| 332 |
|
− line(ctx, a.x + ox, a.y + oy, b.x + ox, b.y + oy) |
| 333 |
|
− ctx.fillStyle = withAlpha(color, 0.10) |
| 334 |
|
− ctx.beginPath(); ctx.moveTo(a.x, a.y); ctx.lineTo(b.x, b.y); ctx.lineTo(b.x + ox, b.y + oy); ctx.lineTo(a.x + ox, a.y + oy); ctx.closePath(); ctx.fill() |
| 335 |
|
− ctx.setLineDash([3, 3]); ctx.globalAlpha *= 0.6 |
| 336 |
|
− line(ctx, a.x + ox / 2, a.y + oy / 2, b.x + ox / 2, b.y + oy / 2) |
| 337 |
|
− ctx.setLineDash([]); ctx.globalAlpha = preview ? 0.85 : 1 |
|
647 |
+ case 'fib-timezones': { if (showLabels) for (const pr of prims) if (pr.k === 'line') tag(String(pr.level), pr.x1 + 3, 10, theme.series[pr.index % theme.series.length]); break } |
|
648 |
+ case 'fib-fan': case 'gann-fan': { |
|
649 |
+ if (!showLabels) break |
|
650 |
+ for (const pr of prims) { |
|
651 |
+ if (pr.k !== 'ray') continue |
|
652 |
+ const [x1, y1, x2, y2] = extendLine(pr.x1, pr.y1, pr.x2, pr.y2, W, H, 'ray') |
|
653 |
+ const hit = borderPoint(x1, y1, x2, y2, W, H) |
|
654 |
+ if (!hit) continue |
|
655 |
+ const col = d.type === 'fib-fan' ? theme.series[pr.index % theme.series.length] : color |
|
656 |
+ tag(d.type === 'gann-fan' ? gannLabel(pr.level) : fmtRatio(pr.level), hit.x - 4, Math.max(8, Math.min(H - 8, hit.y)), col, 'right') |
| 338 |
657 |
} |
| 339 |
658 |
break |
| 340 |
659 |
} |
| 341 |
|
− case 'brush': { |
| 342 |
|
− ctx.lineWidth = Math.max(1.5, lw) |
| 343 |
|
− ctx.beginPath() |
| 344 |
|
− pts.forEach((p, i) => (i ? ctx.lineTo(p.x, p.y) : ctx.moveTo(p.x, p.y))) |
| 345 |
|
− ctx.stroke() |
|
660 |
+ case 'fib-arcs': { if (showLabels) for (const pr of prims) if (pr.k === 'arc') tag(fmtRatio(pr.level), pr.cx + Math.cos((pr.a0 + pr.a1) / 2) * pr.r, pr.cy + Math.sin((pr.a0 + pr.a1) / 2) * pr.r, theme.series[pr.index % theme.series.length], 'center'); break } |
|
661 |
+ case 'regression': { |
|
662 |
+ const rg = this._opts(d, pane).regression |
|
663 |
+ if (rg && showLabels) tag(`σ ${fmt(rg.sd)} · slope ${rg.slope >= 0 ? '+' : ''}${fmt(rg.slope)}/bar`, rg.x1 - 4, rg.up1 - 10, color, 'right') |
| 346 |
664 |
break |
| 347 |
665 |
} |
| 348 |
|
− case 'fib': this._drawFib(ctx, g, d, pts, color); break |
| 349 |
|
− case 'measure': this._drawMeasure(ctx, g, d, pts, theme); break |
|
666 |
+ case 'measure': case 'price-range': case 'date-range': case 'date-price-range': this._drawRangeBox(ctx, g, d, pts, pane, fmt); break |
|
667 |
+ case 'long': case 'short': this._drawPosition(ctx, g, d, pts, pane, fmt); break |
| 350 |
668 |
case 'text': { |
| 351 |
669 |
const text = d.text || 'Text' |
| 352 |
|
− const f = font(theme, { size: 12, weight: 500 }) |
|
670 |
+ const f = font(theme, { size: st.fontSize || 12, weight: 500 }) |
| 353 |
671 |
this._textW = measure(ctx, text, f) + 8 |
| 354 |
672 |
ctx.font = f |
| 355 |
673 |
ctx.fillStyle = withAlpha(theme.bg, 0.75) |
| 356 |
674 |
roundRect(ctx, a.x - 4, a.y - 10, this._textW, 20, 3); ctx.fill() |
| 357 |
|
− ctx.fillStyle = color |
| 358 |
|
− ctx.textBaseline = 'middle'; ctx.textAlign = 'left' |
| 359 |
|
− ctx.fillText(text, a.x, a.y + 0.5) |
|
675 |
+ ctx.fillStyle = st.textColor || color |
|
676 |
+ ctx.textBaseline = 'alphabetic'; ctx.textAlign = 'left' |
|
677 |
+ ctx.fillText(text, snap(a.x), textY(a.y, st.fontSize || 12)) |
| 360 |
678 |
break |
| 361 |
679 |
} |
| 362 |
|
− default: break |
| 363 |
|
− } |
| 364 |
|
− ctx.restore() |
| 365 |
|
− if (selected && d.type !== 'brush') this._drawHandles(ctx, pts, theme, d.locked) |
| 366 |
|
− else if (selected && d.type === 'brush') { ctx.save(); ctx.strokeStyle = theme.selection; ctx.lineWidth = 6; ctx.lineJoin = 'round'; ctx.lineCap = 'round'; ctx.beginPath(); pts.forEach((p, i) => (i ? ctx.lineTo(p.x, p.y) : ctx.moveTo(p.x, p.y))); ctx.stroke(); ctx.restore() } |
| 367 |
|
− } |
| 368 |
|
− |
| 369 |
|
− _drawHandles(ctx, pts, theme, locked = false) { |
| 370 |
|
− for (const p of pts) { |
| 371 |
|
− ctx.beginPath(); ctx.arc(p.x, p.y, HANDLE_R + 4, 0, Math.PI * 2) |
| 372 |
|
− ctx.fillStyle = theme.selection; ctx.fill() |
| 373 |
|
− ctx.beginPath(); ctx.arc(p.x, p.y, HANDLE_R / 2 + 0.5, 0, Math.PI * 2) |
| 374 |
|
− ctx.fillStyle = locked ? theme.textMuted : theme.drawingHandle; ctx.fill() |
| 375 |
|
− ctx.lineWidth = 1.5; ctx.strokeStyle = theme.drawing; ctx.stroke() |
|
680 |
+ case 'callout': { |
|
681 |
+ const text = d.text || 'Note' |
|
682 |
+ const f = font(theme, { size: st.fontSize || 12, weight: 500 }) |
|
683 |
+ const lines = text.split('\n') |
|
684 |
+ const tw = Math.max(...lines.map(l => measure(ctx, l, f))) + 16 |
|
685 |
+ const th = lines.length * 16 + 8 |
|
686 |
+ this._textW = tw |
|
687 |
+ ctx.fillStyle = withAlpha(color, 0.92) |
|
688 |
+ roundRect(ctx, b.x - 6, b.y - th / 2, tw, th, 4); ctx.fill() |
|
689 |
+ ctx.beginPath(); ctx.arc(a.x, a.y, 3, 0, Math.PI * 2); ctx.fill() |
|
690 |
+ ctx.font = f; ctx.fillStyle = st.textColor || inkFor(color); ctx.textBaseline = 'alphabetic'; ctx.textAlign = 'left' |
|
691 |
+ lines.forEach((l, i) => ctx.fillText(l, snap(b.x + 2), textY(b.y - th / 2 + 12 + i * 16, st.fontSize || 12))) |
|
692 |
+ break |
|
693 |
+ } |
|
694 |
+ case 'price-label': { |
|
695 |
+ const text = d.text ? `${d.text} ${fmt(d.points[0].price)}` : fmt(d.points[0].price) |
|
696 |
+ const f = font(theme, { mono: true, size: 11, weight: 600 }) |
|
697 |
+ const bx = pill(ctx, text, a.x + 10, a.y - 14, { bg: color, color: inkFor(color), fontStr: f, h: 20, padX: 6, clampTo: { x0: 0, x1: W, y0: 0, y1: H } }) |
|
698 |
+ ctx.fillStyle = color; ctx.beginPath(); ctx.moveTo(a.x, a.y); ctx.lineTo(bx.x + 2, bx.y + bx.h); ctx.lineTo(bx.x + 12, bx.y + bx.h); ctx.closePath(); ctx.fill() |
|
699 |
+ break |
|
700 |
+ } |
|
701 |
+ case 'flag': { |
|
702 |
+ ctx.lineWidth = 1.5; ctx.strokeStyle = color; ctx.fillStyle = color |
|
703 |
+ strokeLine(ctx, alignFor(1.5, a.x), a.y, alignFor(1.5, a.x), a.y - 22) |
|
704 |
+ ctx.beginPath(); ctx.moveTo(a.x, a.y - 22); ctx.lineTo(a.x + 14, a.y - 17); ctx.lineTo(a.x, a.y - 12); ctx.closePath(); ctx.fill() |
|
705 |
+ if (d.text) tag(d.text, a.x + 18, a.y - 17, color) |
|
706 |
+ break |
|
707 |
+ } |
|
708 |
+ case 'arrow-up': case 'arrow-down': { |
|
709 |
+ const up = d.type === 'arrow-up' |
|
710 |
+ const col = st.color || (up ? theme.up : theme.down) |
|
711 |
+ ctx.fillStyle = col |
|
712 |
+ const s = 7, k = up ? 1 : -1 |
|
713 |
+ ctx.beginPath() |
|
714 |
+ ctx.moveTo(a.x, a.y); ctx.lineTo(a.x - s, a.y + k * s * 1.5); ctx.lineTo(a.x - s / 2, a.y + k * s * 1.5); ctx.lineTo(a.x - s / 2, a.y + k * s * 2.6) |
|
715 |
+ ctx.lineTo(a.x + s / 2, a.y + k * s * 2.6); ctx.lineTo(a.x + s / 2, a.y + k * s * 1.5); ctx.lineTo(a.x + s, a.y + k * s * 1.5) |
|
716 |
+ ctx.closePath(); ctx.fill() |
|
717 |
+ if (d.text) tag(d.text, a.x, a.y + k * (s * 2.6 + 10), col, 'center') |
|
718 |
+ break |
|
719 |
+ } |
|
720 |
+ case 'elliott-impulse': case 'elliott-correction': { |
|
721 |
+ const names = d.type === 'elliott-impulse' ? ['0', '1', '2', '3', '4', '5'] : ['0', 'A', 'B', 'C'] |
|
722 |
+ pts.forEach((p, i) => { if (i < names.length) this._pointLabel(ctx, theme, pts, i, names[i], color) }) |
|
723 |
+ break |
|
724 |
+ } |
|
725 |
+ case 'xabcd': { |
|
726 |
+ const names = ['X', 'A', 'B', 'C', 'D'] |
|
727 |
+ pts.forEach((p, i) => { if (i < 5) this._pointLabel(ctx, theme, pts, i, names[i], color) }) |
|
728 |
+ if (pts.length >= 5 && showLabels) { |
|
729 |
+ const P = d.points.map(p => p.price) |
|
730 |
+ const r = (i, j, k, l) => { const den = Math.abs(P[k] - P[l]); return den ? (Math.abs(P[i] - P[j]) / den).toFixed(3) : '—' } |
|
731 |
+ tag(r(1, 2, 0, 1), (pts[0].x + pts[2].x) / 2, (pts[0].y + pts[2].y) / 2, color, 'center') // AB / XA |
|
732 |
+ tag(r(2, 3, 1, 2), (pts[1].x + pts[3].x) / 2, (pts[1].y + pts[3].y) / 2, color, 'center') // BC / AB |
|
733 |
+ tag(r(3, 4, 2, 3), (pts[2].x + pts[4].x) / 2, (pts[2].y + pts[4].y) / 2, color, 'center') // CD / BC |
|
734 |
+ tag(`AD/XA ${r(0, 4, 0, 1)}`, (pts[0].x + pts[4].x) / 2, Math.max(pts[0].y, pts[4].y) + 14, color, 'center') |
|
735 |
+ } |
|
736 |
+ break |
|
737 |
+ } |
|
738 |
+ case 'head-shoulders': { |
|
739 |
+ const names = ['', 'LS', '', 'H', '', 'RS', ''] |
|
740 |
+ pts.forEach((p, i) => { if (names[i]) this._pointLabel(ctx, theme, pts, i, names[i], color) }) |
|
741 |
+ break |
|
742 |
+ } |
|
743 |
+ default: if (st.text) tag(st.text, a.x + 4, a.y - 10, color); break |
| 376 |
744 |
} |
| 377 |
745 |
} |
| 378 |
746 |
|
| 379 |
|
− _drawFib(ctx, g, d, pts, color) { |
| 380 |
|
− const { theme, width: W } = g |
| 381 |
|
− const [a, b] = pts |
| 382 |
|
− const p0 = d.points[0].price, p1 = d.points[1].price |
| 383 |
|
− const l = Math.min(a.x, b.x), r = Math.max(a.x, b.x) |
| 384 |
|
− const ps = this.chart.mainPane.scale |
| 385 |
|
− const f = font(theme, { mono: true, size: 10 }) |
| 386 |
|
− ctx.font = f; ctx.textBaseline = 'middle' |
| 387 |
|
− const levels = d.style && d.style.extended ? [...FIB_LEVELS, 1.618] : FIB_LEVELS |
| 388 |
|
− let prevY = null |
| 389 |
|
− levels.forEach((lv, i) => { |
| 390 |
|
− const price = p0 + (p1 - p0) * lv |
| 391 |
|
− const y = ps.y(price) |
| 392 |
|
− const c = theme.series[i % theme.series.length] |
| 393 |
|
− if (prevY != null) { ctx.fillStyle = withAlpha(c, 0.06); ctx.fillRect(l, Math.min(prevY, y), r - l, Math.abs(y - prevY)) } |
| 394 |
|
− ctx.strokeStyle = withAlpha(c, 0.9); ctx.lineWidth = 1 |
| 395 |
|
− line(ctx, l, crisp(y), r, crisp(y)) |
| 396 |
|
− const label = `${lv.toFixed(3).replace(/0+$/, '').replace(/\.$/, '')} ${this.chart._formatMain(price)}` |
| 397 |
|
− const lx = Math.min(r + 6, W - 120) |
| 398 |
|
− const lw2 = measure(ctx, label, f) |
| 399 |
|
− ctx.fillStyle = withAlpha(theme.bg, 0.8) |
| 400 |
|
− ctx.fillRect(lx - 3, y - 7, lw2 + 6, 14) |
| 401 |
|
− ctx.fillStyle = c |
| 402 |
|
− ctx.textAlign = 'left' |
| 403 |
|
− ctx.fillText(label, lx, y) |
| 404 |
|
− prevY = y |
| 405 |
|
− }) |
| 406 |
|
− ctx.strokeStyle = withAlpha(color, 0.5); ctx.setLineDash([3, 3]) |
| 407 |
|
− line(ctx, a.x, a.y, b.x, b.y) |
| 408 |
|
− ctx.setLineDash([]) |
|
747 |
+ /** Small label next to a polyline vertex, placed on the outer side of the local turn. */ |
|
748 |
+ _pointLabel(ctx, theme, pts, i, text, color) { |
|
749 |
+ const p = pts[i] |
|
750 |
+ const prev = pts[i - 1] || pts[i + 1], next = pts[i + 1] || pts[i - 1] |
|
751 |
+ const above = prev && next ? p.y <= (prev.y + next.y) / 2 : true |
|
752 |
+ const f = font(theme, { size: 11, weight: 700 }) |
|
753 |
+ ctx.font = f; ctx.textAlign = 'center'; ctx.textBaseline = 'alphabetic' |
|
754 |
+ const w = measure(ctx, text, f) + 8 |
|
755 |
+ const y = above ? p.y - 18 : p.y + 8 |
|
756 |
+ ctx.fillStyle = withAlpha(theme.bg, 0.85); roundRect(ctx, p.x - w / 2, y, w, 14, 3); ctx.fill() |
|
757 |
+ ctx.fillStyle = color; ctx.fillText(text, p.x, textY(y + 7, 11)) |
|
758 |
+ ctx.textAlign = 'left' |
| 409 |
759 |
} |
| 410 |
760 |
|
| 411 |
|
− _drawMeasure(ctx, g, d, pts, theme) { |
| 412 |
|
− const { width: W, height: H } = g |
|
761 |
+ _drawRangeBox(ctx, g, d, pts, pane, fmt) { |
|
762 |
+ const { theme, width: W, height: H } = g |
|
763 |
+ const c = this.chart |
| 413 |
764 |
const [a, b] = pts |
|
765 |
+ const withPrice = d.type !== 'date-range', withTime = d.type !== 'price-range' |
| 414 |
766 |
const up = d.points[1].price >= d.points[0].price |
| 415 |
|
− const color = up ? theme.up : theme.down |
|
767 |
+ const color = (d.style && d.style.color) || (withPrice ? (up ? theme.up : theme.down) : theme.drawing) |
| 416 |
768 |
const l = Math.min(a.x, b.x), t = Math.min(a.y, b.y), w = Math.abs(b.x - a.x), h = Math.abs(b.y - a.y) |
| 417 |
|
− ctx.fillStyle = withAlpha(color, 0.12); ctx.fillRect(l, t, w, h) |
| 418 |
|
− ctx.strokeStyle = withAlpha(color, 0.8); ctx.lineWidth = 1; ctx.setLineDash([3, 3]) |
| 419 |
|
− ctx.strokeRect(crisp(l), crisp(t), Math.round(w), Math.round(h)) |
|
769 |
+ ctx.fillStyle = withAlpha(color, d.style && d.style.fill != null ? d.style.fill : 0.12); ctx.fillRect(l, t, w, h) |
|
770 |
+ ctx.strokeStyle = withAlpha(color, 0.8); ctx.lineWidth = hair(); ctx.setLineDash([3, 3]) |
|
771 |
+ ctx.strokeRect(crisp(l), crisp(t), snap(w), snap(h)) |
| 420 |
772 |
ctx.setLineDash([]) |
| 421 |
|
− // Arrow along the vertical move. |
| 422 |
|
− const mx = l + w / 2 |
| 423 |
|
− ctx.strokeStyle = color; ctx.lineWidth = 1.25 |
| 424 |
|
− line(ctx, mx, a.y, mx, b.y); arrowHead(ctx, mx, a.y, mx, b.y, 8) |
| 425 |
|
− const c = this.chart |
|
773 |
+ ctx.strokeStyle = color; ctx.lineWidth = 1.25; ctx.fillStyle = color |
|
774 |
+ const mx = l + w / 2, my = t + h / 2 |
|
775 |
+ if (withPrice) { strokeLine(ctx, mx, a.y, mx, b.y); arrowHead(ctx, mx, a.y, mx, b.y, 8) } |
|
776 |
+ if (withTime) { strokeLine(ctx, a.x, my, b.x, my); arrowHead(ctx, a.x, my, b.x, my, 8) } |
| 426 |
777 |
const dp = d.points[1].price - d.points[0].price |
| 427 |
778 |
const pct = d.points[0].price ? (dp / d.points[0].price) * 100 : 0 |
| 428 |
779 |
const i0 = c.store.indexOfTime(d.points[0].t), i1 = c.store.indexOfTime(d.points[1].t) |
| 429 |
780 |
const bars = Math.round(i1 - i0) |
| 430 |
|
− const lines = [ |
| 431 |
|
− `${dp >= 0 ? '+' : '−'}${c._formatMain(Math.abs(dp))} (${formatPercent(pct, 2, c.opts.locale)})`, |
| 432 |
|
− `${bars} bars · ${fmtDuration(d.points[1].t - d.points[0].t)}`, |
| 433 |
|
− ] |
|
781 |
+ const lines = [] |
|
782 |
+ if (withPrice) lines.push(`${dp >= 0 ? '+' : '−'}${fmt(Math.abs(dp))} (${formatPercent(pct, 2, c.opts.locale)})`) |
|
783 |
+ if (withTime) lines.push(`${bars} bars · ${fmtDuration(d.points[1].t - d.points[0].t)}`) |
|
784 |
+ if (d.text) lines.push(d.text) |
| 434 |
785 |
const f = font(theme, { mono: true, size: 11, weight: 500 }) |
| 435 |
786 |
ctx.font = f |
| 436 |
787 |
const bw = Math.max(...lines.map(s => measure(ctx, s, f))) + 16 |
| 437 |
788 |
const bh = 18 * lines.length + 6 |
| 438 |
|
− let bx = mx - bw / 2, by = up ? t - bh - 8 : t + h + 8 |
|
789 |
+ let bx = mx - bw / 2, by = withPrice ? (up ? t - bh - 8 : t + h + 8) : t + h + 8 |
| 439 |
790 |
bx = Math.max(2, Math.min(W - bw - 2, bx)); by = Math.max(2, Math.min(H - bh - 2, by)) |
| 440 |
791 |
ctx.fillStyle = color |
| 441 |
|
− roundRect(ctx, bx, by, bw, bh, 4); ctx.fill() |
| 442 |
|
− ctx.fillStyle = inkFor(color); ctx.textBaseline = 'middle'; ctx.textAlign = 'center' |
| 443 |
|
− lines.forEach((s, i) => ctx.fillText(s, bx + bw / 2, by + 3 + 9 + i * 18)) |
|
792 |
+ roundRect(ctx, snap(bx), snap(by), bw, bh, 4); ctx.fill() |
|
793 |
+ ctx.fillStyle = inkFor(color); ctx.textBaseline = 'alphabetic'; ctx.textAlign = 'center' |
|
794 |
+ lines.forEach((s, i) => ctx.fillText(s, snap(bx + bw / 2), textY(by + 3 + 9 + i * 18, 11))) |
| 444 |
795 |
ctx.textAlign = 'left' |
| 445 |
796 |
} |
|
797 |
+ |
|
798 |
+ /** Long / short position: entry line, profit & loss zones, and stats (target, stop, R:R, P/L in % and points, amount with qty). */ |
|
799 |
+ _drawPosition(ctx, g, d, pts, pane, fmt) { |
|
800 |
+ const { theme, width: W } = g |
|
801 |
+ const c = this.chart |
|
802 |
+ const [a, b, s] = pts |
|
803 |
+ const entry = d.points[0].price, target = d.points[1].price, stop = d.points[2].price |
|
804 |
+ const isLong = d.type === 'long' |
|
805 |
+ const reward = Math.abs(target - entry), risk = Math.abs(entry - stop) |
|
806 |
+ const rr = risk > 0 ? reward / risk : Infinity |
|
807 |
+ const qty = d.style && d.style.qty > 0 ? d.style.qty : null |
|
808 |
+ const x0 = Math.min(a.x, b.x), x1 = Math.max(a.x, b.x) |
|
809 |
+ const small = font(theme, { mono: true, size: 10, weight: 600 }) |
|
810 |
+ const box = (text, y, col, alignTop) => { |
|
811 |
+ ctx.font = small |
|
812 |
+ const w = measure(ctx, text, small) + 10 |
|
813 |
+ const bx = Math.max(2, Math.min(W - w - 2, (x0 + x1) / 2 - w / 2)) |
|
814 |
+ const by = alignTop ? y - 20 : y + 4 |
|
815 |
+ ctx.fillStyle = col; roundRect(ctx, snap(bx), snap(by), w, 16, 3); ctx.fill() |
|
816 |
+ ctx.fillStyle = inkFor(col); ctx.textBaseline = 'alphabetic'; ctx.textAlign = 'left'; ctx.fillText(text, snap(bx + 5), textY(by + 8, 10)) |
|
817 |
+ } |
|
818 |
+ const pctT = entry ? (reward / entry) * 100 : 0, pctS = entry ? (risk / entry) * 100 : 0 |
|
819 |
+ const amt = v => (qty ? ` · ${fmt(v * qty)}` : '') |
|
820 |
+ box(`Target ${fmt(target)} ${isLong ? '+' : '−'}${fmt(reward)} (${formatPercent(isLong ? pctT : -pctT, 2, c.opts.locale)})${amt(reward)}`, b.y, theme.up, isLong) |
|
821 |
+ box(`Stop ${fmt(stop)} ${isLong ? '−' : '+'}${fmt(risk)} (${formatPercent(isLong ? -pctS : pctS, 2, c.opts.locale)})${amt(-risk)}`, s.y, theme.down, !isLong) |
|
822 |
+ const entryText = `${isLong ? 'Long' : 'Short'} ${fmt(entry)} · R:R ${Number.isFinite(rr) ? rr.toFixed(2) : '∞'}${qty ? ` · qty ${qty}` : ''}` |
|
823 |
+ ctx.font = small |
|
824 |
+ const w = measure(ctx, entryText, small) + 10 |
|
825 |
+ const bx = Math.max(2, Math.min(W - w - 2, x0)), by = a.y - 8 |
|
826 |
+ ctx.fillStyle = theme.crosshairLabelBg; roundRect(ctx, snap(bx), snap(by), w, 16, 3); ctx.fill() |
|
827 |
+ ctx.fillStyle = theme.crosshairLabelText; ctx.textBaseline = 'alphabetic'; ctx.textAlign = 'left'; ctx.fillText(entryText, snap(bx + 5), textY(by + 8, 10)) |
|
828 |
+ } |
| 446 |
829 |
} |
| 447 |
830 |
|
| 448 |
|
−function line(ctx, x1, y1, x2, y2) { ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.stroke() } |
|
831 |
+function strokeLine(ctx, x1, y1, x2, y2) { ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.stroke() } |
| 449 |
832 |
|
| 450 |
833 |
function arrowHead(ctx, x1, y1, x2, y2, size) { |
| 451 |
834 |
const ang = Math.atan2(y2 - y1, x2 - x1) |
| 456 |
839 |
ctx.closePath() |
| 457 |
840 |
ctx.fill() |
| 458 |
841 |
} |
|
842 |
+ |
|
843 |
+/** Point where the segment (x1,y1)→(x2,y2) leaves the [0,W]×[0,H] box (or null when it never does). */ |
|
844 |
+function borderPoint(x1, y1, x2, y2, W, H) { |
|
845 |
+ const dx = x2 - x1, dy = y2 - y1 |
|
846 |
+ let best = null |
|
847 |
+ const cand = (t, x, y) => { if (t > 1e-9 && (best === null || t < best.t) && x >= -1 && x <= W + 1 && y >= -1 && y <= H + 1) best = { t, x, y } } |
|
848 |
+ if (dx !== 0) for (const X of [0, W]) { const t = (X - x1) / dx; cand(t, X, y1 + dy * t) } |
|
849 |
+ if (dy !== 0) for (const Y of [0, H]) { const t = (Y - y1) / dy; cand(t, x1 + dx * t, Y) } |
|
850 |
+ return best |
|
851 |
+} |
|
852 |
+ |
|
853 |
+function gannLabel(k) { return k >= 1 ? `${k}/1` : `1/${Math.round(1 / k)}` } |