@forgecharts/sdk 1.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/package.json +50 -0
  2. package/src/__tests__/backwardCompatibility.test.ts +191 -0
  3. package/src/__tests__/candleInvariant.test.ts +500 -0
  4. package/src/__tests__/public-api-surface.ts +76 -0
  5. package/src/__tests__/timeframeBoundary.test.ts +583 -0
  6. package/src/api/DrawingManager.ts +188 -0
  7. package/src/api/EventBus.ts +53 -0
  8. package/src/api/IndicatorDAG.ts +389 -0
  9. package/src/api/IndicatorRegistry.ts +47 -0
  10. package/src/api/LayoutManager.ts +72 -0
  11. package/src/api/PaneManager.ts +129 -0
  12. package/src/api/ReferenceAPI.ts +195 -0
  13. package/src/api/TChart.ts +881 -0
  14. package/src/api/createChart.ts +43 -0
  15. package/src/api/drawing tools/fib gann menu/fibRetracement.ts +27 -0
  16. package/src/api/drawing tools/lines menu/crossLine.ts +21 -0
  17. package/src/api/drawing tools/lines menu/disjointChannel.ts +74 -0
  18. package/src/api/drawing tools/lines menu/extendedLine.ts +22 -0
  19. package/src/api/drawing tools/lines menu/flatTopBottom.ts +45 -0
  20. package/src/api/drawing tools/lines menu/horizontal.ts +24 -0
  21. package/src/api/drawing tools/lines menu/horizontalRay.ts +25 -0
  22. package/src/api/drawing tools/lines menu/infoLine.ts +127 -0
  23. package/src/api/drawing tools/lines menu/insidePitchfork.ts +21 -0
  24. package/src/api/drawing tools/lines menu/modifiedSchiffPitchfork.ts +18 -0
  25. package/src/api/drawing tools/lines menu/parallelChannel.ts +47 -0
  26. package/src/api/drawing tools/lines menu/pitchfork.ts +15 -0
  27. package/src/api/drawing tools/lines menu/ray.ts +28 -0
  28. package/src/api/drawing tools/lines menu/regressionTrend.ts +157 -0
  29. package/src/api/drawing tools/lines menu/schiffPitchfork.ts +18 -0
  30. package/src/api/drawing tools/lines menu/trendAngle.ts +64 -0
  31. package/src/api/drawing tools/lines menu/trendline.ts +16 -0
  32. package/src/api/drawing tools/lines menu/vertical.ts +16 -0
  33. package/src/api/drawing tools/pointers menu/crosshair.ts +17 -0
  34. package/src/api/drawing tools/pointers menu/cursor.ts +16 -0
  35. package/src/api/drawing tools/pointers menu/demonstration.ts +35 -0
  36. package/src/api/drawing tools/pointers menu/dot.ts +26 -0
  37. package/src/api/drawing tools/shapes menu/rectangle.ts +24 -0
  38. package/src/api/drawing tools/shapes menu/text.ts +30 -0
  39. package/src/api/drawingUtils.ts +82 -0
  40. package/src/core/CanvasLayer.ts +77 -0
  41. package/src/core/Chart.ts +917 -0
  42. package/src/core/CoordTransform.ts +282 -0
  43. package/src/core/Crosshair.ts +207 -0
  44. package/src/core/IndicatorEngine.ts +216 -0
  45. package/src/core/InteractionManager.ts +899 -0
  46. package/src/core/PriceScale.ts +133 -0
  47. package/src/core/Series.ts +132 -0
  48. package/src/core/TimeScale.ts +175 -0
  49. package/src/datafeed/DatafeedConnector.ts +300 -0
  50. package/src/engine/CandleEngine.ts +458 -0
  51. package/src/engine/__tests__/CandleEngine.test.ts +402 -0
  52. package/src/engine/candleInvariants.ts +172 -0
  53. package/src/engine/mergeUtils.ts +93 -0
  54. package/src/engine/timeframeUtils.ts +118 -0
  55. package/src/index.ts +190 -0
  56. package/src/internal.ts +41 -0
  57. package/src/licensing/ChartRuntimeResolver.ts +380 -0
  58. package/src/licensing/LicenseManager.ts +131 -0
  59. package/src/licensing/__tests__/ChartRuntimeResolver.test.ts +207 -0
  60. package/src/licensing/__tests__/LicenseManager.test.ts +180 -0
  61. package/src/licensing/licenseTypes.ts +19 -0
  62. package/src/pine/PineCompiler.ts +68 -0
  63. package/src/pine/diagnostics.ts +30 -0
  64. package/src/pine/index.ts +7 -0
  65. package/src/pine/pine-ast.ts +163 -0
  66. package/src/pine/pine-lexer.ts +265 -0
  67. package/src/pine/pine-parser.ts +439 -0
  68. package/src/pine/pine-transpiler.ts +301 -0
  69. package/src/pixi/LayerName.ts +35 -0
  70. package/src/pixi/PixiCandlestickRenderer.ts +125 -0
  71. package/src/pixi/PixiChart.ts +425 -0
  72. package/src/pixi/PixiCrosshairRenderer.ts +134 -0
  73. package/src/pixi/PixiDrawingRenderer.ts +121 -0
  74. package/src/pixi/PixiGridRenderer.ts +136 -0
  75. package/src/pixi/PixiLayerManager.ts +102 -0
  76. package/src/renderers/CandlestickRenderer.ts +130 -0
  77. package/src/renderers/HistogramRenderer.ts +63 -0
  78. package/src/renderers/LineRenderer.ts +77 -0
  79. package/src/theme/colors.ts +21 -0
  80. package/src/tools/barDivergenceCheck.ts +305 -0
  81. package/src/trading/TradingOverlayStore.ts +161 -0
  82. package/src/trading/UnmanagedIngestion.ts +156 -0
  83. package/src/trading/__tests__/ManagedTradingController.test.ts +338 -0
  84. package/src/trading/__tests__/TradingOverlayStore.test.ts +323 -0
  85. package/src/trading/__tests__/UnmanagedIngestion.test.ts +205 -0
  86. package/src/trading/managed/ManagedTradingController.ts +292 -0
  87. package/src/trading/managed/managedCapabilities.ts +98 -0
  88. package/src/trading/managed/managedTypes.ts +151 -0
  89. package/src/trading/tradingTypes.ts +135 -0
  90. package/src/tscript/TScriptIndicator.ts +54 -0
  91. package/src/tscript/ast.ts +105 -0
  92. package/src/tscript/lexer.ts +190 -0
  93. package/src/tscript/parser.ts +334 -0
  94. package/src/tscript/runtime.ts +525 -0
  95. package/src/tscript/series.ts +84 -0
  96. package/src/types/IChart.ts +56 -0
  97. package/src/types/IRenderer.ts +16 -0
  98. package/src/types/ISeries.ts +30 -0
  99. package/tsconfig.json +22 -0
  100. package/tsup.config.ts +15 -0
  101. package/vitest.config.ts +25 -0
