@363045841yyt/klinechart-core 0.7.3 → 0.7.5-alpha.2
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/README.md +201 -201
- package/README.zh-CN.md +201 -201
- package/dist/engine/renderers/webgl/candleSurface.js +47 -47
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -2
- package/dist/version.js.map +1 -1
- package/package.json +129 -122
- package/src/__tests__/signal.test.ts +124 -124
- package/src/config/chartSettings.ts +66 -66
- package/src/controllers/__tests__/drawing.test.ts +214 -214
- package/src/controllers/__tests__/indicatorSelector.test.ts +481 -481
- package/src/controllers/__tests__/toolbar.test.ts +225 -225
- package/src/controllers/createChartController.ts +665 -665
- package/src/controllers/createDrawingController.ts +96 -96
- package/src/controllers/createIndicatorSelectorController.ts +307 -307
- package/src/controllers/createToolbarController.ts +146 -146
- package/src/controllers/index.ts +19 -19
- package/src/controllers/types.ts +284 -284
- package/src/engine/__tests__/chart.dpr.test.ts +401 -401
- package/src/engine/__tests__/paneRenderer.resize.test.ts +92 -92
- package/src/engine/chart-store.ts +121 -121
- package/src/engine/chart.d.ts +617 -617
- package/src/engine/chart.ts +2815 -2815
- package/src/engine/controller/__tests__/interaction.dpr.test.ts +259 -259
- package/src/engine/controller/interaction.ts +722 -722
- package/src/engine/controller/markerInteraction.ts +130 -130
- package/src/engine/controller/pinchTracker.ts +82 -82
- package/src/engine/controller/tooltipPosition.ts +48 -48
- package/src/engine/draw/__tests__/pixelAlign.spec.ts +176 -176
- package/src/engine/draw/pixelAlign.ts +259 -259
- package/src/engine/drawing/index.ts +655 -655
- package/src/engine/drawing/interaction.ts +842 -842
- package/src/engine/drawing/plugin.ts +343 -343
- package/src/engine/indicators/__tests__/__fixtures__/golden/atr.json +38 -38
- package/src/engine/indicators/__tests__/__fixtures__/golden/dema.json +14 -14
- package/src/engine/indicators/__tests__/__fixtures__/golden/hma.json +14 -14
- package/src/engine/indicators/__tests__/__fixtures__/golden/index.ts +55 -55
- package/src/engine/indicators/__tests__/__fixtures__/golden/kama.json +14 -14
- package/src/engine/indicators/__tests__/__fixtures__/golden/tema.json +14 -14
- package/src/engine/indicators/__tests__/__fixtures__/golden/wma.json +40 -40
- package/src/engine/indicators/__tests__/__fixtures__/synthetic.ts +65 -65
- package/src/engine/indicators/__tests__/_propertyAssertions.ts +76 -76
- package/src/engine/indicators/__tests__/atr.test.ts +153 -153
- package/src/engine/indicators/__tests__/calculators.test.ts +614 -614
- package/src/engine/indicators/__tests__/cmf-mfi.test.ts +100 -100
- package/src/engine/indicators/__tests__/dema.test.ts +73 -73
- package/src/engine/indicators/__tests__/donchian.test.ts +70 -70
- package/src/engine/indicators/__tests__/hma.test.ts +73 -73
- package/src/engine/indicators/__tests__/ichimoku.test.ts +105 -105
- package/src/engine/indicators/__tests__/kama.test.ts +80 -80
- package/src/engine/indicators/__tests__/keltner.test.ts +65 -65
- package/src/engine/indicators/__tests__/pivot-fib.test.ts +110 -110
- package/src/engine/indicators/__tests__/roc.test.ts +68 -68
- package/src/engine/indicators/__tests__/sar.test.ts +86 -86
- package/src/engine/indicators/__tests__/scheduler.test.ts +831 -831
- package/src/engine/indicators/__tests__/soa.test.ts +533 -533
- package/src/engine/indicators/__tests__/structure.test.ts +110 -110
- package/src/engine/indicators/__tests__/supertrend.test.ts +65 -65
- package/src/engine/indicators/__tests__/tema.test.ts +68 -68
- package/src/engine/indicators/__tests__/trix.test.ts +70 -70
- package/src/engine/indicators/__tests__/volatility.test.ts +117 -117
- package/src/engine/indicators/__tests__/volume.test.ts +115 -115
- package/src/engine/indicators/__tests__/volumeProfile.test.ts +74 -74
- package/src/engine/indicators/__tests__/vwap.test.ts +69 -69
- package/src/engine/indicators/__tests__/wma.test.ts +112 -112
- package/src/engine/indicators/__tests__/zones.test.ts +95 -95
- package/src/engine/indicators/atrState.ts +27 -27
- package/src/engine/indicators/bollState.ts +51 -51
- package/src/engine/indicators/calculators.ts +2593 -2593
- package/src/engine/indicators/cciState.ts +25 -25
- package/src/engine/indicators/chaikinVolState.ts +32 -32
- package/src/engine/indicators/cmfState.ts +27 -27
- package/src/engine/indicators/demaState.ts +27 -27
- package/src/engine/indicators/donchianState.ts +43 -43
- package/src/engine/indicators/eneState.ts +43 -43
- package/src/engine/indicators/expmaState.ts +43 -43
- package/src/engine/indicators/fastkState.ts +25 -25
- package/src/engine/indicators/fibState.ts +41 -41
- package/src/engine/indicators/hmaState.ts +27 -27
- package/src/engine/indicators/hvState.ts +28 -28
- package/src/engine/indicators/ichimokuState.ts +70 -70
- package/src/engine/indicators/indicator.worker.ts +169 -169
- package/src/engine/indicators/indicatorDefinitionRegistry.ts +62 -62
- package/src/engine/indicators/indicatorMetadata.ts +110 -110
- package/src/engine/indicators/indicatorRegistry.ts +106 -106
- package/src/engine/indicators/indicatorRuntime.ts +1548 -1548
- package/src/engine/indicators/kamaState.ts +34 -34
- package/src/engine/indicators/keltnerState.ts +49 -49
- package/src/engine/indicators/kstState.ts +42 -42
- package/src/engine/indicators/maState.ts +36 -36
- package/src/engine/indicators/macdState.ts +76 -76
- package/src/engine/indicators/mfiState.ts +27 -27
- package/src/engine/indicators/momState.ts +25 -25
- package/src/engine/indicators/obvState.ts +25 -25
- package/src/engine/indicators/parkinsonState.ts +28 -28
- package/src/engine/indicators/pivotState.ts +51 -51
- package/src/engine/indicators/pvtState.ts +25 -25
- package/src/engine/indicators/rocState.ts +27 -27
- package/src/engine/indicators/rsiState.ts +65 -65
- package/src/engine/indicators/sarState.ts +41 -41
- package/src/engine/indicators/scheduler.ts +1205 -1205
- package/src/engine/indicators/soa.ts +352 -352
- package/src/engine/indicators/stateComposer.ts +1262 -1262
- package/src/engine/indicators/stochState.ts +26 -26
- package/src/engine/indicators/structureState.ts +69 -69
- package/src/engine/indicators/supertrendState.ts +37 -37
- package/src/engine/indicators/temaState.ts +27 -27
- package/src/engine/indicators/trixState.ts +35 -35
- package/src/engine/indicators/vmaState.ts +27 -27
- package/src/engine/indicators/volumeProfileState.ts +63 -63
- package/src/engine/indicators/vwapState.ts +29 -29
- package/src/engine/indicators/wmaState.ts +27 -27
- package/src/engine/indicators/wmsrState.ts +25 -25
- package/src/engine/indicators/workerProtocol.ts +613 -613
- package/src/engine/indicators/zonesState.ts +47 -47
- package/src/engine/layout/pane.ts +161 -161
- package/src/engine/marker/registry.ts +265 -265
- package/src/engine/paneRenderer.ts +169 -169
- package/src/engine/renderers/Indicator/atr.ts +237 -237
- package/src/engine/renderers/Indicator/boll.ts +317 -317
- package/src/engine/renderers/Indicator/cci.ts +275 -275
- package/src/engine/renderers/Indicator/chaikinVol.ts +138 -138
- package/src/engine/renderers/Indicator/cmf.ts +137 -137
- package/src/engine/renderers/Indicator/dema.ts +136 -136
- package/src/engine/renderers/Indicator/donchian.ts +137 -137
- package/src/engine/renderers/Indicator/ene.ts +271 -271
- package/src/engine/renderers/Indicator/expma.ts +197 -197
- package/src/engine/renderers/Indicator/fastk.ts +316 -316
- package/src/engine/renderers/Indicator/fib.ts +141 -141
- package/src/engine/renderers/Indicator/hma.ts +136 -136
- package/src/engine/renderers/Indicator/hv.ts +124 -124
- package/src/engine/renderers/Indicator/ichimoku.ts +181 -181
- package/src/engine/renderers/Indicator/index.ts +241 -241
- package/src/engine/renderers/Indicator/indicatorData.ts +650 -650
- package/src/engine/renderers/Indicator/kama.ts +136 -136
- package/src/engine/renderers/Indicator/keltner.ts +137 -137
- package/src/engine/renderers/Indicator/kst.ts +302 -302
- package/src/engine/renderers/Indicator/ma.ts +200 -200
- package/src/engine/renderers/Indicator/macd.ts +477 -477
- package/src/engine/renderers/Indicator/macdLegend.ts +141 -141
- package/src/engine/renderers/Indicator/mainIndicatorLegend.ts +272 -272
- package/src/engine/renderers/Indicator/mfi.ts +142 -142
- package/src/engine/renderers/Indicator/mom.ts +311 -311
- package/src/engine/renderers/Indicator/obv.ts +123 -123
- package/src/engine/renderers/Indicator/parkinson.ts +124 -124
- package/src/engine/renderers/Indicator/pivot.ts +131 -131
- package/src/engine/renderers/Indicator/pvt.ts +123 -123
- package/src/engine/renderers/Indicator/roc.ts +143 -143
- package/src/engine/renderers/Indicator/rsi.ts +390 -390
- package/src/engine/renderers/Indicator/sar.ts +113 -113
- package/src/engine/renderers/Indicator/scale/atr_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/cci_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/fastk_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/indicator_scale.ts +204 -204
- package/src/engine/renderers/Indicator/scale/kst_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/macd_scale.ts +22 -22
- package/src/engine/renderers/Indicator/scale/mom_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/rsi_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/stoch_scale.ts +19 -19
- package/src/engine/renderers/Indicator/scale/volume_scale.ts +26 -26
- package/src/engine/renderers/Indicator/scale/wmsr_scale.ts +19 -19
- package/src/engine/renderers/Indicator/stoch.ts +359 -359
- package/src/engine/renderers/Indicator/structure.ts +126 -126
- package/src/engine/renderers/Indicator/subPaneConfig.ts +265 -265
- package/src/engine/renderers/Indicator/supertrend.ts +115 -115
- package/src/engine/renderers/Indicator/tema.ts +136 -136
- package/src/engine/renderers/Indicator/trix.ts +158 -158
- package/src/engine/renderers/Indicator/vma.ts +124 -124
- package/src/engine/renderers/Indicator/volumeProfile.ts +125 -125
- package/src/engine/renderers/Indicator/vwap.ts +123 -123
- package/src/engine/renderers/Indicator/wma.ts +136 -136
- package/src/engine/renderers/Indicator/wmsr.ts +328 -328
- package/src/engine/renderers/Indicator/zones.ts +104 -104
- package/src/engine/renderers/__tests__/boll.renderer.test.ts +314 -314
- package/src/engine/renderers/__tests__/ene.renderer.test.ts +305 -305
- package/src/engine/renderers/__tests__/expma.renderer.test.ts +279 -279
- package/src/engine/renderers/__tests__/ma.renderer.test.ts +426 -426
- package/src/engine/renderers/__tests__/mainIndicatorLegend.renderer.test.ts +502 -502
- package/src/engine/renderers/__tests__/yAxis.renderer.test.ts +173 -173
- package/src/engine/renderers/candle.ts +459 -459
- package/src/engine/renderers/crosshair.ts +69 -69
- package/src/engine/renderers/customMarkers.ts +162 -162
- package/src/engine/renderers/extremaMarkers.ts +246 -246
- package/src/engine/renderers/gridLines.ts +90 -90
- package/src/engine/renderers/lastPrice.ts +97 -97
- package/src/engine/renderers/paneTitle.ts +136 -136
- package/src/engine/renderers/subVolume.ts +236 -236
- package/src/engine/renderers/timeAxis.ts +121 -121
- package/src/engine/renderers/webgl/candleSurface.ts +955 -955
- package/src/engine/renderers/webgl/sharedWebGLSurface.ts +146 -146
- package/src/engine/renderers/yAxis.ts +105 -105
- package/src/engine/scale/__tests__/logFormula.spec.ts +148 -148
- package/src/engine/scale/logFormula.ts +130 -130
- package/src/engine/scale/price.ts +39 -39
- package/src/engine/scale/priceScale.ts +264 -264
- package/src/engine/subPaneManager.ts +427 -427
- package/src/engine/theme/colors.ts +642 -642
- package/src/engine/theme/fonts.ts +20 -20
- package/src/engine/utils/klineConfig.ts +49 -49
- package/src/engine/utils/tickCount.ts +11 -11
- package/src/engine/utils/tickPosition.ts +214 -214
- package/src/engine/utils/zoom.ts +83 -83
- package/src/engine/viewport/viewport.ts +67 -67
- package/src/index.ts +3 -3
- package/src/plugin/ConfigManager.ts +93 -93
- package/src/plugin/EventBus.ts +77 -77
- package/src/plugin/HookSystem.ts +106 -106
- package/src/plugin/PluginHost.ts +243 -243
- package/src/plugin/PluginRegistry.ts +92 -92
- package/src/plugin/StateStore.ts +73 -73
- package/src/plugin/index.ts +19 -19
- package/src/plugin/rendererPluginManager.ts +368 -368
- package/src/plugin/stateKeys.ts +8 -8
- package/src/plugin/types.ts +526 -526
- package/src/reactivity/index.ts +2 -2
- package/src/reactivity/signal.ts +119 -119
- package/src/semantic/controller.ts +251 -251
- package/src/semantic/drawShape.ts +260 -260
- package/src/semantic/index.ts +28 -28
- package/src/semantic/schema.json +256 -256
- package/src/semantic/types.ts +251 -251
- package/src/semantic/validator.ts +349 -349
- package/src/types/kLine.ts +13 -13
- package/src/types/price.ts +56 -56
- package/src/types/volumePrice.ts +33 -33
- package/src/utils/dateFormat.ts +208 -208
- package/src/utils/kLineDraw/axis.ts +562 -562
- package/src/utils/priceToY.ts +34 -34
- package/src/utils/volumePrice.ts +202 -202
- package/src/version.ts +1 -1
|
@@ -1,655 +1,655 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
DrawingObject,
|
|
3
|
-
DrawingKind,
|
|
4
|
-
DrawingDefinition,
|
|
5
|
-
DrawingComputeContext,
|
|
6
|
-
DrawingGeometry,
|
|
7
|
-
DrawingStyle,
|
|
8
|
-
PointPrimitive,
|
|
9
|
-
LinePrimitive,
|
|
10
|
-
AreaPrimitive,
|
|
11
|
-
TextPrimitive,
|
|
12
|
-
} from '../../plugin'
|
|
13
|
-
import type { KLineData } from '../../types/price'
|
|
14
|
-
|
|
15
|
-
export type {
|
|
16
|
-
DrawingObject,
|
|
17
|
-
DrawingKind,
|
|
18
|
-
DrawingDefinition,
|
|
19
|
-
DrawingComputeContext,
|
|
20
|
-
DrawingGeometry,
|
|
21
|
-
DrawingStyle,
|
|
22
|
-
PointPrimitive,
|
|
23
|
-
LinePrimitive,
|
|
24
|
-
AreaPrimitive,
|
|
25
|
-
TextPrimitive,
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export class DrawingStore {
|
|
29
|
-
private drawings: DrawingObject[] = []
|
|
30
|
-
private selectedId: string | null = null
|
|
31
|
-
|
|
32
|
-
getSelectedId(): string | null {
|
|
33
|
-
return this.selectedId
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
setSelectedId(id: string | null): void {
|
|
37
|
-
this.selectedId = id
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
getAll(): DrawingObject[] {
|
|
41
|
-
return this.drawings
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
getVisibleByPane(paneId: string): DrawingObject[] {
|
|
45
|
-
return this.drawings
|
|
46
|
-
.filter((drawing) => drawing.visible && drawing.paneId === paneId)
|
|
47
|
-
.sort((a, b) => (a.zIndex ?? 0) - (b.zIndex ?? 0))
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
setAll(drawings: DrawingObject[]): void {
|
|
51
|
-
this.drawings = [...drawings]
|
|
52
|
-
if (this.selectedId && !this.drawings.some((d) => d.id === this.selectedId)) {
|
|
53
|
-
this.selectedId = null
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
upsert(drawing: DrawingObject): void {
|
|
58
|
-
const index = this.drawings.findIndex((item) => item.id === drawing.id)
|
|
59
|
-
if (index >= 0) {
|
|
60
|
-
this.drawings[index] = drawing
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
this.drawings.push(drawing)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
remove(id: string): void {
|
|
67
|
-
this.drawings = this.drawings.filter((drawing) => drawing.id !== id)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
clear(): void {
|
|
71
|
-
this.drawings = []
|
|
72
|
-
this.selectedId = null
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export class DrawingDefinitionRegistry {
|
|
77
|
-
private definitions = new Map<DrawingKind, DrawingDefinition>()
|
|
78
|
-
|
|
79
|
-
register<TParams = Record<string, unknown>>(definition: DrawingDefinition<TParams>): void {
|
|
80
|
-
this.definitions.set(definition.kind, definition as DrawingDefinition)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
get(kind: DrawingKind): DrawingDefinition | undefined {
|
|
84
|
-
return this.definitions.get(kind)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
compute(drawing: DrawingObject, context: DrawingComputeContext): DrawingGeometry | null {
|
|
88
|
-
const definition = this.get(drawing.kind)
|
|
89
|
-
if (!definition) return null
|
|
90
|
-
return definition.compute(drawing, context)
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export type PrimitiveRendererSet = {
|
|
95
|
-
point: (ctx: CanvasRenderingContext2D, primitive: PointPrimitive, dpr: number) => void
|
|
96
|
-
line: (
|
|
97
|
-
ctx: CanvasRenderingContext2D,
|
|
98
|
-
primitive: LinePrimitive,
|
|
99
|
-
viewportClip: { left: number; top: number; right: number; bottom: number },
|
|
100
|
-
dpr: number
|
|
101
|
-
) => void
|
|
102
|
-
area: (ctx: CanvasRenderingContext2D, primitive: AreaPrimitive, dpr: number) => void
|
|
103
|
-
text: (ctx: CanvasRenderingContext2D, primitive: TextPrimitive, dpr: number) => void
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function applyLineStyle(ctx: CanvasRenderingContext2D, style?: DrawingStyle): void {
|
|
107
|
-
ctx.strokeStyle = style?.stroke ?? '#2962ff'
|
|
108
|
-
ctx.lineWidth = style?.strokeWidth ?? 1
|
|
109
|
-
if (style?.strokeStyle === 'dashed') {
|
|
110
|
-
ctx.setLineDash([6, 4])
|
|
111
|
-
return
|
|
112
|
-
}
|
|
113
|
-
if (style?.strokeStyle === 'dotted') {
|
|
114
|
-
ctx.setLineDash([2, 3])
|
|
115
|
-
return
|
|
116
|
-
}
|
|
117
|
-
ctx.setLineDash([])
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function applyFillStyle(ctx: CanvasRenderingContext2D, style?: DrawingStyle): void {
|
|
121
|
-
ctx.fillStyle = style?.fill ?? style?.stroke ?? '#2962ff'
|
|
122
|
-
ctx.globalAlpha = style?.fillOpacity ?? 1
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function clipLineToRect(
|
|
126
|
-
x1: number,
|
|
127
|
-
y1: number,
|
|
128
|
-
x2: number,
|
|
129
|
-
y2: number,
|
|
130
|
-
rect: { left: number; top: number; right: number; bottom: number }
|
|
131
|
-
): { a: { x: number; y: number }; b: { x: number; y: number } } | null {
|
|
132
|
-
const INSIDE = 0
|
|
133
|
-
const LEFT = 1
|
|
134
|
-
const RIGHT = 2
|
|
135
|
-
const BOTTOM = 4
|
|
136
|
-
const TOP = 8
|
|
137
|
-
|
|
138
|
-
const computeCode = (x: number, y: number) => {
|
|
139
|
-
let code = INSIDE
|
|
140
|
-
if (x < rect.left) code |= LEFT
|
|
141
|
-
else if (x > rect.right) code |= RIGHT
|
|
142
|
-
if (y < rect.top) code |= TOP
|
|
143
|
-
else if (y > rect.bottom) code |= BOTTOM
|
|
144
|
-
return code
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
let ax = x1
|
|
148
|
-
let ay = y1
|
|
149
|
-
let bx = x2
|
|
150
|
-
let by = y2
|
|
151
|
-
|
|
152
|
-
while (true) {
|
|
153
|
-
const codeA = computeCode(ax, ay)
|
|
154
|
-
const codeB = computeCode(bx, by)
|
|
155
|
-
|
|
156
|
-
if (!(codeA | codeB)) {
|
|
157
|
-
return { a: { x: ax, y: ay }, b: { x: bx, y: by } }
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (codeA & codeB) {
|
|
161
|
-
return null
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const codeOut = codeA || codeB
|
|
165
|
-
let x = 0
|
|
166
|
-
let y = 0
|
|
167
|
-
|
|
168
|
-
if (codeOut & TOP) {
|
|
169
|
-
x = ax + ((bx - ax) * (rect.top - ay)) / (by - ay)
|
|
170
|
-
y = rect.top
|
|
171
|
-
} else if (codeOut & BOTTOM) {
|
|
172
|
-
x = ax + ((bx - ax) * (rect.bottom - ay)) / (by - ay)
|
|
173
|
-
y = rect.bottom
|
|
174
|
-
} else if (codeOut & RIGHT) {
|
|
175
|
-
y = ay + ((by - ay) * (rect.right - ax)) / (bx - ax)
|
|
176
|
-
x = rect.right
|
|
177
|
-
} else {
|
|
178
|
-
y = ay + ((by - ay) * (rect.left - ax)) / (bx - ax)
|
|
179
|
-
x = rect.left
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (codeOut === codeA) {
|
|
183
|
-
ax = x
|
|
184
|
-
ay = y
|
|
185
|
-
} else {
|
|
186
|
-
bx = x
|
|
187
|
-
by = y
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function extendLineToViewport(
|
|
193
|
-
primitive: LinePrimitive,
|
|
194
|
-
viewportClip: { left: number; top: number; right: number; bottom: number }
|
|
195
|
-
): { a: { x: number; y: number }; b: { x: number; y: number } } | null {
|
|
196
|
-
const { a, b, extend = 'none' } = primitive
|
|
197
|
-
if (extend === 'none') {
|
|
198
|
-
return clipLineToRect(a.x, a.y, b.x, b.y, viewportClip)
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const dx = b.x - a.x
|
|
202
|
-
const dy = b.y - a.y
|
|
203
|
-
if (dx === 0 && dy === 0) return null
|
|
204
|
-
|
|
205
|
-
const distance = Math.max(viewportClip.right - viewportClip.left, viewportClip.bottom - viewportClip.top) * 4
|
|
206
|
-
let start = a
|
|
207
|
-
let end = b
|
|
208
|
-
|
|
209
|
-
if (extend === 'left' || extend === 'both') {
|
|
210
|
-
start = { x: a.x - dx * distance, y: a.y - dy * distance }
|
|
211
|
-
}
|
|
212
|
-
if (extend === 'right' || extend === 'both') {
|
|
213
|
-
end = { x: b.x + dx * distance, y: b.y + dy * distance }
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
return clipLineToRect(start.x, start.y, end.x, end.y, viewportClip)
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
function getAnchorDataIndex(anchor: DrawingObject['anchors'][number], data: KLineData[]): number {
|
|
220
|
-
if (!Number.isFinite(anchor.index)) return -1
|
|
221
|
-
const index = Math.round(anchor.index)
|
|
222
|
-
if (index < 0 || index >= data.length) return -1
|
|
223
|
-
return index
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function formatSigned(value: number, digits = 2): string {
|
|
227
|
-
if (!Number.isFinite(value)) return '0'
|
|
228
|
-
const fixed = value.toFixed(digits)
|
|
229
|
-
return value > 0 ? `+${fixed}` : fixed
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
export function computeLinearRegression(values: number[]): { slope: number; intercept: number; stdDev: number } | null {
|
|
233
|
-
const n = values.length
|
|
234
|
-
if (n < 2) return null
|
|
235
|
-
|
|
236
|
-
let sumX = 0
|
|
237
|
-
let sumY = 0
|
|
238
|
-
let sumXY = 0
|
|
239
|
-
let sumXX = 0
|
|
240
|
-
for (let i = 0; i < n; i++) {
|
|
241
|
-
const x = i
|
|
242
|
-
const y = values[i]!
|
|
243
|
-
sumX += x
|
|
244
|
-
sumY += y
|
|
245
|
-
sumXY += x * y
|
|
246
|
-
sumXX += x * x
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
const denominator = n * sumXX - sumX * sumX
|
|
250
|
-
if (denominator === 0) return null
|
|
251
|
-
|
|
252
|
-
const slope = (n * sumXY - sumX * sumY) / denominator
|
|
253
|
-
const intercept = (sumY - slope * sumX) / n
|
|
254
|
-
|
|
255
|
-
let variance = 0
|
|
256
|
-
for (let i = 0; i < n; i++) {
|
|
257
|
-
const fitted = intercept + slope * i
|
|
258
|
-
const diff = values[i]! - fitted
|
|
259
|
-
variance += diff * diff
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return {
|
|
263
|
-
slope,
|
|
264
|
-
intercept,
|
|
265
|
-
stdDev: Math.sqrt(variance / n),
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export function createDefaultPrimitiveRendererSet(): PrimitiveRendererSet {
|
|
270
|
-
return {
|
|
271
|
-
point(ctx, primitive, dpr) {
|
|
272
|
-
const radius = primitive.style?.pointRadius ?? 4
|
|
273
|
-
ctx.save()
|
|
274
|
-
ctx.fillStyle = primitive.style?.fill ?? primitive.style?.stroke ?? '#2962ff'
|
|
275
|
-
ctx.beginPath()
|
|
276
|
-
ctx.arc(primitive.point.x, primitive.point.y, Math.max(radius, 1 / dpr), 0, Math.PI * 2)
|
|
277
|
-
ctx.fill()
|
|
278
|
-
ctx.restore()
|
|
279
|
-
},
|
|
280
|
-
|
|
281
|
-
line(ctx, primitive, viewportClip, dpr) {
|
|
282
|
-
const clipped = extendLineToViewport(primitive, viewportClip)
|
|
283
|
-
if (!clipped) return
|
|
284
|
-
|
|
285
|
-
ctx.save()
|
|
286
|
-
applyLineStyle(ctx, primitive.style)
|
|
287
|
-
const lineWidth = primitive.style?.strokeWidth ?? 1
|
|
288
|
-
const align = lineWidth <= 1 ? 0.5 / dpr : 0
|
|
289
|
-
ctx.beginPath()
|
|
290
|
-
ctx.moveTo(clipped.a.x + align, clipped.a.y + align)
|
|
291
|
-
ctx.lineTo(clipped.b.x + align, clipped.b.y + align)
|
|
292
|
-
ctx.stroke()
|
|
293
|
-
|
|
294
|
-
// 绘制端点(使用原始锚点位置,不是裁剪后的位置)
|
|
295
|
-
if (primitive.showEndpoints !== false) {
|
|
296
|
-
const pointRadius = primitive.style?.pointRadius ?? 4
|
|
297
|
-
ctx.fillStyle = primitive.style?.stroke ?? '#2962ff'
|
|
298
|
-
|
|
299
|
-
ctx.beginPath()
|
|
300
|
-
ctx.arc(primitive.a.x, primitive.a.y, Math.max(pointRadius, 1 / dpr), 0, Math.PI * 2)
|
|
301
|
-
ctx.fill()
|
|
302
|
-
|
|
303
|
-
ctx.beginPath()
|
|
304
|
-
ctx.arc(primitive.b.x, primitive.b.y, Math.max(pointRadius, 1 / dpr), 0, Math.PI * 2)
|
|
305
|
-
ctx.fill()
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
ctx.restore()
|
|
309
|
-
},
|
|
310
|
-
|
|
311
|
-
area(ctx, primitive) {
|
|
312
|
-
if (primitive.points.length === 0) return
|
|
313
|
-
ctx.save()
|
|
314
|
-
applyFillStyle(ctx, primitive.style)
|
|
315
|
-
ctx.beginPath()
|
|
316
|
-
ctx.moveTo(primitive.points[0]!.x, primitive.points[0]!.y)
|
|
317
|
-
for (let i = 1; i < primitive.points.length; i++) {
|
|
318
|
-
const point = primitive.points[i]!
|
|
319
|
-
ctx.lineTo(point.x, point.y)
|
|
320
|
-
}
|
|
321
|
-
if (primitive.closed) {
|
|
322
|
-
ctx.closePath()
|
|
323
|
-
}
|
|
324
|
-
ctx.fill()
|
|
325
|
-
ctx.restore()
|
|
326
|
-
},
|
|
327
|
-
|
|
328
|
-
text(ctx, primitive) {
|
|
329
|
-
ctx.save()
|
|
330
|
-
ctx.fillStyle = primitive.style?.textColor ?? primitive.style?.stroke ?? '#2962ff'
|
|
331
|
-
ctx.font = `${primitive.style?.fontSize ?? 12}px sans-serif`
|
|
332
|
-
ctx.textAlign = primitive.align ?? 'left'
|
|
333
|
-
ctx.textBaseline = primitive.baseline ?? 'bottom'
|
|
334
|
-
ctx.fillText(primitive.text, primitive.point.x, primitive.point.y)
|
|
335
|
-
ctx.restore()
|
|
336
|
-
},
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
export function createTwoPointLineDefinition(kind: DrawingKind, extend: LinePrimitive['extend']): DrawingDefinition {
|
|
341
|
-
return {
|
|
342
|
-
kind,
|
|
343
|
-
minAnchors: 2,
|
|
344
|
-
maxAnchors: 2,
|
|
345
|
-
compute(drawing, context) {
|
|
346
|
-
const [first, second] = drawing.anchors
|
|
347
|
-
if (!first || !second) return { primitives: [] }
|
|
348
|
-
return {
|
|
349
|
-
primitives: [
|
|
350
|
-
{
|
|
351
|
-
kind: 'line',
|
|
352
|
-
a: context.toScreen(first),
|
|
353
|
-
b: context.toScreen(second),
|
|
354
|
-
extend,
|
|
355
|
-
style: drawing.style,
|
|
356
|
-
},
|
|
357
|
-
],
|
|
358
|
-
}
|
|
359
|
-
},
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
export function createSingleAnchorLineDefinition(kind: DrawingKind): DrawingDefinition {
|
|
364
|
-
return {
|
|
365
|
-
kind,
|
|
366
|
-
minAnchors: 1,
|
|
367
|
-
maxAnchors: 1,
|
|
368
|
-
compute(drawing, context) {
|
|
369
|
-
const [anchor] = drawing.anchors
|
|
370
|
-
if (!anchor) return { primitives: [] }
|
|
371
|
-
const point = context.toScreen(anchor)
|
|
372
|
-
const bottom = context.pane.height
|
|
373
|
-
const right = context.viewport.plotWidth
|
|
374
|
-
|
|
375
|
-
if (kind === 'horizontal-line') {
|
|
376
|
-
return {
|
|
377
|
-
primitives: [
|
|
378
|
-
{ kind: 'line', a: { x: 0, y: point.y }, b: { x: right, y: point.y }, showEndpoints: false, style: drawing.style },
|
|
379
|
-
{ kind: 'point', point, style: drawing.style },
|
|
380
|
-
],
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
if (kind === 'horizontal-ray') {
|
|
385
|
-
return {
|
|
386
|
-
primitives: [
|
|
387
|
-
{ kind: 'line', a: point, b: { x: right, y: point.y }, showEndpoints: false, style: drawing.style },
|
|
388
|
-
{ kind: 'point', point, style: drawing.style },
|
|
389
|
-
],
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
if (kind === 'vertical-line') {
|
|
394
|
-
return {
|
|
395
|
-
primitives: [
|
|
396
|
-
{ kind: 'line', a: { x: point.x, y: 0 }, b: { x: point.x, y: bottom }, showEndpoints: false, style: drawing.style },
|
|
397
|
-
{ kind: 'point', point, style: drawing.style },
|
|
398
|
-
],
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
// cross-line: 十字线,显示水平和垂直线,锚点显示一个点,边缘不显示端点
|
|
403
|
-
return {
|
|
404
|
-
primitives: [
|
|
405
|
-
{ kind: 'line', a: { x: 0, y: point.y }, b: { x: right, y: point.y }, showEndpoints: false, style: drawing.style },
|
|
406
|
-
{ kind: 'line', a: { x: point.x, y: 0 }, b: { x: point.x, y: bottom }, showEndpoints: false, style: drawing.style },
|
|
407
|
-
{ kind: 'point', point, style: drawing.style },
|
|
408
|
-
],
|
|
409
|
-
}
|
|
410
|
-
},
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
export function createInfoLineDefinition(): DrawingDefinition {
|
|
415
|
-
return {
|
|
416
|
-
kind: 'info-line',
|
|
417
|
-
minAnchors: 2,
|
|
418
|
-
maxAnchors: 2,
|
|
419
|
-
compute(drawing, context) {
|
|
420
|
-
const [first, second] = drawing.anchors
|
|
421
|
-
if (!first || !second) return { primitives: [] }
|
|
422
|
-
const a = context.toScreen(first)
|
|
423
|
-
const b = context.toScreen(second)
|
|
424
|
-
const firstIndex = Math.round(first.index)
|
|
425
|
-
const secondIndex = Math.round(second.index)
|
|
426
|
-
const bars = secondIndex - firstIndex
|
|
427
|
-
const delta = second.price - first.price
|
|
428
|
-
const percent = first.price !== 0 ? (delta / first.price) * 100 : 0
|
|
429
|
-
const angle = Math.atan2(b.y - a.y, b.x - a.x) * (180 / Math.PI)
|
|
430
|
-
const text = `${formatSigned(delta)} (${formatSigned(percent)}%) ${bars} bars ${formatSigned(angle)}°`
|
|
431
|
-
|
|
432
|
-
return {
|
|
433
|
-
primitives: [
|
|
434
|
-
{ kind: 'line', a, b, style: drawing.style },
|
|
435
|
-
{
|
|
436
|
-
kind: 'text',
|
|
437
|
-
point: { x: (a.x + b.x) / 2 + 8, y: Math.min(a.y, b.y) - 8 },
|
|
438
|
-
text,
|
|
439
|
-
align: 'left',
|
|
440
|
-
baseline: 'bottom',
|
|
441
|
-
style: drawing.style,
|
|
442
|
-
},
|
|
443
|
-
],
|
|
444
|
-
meta: { delta, percent, bars, angle },
|
|
445
|
-
}
|
|
446
|
-
},
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
export function createParallelChannelDefinition(): DrawingDefinition {
|
|
451
|
-
return {
|
|
452
|
-
kind: 'parallel-channel',
|
|
453
|
-
minAnchors: 3,
|
|
454
|
-
maxAnchors: 3,
|
|
455
|
-
compute(drawing, context) {
|
|
456
|
-
const [first, second, third] = drawing.anchors
|
|
457
|
-
if (!first || !second || !third) return { primitives: [] }
|
|
458
|
-
const p1 = context.toScreen(first)
|
|
459
|
-
const p2 = context.toScreen(second)
|
|
460
|
-
const p3 = context.toScreen(third)
|
|
461
|
-
const dx = p2.x - p1.x
|
|
462
|
-
const dy = p2.y - p1.y
|
|
463
|
-
const p4 = { x: p3.x + dx, y: p3.y + dy }
|
|
464
|
-
const extend = (drawing.params as { extend?: LinePrimitive['extend'] } | undefined)?.extend ?? 'none'
|
|
465
|
-
|
|
466
|
-
// 计算 p4 对应的锚点信息(用于轴标签注册)
|
|
467
|
-
const p4Index = third.index + (second.index - first.index)
|
|
468
|
-
const p4Time = third.time
|
|
469
|
-
? (typeof third.time === 'string' ? new Date(third.time).getTime() : third.time) +
|
|
470
|
-
((typeof second.time === 'string' ? new Date(second.time).getTime() : second.time ?? 0) -
|
|
471
|
-
(typeof first.time === 'string' ? new Date(first.time).getTime() : first.time ?? 0))
|
|
472
|
-
: undefined
|
|
473
|
-
|
|
474
|
-
return {
|
|
475
|
-
primitives: [
|
|
476
|
-
{
|
|
477
|
-
kind: 'area',
|
|
478
|
-
points: [p1, p2, p4, p3],
|
|
479
|
-
closed: true,
|
|
480
|
-
style: drawing.style,
|
|
481
|
-
},
|
|
482
|
-
{ kind: 'line', a: p1, b: p2, extend, style: drawing.style },
|
|
483
|
-
{ kind: 'line', a: p3, b: p4, extend, style: drawing.style },
|
|
484
|
-
],
|
|
485
|
-
computedAnchors: [
|
|
486
|
-
{ id: `${drawing.id}-p4`, index: p4Index, time: p4Time, price: third.price + (second.price - first.price) },
|
|
487
|
-
],
|
|
488
|
-
}
|
|
489
|
-
},
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
export function createFlatLineDefinition(): DrawingDefinition {
|
|
494
|
-
return {
|
|
495
|
-
kind: 'flat-line',
|
|
496
|
-
minAnchors: 3,
|
|
497
|
-
maxAnchors: 3,
|
|
498
|
-
compute(drawing, context) {
|
|
499
|
-
const [first, second, third] = drawing.anchors
|
|
500
|
-
if (!first || !second || !third) return { primitives: [] }
|
|
501
|
-
|
|
502
|
-
const p1 = context.toScreen(first)
|
|
503
|
-
const p2 = context.toScreen(second)
|
|
504
|
-
const thirdScreen = context.toScreen(third)
|
|
505
|
-
const h1 = { x: p1.x, y: thirdScreen.y }
|
|
506
|
-
const h2 = { x: p2.x, y: thirdScreen.y }
|
|
507
|
-
|
|
508
|
-
return {
|
|
509
|
-
primitives: [
|
|
510
|
-
{
|
|
511
|
-
kind: 'area',
|
|
512
|
-
points: [p1, p2, h2, h1],
|
|
513
|
-
closed: true,
|
|
514
|
-
style: drawing.style,
|
|
515
|
-
},
|
|
516
|
-
{ kind: 'line', a: p1, b: p2, style: drawing.style },
|
|
517
|
-
{ kind: 'line', a: h1, b: h2, style: drawing.style },
|
|
518
|
-
{ kind: 'point', point: h1, style: drawing.style },
|
|
519
|
-
{ kind: 'point', point: h2, style: drawing.style },
|
|
520
|
-
],
|
|
521
|
-
computedAnchors: [
|
|
522
|
-
{ id: `${drawing.id}-h1`, index: first.index, time: first.time, price: third.price },
|
|
523
|
-
{ id: `${drawing.id}-h2`, index: second.index, time: second.time, price: third.price },
|
|
524
|
-
],
|
|
525
|
-
}
|
|
526
|
-
},
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
export function createDisjointChannelDefinition(): DrawingDefinition {
|
|
531
|
-
return {
|
|
532
|
-
kind: 'disjoint-channel',
|
|
533
|
-
minAnchors: 3,
|
|
534
|
-
maxAnchors: 3,
|
|
535
|
-
compute(drawing, context) {
|
|
536
|
-
const [first, second, third] = drawing.anchors
|
|
537
|
-
if (!first || !second || !third) return { primitives: [] }
|
|
538
|
-
|
|
539
|
-
const p1 = context.toScreen(first)
|
|
540
|
-
const p2 = context.toScreen(second)
|
|
541
|
-
const p3 = context.toScreen(third)
|
|
542
|
-
|
|
543
|
-
// 第二条线:过 p3,斜率取反
|
|
544
|
-
const dx = p2.x - p1.x
|
|
545
|
-
const dy = p2.y - p1.y
|
|
546
|
-
const p4 = { x: p3.x + dx, y: p3.y - dy }
|
|
547
|
-
|
|
548
|
-
// 计算 p4 对应的锚点信息(用于轴标签注册)
|
|
549
|
-
const p4Index = third.index + (second.index - first.index)
|
|
550
|
-
const p4Price = third.price - (second.price - first.price)
|
|
551
|
-
const p4Time = third.time
|
|
552
|
-
? (typeof third.time === 'string' ? new Date(third.time).getTime() : third.time) -
|
|
553
|
-
((typeof second.time === 'string' ? new Date(second.time).getTime() : second.time ?? 0) -
|
|
554
|
-
(typeof first.time === 'string' ? new Date(first.time).getTime() : first.time ?? 0))
|
|
555
|
-
: undefined
|
|
556
|
-
|
|
557
|
-
return {
|
|
558
|
-
primitives: [
|
|
559
|
-
// 填充区域
|
|
560
|
-
{
|
|
561
|
-
kind: 'area',
|
|
562
|
-
points: [p1, p2, p4, p3],
|
|
563
|
-
closed: true,
|
|
564
|
-
style: drawing.style,
|
|
565
|
-
},
|
|
566
|
-
// 斜率 k 的线
|
|
567
|
-
{ kind: 'line', a: p1, b: p2, style: drawing.style },
|
|
568
|
-
// 斜率 -k 的线
|
|
569
|
-
{ kind: 'line', a: p3, b: p4, style: drawing.style },
|
|
570
|
-
],
|
|
571
|
-
computedAnchors: [
|
|
572
|
-
{ id: `${drawing.id}-p4`, index: p4Index, time: p4Time, price: p4Price },
|
|
573
|
-
],
|
|
574
|
-
}
|
|
575
|
-
},
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
export function createRegressionChannelDefinition(): DrawingDefinition {
|
|
580
|
-
return {
|
|
581
|
-
kind: 'regression-channel',
|
|
582
|
-
minAnchors: 2,
|
|
583
|
-
maxAnchors: 2,
|
|
584
|
-
compute(drawing, context) {
|
|
585
|
-
const [first, second] = drawing.anchors
|
|
586
|
-
if (!first || !second) return { primitives: [] }
|
|
587
|
-
const firstIndex = getAnchorDataIndex(first, context.seriesData)
|
|
588
|
-
const secondIndex = getAnchorDataIndex(second, context.seriesData)
|
|
589
|
-
if (firstIndex < 0 && secondIndex < 0) return { primitives: [] }
|
|
590
|
-
|
|
591
|
-
const clampedFirstIndex = Math.min(Math.max(Math.round(first.index), 0), context.seriesData.length - 1)
|
|
592
|
-
const clampedSecondIndex = Math.min(Math.max(Math.round(second.index), 0), context.seriesData.length - 1)
|
|
593
|
-
const startIndex = Math.min(clampedFirstIndex, clampedSecondIndex)
|
|
594
|
-
const endIndex = Math.max(clampedFirstIndex, clampedSecondIndex)
|
|
595
|
-
const slice = context.seriesData.slice(startIndex, endIndex + 1)
|
|
596
|
-
const regression = computeLinearRegression(slice.map((item) => item.close))
|
|
597
|
-
if (!regression) return { primitives: [] }
|
|
598
|
-
|
|
599
|
-
const sigma = (drawing.params as { sigma?: number } | undefined)?.sigma ?? 2
|
|
600
|
-
const offset = regression.stdDev * sigma
|
|
601
|
-
const firstValue = regression.intercept
|
|
602
|
-
const lastValue = regression.intercept + regression.slope * (slice.length - 1)
|
|
603
|
-
|
|
604
|
-
const startAnchor = { id: `${drawing.id}-reg-start`, index: Math.round(first.index), time: context.seriesData[startIndex]!.timestamp, price: firstValue }
|
|
605
|
-
const endAnchor = { id: `${drawing.id}-reg-end`, index: Math.round(second.index), time: context.seriesData[endIndex]!.timestamp, price: lastValue }
|
|
606
|
-
const upperStartAnchor = { ...startAnchor, id: `${drawing.id}-reg-upper-start`, price: firstValue + offset }
|
|
607
|
-
const upperEndAnchor = { ...endAnchor, id: `${drawing.id}-reg-upper-end`, price: lastValue + offset }
|
|
608
|
-
const lowerStartAnchor = { ...startAnchor, id: `${drawing.id}-reg-lower-start`, price: firstValue - offset }
|
|
609
|
-
const lowerEndAnchor = { ...endAnchor, id: `${drawing.id}-reg-lower-end`, price: lastValue - offset }
|
|
610
|
-
|
|
611
|
-
const middleA = context.toScreen(startAnchor)
|
|
612
|
-
const middleB = context.toScreen(endAnchor)
|
|
613
|
-
const upperA = context.toScreen(upperStartAnchor)
|
|
614
|
-
const upperB = context.toScreen(upperEndAnchor)
|
|
615
|
-
const lowerA = context.toScreen(lowerStartAnchor)
|
|
616
|
-
const lowerB = context.toScreen(lowerEndAnchor)
|
|
617
|
-
|
|
618
|
-
return {
|
|
619
|
-
primitives: [
|
|
620
|
-
{
|
|
621
|
-
kind: 'area',
|
|
622
|
-
points: [upperA, upperB, lowerB, lowerA],
|
|
623
|
-
closed: true,
|
|
624
|
-
style: drawing.style,
|
|
625
|
-
},
|
|
626
|
-
// 中间回归线使用虚线
|
|
627
|
-
{ kind: 'line', a: middleA, b: middleB, style: { ...drawing.style, strokeStyle: 'dashed' } },
|
|
628
|
-
{ kind: 'line', a: upperA, b: upperB, style: drawing.style },
|
|
629
|
-
{ kind: 'line', a: lowerA, b: lowerB, style: drawing.style },
|
|
630
|
-
],
|
|
631
|
-
computedAnchors: [startAnchor, endAnchor],
|
|
632
|
-
meta: { sigma, stdDev: regression.stdDev, slope: regression.slope },
|
|
633
|
-
}
|
|
634
|
-
},
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
export function registerDefaultDrawingDefinitions(registry: DrawingDefinitionRegistry): void {
|
|
639
|
-
registry.register(createTwoPointLineDefinition('trend-line', 'none'))
|
|
640
|
-
registry.register(createTwoPointLineDefinition('ray', 'right'))
|
|
641
|
-
registry.register(createTwoPointLineDefinition('extended-line', 'both'))
|
|
642
|
-
registry.register(createSingleAnchorLineDefinition('horizontal-line'))
|
|
643
|
-
registry.register(createSingleAnchorLineDefinition('horizontal-ray'))
|
|
644
|
-
registry.register(createSingleAnchorLineDefinition('vertical-line'))
|
|
645
|
-
registry.register(createSingleAnchorLineDefinition('cross-line'))
|
|
646
|
-
registry.register(createInfoLineDefinition())
|
|
647
|
-
registry.register(createParallelChannelDefinition())
|
|
648
|
-
registry.register(createRegressionChannelDefinition())
|
|
649
|
-
registry.register(createFlatLineDefinition())
|
|
650
|
-
registry.register(createDisjointChannelDefinition())
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
// 导出交互控制器
|
|
654
|
-
export { DrawingInteractionController } from './interaction'
|
|
655
|
-
export type { DrawingToolId, DrawingAnchorInput, DrawingInteractionCallbacks } from './interaction'
|
|
1
|
+
import type {
|
|
2
|
+
DrawingObject,
|
|
3
|
+
DrawingKind,
|
|
4
|
+
DrawingDefinition,
|
|
5
|
+
DrawingComputeContext,
|
|
6
|
+
DrawingGeometry,
|
|
7
|
+
DrawingStyle,
|
|
8
|
+
PointPrimitive,
|
|
9
|
+
LinePrimitive,
|
|
10
|
+
AreaPrimitive,
|
|
11
|
+
TextPrimitive,
|
|
12
|
+
} from '../../plugin'
|
|
13
|
+
import type { KLineData } from '../../types/price'
|
|
14
|
+
|
|
15
|
+
export type {
|
|
16
|
+
DrawingObject,
|
|
17
|
+
DrawingKind,
|
|
18
|
+
DrawingDefinition,
|
|
19
|
+
DrawingComputeContext,
|
|
20
|
+
DrawingGeometry,
|
|
21
|
+
DrawingStyle,
|
|
22
|
+
PointPrimitive,
|
|
23
|
+
LinePrimitive,
|
|
24
|
+
AreaPrimitive,
|
|
25
|
+
TextPrimitive,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class DrawingStore {
|
|
29
|
+
private drawings: DrawingObject[] = []
|
|
30
|
+
private selectedId: string | null = null
|
|
31
|
+
|
|
32
|
+
getSelectedId(): string | null {
|
|
33
|
+
return this.selectedId
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
setSelectedId(id: string | null): void {
|
|
37
|
+
this.selectedId = id
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getAll(): DrawingObject[] {
|
|
41
|
+
return this.drawings
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
getVisibleByPane(paneId: string): DrawingObject[] {
|
|
45
|
+
return this.drawings
|
|
46
|
+
.filter((drawing) => drawing.visible && drawing.paneId === paneId)
|
|
47
|
+
.sort((a, b) => (a.zIndex ?? 0) - (b.zIndex ?? 0))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
setAll(drawings: DrawingObject[]): void {
|
|
51
|
+
this.drawings = [...drawings]
|
|
52
|
+
if (this.selectedId && !this.drawings.some((d) => d.id === this.selectedId)) {
|
|
53
|
+
this.selectedId = null
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
upsert(drawing: DrawingObject): void {
|
|
58
|
+
const index = this.drawings.findIndex((item) => item.id === drawing.id)
|
|
59
|
+
if (index >= 0) {
|
|
60
|
+
this.drawings[index] = drawing
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
this.drawings.push(drawing)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
remove(id: string): void {
|
|
67
|
+
this.drawings = this.drawings.filter((drawing) => drawing.id !== id)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
clear(): void {
|
|
71
|
+
this.drawings = []
|
|
72
|
+
this.selectedId = null
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export class DrawingDefinitionRegistry {
|
|
77
|
+
private definitions = new Map<DrawingKind, DrawingDefinition>()
|
|
78
|
+
|
|
79
|
+
register<TParams = Record<string, unknown>>(definition: DrawingDefinition<TParams>): void {
|
|
80
|
+
this.definitions.set(definition.kind, definition as DrawingDefinition)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
get(kind: DrawingKind): DrawingDefinition | undefined {
|
|
84
|
+
return this.definitions.get(kind)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
compute(drawing: DrawingObject, context: DrawingComputeContext): DrawingGeometry | null {
|
|
88
|
+
const definition = this.get(drawing.kind)
|
|
89
|
+
if (!definition) return null
|
|
90
|
+
return definition.compute(drawing, context)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export type PrimitiveRendererSet = {
|
|
95
|
+
point: (ctx: CanvasRenderingContext2D, primitive: PointPrimitive, dpr: number) => void
|
|
96
|
+
line: (
|
|
97
|
+
ctx: CanvasRenderingContext2D,
|
|
98
|
+
primitive: LinePrimitive,
|
|
99
|
+
viewportClip: { left: number; top: number; right: number; bottom: number },
|
|
100
|
+
dpr: number
|
|
101
|
+
) => void
|
|
102
|
+
area: (ctx: CanvasRenderingContext2D, primitive: AreaPrimitive, dpr: number) => void
|
|
103
|
+
text: (ctx: CanvasRenderingContext2D, primitive: TextPrimitive, dpr: number) => void
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function applyLineStyle(ctx: CanvasRenderingContext2D, style?: DrawingStyle): void {
|
|
107
|
+
ctx.strokeStyle = style?.stroke ?? '#2962ff'
|
|
108
|
+
ctx.lineWidth = style?.strokeWidth ?? 1
|
|
109
|
+
if (style?.strokeStyle === 'dashed') {
|
|
110
|
+
ctx.setLineDash([6, 4])
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
if (style?.strokeStyle === 'dotted') {
|
|
114
|
+
ctx.setLineDash([2, 3])
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
ctx.setLineDash([])
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function applyFillStyle(ctx: CanvasRenderingContext2D, style?: DrawingStyle): void {
|
|
121
|
+
ctx.fillStyle = style?.fill ?? style?.stroke ?? '#2962ff'
|
|
122
|
+
ctx.globalAlpha = style?.fillOpacity ?? 1
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function clipLineToRect(
|
|
126
|
+
x1: number,
|
|
127
|
+
y1: number,
|
|
128
|
+
x2: number,
|
|
129
|
+
y2: number,
|
|
130
|
+
rect: { left: number; top: number; right: number; bottom: number }
|
|
131
|
+
): { a: { x: number; y: number }; b: { x: number; y: number } } | null {
|
|
132
|
+
const INSIDE = 0
|
|
133
|
+
const LEFT = 1
|
|
134
|
+
const RIGHT = 2
|
|
135
|
+
const BOTTOM = 4
|
|
136
|
+
const TOP = 8
|
|
137
|
+
|
|
138
|
+
const computeCode = (x: number, y: number) => {
|
|
139
|
+
let code = INSIDE
|
|
140
|
+
if (x < rect.left) code |= LEFT
|
|
141
|
+
else if (x > rect.right) code |= RIGHT
|
|
142
|
+
if (y < rect.top) code |= TOP
|
|
143
|
+
else if (y > rect.bottom) code |= BOTTOM
|
|
144
|
+
return code
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let ax = x1
|
|
148
|
+
let ay = y1
|
|
149
|
+
let bx = x2
|
|
150
|
+
let by = y2
|
|
151
|
+
|
|
152
|
+
while (true) {
|
|
153
|
+
const codeA = computeCode(ax, ay)
|
|
154
|
+
const codeB = computeCode(bx, by)
|
|
155
|
+
|
|
156
|
+
if (!(codeA | codeB)) {
|
|
157
|
+
return { a: { x: ax, y: ay }, b: { x: bx, y: by } }
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (codeA & codeB) {
|
|
161
|
+
return null
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const codeOut = codeA || codeB
|
|
165
|
+
let x = 0
|
|
166
|
+
let y = 0
|
|
167
|
+
|
|
168
|
+
if (codeOut & TOP) {
|
|
169
|
+
x = ax + ((bx - ax) * (rect.top - ay)) / (by - ay)
|
|
170
|
+
y = rect.top
|
|
171
|
+
} else if (codeOut & BOTTOM) {
|
|
172
|
+
x = ax + ((bx - ax) * (rect.bottom - ay)) / (by - ay)
|
|
173
|
+
y = rect.bottom
|
|
174
|
+
} else if (codeOut & RIGHT) {
|
|
175
|
+
y = ay + ((by - ay) * (rect.right - ax)) / (bx - ax)
|
|
176
|
+
x = rect.right
|
|
177
|
+
} else {
|
|
178
|
+
y = ay + ((by - ay) * (rect.left - ax)) / (bx - ax)
|
|
179
|
+
x = rect.left
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (codeOut === codeA) {
|
|
183
|
+
ax = x
|
|
184
|
+
ay = y
|
|
185
|
+
} else {
|
|
186
|
+
bx = x
|
|
187
|
+
by = y
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function extendLineToViewport(
|
|
193
|
+
primitive: LinePrimitive,
|
|
194
|
+
viewportClip: { left: number; top: number; right: number; bottom: number }
|
|
195
|
+
): { a: { x: number; y: number }; b: { x: number; y: number } } | null {
|
|
196
|
+
const { a, b, extend = 'none' } = primitive
|
|
197
|
+
if (extend === 'none') {
|
|
198
|
+
return clipLineToRect(a.x, a.y, b.x, b.y, viewportClip)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const dx = b.x - a.x
|
|
202
|
+
const dy = b.y - a.y
|
|
203
|
+
if (dx === 0 && dy === 0) return null
|
|
204
|
+
|
|
205
|
+
const distance = Math.max(viewportClip.right - viewportClip.left, viewportClip.bottom - viewportClip.top) * 4
|
|
206
|
+
let start = a
|
|
207
|
+
let end = b
|
|
208
|
+
|
|
209
|
+
if (extend === 'left' || extend === 'both') {
|
|
210
|
+
start = { x: a.x - dx * distance, y: a.y - dy * distance }
|
|
211
|
+
}
|
|
212
|
+
if (extend === 'right' || extend === 'both') {
|
|
213
|
+
end = { x: b.x + dx * distance, y: b.y + dy * distance }
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return clipLineToRect(start.x, start.y, end.x, end.y, viewportClip)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function getAnchorDataIndex(anchor: DrawingObject['anchors'][number], data: KLineData[]): number {
|
|
220
|
+
if (!Number.isFinite(anchor.index)) return -1
|
|
221
|
+
const index = Math.round(anchor.index)
|
|
222
|
+
if (index < 0 || index >= data.length) return -1
|
|
223
|
+
return index
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function formatSigned(value: number, digits = 2): string {
|
|
227
|
+
if (!Number.isFinite(value)) return '0'
|
|
228
|
+
const fixed = value.toFixed(digits)
|
|
229
|
+
return value > 0 ? `+${fixed}` : fixed
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function computeLinearRegression(values: number[]): { slope: number; intercept: number; stdDev: number } | null {
|
|
233
|
+
const n = values.length
|
|
234
|
+
if (n < 2) return null
|
|
235
|
+
|
|
236
|
+
let sumX = 0
|
|
237
|
+
let sumY = 0
|
|
238
|
+
let sumXY = 0
|
|
239
|
+
let sumXX = 0
|
|
240
|
+
for (let i = 0; i < n; i++) {
|
|
241
|
+
const x = i
|
|
242
|
+
const y = values[i]!
|
|
243
|
+
sumX += x
|
|
244
|
+
sumY += y
|
|
245
|
+
sumXY += x * y
|
|
246
|
+
sumXX += x * x
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const denominator = n * sumXX - sumX * sumX
|
|
250
|
+
if (denominator === 0) return null
|
|
251
|
+
|
|
252
|
+
const slope = (n * sumXY - sumX * sumY) / denominator
|
|
253
|
+
const intercept = (sumY - slope * sumX) / n
|
|
254
|
+
|
|
255
|
+
let variance = 0
|
|
256
|
+
for (let i = 0; i < n; i++) {
|
|
257
|
+
const fitted = intercept + slope * i
|
|
258
|
+
const diff = values[i]! - fitted
|
|
259
|
+
variance += diff * diff
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return {
|
|
263
|
+
slope,
|
|
264
|
+
intercept,
|
|
265
|
+
stdDev: Math.sqrt(variance / n),
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function createDefaultPrimitiveRendererSet(): PrimitiveRendererSet {
|
|
270
|
+
return {
|
|
271
|
+
point(ctx, primitive, dpr) {
|
|
272
|
+
const radius = primitive.style?.pointRadius ?? 4
|
|
273
|
+
ctx.save()
|
|
274
|
+
ctx.fillStyle = primitive.style?.fill ?? primitive.style?.stroke ?? '#2962ff'
|
|
275
|
+
ctx.beginPath()
|
|
276
|
+
ctx.arc(primitive.point.x, primitive.point.y, Math.max(radius, 1 / dpr), 0, Math.PI * 2)
|
|
277
|
+
ctx.fill()
|
|
278
|
+
ctx.restore()
|
|
279
|
+
},
|
|
280
|
+
|
|
281
|
+
line(ctx, primitive, viewportClip, dpr) {
|
|
282
|
+
const clipped = extendLineToViewport(primitive, viewportClip)
|
|
283
|
+
if (!clipped) return
|
|
284
|
+
|
|
285
|
+
ctx.save()
|
|
286
|
+
applyLineStyle(ctx, primitive.style)
|
|
287
|
+
const lineWidth = primitive.style?.strokeWidth ?? 1
|
|
288
|
+
const align = lineWidth <= 1 ? 0.5 / dpr : 0
|
|
289
|
+
ctx.beginPath()
|
|
290
|
+
ctx.moveTo(clipped.a.x + align, clipped.a.y + align)
|
|
291
|
+
ctx.lineTo(clipped.b.x + align, clipped.b.y + align)
|
|
292
|
+
ctx.stroke()
|
|
293
|
+
|
|
294
|
+
// 绘制端点(使用原始锚点位置,不是裁剪后的位置)
|
|
295
|
+
if (primitive.showEndpoints !== false) {
|
|
296
|
+
const pointRadius = primitive.style?.pointRadius ?? 4
|
|
297
|
+
ctx.fillStyle = primitive.style?.stroke ?? '#2962ff'
|
|
298
|
+
|
|
299
|
+
ctx.beginPath()
|
|
300
|
+
ctx.arc(primitive.a.x, primitive.a.y, Math.max(pointRadius, 1 / dpr), 0, Math.PI * 2)
|
|
301
|
+
ctx.fill()
|
|
302
|
+
|
|
303
|
+
ctx.beginPath()
|
|
304
|
+
ctx.arc(primitive.b.x, primitive.b.y, Math.max(pointRadius, 1 / dpr), 0, Math.PI * 2)
|
|
305
|
+
ctx.fill()
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
ctx.restore()
|
|
309
|
+
},
|
|
310
|
+
|
|
311
|
+
area(ctx, primitive) {
|
|
312
|
+
if (primitive.points.length === 0) return
|
|
313
|
+
ctx.save()
|
|
314
|
+
applyFillStyle(ctx, primitive.style)
|
|
315
|
+
ctx.beginPath()
|
|
316
|
+
ctx.moveTo(primitive.points[0]!.x, primitive.points[0]!.y)
|
|
317
|
+
for (let i = 1; i < primitive.points.length; i++) {
|
|
318
|
+
const point = primitive.points[i]!
|
|
319
|
+
ctx.lineTo(point.x, point.y)
|
|
320
|
+
}
|
|
321
|
+
if (primitive.closed) {
|
|
322
|
+
ctx.closePath()
|
|
323
|
+
}
|
|
324
|
+
ctx.fill()
|
|
325
|
+
ctx.restore()
|
|
326
|
+
},
|
|
327
|
+
|
|
328
|
+
text(ctx, primitive) {
|
|
329
|
+
ctx.save()
|
|
330
|
+
ctx.fillStyle = primitive.style?.textColor ?? primitive.style?.stroke ?? '#2962ff'
|
|
331
|
+
ctx.font = `${primitive.style?.fontSize ?? 12}px sans-serif`
|
|
332
|
+
ctx.textAlign = primitive.align ?? 'left'
|
|
333
|
+
ctx.textBaseline = primitive.baseline ?? 'bottom'
|
|
334
|
+
ctx.fillText(primitive.text, primitive.point.x, primitive.point.y)
|
|
335
|
+
ctx.restore()
|
|
336
|
+
},
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export function createTwoPointLineDefinition(kind: DrawingKind, extend: LinePrimitive['extend']): DrawingDefinition {
|
|
341
|
+
return {
|
|
342
|
+
kind,
|
|
343
|
+
minAnchors: 2,
|
|
344
|
+
maxAnchors: 2,
|
|
345
|
+
compute(drawing, context) {
|
|
346
|
+
const [first, second] = drawing.anchors
|
|
347
|
+
if (!first || !second) return { primitives: [] }
|
|
348
|
+
return {
|
|
349
|
+
primitives: [
|
|
350
|
+
{
|
|
351
|
+
kind: 'line',
|
|
352
|
+
a: context.toScreen(first),
|
|
353
|
+
b: context.toScreen(second),
|
|
354
|
+
extend,
|
|
355
|
+
style: drawing.style,
|
|
356
|
+
},
|
|
357
|
+
],
|
|
358
|
+
}
|
|
359
|
+
},
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export function createSingleAnchorLineDefinition(kind: DrawingKind): DrawingDefinition {
|
|
364
|
+
return {
|
|
365
|
+
kind,
|
|
366
|
+
minAnchors: 1,
|
|
367
|
+
maxAnchors: 1,
|
|
368
|
+
compute(drawing, context) {
|
|
369
|
+
const [anchor] = drawing.anchors
|
|
370
|
+
if (!anchor) return { primitives: [] }
|
|
371
|
+
const point = context.toScreen(anchor)
|
|
372
|
+
const bottom = context.pane.height
|
|
373
|
+
const right = context.viewport.plotWidth
|
|
374
|
+
|
|
375
|
+
if (kind === 'horizontal-line') {
|
|
376
|
+
return {
|
|
377
|
+
primitives: [
|
|
378
|
+
{ kind: 'line', a: { x: 0, y: point.y }, b: { x: right, y: point.y }, showEndpoints: false, style: drawing.style },
|
|
379
|
+
{ kind: 'point', point, style: drawing.style },
|
|
380
|
+
],
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (kind === 'horizontal-ray') {
|
|
385
|
+
return {
|
|
386
|
+
primitives: [
|
|
387
|
+
{ kind: 'line', a: point, b: { x: right, y: point.y }, showEndpoints: false, style: drawing.style },
|
|
388
|
+
{ kind: 'point', point, style: drawing.style },
|
|
389
|
+
],
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (kind === 'vertical-line') {
|
|
394
|
+
return {
|
|
395
|
+
primitives: [
|
|
396
|
+
{ kind: 'line', a: { x: point.x, y: 0 }, b: { x: point.x, y: bottom }, showEndpoints: false, style: drawing.style },
|
|
397
|
+
{ kind: 'point', point, style: drawing.style },
|
|
398
|
+
],
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// cross-line: 十字线,显示水平和垂直线,锚点显示一个点,边缘不显示端点
|
|
403
|
+
return {
|
|
404
|
+
primitives: [
|
|
405
|
+
{ kind: 'line', a: { x: 0, y: point.y }, b: { x: right, y: point.y }, showEndpoints: false, style: drawing.style },
|
|
406
|
+
{ kind: 'line', a: { x: point.x, y: 0 }, b: { x: point.x, y: bottom }, showEndpoints: false, style: drawing.style },
|
|
407
|
+
{ kind: 'point', point, style: drawing.style },
|
|
408
|
+
],
|
|
409
|
+
}
|
|
410
|
+
},
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
export function createInfoLineDefinition(): DrawingDefinition {
|
|
415
|
+
return {
|
|
416
|
+
kind: 'info-line',
|
|
417
|
+
minAnchors: 2,
|
|
418
|
+
maxAnchors: 2,
|
|
419
|
+
compute(drawing, context) {
|
|
420
|
+
const [first, second] = drawing.anchors
|
|
421
|
+
if (!first || !second) return { primitives: [] }
|
|
422
|
+
const a = context.toScreen(first)
|
|
423
|
+
const b = context.toScreen(second)
|
|
424
|
+
const firstIndex = Math.round(first.index)
|
|
425
|
+
const secondIndex = Math.round(second.index)
|
|
426
|
+
const bars = secondIndex - firstIndex
|
|
427
|
+
const delta = second.price - first.price
|
|
428
|
+
const percent = first.price !== 0 ? (delta / first.price) * 100 : 0
|
|
429
|
+
const angle = Math.atan2(b.y - a.y, b.x - a.x) * (180 / Math.PI)
|
|
430
|
+
const text = `${formatSigned(delta)} (${formatSigned(percent)}%) ${bars} bars ${formatSigned(angle)}°`
|
|
431
|
+
|
|
432
|
+
return {
|
|
433
|
+
primitives: [
|
|
434
|
+
{ kind: 'line', a, b, style: drawing.style },
|
|
435
|
+
{
|
|
436
|
+
kind: 'text',
|
|
437
|
+
point: { x: (a.x + b.x) / 2 + 8, y: Math.min(a.y, b.y) - 8 },
|
|
438
|
+
text,
|
|
439
|
+
align: 'left',
|
|
440
|
+
baseline: 'bottom',
|
|
441
|
+
style: drawing.style,
|
|
442
|
+
},
|
|
443
|
+
],
|
|
444
|
+
meta: { delta, percent, bars, angle },
|
|
445
|
+
}
|
|
446
|
+
},
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
export function createParallelChannelDefinition(): DrawingDefinition {
|
|
451
|
+
return {
|
|
452
|
+
kind: 'parallel-channel',
|
|
453
|
+
minAnchors: 3,
|
|
454
|
+
maxAnchors: 3,
|
|
455
|
+
compute(drawing, context) {
|
|
456
|
+
const [first, second, third] = drawing.anchors
|
|
457
|
+
if (!first || !second || !third) return { primitives: [] }
|
|
458
|
+
const p1 = context.toScreen(first)
|
|
459
|
+
const p2 = context.toScreen(second)
|
|
460
|
+
const p3 = context.toScreen(third)
|
|
461
|
+
const dx = p2.x - p1.x
|
|
462
|
+
const dy = p2.y - p1.y
|
|
463
|
+
const p4 = { x: p3.x + dx, y: p3.y + dy }
|
|
464
|
+
const extend = (drawing.params as { extend?: LinePrimitive['extend'] } | undefined)?.extend ?? 'none'
|
|
465
|
+
|
|
466
|
+
// 计算 p4 对应的锚点信息(用于轴标签注册)
|
|
467
|
+
const p4Index = third.index + (second.index - first.index)
|
|
468
|
+
const p4Time = third.time
|
|
469
|
+
? (typeof third.time === 'string' ? new Date(third.time).getTime() : third.time) +
|
|
470
|
+
((typeof second.time === 'string' ? new Date(second.time).getTime() : second.time ?? 0) -
|
|
471
|
+
(typeof first.time === 'string' ? new Date(first.time).getTime() : first.time ?? 0))
|
|
472
|
+
: undefined
|
|
473
|
+
|
|
474
|
+
return {
|
|
475
|
+
primitives: [
|
|
476
|
+
{
|
|
477
|
+
kind: 'area',
|
|
478
|
+
points: [p1, p2, p4, p3],
|
|
479
|
+
closed: true,
|
|
480
|
+
style: drawing.style,
|
|
481
|
+
},
|
|
482
|
+
{ kind: 'line', a: p1, b: p2, extend, style: drawing.style },
|
|
483
|
+
{ kind: 'line', a: p3, b: p4, extend, style: drawing.style },
|
|
484
|
+
],
|
|
485
|
+
computedAnchors: [
|
|
486
|
+
{ id: `${drawing.id}-p4`, index: p4Index, time: p4Time, price: third.price + (second.price - first.price) },
|
|
487
|
+
],
|
|
488
|
+
}
|
|
489
|
+
},
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
export function createFlatLineDefinition(): DrawingDefinition {
|
|
494
|
+
return {
|
|
495
|
+
kind: 'flat-line',
|
|
496
|
+
minAnchors: 3,
|
|
497
|
+
maxAnchors: 3,
|
|
498
|
+
compute(drawing, context) {
|
|
499
|
+
const [first, second, third] = drawing.anchors
|
|
500
|
+
if (!first || !second || !third) return { primitives: [] }
|
|
501
|
+
|
|
502
|
+
const p1 = context.toScreen(first)
|
|
503
|
+
const p2 = context.toScreen(second)
|
|
504
|
+
const thirdScreen = context.toScreen(third)
|
|
505
|
+
const h1 = { x: p1.x, y: thirdScreen.y }
|
|
506
|
+
const h2 = { x: p2.x, y: thirdScreen.y }
|
|
507
|
+
|
|
508
|
+
return {
|
|
509
|
+
primitives: [
|
|
510
|
+
{
|
|
511
|
+
kind: 'area',
|
|
512
|
+
points: [p1, p2, h2, h1],
|
|
513
|
+
closed: true,
|
|
514
|
+
style: drawing.style,
|
|
515
|
+
},
|
|
516
|
+
{ kind: 'line', a: p1, b: p2, style: drawing.style },
|
|
517
|
+
{ kind: 'line', a: h1, b: h2, style: drawing.style },
|
|
518
|
+
{ kind: 'point', point: h1, style: drawing.style },
|
|
519
|
+
{ kind: 'point', point: h2, style: drawing.style },
|
|
520
|
+
],
|
|
521
|
+
computedAnchors: [
|
|
522
|
+
{ id: `${drawing.id}-h1`, index: first.index, time: first.time, price: third.price },
|
|
523
|
+
{ id: `${drawing.id}-h2`, index: second.index, time: second.time, price: third.price },
|
|
524
|
+
],
|
|
525
|
+
}
|
|
526
|
+
},
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
export function createDisjointChannelDefinition(): DrawingDefinition {
|
|
531
|
+
return {
|
|
532
|
+
kind: 'disjoint-channel',
|
|
533
|
+
minAnchors: 3,
|
|
534
|
+
maxAnchors: 3,
|
|
535
|
+
compute(drawing, context) {
|
|
536
|
+
const [first, second, third] = drawing.anchors
|
|
537
|
+
if (!first || !second || !third) return { primitives: [] }
|
|
538
|
+
|
|
539
|
+
const p1 = context.toScreen(first)
|
|
540
|
+
const p2 = context.toScreen(second)
|
|
541
|
+
const p3 = context.toScreen(third)
|
|
542
|
+
|
|
543
|
+
// 第二条线:过 p3,斜率取反
|
|
544
|
+
const dx = p2.x - p1.x
|
|
545
|
+
const dy = p2.y - p1.y
|
|
546
|
+
const p4 = { x: p3.x + dx, y: p3.y - dy }
|
|
547
|
+
|
|
548
|
+
// 计算 p4 对应的锚点信息(用于轴标签注册)
|
|
549
|
+
const p4Index = third.index + (second.index - first.index)
|
|
550
|
+
const p4Price = third.price - (second.price - first.price)
|
|
551
|
+
const p4Time = third.time
|
|
552
|
+
? (typeof third.time === 'string' ? new Date(third.time).getTime() : third.time) -
|
|
553
|
+
((typeof second.time === 'string' ? new Date(second.time).getTime() : second.time ?? 0) -
|
|
554
|
+
(typeof first.time === 'string' ? new Date(first.time).getTime() : first.time ?? 0))
|
|
555
|
+
: undefined
|
|
556
|
+
|
|
557
|
+
return {
|
|
558
|
+
primitives: [
|
|
559
|
+
// 填充区域
|
|
560
|
+
{
|
|
561
|
+
kind: 'area',
|
|
562
|
+
points: [p1, p2, p4, p3],
|
|
563
|
+
closed: true,
|
|
564
|
+
style: drawing.style,
|
|
565
|
+
},
|
|
566
|
+
// 斜率 k 的线
|
|
567
|
+
{ kind: 'line', a: p1, b: p2, style: drawing.style },
|
|
568
|
+
// 斜率 -k 的线
|
|
569
|
+
{ kind: 'line', a: p3, b: p4, style: drawing.style },
|
|
570
|
+
],
|
|
571
|
+
computedAnchors: [
|
|
572
|
+
{ id: `${drawing.id}-p4`, index: p4Index, time: p4Time, price: p4Price },
|
|
573
|
+
],
|
|
574
|
+
}
|
|
575
|
+
},
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
export function createRegressionChannelDefinition(): DrawingDefinition {
|
|
580
|
+
return {
|
|
581
|
+
kind: 'regression-channel',
|
|
582
|
+
minAnchors: 2,
|
|
583
|
+
maxAnchors: 2,
|
|
584
|
+
compute(drawing, context) {
|
|
585
|
+
const [first, second] = drawing.anchors
|
|
586
|
+
if (!first || !second) return { primitives: [] }
|
|
587
|
+
const firstIndex = getAnchorDataIndex(first, context.seriesData)
|
|
588
|
+
const secondIndex = getAnchorDataIndex(second, context.seriesData)
|
|
589
|
+
if (firstIndex < 0 && secondIndex < 0) return { primitives: [] }
|
|
590
|
+
|
|
591
|
+
const clampedFirstIndex = Math.min(Math.max(Math.round(first.index), 0), context.seriesData.length - 1)
|
|
592
|
+
const clampedSecondIndex = Math.min(Math.max(Math.round(second.index), 0), context.seriesData.length - 1)
|
|
593
|
+
const startIndex = Math.min(clampedFirstIndex, clampedSecondIndex)
|
|
594
|
+
const endIndex = Math.max(clampedFirstIndex, clampedSecondIndex)
|
|
595
|
+
const slice = context.seriesData.slice(startIndex, endIndex + 1)
|
|
596
|
+
const regression = computeLinearRegression(slice.map((item) => item.close))
|
|
597
|
+
if (!regression) return { primitives: [] }
|
|
598
|
+
|
|
599
|
+
const sigma = (drawing.params as { sigma?: number } | undefined)?.sigma ?? 2
|
|
600
|
+
const offset = regression.stdDev * sigma
|
|
601
|
+
const firstValue = regression.intercept
|
|
602
|
+
const lastValue = regression.intercept + regression.slope * (slice.length - 1)
|
|
603
|
+
|
|
604
|
+
const startAnchor = { id: `${drawing.id}-reg-start`, index: Math.round(first.index), time: context.seriesData[startIndex]!.timestamp, price: firstValue }
|
|
605
|
+
const endAnchor = { id: `${drawing.id}-reg-end`, index: Math.round(second.index), time: context.seriesData[endIndex]!.timestamp, price: lastValue }
|
|
606
|
+
const upperStartAnchor = { ...startAnchor, id: `${drawing.id}-reg-upper-start`, price: firstValue + offset }
|
|
607
|
+
const upperEndAnchor = { ...endAnchor, id: `${drawing.id}-reg-upper-end`, price: lastValue + offset }
|
|
608
|
+
const lowerStartAnchor = { ...startAnchor, id: `${drawing.id}-reg-lower-start`, price: firstValue - offset }
|
|
609
|
+
const lowerEndAnchor = { ...endAnchor, id: `${drawing.id}-reg-lower-end`, price: lastValue - offset }
|
|
610
|
+
|
|
611
|
+
const middleA = context.toScreen(startAnchor)
|
|
612
|
+
const middleB = context.toScreen(endAnchor)
|
|
613
|
+
const upperA = context.toScreen(upperStartAnchor)
|
|
614
|
+
const upperB = context.toScreen(upperEndAnchor)
|
|
615
|
+
const lowerA = context.toScreen(lowerStartAnchor)
|
|
616
|
+
const lowerB = context.toScreen(lowerEndAnchor)
|
|
617
|
+
|
|
618
|
+
return {
|
|
619
|
+
primitives: [
|
|
620
|
+
{
|
|
621
|
+
kind: 'area',
|
|
622
|
+
points: [upperA, upperB, lowerB, lowerA],
|
|
623
|
+
closed: true,
|
|
624
|
+
style: drawing.style,
|
|
625
|
+
},
|
|
626
|
+
// 中间回归线使用虚线
|
|
627
|
+
{ kind: 'line', a: middleA, b: middleB, style: { ...drawing.style, strokeStyle: 'dashed' } },
|
|
628
|
+
{ kind: 'line', a: upperA, b: upperB, style: drawing.style },
|
|
629
|
+
{ kind: 'line', a: lowerA, b: lowerB, style: drawing.style },
|
|
630
|
+
],
|
|
631
|
+
computedAnchors: [startAnchor, endAnchor],
|
|
632
|
+
meta: { sigma, stdDev: regression.stdDev, slope: regression.slope },
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
export function registerDefaultDrawingDefinitions(registry: DrawingDefinitionRegistry): void {
|
|
639
|
+
registry.register(createTwoPointLineDefinition('trend-line', 'none'))
|
|
640
|
+
registry.register(createTwoPointLineDefinition('ray', 'right'))
|
|
641
|
+
registry.register(createTwoPointLineDefinition('extended-line', 'both'))
|
|
642
|
+
registry.register(createSingleAnchorLineDefinition('horizontal-line'))
|
|
643
|
+
registry.register(createSingleAnchorLineDefinition('horizontal-ray'))
|
|
644
|
+
registry.register(createSingleAnchorLineDefinition('vertical-line'))
|
|
645
|
+
registry.register(createSingleAnchorLineDefinition('cross-line'))
|
|
646
|
+
registry.register(createInfoLineDefinition())
|
|
647
|
+
registry.register(createParallelChannelDefinition())
|
|
648
|
+
registry.register(createRegressionChannelDefinition())
|
|
649
|
+
registry.register(createFlatLineDefinition())
|
|
650
|
+
registry.register(createDisjointChannelDefinition())
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// 导出交互控制器
|
|
654
|
+
export { DrawingInteractionController } from './interaction'
|
|
655
|
+
export type { DrawingToolId, DrawingAnchorInput, DrawingInteractionCallbacks } from './interaction'
|