@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.
- package/package.json +50 -0
- package/src/__tests__/backwardCompatibility.test.ts +191 -0
- package/src/__tests__/candleInvariant.test.ts +500 -0
- package/src/__tests__/public-api-surface.ts +76 -0
- package/src/__tests__/timeframeBoundary.test.ts +583 -0
- package/src/api/DrawingManager.ts +188 -0
- package/src/api/EventBus.ts +53 -0
- package/src/api/IndicatorDAG.ts +389 -0
- package/src/api/IndicatorRegistry.ts +47 -0
- package/src/api/LayoutManager.ts +72 -0
- package/src/api/PaneManager.ts +129 -0
- package/src/api/ReferenceAPI.ts +195 -0
- package/src/api/TChart.ts +881 -0
- package/src/api/createChart.ts +43 -0
- package/src/api/drawing tools/fib gann menu/fibRetracement.ts +27 -0
- package/src/api/drawing tools/lines menu/crossLine.ts +21 -0
- package/src/api/drawing tools/lines menu/disjointChannel.ts +74 -0
- package/src/api/drawing tools/lines menu/extendedLine.ts +22 -0
- package/src/api/drawing tools/lines menu/flatTopBottom.ts +45 -0
- package/src/api/drawing tools/lines menu/horizontal.ts +24 -0
- package/src/api/drawing tools/lines menu/horizontalRay.ts +25 -0
- package/src/api/drawing tools/lines menu/infoLine.ts +127 -0
- package/src/api/drawing tools/lines menu/insidePitchfork.ts +21 -0
- package/src/api/drawing tools/lines menu/modifiedSchiffPitchfork.ts +18 -0
- package/src/api/drawing tools/lines menu/parallelChannel.ts +47 -0
- package/src/api/drawing tools/lines menu/pitchfork.ts +15 -0
- package/src/api/drawing tools/lines menu/ray.ts +28 -0
- package/src/api/drawing tools/lines menu/regressionTrend.ts +157 -0
- package/src/api/drawing tools/lines menu/schiffPitchfork.ts +18 -0
- package/src/api/drawing tools/lines menu/trendAngle.ts +64 -0
- package/src/api/drawing tools/lines menu/trendline.ts +16 -0
- package/src/api/drawing tools/lines menu/vertical.ts +16 -0
- package/src/api/drawing tools/pointers menu/crosshair.ts +17 -0
- package/src/api/drawing tools/pointers menu/cursor.ts +16 -0
- package/src/api/drawing tools/pointers menu/demonstration.ts +35 -0
- package/src/api/drawing tools/pointers menu/dot.ts +26 -0
- package/src/api/drawing tools/shapes menu/rectangle.ts +24 -0
- package/src/api/drawing tools/shapes menu/text.ts +30 -0
- package/src/api/drawingUtils.ts +82 -0
- package/src/core/CanvasLayer.ts +77 -0
- package/src/core/Chart.ts +917 -0
- package/src/core/CoordTransform.ts +282 -0
- package/src/core/Crosshair.ts +207 -0
- package/src/core/IndicatorEngine.ts +216 -0
- package/src/core/InteractionManager.ts +899 -0
- package/src/core/PriceScale.ts +133 -0
- package/src/core/Series.ts +132 -0
- package/src/core/TimeScale.ts +175 -0
- package/src/datafeed/DatafeedConnector.ts +300 -0
- package/src/engine/CandleEngine.ts +458 -0
- package/src/engine/__tests__/CandleEngine.test.ts +402 -0
- package/src/engine/candleInvariants.ts +172 -0
- package/src/engine/mergeUtils.ts +93 -0
- package/src/engine/timeframeUtils.ts +118 -0
- package/src/index.ts +190 -0
- package/src/internal.ts +41 -0
- package/src/licensing/ChartRuntimeResolver.ts +380 -0
- package/src/licensing/LicenseManager.ts +131 -0
- package/src/licensing/__tests__/ChartRuntimeResolver.test.ts +207 -0
- package/src/licensing/__tests__/LicenseManager.test.ts +180 -0
- package/src/licensing/licenseTypes.ts +19 -0
- package/src/pine/PineCompiler.ts +68 -0
- package/src/pine/diagnostics.ts +30 -0
- package/src/pine/index.ts +7 -0
- package/src/pine/pine-ast.ts +163 -0
- package/src/pine/pine-lexer.ts +265 -0
- package/src/pine/pine-parser.ts +439 -0
- package/src/pine/pine-transpiler.ts +301 -0
- package/src/pixi/LayerName.ts +35 -0
- package/src/pixi/PixiCandlestickRenderer.ts +125 -0
- package/src/pixi/PixiChart.ts +425 -0
- package/src/pixi/PixiCrosshairRenderer.ts +134 -0
- package/src/pixi/PixiDrawingRenderer.ts +121 -0
- package/src/pixi/PixiGridRenderer.ts +136 -0
- package/src/pixi/PixiLayerManager.ts +102 -0
- package/src/renderers/CandlestickRenderer.ts +130 -0
- package/src/renderers/HistogramRenderer.ts +63 -0
- package/src/renderers/LineRenderer.ts +77 -0
- package/src/theme/colors.ts +21 -0
- package/src/tools/barDivergenceCheck.ts +305 -0
- package/src/trading/TradingOverlayStore.ts +161 -0
- package/src/trading/UnmanagedIngestion.ts +156 -0
- package/src/trading/__tests__/ManagedTradingController.test.ts +338 -0
- package/src/trading/__tests__/TradingOverlayStore.test.ts +323 -0
- package/src/trading/__tests__/UnmanagedIngestion.test.ts +205 -0
- package/src/trading/managed/ManagedTradingController.ts +292 -0
- package/src/trading/managed/managedCapabilities.ts +98 -0
- package/src/trading/managed/managedTypes.ts +151 -0
- package/src/trading/tradingTypes.ts +135 -0
- package/src/tscript/TScriptIndicator.ts +54 -0
- package/src/tscript/ast.ts +105 -0
- package/src/tscript/lexer.ts +190 -0
- package/src/tscript/parser.ts +334 -0
- package/src/tscript/runtime.ts +525 -0
- package/src/tscript/series.ts +84 -0
- package/src/types/IChart.ts +56 -0
- package/src/types/IRenderer.ts +16 -0
- package/src/types/ISeries.ts +30 -0
- package/tsconfig.json +22 -0
- package/tsup.config.ts +15 -0
- package/vitest.config.ts +25 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import type { Drawing, OHLCV } from '@forgecharts/types';
|
|
2
|
+
import type { CoordTransform } from '../../../core/CoordTransform';
|
|
3
|
+
|
|
4
|
+
// ─── OLS linear regression ────────────────────────────────────────────────────
|
|
5
|
+
|
|
6
|
+
type RegressionParams = {
|
|
7
|
+
slope: number;
|
|
8
|
+
intercept: number;
|
|
9
|
+
stdDev: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Ordinary least-squares regression on the close prices of a candle slice.
|
|
14
|
+
*
|
|
15
|
+
* x = candle index (0 … N-1)
|
|
16
|
+
* y = close price
|
|
17
|
+
*
|
|
18
|
+
* m = (N·Σxy − Σx·Σy) / (N·Σx² − (Σx)²)
|
|
19
|
+
* b = (Σy − m·Σx) / N
|
|
20
|
+
*/
|
|
21
|
+
function calcOLS(closes: readonly number[]): RegressionParams | null {
|
|
22
|
+
const N = closes.length;
|
|
23
|
+
if (N < 2) return null;
|
|
24
|
+
|
|
25
|
+
let sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;
|
|
26
|
+
for (let i = 0; i < N; i++) {
|
|
27
|
+
const y = closes[i]!;
|
|
28
|
+
sumX += i;
|
|
29
|
+
sumY += y;
|
|
30
|
+
sumXY += i * y;
|
|
31
|
+
sumX2 += i * i;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const denom = N * sumX2 - sumX * sumX;
|
|
35
|
+
if (denom === 0) return null;
|
|
36
|
+
|
|
37
|
+
const slope = (N * sumXY - sumX * sumY) / denom;
|
|
38
|
+
const intercept = (sumY - slope * sumX) / N;
|
|
39
|
+
|
|
40
|
+
// Standard deviation of residuals
|
|
41
|
+
let sumRes2 = 0;
|
|
42
|
+
for (let i = 0; i < N; i++) {
|
|
43
|
+
const residual = closes[i]! - (slope * i + intercept);
|
|
44
|
+
sumRes2 += residual * residual;
|
|
45
|
+
}
|
|
46
|
+
const stdDev = Math.sqrt(sumRes2 / N);
|
|
47
|
+
|
|
48
|
+
return { slope, intercept, stdDev };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ─── Candle slice helper ──────────────────────────────────────────────────────
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Returns candles whose timestamps fall within [tStart, tEnd] (inclusive).
|
|
55
|
+
* Assumes the input array is sorted ascending by time (Series guarantees this).
|
|
56
|
+
*/
|
|
57
|
+
function sliceByTime(candles: readonly OHLCV[], tStart: number, tEnd: number): readonly OHLCV[] {
|
|
58
|
+
let lo = 0, hi = candles.length - 1, start = candles.length;
|
|
59
|
+
// Binary search for first index >= tStart
|
|
60
|
+
while (lo <= hi) {
|
|
61
|
+
const mid = (lo + hi) >>> 1;
|
|
62
|
+
if (candles[mid]!.time < tStart) lo = mid + 1;
|
|
63
|
+
else { start = mid; hi = mid - 1; }
|
|
64
|
+
}
|
|
65
|
+
// Walk forward to find last index <= tEnd
|
|
66
|
+
let end = start - 1;
|
|
67
|
+
for (let i = start; i < candles.length && candles[i]!.time <= tEnd; i++) end = i;
|
|
68
|
+
if (end < start) return [];
|
|
69
|
+
return candles.slice(start, end + 1);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ─── Renderer ─────────────────────────────────────────────────────────────────
|
|
73
|
+
|
|
74
|
+
export function renderRegressionTrend(
|
|
75
|
+
ctx: CanvasRenderingContext2D,
|
|
76
|
+
d: Drawing,
|
|
77
|
+
t: CoordTransform,
|
|
78
|
+
candles: readonly OHLCV[],
|
|
79
|
+
): void {
|
|
80
|
+
const p0 = d.points[0];
|
|
81
|
+
const p1 = d.points[1];
|
|
82
|
+
if (!p0 || !p1) return;
|
|
83
|
+
|
|
84
|
+
const tStart = Math.min(p0.time, p1.time);
|
|
85
|
+
const tEnd = Math.max(p0.time, p1.time);
|
|
86
|
+
|
|
87
|
+
const slice = sliceByTime(candles, tStart, tEnd);
|
|
88
|
+
const N = slice.length;
|
|
89
|
+
if (N < 2) return;
|
|
90
|
+
|
|
91
|
+
const closes = slice.map(c => c.close);
|
|
92
|
+
const reg = calcOLS(closes);
|
|
93
|
+
if (!reg) return;
|
|
94
|
+
|
|
95
|
+
const { slope, intercept, stdDev } = reg;
|
|
96
|
+
|
|
97
|
+
// Regression price at the first and last candle of the slice
|
|
98
|
+
const regPriceStart = intercept; // index 0
|
|
99
|
+
const regPriceEnd = slope * (N - 1) + intercept; // index N-1
|
|
100
|
+
|
|
101
|
+
// Pixel x from actual candle timestamps; y from regression-derived prices
|
|
102
|
+
const x0 = t.timeToX(slice[0]!.time);
|
|
103
|
+
const x1 = t.timeToX(slice[N - 1]!.time);
|
|
104
|
+
const y0 = t.priceToY(regPriceStart);
|
|
105
|
+
const y1 = t.priceToY(regPriceEnd);
|
|
106
|
+
|
|
107
|
+
const color = d.color ?? '#2196f3';
|
|
108
|
+
|
|
109
|
+
// ── Regression line ───────────────────────────────────────────────────────
|
|
110
|
+
ctx.beginPath();
|
|
111
|
+
ctx.moveTo(x0, y0);
|
|
112
|
+
ctx.lineTo(x1, y1);
|
|
113
|
+
ctx.stroke();
|
|
114
|
+
|
|
115
|
+
// ── Deviation bands ───────────────────────────────────────────────────────
|
|
116
|
+
if (stdDev === 0) return; // flat data — no meaningful channel
|
|
117
|
+
|
|
118
|
+
const mult = d.deviationMultiplier ?? 2;
|
|
119
|
+
const bandOffset = stdDev * mult; // price units
|
|
120
|
+
|
|
121
|
+
const yUp0 = t.priceToY(regPriceStart + bandOffset);
|
|
122
|
+
const yUp1 = t.priceToY(regPriceEnd + bandOffset);
|
|
123
|
+
const yDn0 = t.priceToY(regPriceStart - bandOffset);
|
|
124
|
+
const yDn1 = t.priceToY(regPriceEnd - bandOffset);
|
|
125
|
+
|
|
126
|
+
ctx.save();
|
|
127
|
+
ctx.setLineDash([4, 3]);
|
|
128
|
+
ctx.globalAlpha = 0.65;
|
|
129
|
+
|
|
130
|
+
// Upper band
|
|
131
|
+
ctx.beginPath();
|
|
132
|
+
ctx.moveTo(x0, yUp0);
|
|
133
|
+
ctx.lineTo(x1, yUp1);
|
|
134
|
+
ctx.stroke();
|
|
135
|
+
|
|
136
|
+
// Lower band
|
|
137
|
+
ctx.beginPath();
|
|
138
|
+
ctx.moveTo(x0, yDn0);
|
|
139
|
+
ctx.lineTo(x1, yDn1);
|
|
140
|
+
ctx.stroke();
|
|
141
|
+
|
|
142
|
+
ctx.setLineDash([]);
|
|
143
|
+
|
|
144
|
+
// Fill between bands
|
|
145
|
+
ctx.globalAlpha = 0.07;
|
|
146
|
+
ctx.fillStyle = color;
|
|
147
|
+
ctx.beginPath();
|
|
148
|
+
ctx.moveTo(x0, yUp0);
|
|
149
|
+
ctx.lineTo(x1, yUp1);
|
|
150
|
+
ctx.lineTo(x1, yDn1);
|
|
151
|
+
ctx.lineTo(x0, yDn0);
|
|
152
|
+
ctx.closePath();
|
|
153
|
+
ctx.fill();
|
|
154
|
+
|
|
155
|
+
ctx.restore();
|
|
156
|
+
}
|
|
157
|
+
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Drawing } from '@forgecharts/types';
|
|
2
|
+
import type { CoordTransform } from '../../../core/CoordTransform';
|
|
3
|
+
import { getPF3, drawPitchforkLines } from '../../drawingUtils';
|
|
4
|
+
import { renderTrendline } from './trendline';
|
|
5
|
+
|
|
6
|
+
export function renderSchiffPitchfork(
|
|
7
|
+
ctx: CanvasRenderingContext2D,
|
|
8
|
+
d: Drawing,
|
|
9
|
+
t: CoordTransform,
|
|
10
|
+
): void {
|
|
11
|
+
const pf = getPF3(d, t);
|
|
12
|
+
if (!pf) { renderTrendline(ctx, d, t); return; }
|
|
13
|
+
const { x0, y0, x1, y1, x2, y2 } = pf;
|
|
14
|
+
const midX = (x1 + x2) / 2, midY = (y1 + y2) / 2;
|
|
15
|
+
const pivX = (x0 + midX) / 2;
|
|
16
|
+
const pivY = (y0 + midY) / 2;
|
|
17
|
+
drawPitchforkLines(ctx, t, pivX, pivY, midX, midY, x1, y1, x2, y2);
|
|
18
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Drawing } from '@forgecharts/types';
|
|
2
|
+
import type { CoordTransform } from '../../../core/CoordTransform';
|
|
3
|
+
import { renderTrendline } from './trendline';
|
|
4
|
+
|
|
5
|
+
export function renderTrendAngle(
|
|
6
|
+
ctx: CanvasRenderingContext2D,
|
|
7
|
+
d: Drawing,
|
|
8
|
+
t: CoordTransform,
|
|
9
|
+
): void {
|
|
10
|
+
const p0 = d.points[0];
|
|
11
|
+
const p1 = d.points[1];
|
|
12
|
+
if (!p0 || !p1) return;
|
|
13
|
+
|
|
14
|
+
const x0 = t.timeToX(p0.time), y0 = t.priceToY(p0.price);
|
|
15
|
+
const x1 = t.timeToX(p1.time), y1 = t.priceToY(p1.price);
|
|
16
|
+
|
|
17
|
+
const angleRad = Math.atan2(-(y1 - y0), x1 - x0);
|
|
18
|
+
const angleDeg = (angleRad * 180 / Math.PI).toFixed(2);
|
|
19
|
+
const color = d.color ?? '#2196f3';
|
|
20
|
+
|
|
21
|
+
// Main trendline
|
|
22
|
+
renderTrendline(ctx, d, t);
|
|
23
|
+
|
|
24
|
+
ctx.save();
|
|
25
|
+
ctx.strokeStyle = color;
|
|
26
|
+
ctx.fillStyle = color;
|
|
27
|
+
|
|
28
|
+
// Dotted horizontal baseline from p0 to p1's x-position
|
|
29
|
+
ctx.beginPath();
|
|
30
|
+
ctx.setLineDash([3, 3]);
|
|
31
|
+
ctx.moveTo(x0, y0);
|
|
32
|
+
ctx.lineTo(x1, y0);
|
|
33
|
+
ctx.stroke();
|
|
34
|
+
ctx.setLineDash([]);
|
|
35
|
+
|
|
36
|
+
// Arc at p0 sweeping from baseline to the trendline
|
|
37
|
+
const ARC_R = Math.min(36, Math.abs(x1 - x0) * 0.35);
|
|
38
|
+
if (ARC_R > 6) {
|
|
39
|
+
const goingRight = x1 >= x0;
|
|
40
|
+
const startAngle = goingRight ? 0 : Math.PI;
|
|
41
|
+
const lineAngle = Math.atan2(y1 - y0, x1 - x0);
|
|
42
|
+
ctx.beginPath();
|
|
43
|
+
ctx.lineWidth = 1;
|
|
44
|
+
if (goingRight) {
|
|
45
|
+
ctx.arc(x0, y0, ARC_R, startAngle, lineAngle, lineAngle < 0);
|
|
46
|
+
} else {
|
|
47
|
+
ctx.arc(x0, y0, ARC_R, startAngle, lineAngle, lineAngle > Math.PI);
|
|
48
|
+
}
|
|
49
|
+
ctx.stroke();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Angle label near p0, just outside the arc
|
|
53
|
+
const labelR = ARC_R + 6;
|
|
54
|
+
const halfAngle = Math.atan2(y1 - y0, x1 - x0) / 2;
|
|
55
|
+
const lx = x0 + labelR * Math.cos(halfAngle);
|
|
56
|
+
const ly = y0 + labelR * Math.sin(halfAngle);
|
|
57
|
+
|
|
58
|
+
ctx.font = '11px -apple-system, BlinkMacSystemFont, sans-serif';
|
|
59
|
+
ctx.textAlign = x1 >= x0 ? 'left' : 'right';
|
|
60
|
+
ctx.textBaseline = y1 <= y0 ? 'top' : 'bottom';
|
|
61
|
+
ctx.fillText(`${angleDeg}\u00b0`, lx + (x1 >= x0 ? 2 : -2), ly);
|
|
62
|
+
|
|
63
|
+
ctx.restore();
|
|
64
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Drawing } from '@forgecharts/types';
|
|
2
|
+
import type { CoordTransform } from '../../../core/CoordTransform';
|
|
3
|
+
|
|
4
|
+
export function renderTrendline(
|
|
5
|
+
ctx: CanvasRenderingContext2D,
|
|
6
|
+
d: Drawing,
|
|
7
|
+
t: CoordTransform,
|
|
8
|
+
): void {
|
|
9
|
+
const p0 = d.points[0];
|
|
10
|
+
const p1 = d.points[1];
|
|
11
|
+
if (!p0 || !p1) return;
|
|
12
|
+
ctx.beginPath();
|
|
13
|
+
ctx.moveTo(t.timeToX(p0.time), t.priceToY(p0.price));
|
|
14
|
+
ctx.lineTo(t.timeToX(p1.time), t.priceToY(p1.price));
|
|
15
|
+
ctx.stroke();
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Drawing } from '@forgecharts/types';
|
|
2
|
+
import type { CoordTransform } from '../../../core/CoordTransform';
|
|
3
|
+
|
|
4
|
+
export function renderVertical(
|
|
5
|
+
ctx: CanvasRenderingContext2D,
|
|
6
|
+
d: Drawing,
|
|
7
|
+
t: CoordTransform,
|
|
8
|
+
): void {
|
|
9
|
+
const p0 = d.points[0];
|
|
10
|
+
if (!p0) return;
|
|
11
|
+
const x = t.timeToX(p0.time);
|
|
12
|
+
ctx.beginPath();
|
|
13
|
+
ctx.moveTo(x, 0);
|
|
14
|
+
ctx.lineTo(x, t.plotHeight);
|
|
15
|
+
ctx.stroke();
|
|
16
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Crosshair pointer tool.
|
|
3
|
+
*
|
|
4
|
+
* Hides the native cursor (`cursor: none`) and activates the SDK-drawn
|
|
5
|
+
* dashed crosshair lines (vertical + horizontal) via `Chart.setCrosshairEnabled(true)`.
|
|
6
|
+
* No PointerOverlay is rendered for this mode.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const CROSSHAIR_LABEL = 'Cross';
|
|
10
|
+
export const CROSSHAIR_STYLE = 'none' as const;
|
|
11
|
+
|
|
12
|
+
/** Descriptor used by the toolbar and tool-selection logic. */
|
|
13
|
+
export const crosshairTool = {
|
|
14
|
+
type: 'crosshair' as const,
|
|
15
|
+
label: CROSSHAIR_LABEL,
|
|
16
|
+
cursorStyle: CROSSHAIR_STYLE,
|
|
17
|
+
} as const;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cursor (Arrow) pointer tool.
|
|
3
|
+
*
|
|
4
|
+
* The default interaction mode — shows the native OS arrow cursor.
|
|
5
|
+
* No custom overlay is rendered; the SDK crosshair is disabled.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export const CURSOR_LABEL = 'Arrow';
|
|
9
|
+
export const CURSOR_STYLE = 'default' as const;
|
|
10
|
+
|
|
11
|
+
/** Descriptor used by the toolbar and tool-selection logic. */
|
|
12
|
+
export const cursorTool = {
|
|
13
|
+
type: 'cursor' as const,
|
|
14
|
+
label: CURSOR_LABEL,
|
|
15
|
+
cursorStyle: CURSOR_STYLE,
|
|
16
|
+
} as const;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Demonstration pointer tool.
|
|
3
|
+
*
|
|
4
|
+
* Hides the native cursor and renders a PointerOverlay with:
|
|
5
|
+
* - dashed crosshair lines (vertical + horizontal)
|
|
6
|
+
* - a large semi-transparent circle centred on the cursor
|
|
7
|
+
*
|
|
8
|
+
* Useful for screen-sharing / presentations where the cursor position
|
|
9
|
+
* needs to be clearly visible to an audience.
|
|
10
|
+
*
|
|
11
|
+
* The SDK crosshair (`Chart.setCrosshairEnabled`) is NOT active in this mode;
|
|
12
|
+
* all visuals come from the SVG overlay.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export const DEMONSTRATION_LABEL = 'Demonstration';
|
|
16
|
+
export const DEMONSTRATION_STYLE = 'none' as const;
|
|
17
|
+
|
|
18
|
+
/** Radius (px) of the large highlight circle drawn around the cursor. */
|
|
19
|
+
export const DEMONSTRATION_RADIUS = 44;
|
|
20
|
+
|
|
21
|
+
/** Fill colour of the demonstration circle. */
|
|
22
|
+
export const DEMONSTRATION_FILL = 'rgba(255, 200, 50, 0.18)';
|
|
23
|
+
|
|
24
|
+
/** Stroke colour of the demonstration circle. */
|
|
25
|
+
export const DEMONSTRATION_STROKE = 'rgba(255, 200, 50, 0.7)';
|
|
26
|
+
|
|
27
|
+
/** Colour used for the dashed crosshair lines. */
|
|
28
|
+
export const DEMONSTRATION_COLOR = 'var(--crosshair-overlay, rgba(255,255,255,0.75))';
|
|
29
|
+
|
|
30
|
+
/** Descriptor used by the toolbar and tool-selection logic. */
|
|
31
|
+
export const demonstrationTool = {
|
|
32
|
+
type: 'demonstration' as const,
|
|
33
|
+
label: DEMONSTRATION_LABEL,
|
|
34
|
+
cursorStyle: DEMONSTRATION_STYLE,
|
|
35
|
+
} as const;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dot pointer tool.
|
|
3
|
+
*
|
|
4
|
+
* Hides the native cursor and renders a PointerOverlay with:
|
|
5
|
+
* - dashed crosshair lines (vertical + horizontal)
|
|
6
|
+
* - a small filled circle at the cursor intersection
|
|
7
|
+
*
|
|
8
|
+
* The SDK crosshair (`Chart.setCrosshairEnabled`) is NOT active in this mode;
|
|
9
|
+
* all visuals come from the SVG overlay.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export const DOT_LABEL = 'Dot';
|
|
13
|
+
export const DOT_STYLE = 'none' as const;
|
|
14
|
+
|
|
15
|
+
/** Radius (px) of the filled dot drawn at the crosshair intersection. */
|
|
16
|
+
export const DOT_RADIUS = 3;
|
|
17
|
+
|
|
18
|
+
/** Colour used for both the crosshair lines and the dot fill. */
|
|
19
|
+
export const DOT_COLOR = 'var(--crosshair-overlay, rgba(255,255,255,0.75))';
|
|
20
|
+
|
|
21
|
+
/** Descriptor used by the toolbar and tool-selection logic. */
|
|
22
|
+
export const dotTool = {
|
|
23
|
+
type: 'dot' as const,
|
|
24
|
+
label: DOT_LABEL,
|
|
25
|
+
cursorStyle: DOT_STYLE,
|
|
26
|
+
} as const;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Drawing } from '@forgecharts/types';
|
|
2
|
+
import type { CoordTransform } from '../../../core/CoordTransform';
|
|
3
|
+
|
|
4
|
+
export function renderRectangle(
|
|
5
|
+
ctx: CanvasRenderingContext2D,
|
|
6
|
+
d: Drawing,
|
|
7
|
+
t: CoordTransform,
|
|
8
|
+
): void {
|
|
9
|
+
const p0 = d.points[0];
|
|
10
|
+
const p1 = d.points[1];
|
|
11
|
+
if (!p0 || !p1) return;
|
|
12
|
+
const rx = Math.min(t.timeToX(p0.time), t.timeToX(p1.time));
|
|
13
|
+
const ry = Math.min(t.priceToY(p0.price), t.priceToY(p1.price));
|
|
14
|
+
const rw = Math.abs(t.timeToX(p1.time) - t.timeToX(p0.time));
|
|
15
|
+
const rh = Math.abs(t.priceToY(p1.price) - t.priceToY(p0.price));
|
|
16
|
+
ctx.save();
|
|
17
|
+
ctx.fillStyle = d.fillColor ?? (d.color ?? '#2196f3');
|
|
18
|
+
ctx.globalAlpha *= d.fillOpacity ?? 0.1;
|
|
19
|
+
ctx.fillRect(rx, ry, rw, rh);
|
|
20
|
+
ctx.restore();
|
|
21
|
+
ctx.beginPath();
|
|
22
|
+
ctx.rect(rx, ry, rw, rh);
|
|
23
|
+
ctx.stroke();
|
|
24
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Drawing } from '@forgecharts/types';
|
|
2
|
+
import type { CoordTransform } from '../../../core/CoordTransform';
|
|
3
|
+
|
|
4
|
+
export function renderText(
|
|
5
|
+
ctx: CanvasRenderingContext2D,
|
|
6
|
+
d: Drawing,
|
|
7
|
+
t: CoordTransform,
|
|
8
|
+
): void {
|
|
9
|
+
const p0 = d.points[0];
|
|
10
|
+
if (!p0) return;
|
|
11
|
+
const label = d.text ?? 'Text';
|
|
12
|
+
const x = t.timeToX(p0.time);
|
|
13
|
+
const y = t.priceToY(p0.price);
|
|
14
|
+
const fs = d.fontSize ?? 13;
|
|
15
|
+
ctx.font = `${fs}px -apple-system, BlinkMacSystemFont, sans-serif`;
|
|
16
|
+
const metrics = ctx.measureText(label);
|
|
17
|
+
const pad = 4;
|
|
18
|
+
const boxW = metrics.width + pad * 2;
|
|
19
|
+
const boxH = fs + pad * 2;
|
|
20
|
+
ctx.save();
|
|
21
|
+
ctx.fillStyle = d.color ?? '#2196f3';
|
|
22
|
+
ctx.globalAlpha *= 0.15;
|
|
23
|
+
ctx.fillRect(x, y - boxH, boxW, boxH);
|
|
24
|
+
ctx.restore();
|
|
25
|
+
ctx.font = `${fs}px -apple-system, BlinkMacSystemFont, sans-serif`;
|
|
26
|
+
ctx.fillStyle = d.color ?? '#2196f3';
|
|
27
|
+
ctx.textAlign = 'left';
|
|
28
|
+
ctx.textBaseline = 'bottom';
|
|
29
|
+
ctx.fillText(label, x + pad, y);
|
|
30
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Drawing } from '@forgecharts/types';
|
|
2
|
+
import type { CoordTransform } from '../core/CoordTransform';
|
|
3
|
+
|
|
4
|
+
// ─── Handle appearance ────────────────────────────────────────────────────────
|
|
5
|
+
export const HANDLE_HALF = 5;
|
|
6
|
+
export const HANDLE_FILL = '#2196f3';
|
|
7
|
+
export const HANDLE_STROKE = 'rgba(255,255,255,0.9)';
|
|
8
|
+
|
|
9
|
+
// ─── Ray exit helper ──────────────────────────────────────────────────────────
|
|
10
|
+
export function rayExit(
|
|
11
|
+
plotW: number,
|
|
12
|
+
plotH: number,
|
|
13
|
+
px: number,
|
|
14
|
+
py: number,
|
|
15
|
+
dx: number,
|
|
16
|
+
dy: number,
|
|
17
|
+
): { x: number; y: number } {
|
|
18
|
+
if (dx === 0 && dy === 0) return { x: px, y: py };
|
|
19
|
+
let tMin = Infinity;
|
|
20
|
+
if (dx > 0) tMin = Math.min(tMin, (plotW - px) / dx);
|
|
21
|
+
else if (dx < 0) tMin = Math.min(tMin, -px / dx);
|
|
22
|
+
if (dy > 0) tMin = Math.min(tMin, (plotH - py) / dy);
|
|
23
|
+
else if (dy < 0) tMin = Math.min(tMin, -py / dy);
|
|
24
|
+
return tMin === Infinity
|
|
25
|
+
? { x: px, y: py }
|
|
26
|
+
: { x: px + dx * tMin, y: py + dy * tMin };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ─── Timeframe seconds map ────────────────────────────────────────────────────
|
|
30
|
+
export const TF_SECS: Record<string, number> = {
|
|
31
|
+
'1s': 1, '5s': 5, '10s': 10, '30s': 30,
|
|
32
|
+
'1m': 60, '3m': 180, '5m': 300, '15m': 900, '30m': 1800,
|
|
33
|
+
'1h': 3600, '2h': 7200, '4h': 14400, '6h': 21600, '12h': 43200,
|
|
34
|
+
'1d': 86400, '3d': 259200, '1w': 604800, '1M': 2592000,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export function formatDuration(seconds: number): string {
|
|
38
|
+
if (seconds < 60) return `${seconds}s`;
|
|
39
|
+
if (seconds < 3600) { const m = Math.floor(seconds / 60); return `${m}m`; }
|
|
40
|
+
const h = Math.floor(seconds / 3600);
|
|
41
|
+
const m = Math.round((seconds % 3600) / 60);
|
|
42
|
+
return m > 0 ? `${h}h ${m}m` : `${h}h`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ─── Pitchfork helpers ────────────────────────────────────────────────────────
|
|
46
|
+
export type PF3 = {
|
|
47
|
+
x0: number; y0: number;
|
|
48
|
+
x1: number; y1: number;
|
|
49
|
+
x2: number; y2: number;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export function getPF3(d: Drawing, t: CoordTransform): PF3 | null {
|
|
53
|
+
const p0 = d.points[0], p1 = d.points[1], p2 = d.points[2];
|
|
54
|
+
if (!p0 || !p1 || !p2) return null;
|
|
55
|
+
return {
|
|
56
|
+
x0: t.timeToX(p0.time), y0: t.priceToY(p0.price),
|
|
57
|
+
x1: t.timeToX(p1.time), y1: t.priceToY(p1.price),
|
|
58
|
+
x2: t.timeToX(p2.time), y2: t.priceToY(p2.price),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function drawPitchforkLines(
|
|
63
|
+
ctx: CanvasRenderingContext2D,
|
|
64
|
+
t: CoordTransform,
|
|
65
|
+
pivotX: number, pivotY: number,
|
|
66
|
+
midX: number, midY: number,
|
|
67
|
+
x1: number, y1: number,
|
|
68
|
+
x2: number, y2: number,
|
|
69
|
+
): void {
|
|
70
|
+
const dx = midX - pivotX, dy = midY - pivotY;
|
|
71
|
+
const medExit = rayExit(t.plotWidth, t.plotHeight, midX, midY, dx, dy);
|
|
72
|
+
ctx.beginPath(); ctx.moveTo(pivotX, pivotY); ctx.lineTo(medExit.x, medExit.y); ctx.stroke();
|
|
73
|
+
const upExit = rayExit(t.plotWidth, t.plotHeight, x1, y1, dx, dy);
|
|
74
|
+
ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(upExit.x, upExit.y); ctx.stroke();
|
|
75
|
+
const dnExit = rayExit(t.plotWidth, t.plotHeight, x2, y2, dx, dy);
|
|
76
|
+
ctx.beginPath(); ctx.moveTo(x2, y2); ctx.lineTo(dnExit.x, dnExit.y); ctx.stroke();
|
|
77
|
+
ctx.save(); ctx.globalAlpha = 0.4;
|
|
78
|
+
ctx.beginPath(); ctx.moveTo(pivotX, pivotY); ctx.lineTo(x1, y1); ctx.stroke();
|
|
79
|
+
ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.stroke();
|
|
80
|
+
ctx.beginPath(); ctx.moveTo(x2, y2); ctx.lineTo(pivotX, pivotY); ctx.stroke();
|
|
81
|
+
ctx.restore();
|
|
82
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CanvasLayer — a single <canvas> element inside the chart container.
|
|
3
|
+
* Each layer has an independent bitmap; layers are stacked via CSS z-index.
|
|
4
|
+
* Physical pixels are pre-scaled by devicePixelRatio for crisp rendering.
|
|
5
|
+
*/
|
|
6
|
+
export type CanvasLayerOptions = {
|
|
7
|
+
readonly width: number;
|
|
8
|
+
readonly height: number;
|
|
9
|
+
readonly dpr: number;
|
|
10
|
+
readonly zIndex: number;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export class CanvasLayer {
|
|
14
|
+
readonly canvas: HTMLCanvasElement;
|
|
15
|
+
readonly context: CanvasRenderingContext2D;
|
|
16
|
+
|
|
17
|
+
private _width: number;
|
|
18
|
+
private _height: number;
|
|
19
|
+
private _dpr: number;
|
|
20
|
+
|
|
21
|
+
constructor(container: HTMLElement, options: CanvasLayerOptions) {
|
|
22
|
+
this._width = options.width;
|
|
23
|
+
this._height = options.height;
|
|
24
|
+
this._dpr = options.dpr;
|
|
25
|
+
|
|
26
|
+
this.canvas = document.createElement('canvas');
|
|
27
|
+
this.canvas.style.position = 'absolute';
|
|
28
|
+
this.canvas.style.inset = '0';
|
|
29
|
+
this.canvas.style.zIndex = String(options.zIndex);
|
|
30
|
+
this.canvas.style.pointerEvents = options.zIndex === 3 ? 'auto' : 'none';
|
|
31
|
+
|
|
32
|
+
const ctx = this.canvas.getContext('2d');
|
|
33
|
+
if (!ctx) throw new Error('[ForgeCharts] Failed to acquire 2D canvas context');
|
|
34
|
+
this.context = ctx;
|
|
35
|
+
|
|
36
|
+
this._applyDpr();
|
|
37
|
+
container.appendChild(this.canvas);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get width(): number {
|
|
41
|
+
return this._width;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get height(): number {
|
|
45
|
+
return this._height;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get dpr(): number {
|
|
49
|
+
return this._dpr;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
resize(width: number, height: number, dpr: number): void {
|
|
53
|
+
const physW = Math.round(width * dpr);
|
|
54
|
+
const physH = Math.round(height * dpr);
|
|
55
|
+
// Setting canvas.width/height always clears the bitmap, so skip if nothing changed.
|
|
56
|
+
if (physW === this.canvas.width && physH === this.canvas.height && dpr === this._dpr) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
this._width = width;
|
|
60
|
+
this._height = height;
|
|
61
|
+
this._dpr = dpr;
|
|
62
|
+
this._applyDpr();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
destroy(): void {
|
|
66
|
+
this.canvas.remove();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private _applyDpr(): void {
|
|
70
|
+
const { canvas, context, _width: w, _height: h, _dpr: dpr } = this;
|
|
71
|
+
canvas.width = Math.round(w * dpr);
|
|
72
|
+
canvas.height = Math.round(h * dpr);
|
|
73
|
+
canvas.style.width = `${w}px`;
|
|
74
|
+
canvas.style.height = `${h}px`;
|
|
75
|
+
context.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
76
|
+
}
|
|
77
|
+
}
|