@@ -0,0 +1,917 @@
1
+ import type { ChartOptions, ChartColors, ChartTheme, ChartKeyAction, Drawing, DrawingType, DrawingPoint, OHLCV, Timeframe } from '@forgecharts/types';
2
+ import type { IChart, IChartPlugin } from '../types/IChart';
3
+ import type { ISeries } from '../types/ISeries';
4
+ import { CanvasLayer } from './CanvasLayer';
5
+ import { TimeScale } from './TimeScale';
6
+ import { PriceScale } from './PriceScale';
7
+ import { CoordTransform } from './CoordTransform';
8
+ import { Crosshair } from './Crosshair';
9
+ import { Series } from './Series';
10
+ import { InteractionManager } from './InteractionManager';
11
+ import { DrawingManager } from '../api/DrawingManager';
12
+ import { DARK_COLORS, LIGHT_COLORS } from '../theme/colors';
13
+
14
+ // ─── Last-price helpers (module-level) ────────────────────────────────────────
15
+
16
+ const _TF_SECONDS: Record<string, number> = {
17
+ '1s': 1, '5s': 5, '10s': 10, '30s': 30,
18
+ '1m': 60, '3m': 180, '5m': 300, '15m': 900, '30m': 1800,
19
+ '1h': 3600, '2h': 7200, '4h': 14400, '6h': 21600, '12h': 43200,
20
+ '1d': 86400, '3d': 259200, '1w': 604800, '1M': 2592000,
21
+ };
22
+
23
+ function _tfToSeconds(tf: Timeframe): number {
24
+ return _TF_SECONDS[tf] ?? 3600;
25
+ }
26
+
27
+ function _formatLastPrice(price: number): string {
28
+ const decimals = price < 1 ? 6 : price < 1000 ? 4 : 2;
29
+ return price.toFixed(decimals);
30
+ }
31
+
32
+ function _formatCountdown(secs: number): string {
33
+ const h = Math.floor(secs / 3600);
34
+ const m = Math.floor((secs % 3600) / 60);
35
+ const s = secs % 60;
36
+ if (h > 0) return `${h}:${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`;
37
+ return `${m}:${String(s).padStart(2, '0')}`;
38
+ }
39
+
40
+ /**
41
+ * Chart — entry point for the ForgeCharts SDK.
42
+ *
43
+ * Manages the canvas layer stack, time/price scales, all series,
44
+ * and the interaction model. Uses the browser Canvas 2D API directly;
45
+ * no third-party chart library dependencies.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * const chart = new Chart(containerEl, { theme: 'dark' });
50
+ * const series = chart.addSeries({ type: 'candlestick' });
51
+ * series.setData(ohlcvArray);
52
+ * ```
53
+ */
54
+ export class Chart implements IChart {
55
+ private readonly _container: HTMLElement;
56
+ private readonly _options: Required<ChartOptions>;
57
+ private readonly _colors: ChartColors;
58
+
59
+ // Canvas layers (back → front): grid, series, overlay
60
+ private readonly _gridLayer: CanvasLayer;
61
+ private readonly _seriesLayer: CanvasLayer;
62
+ private readonly _overlayLayer: CanvasLayer;
63
+
64
+ private readonly _timeScale: TimeScale;
65
+ private readonly _priceScale: PriceScale;
66
+ private readonly _transform: CoordTransform;
67
+ private readonly _crosshair: Crosshair;
68
+
69
+ private _series: Series[] = [];
70
+ private _plugins: IChartPlugin[] = [];
71
+ private _animationFrame: number | null = null;
72
+ private _dirty = true;
73
+ private _indicatorsVisible = true;
74
+ /** When true the user has manually adjusted the price scale; auto-fit is suspended. */
75
+ private _priceScaleManual = false;
76
+ /** Active timeframe — used for last-price countdown. */
77
+ private _interval: Timeframe = '1h';
78
+ /** Last second at which we ticked the countdown to force a re-render. */
79
+ private _lastCountdownSec = 0;
80
+
81
+ private readonly _resizeObserver: ResizeObserver;
82
+ private readonly _interaction: InteractionManager;
83
+
84
+ // ── Drawing system ───────────────────────────────────────────────────────────────
85
+ private readonly _drawingMgr: DrawingManager;
86
+ private _drawingTool: DrawingType | null = null;
87
+ private _drawingState: 'idle' | 'placing_second' | 'placing_third' = 'idle';
88
+ private _drawingP0: DrawingPoint | null = null;
89
+ private _drawingP1: DrawingPoint | null = null;
90
+ private _drawingCursorPt: DrawingPoint | null = null;
91
+ /** When false the crosshair is suppressed (e.g. Arrow/cursor mode). */
92
+ private _crosshairEnabled = true;
93
+
94
+ /** Called whenever a drawing is committed (created or updated). */
95
+ onDrawingCommitted: ((drawing: Drawing) => void) | undefined;
96
+ /** Called whenever a drawing is deleted. */
97
+ onDrawingDeleted: ((id: string) => void) | undefined;
98
+ /** Called when the user right-clicks a drawing. x/y are canvas-relative pixels. */
99
+ onDrawingContextMenu: ((id: string, x: number, y: number) => void) | undefined;
100
+ /**
101
+ * Called after every pan or zoom interaction so external code (e.g. TChart)
102
+ * can check whether the visible time range now extends past the oldest
103
+ * loaded bar and trigger a lazy history fetch.
104
+ */
105
+ onViewportChanged: (() => void) | undefined;
106
+
107
+ constructor(container: HTMLElement, options: ChartOptions = {}) {
108
+ this._container = container;
109
+ this._container.style.position = 'relative';
110
+ this._container.style.overflow = 'hidden';
111
+
112
+ this._options = this._resolveOptions(options);
113
+ this._colors = this._resolveColors(this._options.theme, this._options.colors);
114
+
115
+ const { width, height } = container.getBoundingClientRect();
116
+ const dpr = window.devicePixelRatio ?? 1;
117
+
118
+ this._gridLayer = new CanvasLayer(container, { width, height, dpr, zIndex: 1 });
119
+ this._seriesLayer = new CanvasLayer(container, { width, height, dpr, zIndex: 2 });
120
+ this._overlayLayer = new CanvasLayer(container, { width, height, dpr, zIndex: 3 });
121
+
122
+ this._timeScale = new TimeScale(this._gridLayer, this._colors, this._options.timeScale);
123
+ this._priceScale = new PriceScale(this._gridLayer, this._colors, this._options.priceScale);
124
+
125
+ // CoordTransform is the single source of truth for all pixel ↔ data conversions.
126
+ // Created after the scales so it can hold live references to them.
127
+ this._transform = new CoordTransform(
128
+ this._timeScale,
129
+ this._priceScale,
130
+ this._priceScale.width,
131
+ this._timeScale.height,
132
+ );
133
+ this._transform.update(width, height);
134
+
135
+ this._crosshair = new Crosshair(this._overlayLayer, this._colors, this._options, this._transform);
136
+
137
+ // Propagate initial timezone to time axis and crosshair
138
+ if (this._options.timezone) {
139
+ this._timeScale.timezone = this._options.timezone;
140
+ this._crosshair.timezone = this._options.timezone;
141
+ }
142
+ this._drawingMgr = new DrawingManager();
143
+
144
+ this._interaction = new InteractionManager(
145
+ this._overlayLayer.canvas,
146
+ this._transform,
147
+ {
148
+ onPan: (dx, dy) => {
149
+ this._transform.pan(dx, dy);
150
+ this._dirty = true;
151
+ this.onViewportChanged?.();
152
+ },
153
+ onZoom: (factor, originX) => {
154
+ this._transform.zoomTime(factor, originX);
155
+ this._dirty = true;
156
+ this.onViewportChanged?.();
157
+ },
158
+ onCrosshairMove: (x, y) => {
159
+ if (!this._crosshairEnabled) return;
160
+ // Snap the vertical line to the nearest bar's timestamp so it always
161
+ // sits centred on a candle rather than floating between candles.
162
+ const { x: snappedX, time: snappedTime } = this._snapXToBar(x);
163
+ this._crosshair.update(snappedX, y, snappedTime);
164
+ this._dirty = true;
165
+ },
166
+ onCrosshairHide: () => {
167
+ this._crosshair.hide();
168
+ this._dirty = true;
169
+ },
170
+ onKeyAction: (action) => {
171
+ this._handleKeyAction(action);
172
+ },
173
+ onFitContent: () => {
174
+ this.scrollToEnd();
175
+ },
176
+ onPriceScaleZoom: (factor, originY) => {
177
+ this._priceScaleManual = true;
178
+ this._transform.zoomPrice(factor, originY);
179
+ this._dirty = true;
180
+ },
181
+ onPriceAxisDrag: (dy) => {
182
+ this._priceScaleManual = true;
183
+ // Exponential factor: drag down (dy > 0) = expand range, up = compress
184
+ this._transform.zoomPrice(Math.pow(1.005, dy), this._transform.plotHeight / 2);
185
+ this._dirty = true;
186
+ },
187
+ // Drawing tool callbacks
188
+ onDrawClick: (x, y) => {
189
+ this._handleDrawClick(x, y);
190
+ },
191
+ onDrawPointerMove: (x, y) => {
192
+ this._drawingCursorPt = {
193
+ time: this._transform.xToTime(x),
194
+ price: this._transform.yToPrice(y),
195
+ };
196
+ this._dirty = true;
197
+ },
198
+ onHandleDragStart: (_id, _hi) => {
199
+ this._dirty = true;
200
+ },
201
+ onHandleDragMove: (id, handleIndex, x, y) => {
202
+ const drawing = this._drawingMgr.get(id);
203
+ if (!drawing) return;
204
+ const snapped = this._snapToBar(x, y);
205
+ const newPoints = [...drawing.points] as DrawingPoint[];
206
+ newPoints[handleIndex] = snapped;
207
+ this._drawingMgr.update(id, { points: newPoints });
208
+ this._dirty = true;
209
+ },
210
+ onHandleDragEnd: (id, _hi) => {
211
+ const drawing = this._drawingMgr.get(id);
212
+ if (drawing) this.onDrawingCommitted?.(drawing);
213
+ this._dirty = true;
214
+ },
215
+ onBodyMoveStart: (_id) => {
216
+ this._dirty = true;
217
+ },
218
+ onBodyMoveMove: (id, dx, dy) => {
219
+ const drawing = this._drawingMgr.get(id);
220
+ if (!drawing) return;
221
+ const t = this._transform;
222
+ const newPoints = drawing.points.map((p): DrawingPoint => ({
223
+ time: t.xToTime(t.timeToX(p.time) + dx),
224
+ price: t.yToPrice(t.priceToY(p.price) + dy),
225
+ }));
226
+ this._drawingMgr.update(id, { points: newPoints });
227
+ this._dirty = true;
228
+ },
229
+ onBodyMoveEnd: (id) => {
230
+ const drawing = this._drawingMgr.get(id);
231
+ if (drawing) this.onDrawingCommitted?.(drawing);
232
+ this._dirty = true;
233
+ },
234
+ onContextMenu: (x, y, hit) => {
235
+ // Right-click while a drawing tool is active cancels placement.
236
+ if (this._drawingTool !== null) {
237
+ this.cancelDrawingTool();
238
+ return;
239
+ }
240
+ if (hit.kind === 'drawing') {
241
+ this.onDrawingContextMenu?.(hit.id, x, y);
242
+ }
243
+ },
244
+ },
245
+ {
246
+ enablePan: this._options.handleScroll,
247
+ enableZoom: this._options.handleScale,
248
+ },
249
+ );
250
+
251
+ this._resizeObserver = new ResizeObserver(() => this._onResize());
252
+ this._resizeObserver.observe(container);
253
+
254
+ this._scheduleRender();
255
+ }
256
+
257
+ // ─── Public API ──────────────────────────────────────────────────────────────
258
+
259
+ addSeries(options: Parameters<IChart['addSeries']>[0]): ISeries {
260
+ const series = new Series(options, this._colors);
261
+ // Wire live-tick repaints: whenever series data changes (WS tick calls
262
+ // series.update()), immediately mark the canvas dirty so the forming bar
263
+ // and last-price label repaint on the very next RAF frame — not once/second.
264
+ series.onDataChanged = () => { this._dirty = true; };
265
+ this._series.push(series);
266
+ this._dirty = true;
267
+ return series;
268
+ }
269
+
270
+ removeSeries(series: ISeries): void {
271
+ this._series = this._series.filter((s) => s !== series);
272
+ this._dirty = true;
273
+ }
274
+
275
+ addPlugin(plugin: IChartPlugin): void {
276
+ this._plugins.push(plugin);
277
+ plugin.onAttach(this);
278
+ }
279
+
280
+ removePlugin(plugin: IChartPlugin): void {
281
+ this._plugins = this._plugins.filter((p) => p !== plugin);
282
+ plugin.onDetach();
283
+ }
284
+
285
+ /** Set the active timeframe so the last-price countdown is accurate. */
286
+ setInterval(tf: Timeframe): void {
287
+ this._interval = tf;
288
+ this._crosshair.interval = tf;
289
+ this._timeScale.setTimeframe(tf);
290
+ this._dirty = true;
291
+ }
292
+
293
+ timeScale(): TimeScale {
294
+ return this._timeScale;
295
+ }
296
+
297
+ priceScale(): PriceScale {
298
+ return this._priceScale;
299
+ }
300
+
301
+ applyOptions(options: Partial<ChartOptions>): void {
302
+ Object.assign(this._options as Record<string, unknown>, options);
303
+ if (options.timezone !== undefined) {
304
+ this._timeScale.timezone = options.timezone;
305
+ this._crosshair.timezone = options.timezone;
306
+ }
307
+ if (options.colors !== undefined || options.theme !== undefined) {
308
+ const fresh = this._resolveColors(this._options.theme, this._options.colors);
309
+ Object.assign(this._colors as Record<string, unknown>, fresh);
310
+ }
311
+ this._dirty = true;
312
+ }
313
+
314
+ /** Pans the viewport so the latest bar is visible at the right edge. */
315
+ scrollToEnd(): void {
316
+ this._scrollToLatestBar();
317
+ this._dirty = true;
318
+ }
319
+
320
+ /** Returns the coordinate transform for this chart. */
321
+ transform(): CoordTransform {
322
+ return this._transform;
323
+ }
324
+
325
+ resize(width: number, height: number): void {
326
+ const dpr = window.devicePixelRatio ?? 1;
327
+ this._gridLayer.resize(width, height, dpr);
328
+ this._seriesLayer.resize(width, height, dpr);
329
+ this._overlayLayer.resize(width, height, dpr);
330
+ this._transform.update(width, height);
331
+ // Repaint synchronously so the canvas is never left blank between the
332
+ // clear (caused by assigning canvas.width/height) and the next RAF tick.
333
+ this._dirty = true;
334
+ this._render();
335
+ }
336
+
337
+ destroy(): void {
338
+ if (this._animationFrame !== null) cancelAnimationFrame(this._animationFrame);
339
+ this._resizeObserver.disconnect();
340
+ this._interaction.destroy();
341
+ this._gridLayer.destroy();
342
+ this._seriesLayer.destroy();
343
+ this._overlayLayer.destroy();
344
+ this._crosshair.destroy();
345
+ this._plugins.forEach((p) => p.onDetach());
346
+ this._series = [];
347
+ this._plugins = [];
348
+ }
349
+
350
+ // ─── Rendering ───────────────────────────────────────────────────────────────
351
+
352
+ private _scheduleRender(): void {
353
+ this._animationFrame = requestAnimationFrame(() => {
354
+ // Tick the countdown once per second so the label stays current.
355
+ const nowSec = Math.floor(Date.now() / 1000);
356
+ if (this._series.length > 0 && nowSec !== this._lastCountdownSec) {
357
+ this._lastCountdownSec = nowSec;
358
+ this._dirty = true;
359
+ }
360
+ this._render();
361
+ this._scheduleRender();
362
+ });
363
+ }
364
+
365
+ private _render(): void {
366
+ if (!this._dirty) return;
367
+ this._dirty = false;
368
+
369
+ // Auto-fit the price scale to the currently visible bars every frame.
370
+ // This keeps the candles in view after panning/zooming the time axis.
371
+ this._autoFitPriceToData();
372
+
373
+ this._renderGrid();
374
+ this._renderSeries();
375
+ this._renderOverlay();
376
+
377
+ this._plugins.forEach((p) => p.onRender());
378
+ }
379
+
380
+ private _renderGrid(): void {
381
+ const ctx = this._gridLayer.context;
382
+ const { width, height } = this._gridLayer;
383
+ ctx.clearRect(0, 0, width, height);
384
+
385
+ // Background
386
+ ctx.fillStyle = this._colors.background;
387
+ ctx.fillRect(0, 0, width, height);
388
+
389
+ this._timeScale.render(ctx, width, height);
390
+ this._priceScale.render(ctx, width, height);
391
+ }
392
+
393
+ setAllDrawingsVisible(v: boolean): void {
394
+ for (const d of this._drawingMgr.all()) {
395
+ this._drawingMgr.update(d.id, { visible: v });
396
+ }
397
+ this._dirty = true;
398
+ }
399
+
400
+ setIndicatorsVisible(v: boolean): void {
401
+ this._indicatorsVisible = v;
402
+ this._dirty = true;
403
+ }
404
+
405
+ private _renderSeries(): void {
406
+ const ctx = this._seriesLayer.context;
407
+ const { width, height } = this._seriesLayer;
408
+ ctx.clearRect(0, 0, width, height);
409
+
410
+ if (!this._indicatorsVisible) return;
411
+
412
+ const viewport = this._computeViewport();
413
+ for (const series of this._series) {
414
+ series.render(ctx, viewport, { x: 0, y: 0, width, height });
415
+ }
416
+ }
417
+
418
+ private _renderOverlay(): void {
419
+ const ctx = this._overlayLayer.context;
420
+ const { width, height } = this._overlayLayer;
421
+ ctx.clearRect(0, 0, width, height);
422
+
423
+ // Committed drawings
424
+ const selectedId = this._interaction.selectedDrawingId;
425
+ const candles = this._series[0]?.data() ?? [];
426
+ this._drawingMgr.render(ctx, this._transform, selectedId, this._interval, candles);
427
+ this._interaction.setDrawings(this._drawingMgr.all());
428
+
429
+ // In-progress placement preview
430
+ if (this._drawingTool !== null && this._drawingCursorPt !== null) {
431
+ const draft = this._buildDraftPreview();
432
+ if (draft) this._drawingMgr.renderPreview(ctx, this._transform, draft, this._interval, candles);
433
+ }
434
+
435
+ // Last-price dotted line + countdown
436
+ this._renderLastPriceLine(ctx);
437
+
438
+ this._crosshair.render(ctx, width, height);
439
+ }
440
+
441
+ // ─── Private helpers ─────────────────────────────────────────────────────────
442
+
443
+ /**
444
+ * Snaps a pixel X coordinate to the nearest bar's centre pixel.
445
+ * If no bars are loaded, or the nearest bar is more than SNAP_PX pixels away,
446
+ * returns the original x so the crosshair always renders at the cursor position.
447
+ */
448
+ private _snapXToBar(x: number): { x: number; time: number } {
449
+ const SNAP_PX = 20; // only snap when within this many pixels of a bar
450
+
451
+ // Collect all bar timestamps from the first series that has data.
452
+ let times: number[] | null = null;
453
+ for (const s of this._series) {
454
+ const data = s.data();
455
+ if (data.length > 0) {
456
+ times = data.map((b) => b.time);
457
+ break;
458
+ }
459
+ }
460
+ if (!times || times.length === 0) return { x, time: this._transform.xToTime(x) };
461
+
462
+ // Find the nearest bar in pixel space (not time space) so the snap radius is
463
+ // consistent regardless of timeframe or zoom level.
464
+ let nearestTime = times[0]!;
465
+ let minPixDist = Math.abs(this._transform.timeToX(nearestTime) - x);
466
+ for (let i = 1; i < times.length; i++) {
467
+ const px = this._transform.timeToX(times[i]!);
468
+ const d = Math.abs(px - x);
469
+ if (d < minPixDist) { minPixDist = d; nearestTime = times[i]!; }
470
+ }
471
+
472
+ // If the nearest bar is within SNAP_PX, snap to it; otherwise keep raw cursor x.
473
+ if (minPixDist <= SNAP_PX) {
474
+ return { x: this._transform.timeToX(nearestTime), time: nearestTime };
475
+ }
476
+ return { x, time: this._transform.xToTime(x) };
477
+ }
478
+
479
+ private _computeViewport() {
480
+ return {
481
+ timeRange: this._timeScale.visibleRange,
482
+ priceRange: this._priceScale.visibleRange,
483
+ };
484
+ }
485
+
486
+ private _onResize(): void {
487
+ const { width, height } = this._container.getBoundingClientRect();
488
+ this.resize(width, height);
489
+ }
490
+
491
+ private _handleKeyAction(action: ChartKeyAction): void {
492
+ const step = 40;
493
+ const zf = 1.1;
494
+ const cx = this._transform.plotWidth / 2;
495
+
496
+ switch (action) {
497
+ case 'panLeft': this._transform.pan( step, 0); break;
498
+ case 'panRight': this._transform.pan(-step, 0); break;
499
+ case 'panUp': this._transform.pan(0, step); break;
500
+ case 'panDown': this._transform.pan(0, -step); break;
501
+ case 'zoomIn': this._transform.zoomTime(1 / zf, cx); break;
502
+ case 'zoomOut': this._transform.zoomTime(zf, cx); break;
503
+ case 'scrollToEnd': this._scrollToLatestBar(); break;
504
+ case 'scrollToStart': break;
505
+ case 'deselect':
506
+ if (this._drawingTool !== null) {
507
+ this.cancelDrawingTool();
508
+ } else {
509
+ this._interaction.clearSelection();
510
+ }
511
+ break;
512
+ case 'deleteSelection':
513
+ this.deleteSelectedDrawing();
514
+ break;
515
+ }
516
+ this._dirty = true;
517
+ }
518
+
519
+ /**
520
+ * Fits the price scale to the high/low range of bars visible in the current
521
+ * time window. Called every render frame so the scale tracks panning/zooming.
522
+ */
523
+ private _autoFitPriceToData(): void {
524
+ if (this._priceScaleManual) return;
525
+ const { from, to } = this._timeScale.visibleRange;
526
+ let low = Infinity, high = -Infinity;
527
+ for (const series of this._series) {
528
+ for (const bar of series.data()) {
529
+ if (bar.time < from || bar.time > to) continue;
530
+ if (bar.low < low) low = bar.low;
531
+ if (bar.high > high) high = bar.high;
532
+ }
533
+ }
534
+ if (!isFinite(low) || !isFinite(high)) return;
535
+ const pad = (high - low) * 0.05;
536
+ this._priceScale.setVisibleRange({ min: low - pad, max: high + pad });
537
+ }
538
+
539
+ /**
540
+ * Renders the last-price dotted line, price label, and countdown timer.
541
+ * Drawn on the overlay canvas above all series but below the crosshair.
542
+ */
543
+ private _renderLastPriceLine(ctx: CanvasRenderingContext2D): void {
544
+ if (this._series.length === 0) return;
545
+ const data = this._series[0]!.data();
546
+ if (data.length === 0) return;
547
+ const lastBar = data[data.length - 1]!;
548
+
549
+ const plotWidth = this._transform.plotWidth;
550
+ const plotHeight = this._transform.plotHeight;
551
+ const psWidth = this._transform.priceScaleWidth;
552
+ const price = lastBar.close;
553
+ const y = this._transform.priceToY(price);
554
+
555
+ if (y < 0 || y > plotHeight) return;
556
+
557
+ const isUp = lastBar.close >= lastBar.open;
558
+ const lineColor = isUp ? '#26a641' : '#f85149';
559
+
560
+ ctx.save();
561
+
562
+ // Dotted horizontal line across the plot area
563
+ ctx.beginPath();
564
+ ctx.strokeStyle = lineColor;
565
+ ctx.lineWidth = 1;
566
+ ctx.setLineDash([2, 3]);
567
+ ctx.moveTo(0, y);
568
+ ctx.lineTo(plotWidth, y);
569
+ ctx.stroke();
570
+ ctx.setLineDash([]);
571
+
572
+ // Countdown calculation
573
+ const tfSecs = _tfToSeconds(this._interval);
574
+ const now = Math.floor(Date.now() / 1000);
575
+ const remaining = lastBar.time + tfSecs - now;
576
+ const showCd = remaining > 0 && remaining <= tfSecs;
577
+
578
+ // Label box — taller when countdown is shown
579
+ const priceLabel = _formatLastPrice(price);
580
+ const lineH = 15;
581
+ const boxH = showCd ? lineH * 2 + 1 : lineH;
582
+ const boxY = y - lineH / 2;
583
+
584
+ ctx.fillStyle = lineColor;
585
+ ctx.fillRect(plotWidth, boxY, psWidth, boxH);
586
+
587
+ ctx.fillStyle = '#ffffff';
588
+ ctx.textAlign = 'left';
589
+ ctx.textBaseline = 'middle';
590
+ ctx.font = 'bold 11px system-ui, sans-serif';
591
+ ctx.fillText(priceLabel, plotWidth + 5, boxY + lineH / 2);
592
+
593
+ if (showCd) {
594
+ ctx.font = '10px system-ui, sans-serif';
595
+ ctx.fillStyle = 'rgba(255,255,255,0.85)';
596
+ ctx.fillText(_formatCountdown(remaining), plotWidth + 5, boxY + lineH + 1 + lineH / 2);
597
+ }
598
+
599
+ ctx.restore();
600
+ }
601
+
602
+ /** Pans the viewport so the latest bar sits near the right edge. */
603
+ private _scrollToLatestBar(): void {
604
+ let latestTime = -Infinity;
605
+ for (const s of this._series) {
606
+ const data = s.data();
607
+ const last = data[data.length - 1];
608
+ if (last && last.time > latestTime) latestTime = last.time;
609
+ }
610
+ if (!isFinite(latestTime)) return;
611
+ const pad = this._transform.plotWidth * 0.05;
612
+ const px = this._transform.timeToX(latestTime);
613
+ this._transform.pan(-(px - (this._transform.plotWidth - pad)), 0);
614
+ }
615
+
616
+ /**
617
+ * Returns the current visible time and price ranges.
618
+ */
619
+ getViewport(): import('@forgecharts/types').Viewport {
620
+ return {
621
+ timeRange: this._timeScale.visibleRange,
622
+ priceRange: this._priceScale.visibleRange,
623
+ };
624
+ }
625
+
626
+ /**
627
+ * Restores a previously saved viewport (time range + manual price range).
628
+ */
629
+ restoreViewport(v: import('@forgecharts/types').Viewport): void {
630
+ this._timeScale.setVisibleRange(v.timeRange);
631
+ this._priceScale.setVisibleRange(v.priceRange);
632
+ this._priceScaleManual = true;
633
+ this._dirty = true;
634
+ }
635
+
636
+ /**
637
+ * Clears the manual-price-scale flag so the price axis returns to auto-fit mode.
638
+ * Call this after restoring a saved viewport so stale price ranges don't persist.
639
+ */
640
+ resetPriceScale(): void {
641
+ this._priceScaleManual = false;
642
+ this._dirty = true;
643
+ }
644
+
645
+ /**
646
+ * Sets the default viewport: shows 10 days of price history with the latest
647
+ * bar positioned near the right edge (5% right margin for breathing room).
648
+ * Also resets the price scale to auto-fit so prices center vertically.
649
+ * Called on initial data load and when the user double-clicks either axis.
650
+ */
651
+ fitDefaultView(): void {
652
+ let latestTime = -Infinity;
653
+ for (const s of this._series) {
654
+ const data = s.data();
655
+ const last = data[data.length - 1];
656
+ if (last && last.time > latestTime) latestTime = last.time;
657
+ }
658
+ if (!isFinite(latestTime)) return;
659
+ // Show ~150 candles worth of history, scaled to the current timeframe.
660
+ // Latest bar sits at the 90% position (10% breathing room on the right).
661
+ const tfSec = _tfToSeconds(this._interval);
662
+ const WINDOW = tfSec * 150;
663
+ this._timeScale.setVisibleRange({
664
+ from: latestTime - WINDOW * 0.9,
665
+ to: latestTime + WINDOW * 0.1,
666
+ });
667
+ // Reset manual price scale so prices auto-fit to visible bars
668
+ this._priceScaleManual = false;
669
+ this._dirty = true;
670
+ }
671
+
672
+ // ─── Drawing tool API ──────────────────────────────────────────────────────
673
+
674
+ /** Returns the DrawingManager owned by this chart. */
675
+ drawingManager(): DrawingManager {
676
+ return this._drawingMgr;
677
+ }
678
+
679
+ /** Force a redraw on the next animation frame. */
680
+ markDirty(): void {
681
+ this._dirty = true;
682
+ }
683
+
684
+ /**
685
+ * Activates a drawing tool. While active, mouse clicks place anchors.
686
+ * The tool auto-cancels after each commit. Press Escape to cancel mid-placement.
687
+ */
688
+ startDrawingTool(type: DrawingType): void {
689
+ this._drawingTool = type;
690
+ this._drawingState = 'idle';
691
+ this._drawingP0 = null;
692
+ this._drawingP1 = null;
693
+ this._drawingCursorPt = null;
694
+ this._interaction.setDrawingMode(true);
695
+ this._dirty = true;
696
+ }
697
+
698
+ /** Cancels any in-progress drawing and returns to cursor mode. */
699
+ cancelDrawingTool(): void {
700
+ this._drawingTool = null;
701
+ this._drawingState = 'idle';
702
+ this._drawingP0 = null;
703
+ this._drawingP1 = null;
704
+ this._drawingCursorPt = null;
705
+ this._interaction.setDrawingMode(false);
706
+ this._dirty = true;
707
+ }
708
+
709
+ /**
710
+ * Show or hide the interactive crosshair lines.
711
+ * Pass `false` for Arrow/cursor mode; `true` for crosshair / dot / demonstration.
712
+ */
713
+ setCrosshairEnabled(enabled: boolean): void {
714
+ this._crosshairEnabled = enabled;
715
+ this._interaction.setHideCursor(enabled);
716
+ if (!enabled) {
717
+ this._crosshair.hide();
718
+ this._dirty = true;
719
+ }
720
+ }
721
+
722
+ /**
723
+ * Hides (`true`) or restores (`false`) the native OS cursor.
724
+ * Call with `true` for dot and demonstration pointer modes whose visuals
725
+ * come from PointerOverlay rather than the SDK crosshair.
726
+ */
727
+ setNativeCursorHidden(hidden: boolean): void {
728
+ this._interaction.setHideCursor(hidden);
729
+ }
730
+
731
+ /** Deletes the currently selected drawing (if any). */
732
+ deleteSelectedDrawing(): void {
733
+ const id = this._interaction.selectedDrawingId;
734
+ if (!id) return;
735
+ this._drawingMgr.remove(id);
736
+ this._interaction.clearSelection();
737
+ this.onDrawingDeleted?.(id);
738
+ this._dirty = true;
739
+ }
740
+
741
+ // ─── Drawing placement state machine ──────────────────────────────────────
742
+
743
+ private _handleDrawClick(x: number, y: number): void {
744
+ const tool = this._drawingTool;
745
+ if (!tool) return;
746
+
747
+ const snapped = this._snapToBar(x, y);
748
+
749
+ const isSingleAnchor =
750
+ tool === 'horizontal' || tool === 'vertical' || tool === 'text' ||
751
+ tool === 'horizontalRay' || tool === 'crossLine';
752
+
753
+ if (isSingleAnchor) {
754
+ let label: string | undefined;
755
+ if (tool === 'text') {
756
+ const input = window.prompt('Enter text:');
757
+ if (input === null) return; // cancelled
758
+ label = input || 'Text';
759
+ }
760
+ const id = this._drawingMgr.add({
761
+ type: tool,
762
+ points: [snapped],
763
+ color: this._colors.crosshair,
764
+ ...(label !== undefined ? { text: label } : {}),
765
+ });
766
+ const drawing = this._drawingMgr.get(id)!;
767
+ this.onDrawingCommitted?.(drawing);
768
+ this.cancelDrawingTool();
769
+ return;
770
+ }
771
+
772
+ const isThreeAnchor =
773
+ tool === 'parallelChannel' || tool === 'flatTopBottom' ||
774
+ tool === 'pitchfork' || tool === 'schiffPitchfork' ||
775
+ tool === 'modifiedSchiffPitchfork' || tool === 'insidePitchfork';
776
+
777
+ if (isThreeAnchor) {
778
+ if (this._drawingState === 'idle') {
779
+ this._drawingP0 = snapped;
780
+ this._drawingState = 'placing_second';
781
+ this._dirty = true;
782
+ } else if (this._drawingState === 'placing_second') {
783
+ this._drawingP1 = snapped;
784
+ this._drawingState = 'placing_third';
785
+ this._dirty = true;
786
+ } else {
787
+ // For parallel channel, only the cursor's Y/price matters for the offset;
788
+ // lock p2.time to p1.time so the handle sits at the channel corner.
789
+ const p2: DrawingPoint = tool === 'parallelChannel'
790
+ ? { time: this._drawingP1!.time, price: snapped.price }
791
+ : snapped;
792
+ const id = this._drawingMgr.add({
793
+ type: tool,
794
+ points: [this._drawingP0!, this._drawingP1!, p2],
795
+ color: this._colors.crosshair,
796
+ });
797
+ const drawing = this._drawingMgr.get(id)!;
798
+ this.onDrawingCommitted?.(drawing);
799
+ this.cancelDrawingTool();
800
+ }
801
+ return;
802
+ }
803
+
804
+ // Two-anchor tools
805
+ if (this._drawingState === 'idle') {
806
+ this._drawingP0 = snapped;
807
+ this._drawingState = 'placing_second';
808
+ this._dirty = true;
809
+ } else {
810
+ const p0 = this._drawingP0!;
811
+
812
+ if (tool === 'disjointChannel') {
813
+ // Auto-place the second line below the first by a default pixel offset.
814
+ // This gives an immediate wedge shape the user can then drag to reshape.
815
+ const DEFAULT_OFFSET_PX = 60;
816
+ const p2: DrawingPoint = {
817
+ time: p0.time,
818
+ price: this._transform.yToPrice(this._transform.priceToY(p0.price) + DEFAULT_OFFSET_PX),
819
+ };
820
+ const p3: DrawingPoint = {
821
+ time: snapped.time,
822
+ price: this._transform.yToPrice(this._transform.priceToY(snapped.price) + DEFAULT_OFFSET_PX),
823
+ };
824
+ const id = this._drawingMgr.add({
825
+ type: tool,
826
+ points: [p0, snapped, p2, p3],
827
+ color: this._colors.crosshair,
828
+ });
829
+ const drawing = this._drawingMgr.get(id)!;
830
+ this.onDrawingCommitted?.(drawing);
831
+ this.cancelDrawingTool();
832
+ } else {
833
+ const id = this._drawingMgr.add({ type: tool, points: [p0, snapped], color: this._colors.crosshair });
834
+ const drawing = this._drawingMgr.get(id)!;
835
+ this.onDrawingCommitted?.(drawing);
836
+ this.cancelDrawingTool();
837
+ }
838
+ }
839
+ }
840
+
841
+ private _buildDraftPreview(): Omit<Drawing, 'id'> | null {
842
+ const tool = this._drawingTool;
843
+ const cursor = this._drawingCursorPt;
844
+ if (!tool || !cursor) return null;
845
+
846
+ const isSingleAnchor =
847
+ tool === 'horizontal' || tool === 'vertical' || tool === 'text' ||
848
+ tool === 'horizontalRay' || tool === 'crossLine';
849
+
850
+ if (isSingleAnchor) {
851
+ return { type: tool, points: [cursor], color: this._colors.crosshair };
852
+ }
853
+
854
+ if (this._drawingState === 'placing_third' && this._drawingP0 && this._drawingP1) {
855
+ // For parallel channel, lock p2 to p1's time so the cursor tracks the channel corner
856
+ const p2 = tool === 'parallelChannel'
857
+ ? { time: this._drawingP1.time, price: cursor.price }
858
+ : cursor;
859
+ return { type: tool, points: [this._drawingP0, this._drawingP1, p2], color: this._colors.crosshair };
860
+ }
861
+ if (this._drawingState === 'placing_second' && this._drawingP0) {
862
+ return { type: tool, points: [this._drawingP0, cursor], color: this._colors.crosshair };
863
+ }
864
+ if (this._drawingP0 === null) {
865
+ return { type: tool, points: [cursor], color: this._colors.crosshair };
866
+ }
867
+ return null;
868
+ }
869
+
870
+ /**
871
+ * Snaps the pixel position to the nearest bar's time within SNAP_PX.
872
+ * Returns a DrawingPoint with the snapped time and the raw price at y.
873
+ */
874
+ private _snapToBar(x: number, y: number): DrawingPoint {
875
+ const SNAP_PX = 10;
876
+ const t = this._transform;
877
+ const price = t.yToPrice(y);
878
+ const time = t.xToTime(x);
879
+
880
+ let bestBar: OHLCV | null = null;
881
+ let bestDist = SNAP_PX;
882
+
883
+ for (const series of this._series) {
884
+ for (const bar of series.data()) {
885
+ const bx = t.timeToX(bar.time);
886
+ const dist = Math.abs(bx - x);
887
+ if (dist < bestDist) {
888
+ bestDist = dist;
889
+ bestBar = bar;
890
+ }
891
+ }
892
+ }
893
+
894
+ return { time: bestBar ? bestBar.time : time, price };
895
+ }
896
+
897
+ private _resolveOptions(opts: ChartOptions): Required<ChartOptions> {
898
+ return {
899
+ theme: opts.theme ?? 'dark',
900
+ colors: opts.colors ?? {},
901
+ timeScale: opts.timeScale ?? {},
902
+ priceScale: opts.priceScale ?? {},
903
+ crosshair: opts.crosshair ?? {},
904
+ handleScroll: opts.handleScroll ?? true,
905
+ handleScale: opts.handleScale ?? true,
906
+ timezone: opts.timezone ?? 'UTC',
907
+ };
908
+ }
909
+
910
+ private _resolveColors(
911
+ theme: ChartTheme,
912
+ overrides: Partial<import('@forgecharts/types').ChartColors>,
913
+ ): ChartColors {
914
+ const base = theme === 'dark' ? DARK_COLORS : LIGHT_COLORS;
915
+ return { ...base, ...overrides };
916
+ }
917
+ }