@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,562 +1,562 @@
|
|
|
1
|
-
import type { KLineData } from '../../types/price'
|
|
2
|
-
import { priceToY, yToPrice } from '../priceToY'
|
|
3
|
-
import { alignToPhysicalPixelCenter, roundToPhysicalPixel } from '../../engine/draw/pixelAlign'
|
|
4
|
-
import { formatYMDShanghai, formatMonthOrYear, findMonthBoundaries } from '../../utils/dateFormat'
|
|
5
|
-
import { getColors } from '../../engine/theme/colors'
|
|
6
|
-
import { getFont, setCanvasFont } from '../../engine/theme/fonts'
|
|
7
|
-
|
|
8
|
-
const textWidthCache = new Map<string, number>()
|
|
9
|
-
const TEXT_WIDTH_CACHE_LIMIT = 512
|
|
10
|
-
|
|
11
|
-
function measureTextWidth(ctx: CanvasRenderingContext2D, text: string): number {
|
|
12
|
-
const key = `${ctx.font}\n${text}`
|
|
13
|
-
const cached = textWidthCache.get(key)
|
|
14
|
-
if (cached !== undefined) {
|
|
15
|
-
return cached
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const width = ctx.measureText(text).width
|
|
19
|
-
if (textWidthCache.size >= TEXT_WIDTH_CACHE_LIMIT) {
|
|
20
|
-
textWidthCache.clear()
|
|
21
|
-
}
|
|
22
|
-
textWidthCache.set(key, width)
|
|
23
|
-
return width
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface PriceAxisOptions {
|
|
27
|
-
x: number
|
|
28
|
-
y: number
|
|
29
|
-
width: number
|
|
30
|
-
height: number
|
|
31
|
-
priceRange: { maxPrice: number; minPrice: number }
|
|
32
|
-
yPaddingPx?: number
|
|
33
|
-
dpr: number
|
|
34
|
-
ticks?: number
|
|
35
|
-
bgColor?: string
|
|
36
|
-
textColor?: string
|
|
37
|
-
lineColor?: string
|
|
38
|
-
fontSize?: number
|
|
39
|
-
paddingX?: number
|
|
40
|
-
/** 是否绘制左侧边界竖线(默认 true) */
|
|
41
|
-
drawLeftBorder?: boolean
|
|
42
|
-
/** 是否绘制刻度短线(默认 true) */
|
|
43
|
-
drawTickLines?: boolean
|
|
44
|
-
/** 价格偏移量(用于价格轴平移时同步显示) */
|
|
45
|
-
priceOffset?: number
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** 右侧价格轴(固定,不随 translate/scroll 变化) */
|
|
49
|
-
export function drawPriceAxis(ctx: CanvasRenderingContext2D, opts: PriceAxisOptions, theme: 'light' | 'dark' = 'light') {
|
|
50
|
-
const colors = getColors(theme)
|
|
51
|
-
const {
|
|
52
|
-
x,
|
|
53
|
-
y,
|
|
54
|
-
width,
|
|
55
|
-
height,
|
|
56
|
-
priceRange,
|
|
57
|
-
yPaddingPx = 0,
|
|
58
|
-
dpr,
|
|
59
|
-
ticks = 10,
|
|
60
|
-
bgColor = colors.TAG_BG.TRANSPARENT,
|
|
61
|
-
textColor = colors.TEXT.SECONDARY,
|
|
62
|
-
lineColor = colors.BORDER.DARK,
|
|
63
|
-
fontSize = 16,
|
|
64
|
-
drawLeftBorder = true,
|
|
65
|
-
drawTickLines = true,
|
|
66
|
-
priceOffset = 0,
|
|
67
|
-
} = opts
|
|
68
|
-
|
|
69
|
-
const wantPad = yPaddingPx
|
|
70
|
-
const pad = Math.max(0, Math.min(wantPad, Math.floor(height / 2) - 1))
|
|
71
|
-
|
|
72
|
-
const { maxPrice, minPrice } = priceRange
|
|
73
|
-
const range = maxPrice - minPrice
|
|
74
|
-
const step = range === 0 ? 0 : range / (Math.max(2, ticks) - 1)
|
|
75
|
-
|
|
76
|
-
ctx.fillStyle = bgColor
|
|
77
|
-
ctx.fillRect(x, y, width, height)
|
|
78
|
-
|
|
79
|
-
if (drawLeftBorder) {
|
|
80
|
-
ctx.strokeStyle = lineColor
|
|
81
|
-
ctx.lineWidth = 1
|
|
82
|
-
ctx.beginPath()
|
|
83
|
-
ctx.moveTo(alignToPhysicalPixelCenter(x, dpr), y)
|
|
84
|
-
ctx.lineTo(alignToPhysicalPixelCenter(x, dpr), y + height)
|
|
85
|
-
ctx.stroke()
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
setCanvasFont(ctx, getFont(fontSize))
|
|
89
|
-
ctx.textBaseline = 'middle'
|
|
90
|
-
ctx.textAlign = 'center'
|
|
91
|
-
ctx.strokeStyle = lineColor
|
|
92
|
-
ctx.fillStyle = textColor
|
|
93
|
-
|
|
94
|
-
const centerX = x + width / 2
|
|
95
|
-
|
|
96
|
-
for (let i = 0; i < Math.max(2, ticks); i++) {
|
|
97
|
-
const p = range === 0 ? maxPrice : maxPrice - step * i
|
|
98
|
-
const yy = Math.round(priceToY(p, maxPrice, minPrice, height, pad, pad) + y)
|
|
99
|
-
|
|
100
|
-
if (drawTickLines) {
|
|
101
|
-
ctx.beginPath()
|
|
102
|
-
const lineY = alignToPhysicalPixelCenter(yy, dpr)
|
|
103
|
-
ctx.moveTo(x, lineY)
|
|
104
|
-
ctx.lineTo(x + 4, lineY)
|
|
105
|
-
ctx.stroke()
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const displayPrice = p + priceOffset
|
|
109
|
-
ctx.fillText(displayPrice.toFixed(2), roundToPhysicalPixel(centerX, dpr), alignToPhysicalPixelCenter(yy, dpr))
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export interface TimeAxisOptions {
|
|
114
|
-
x: number
|
|
115
|
-
y: number
|
|
116
|
-
width: number
|
|
117
|
-
height: number
|
|
118
|
-
data: KLineData[]
|
|
119
|
-
scrollLeft: number
|
|
120
|
-
kWidth: number
|
|
121
|
-
kGap: number
|
|
122
|
-
startIndex: number
|
|
123
|
-
endIndex: number
|
|
124
|
-
dpr: number
|
|
125
|
-
bgColor?: string
|
|
126
|
-
textColor?: string
|
|
127
|
-
lineColor?: string
|
|
128
|
-
fontSize?: number
|
|
129
|
-
/** 左右内边距(逻辑像素),避免月份/年份文字贴边 */
|
|
130
|
-
paddingX?: number
|
|
131
|
-
/** 是否绘制顶部边界线(默认 true,如果主图已有底边框则设为 false 避免重复) */
|
|
132
|
-
drawTopBorder?: boolean
|
|
133
|
-
/** 是否绘制底部边界线(默认 true,如果副图已有下边框则设为 false 避免重复) */
|
|
134
|
-
drawBottomBorder?: boolean
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export interface LastPriceLineOptions {
|
|
138
|
-
/** 绘图区宽度(逻辑像素) */
|
|
139
|
-
plotWidth: number
|
|
140
|
-
/** 绘图区高度(逻辑像素) */
|
|
141
|
-
plotHeight: number
|
|
142
|
-
/** 当前滚动位置(逻辑像素) */
|
|
143
|
-
scrollLeft: number
|
|
144
|
-
/** 可视范围:用于确定虚线的起止 worldX */
|
|
145
|
-
startIndex: number
|
|
146
|
-
endIndex: number
|
|
147
|
-
/** K线布局 */
|
|
148
|
-
kWidth: number
|
|
149
|
-
kGap: number
|
|
150
|
-
/** 价格范围 */
|
|
151
|
-
priceRange: { maxPrice: number; minPrice: number }
|
|
152
|
-
/** 最新价 */
|
|
153
|
-
lastPrice: number
|
|
154
|
-
/** Y轴 padding(与绘图区一致) */
|
|
155
|
-
yPaddingPx?: number
|
|
156
|
-
dpr: number
|
|
157
|
-
color?: string
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export interface CrosshairPriceLabelOptions {
|
|
161
|
-
x: number
|
|
162
|
-
y: number
|
|
163
|
-
width: number
|
|
164
|
-
height: number
|
|
165
|
-
/** 十字线的 y(相对该 canvas 的逻辑像素坐标) */
|
|
166
|
-
crosshairY: number
|
|
167
|
-
priceRange: { maxPrice: number; minPrice: number }
|
|
168
|
-
yPaddingPx?: number
|
|
169
|
-
dpr: number
|
|
170
|
-
bgColor?: string
|
|
171
|
-
borderColor?: string
|
|
172
|
-
textColor?: string
|
|
173
|
-
fontSize?: number
|
|
174
|
-
paddingX?: number
|
|
175
|
-
/** 价格偏移量(用于价格轴平移时同步显示) */
|
|
176
|
-
priceOffset?: number
|
|
177
|
-
/** 优先显示的价格(如十字线已按 active pane 算好) */
|
|
178
|
-
price?: number
|
|
179
|
-
formatPrice?: (price: number) => string
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export interface CrosshairTimeLabelOptions {
|
|
183
|
-
x: number
|
|
184
|
-
y: number
|
|
185
|
-
width: number
|
|
186
|
-
height: number
|
|
187
|
-
/** 十字线的 x(相对该 canvas 的逻辑像素坐标) */
|
|
188
|
-
crosshairX: number
|
|
189
|
-
/** 命中的交易日时间戳(毫秒) */
|
|
190
|
-
timestamp: number
|
|
191
|
-
dpr: number
|
|
192
|
-
bgColor?: string
|
|
193
|
-
textColor?: string
|
|
194
|
-
fontSize?: number
|
|
195
|
-
paddingX?: number
|
|
196
|
-
paddingY?: number
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export function drawCrosshairTimeLabel(ctx: CanvasRenderingContext2D, opts: CrosshairTimeLabelOptions, theme: 'light' | 'dark' = 'light') {
|
|
200
|
-
const colors = getColors(theme)
|
|
201
|
-
const {
|
|
202
|
-
x,
|
|
203
|
-
y,
|
|
204
|
-
width,
|
|
205
|
-
height,
|
|
206
|
-
crosshairX,
|
|
207
|
-
timestamp,
|
|
208
|
-
dpr,
|
|
209
|
-
fontSize = 16,
|
|
210
|
-
paddingX = 8,
|
|
211
|
-
} = opts
|
|
212
|
-
|
|
213
|
-
const text = formatYMDShanghai(timestamp)
|
|
214
|
-
|
|
215
|
-
ctx.save()
|
|
216
|
-
setCanvasFont(ctx, getFont(fontSize))
|
|
217
|
-
ctx.textBaseline = 'middle'
|
|
218
|
-
ctx.textAlign = 'center'
|
|
219
|
-
|
|
220
|
-
const tw = Math.round(measureTextWidth(ctx, text))
|
|
221
|
-
const rectW = Math.min(width, tw + paddingX * 2)
|
|
222
|
-
const rectH = height
|
|
223
|
-
|
|
224
|
-
const centerX = Math.min(Math.max(crosshairX, x + rectW / 2), x + width - rectW / 2)
|
|
225
|
-
const centerY = y + height / 2
|
|
226
|
-
|
|
227
|
-
const rectX = centerX - rectW / 2
|
|
228
|
-
const rectY = y
|
|
229
|
-
|
|
230
|
-
ctx.fillStyle = colors.LABEL.BG
|
|
231
|
-
ctx.fillRect(
|
|
232
|
-
roundToPhysicalPixel(rectX, dpr),
|
|
233
|
-
roundToPhysicalPixel(rectY, dpr),
|
|
234
|
-
roundToPhysicalPixel(rectW, dpr),
|
|
235
|
-
roundToPhysicalPixel(rectH, dpr),
|
|
236
|
-
)
|
|
237
|
-
|
|
238
|
-
ctx.fillStyle = colors.LABEL.TEXT
|
|
239
|
-
ctx.fillText(text, roundToPhysicalPixel(centerX, dpr), alignToPhysicalPixelCenter(centerY, dpr))
|
|
240
|
-
|
|
241
|
-
ctx.restore()
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
export function drawCrosshairPriceLabel(ctx: CanvasRenderingContext2D, opts: CrosshairPriceLabelOptions, theme: 'light' | 'dark' = 'light') {
|
|
245
|
-
const colors = getColors(theme)
|
|
246
|
-
const {
|
|
247
|
-
x,
|
|
248
|
-
y,
|
|
249
|
-
width,
|
|
250
|
-
height,
|
|
251
|
-
crosshairY,
|
|
252
|
-
priceRange,
|
|
253
|
-
yPaddingPx = 0,
|
|
254
|
-
dpr,
|
|
255
|
-
bgColor = colors.LABEL.BG,
|
|
256
|
-
borderColor,
|
|
257
|
-
textColor = colors.LABEL.TEXT,
|
|
258
|
-
fontSize = 16,
|
|
259
|
-
priceOffset = 0,
|
|
260
|
-
price,
|
|
261
|
-
formatPrice,
|
|
262
|
-
} = opts
|
|
263
|
-
|
|
264
|
-
const pad = Math.max(0, Math.min(yPaddingPx, Math.floor(height / 2) - 1))
|
|
265
|
-
const { maxPrice, minPrice } = priceRange
|
|
266
|
-
const displayPrice = price ?? (yToPrice(crosshairY - y, maxPrice, minPrice, height, pad, pad) + priceOffset)
|
|
267
|
-
const priceText = formatPrice ? formatPrice(displayPrice) : displayPrice.toFixed(2)
|
|
268
|
-
|
|
269
|
-
ctx.save()
|
|
270
|
-
setCanvasFont(ctx, getFont(fontSize))
|
|
271
|
-
ctx.textBaseline = 'middle'
|
|
272
|
-
ctx.textAlign = 'center'
|
|
273
|
-
|
|
274
|
-
const textH = fontSize + 4
|
|
275
|
-
const rectH = textH
|
|
276
|
-
|
|
277
|
-
const yy = Math.min(Math.max(crosshairY, y + rectH / 2), y + height - rectH / 2)
|
|
278
|
-
const rectY = yy - rectH / 2
|
|
279
|
-
|
|
280
|
-
const rx = x
|
|
281
|
-
const ry = roundToPhysicalPixel(rectY, dpr)
|
|
282
|
-
const rw = width
|
|
283
|
-
const rh = roundToPhysicalPixel(rectH, dpr)
|
|
284
|
-
ctx.fillStyle = bgColor
|
|
285
|
-
ctx.fillRect(rx, ry, rw, rh)
|
|
286
|
-
|
|
287
|
-
if (borderColor) {
|
|
288
|
-
ctx.strokeStyle = borderColor
|
|
289
|
-
ctx.lineWidth = 1
|
|
290
|
-
ctx.strokeRect(
|
|
291
|
-
alignToPhysicalPixelCenter(rx, dpr),
|
|
292
|
-
alignToPhysicalPixelCenter(ry, dpr),
|
|
293
|
-
Math.max(0, rw - 1 / dpr),
|
|
294
|
-
Math.max(0, rh - 1 / dpr)
|
|
295
|
-
)
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
const centerX = x + width / 2
|
|
299
|
-
ctx.fillStyle = textColor
|
|
300
|
-
ctx.fillText(priceText, roundToPhysicalPixel(centerX, dpr), alignToPhysicalPixelCenter(yy, dpr))
|
|
301
|
-
|
|
302
|
-
ctx.restore()
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/** 绘制"最新价水平虚线"(画在 plotCanvas 的 world 坐标系:需在 translate(-scrollLeft,0) 之后调用) */
|
|
306
|
-
export function drawLastPriceDashedLine(ctx: CanvasRenderingContext2D, opts: LastPriceLineOptions, theme: 'light' | 'dark' = 'light') {
|
|
307
|
-
const colors = getColors(theme)
|
|
308
|
-
const {
|
|
309
|
-
plotWidth,
|
|
310
|
-
plotHeight,
|
|
311
|
-
scrollLeft,
|
|
312
|
-
startIndex,
|
|
313
|
-
endIndex,
|
|
314
|
-
kWidth,
|
|
315
|
-
kGap,
|
|
316
|
-
priceRange,
|
|
317
|
-
lastPrice,
|
|
318
|
-
yPaddingPx = 0,
|
|
319
|
-
dpr,
|
|
320
|
-
color = colors.CROSSHAIR.LINE,
|
|
321
|
-
} = opts
|
|
322
|
-
|
|
323
|
-
const { maxPrice, minPrice } = priceRange
|
|
324
|
-
if (!(lastPrice >= minPrice && lastPrice <= maxPrice)) return
|
|
325
|
-
|
|
326
|
-
const pad = Math.max(0, Math.min(yPaddingPx, Math.floor(plotHeight / 2) - 1))
|
|
327
|
-
const y = priceToY(lastPrice, maxPrice, minPrice, plotHeight, pad, pad)
|
|
328
|
-
|
|
329
|
-
const unit = kWidth + kGap
|
|
330
|
-
const startX = kGap + startIndex * unit
|
|
331
|
-
const endX = kGap + endIndex * unit
|
|
332
|
-
|
|
333
|
-
ctx.save()
|
|
334
|
-
ctx.strokeStyle = color
|
|
335
|
-
ctx.lineWidth = 1
|
|
336
|
-
ctx.setLineDash([4, 3])
|
|
337
|
-
ctx.beginPath()
|
|
338
|
-
const yy = alignToPhysicalPixelCenter(y, dpr)
|
|
339
|
-
ctx.moveTo(roundToPhysicalPixel(startX, dpr), yy)
|
|
340
|
-
ctx.lineTo(roundToPhysicalPixel(endX, dpr), yy)
|
|
341
|
-
ctx.stroke()
|
|
342
|
-
ctx.setLineDash([])
|
|
343
|
-
ctx.restore()
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
/** 底部时间轴(X方向随 scrollLeft 变化) */
|
|
347
|
-
export function drawTimeAxis(ctx: CanvasRenderingContext2D, opts: TimeAxisOptions, theme: 'light' | 'dark' = 'light') {
|
|
348
|
-
const colors = getColors(theme)
|
|
349
|
-
const {
|
|
350
|
-
x,
|
|
351
|
-
y,
|
|
352
|
-
width,
|
|
353
|
-
height,
|
|
354
|
-
data,
|
|
355
|
-
scrollLeft,
|
|
356
|
-
kWidth,
|
|
357
|
-
kGap,
|
|
358
|
-
startIndex,
|
|
359
|
-
endIndex,
|
|
360
|
-
dpr,
|
|
361
|
-
bgColor = colors.TAG_BG.TRANSPARENT,
|
|
362
|
-
textColor = colors.TEXT.SECONDARY,
|
|
363
|
-
lineColor = colors.BORDER.DARK,
|
|
364
|
-
fontSize = 12,
|
|
365
|
-
paddingX = 8,
|
|
366
|
-
drawTopBorder = true,
|
|
367
|
-
drawBottomBorder = true,
|
|
368
|
-
} = opts
|
|
369
|
-
|
|
370
|
-
const physKWidth = Math.round(kWidth * dpr)
|
|
371
|
-
const alignedPhysKWidth = physKWidth % 2 === 0 ? physKWidth + 1 : physKWidth
|
|
372
|
-
const physKGap = Math.round(kGap * dpr)
|
|
373
|
-
const unitPx = alignedPhysKWidth + physKGap
|
|
374
|
-
const startXPx = physKGap
|
|
375
|
-
|
|
376
|
-
const unit = unitPx / dpr
|
|
377
|
-
const startX = startXPx / dpr
|
|
378
|
-
const alignedKWidth = alignedPhysKWidth / dpr
|
|
379
|
-
|
|
380
|
-
ctx.fillStyle = bgColor
|
|
381
|
-
ctx.fillRect(x, y, width, height)
|
|
382
|
-
|
|
383
|
-
if (drawTopBorder) {
|
|
384
|
-
ctx.strokeStyle = lineColor
|
|
385
|
-
ctx.lineWidth = 1
|
|
386
|
-
ctx.beginPath()
|
|
387
|
-
ctx.moveTo(x, alignToPhysicalPixelCenter(y, dpr))
|
|
388
|
-
ctx.lineTo(x + width, alignToPhysicalPixelCenter(y, dpr))
|
|
389
|
-
ctx.stroke()
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
if (drawBottomBorder) {
|
|
393
|
-
ctx.strokeStyle = lineColor
|
|
394
|
-
ctx.lineWidth = 1
|
|
395
|
-
ctx.beginPath()
|
|
396
|
-
ctx.moveTo(x, alignToPhysicalPixelCenter(y + height, dpr))
|
|
397
|
-
ctx.lineTo(x + width, alignToPhysicalPixelCenter(y + height, dpr))
|
|
398
|
-
ctx.stroke()
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
ctx.textAlign = 'center'
|
|
402
|
-
ctx.textBaseline = 'middle'
|
|
403
|
-
ctx.fillStyle = textColor
|
|
404
|
-
const textY = y + height / 2
|
|
405
|
-
const regularFont = getFont(fontSize)
|
|
406
|
-
const boldFont = getFont(fontSize, { bold: true })
|
|
407
|
-
|
|
408
|
-
const boundaries = findMonthBoundaries(data)
|
|
409
|
-
const visibleBoundaries = boundaries.filter((idx: number) => idx >= startIndex && idx < endIndex)
|
|
410
|
-
|
|
411
|
-
let lastWasYear: boolean | null = null
|
|
412
|
-
|
|
413
|
-
for (const idx of visibleBoundaries) {
|
|
414
|
-
const k = data[idx]
|
|
415
|
-
if (!k) continue
|
|
416
|
-
|
|
417
|
-
const worldX = startX + idx * unit + alignedKWidth / 2
|
|
418
|
-
const screenX = worldX - scrollLeft
|
|
419
|
-
|
|
420
|
-
const minX = paddingX
|
|
421
|
-
const maxX = Math.max(paddingX, width - paddingX)
|
|
422
|
-
|
|
423
|
-
if (screenX >= minX && screenX <= maxX) {
|
|
424
|
-
const drawX = Math.min(Math.max(screenX, minX), maxX)
|
|
425
|
-
const { text, isYear } = formatMonthOrYear(k.timestamp)
|
|
426
|
-
if (lastWasYear !== isYear) {
|
|
427
|
-
setCanvasFont(ctx, isYear ? boldFont : regularFont)
|
|
428
|
-
lastWasYear = isYear
|
|
429
|
-
}
|
|
430
|
-
ctx.fillText(text, roundToPhysicalPixel(drawX, dpr), alignToPhysicalPixelCenter(textY, dpr))
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
/** ============ 轴标签绘制函数 ============ */
|
|
436
|
-
|
|
437
|
-
export interface AxisPriceLabelOptions {
|
|
438
|
-
x: number
|
|
439
|
-
y: number
|
|
440
|
-
width: number
|
|
441
|
-
height: number
|
|
442
|
-
priceY: number
|
|
443
|
-
price: number
|
|
444
|
-
dpr: number
|
|
445
|
-
bgColor?: string
|
|
446
|
-
borderColor?: string
|
|
447
|
-
textColor?: string
|
|
448
|
-
fontSize?: number
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
export function drawAxisPriceLabel(ctx: CanvasRenderingContext2D, opts: AxisPriceLabelOptions, theme: 'light' | 'dark' = 'light') {
|
|
452
|
-
const colors = getColors(theme)
|
|
453
|
-
const {
|
|
454
|
-
x,
|
|
455
|
-
y,
|
|
456
|
-
width,
|
|
457
|
-
height,
|
|
458
|
-
priceY,
|
|
459
|
-
price,
|
|
460
|
-
dpr,
|
|
461
|
-
bgColor = colors.LABEL.BG,
|
|
462
|
-
borderColor,
|
|
463
|
-
textColor = colors.LABEL.TEXT,
|
|
464
|
-
fontSize = 12,
|
|
465
|
-
} = opts
|
|
466
|
-
|
|
467
|
-
const priceText = price.toFixed(2)
|
|
468
|
-
|
|
469
|
-
ctx.save()
|
|
470
|
-
setCanvasFont(ctx, getFont(fontSize))
|
|
471
|
-
ctx.textBaseline = 'middle'
|
|
472
|
-
ctx.textAlign = 'center'
|
|
473
|
-
|
|
474
|
-
const textH = fontSize + 4
|
|
475
|
-
const rectH = textH
|
|
476
|
-
|
|
477
|
-
const yy = Math.min(Math.max(priceY, y + rectH / 2), y + height - rectH / 2)
|
|
478
|
-
const rectY = yy - rectH / 2
|
|
479
|
-
|
|
480
|
-
const rx = x
|
|
481
|
-
const ry = roundToPhysicalPixel(rectY, dpr)
|
|
482
|
-
const rw = width
|
|
483
|
-
const rh = roundToPhysicalPixel(rectH, dpr)
|
|
484
|
-
ctx.fillStyle = bgColor
|
|
485
|
-
ctx.fillRect(rx, ry, rw, rh)
|
|
486
|
-
|
|
487
|
-
if (borderColor) {
|
|
488
|
-
ctx.strokeStyle = borderColor
|
|
489
|
-
ctx.lineWidth = 1
|
|
490
|
-
ctx.strokeRect(
|
|
491
|
-
alignToPhysicalPixelCenter(rx, dpr),
|
|
492
|
-
alignToPhysicalPixelCenter(ry, dpr),
|
|
493
|
-
Math.max(0, rw - 1 / dpr),
|
|
494
|
-
Math.max(0, rh - 1 / dpr)
|
|
495
|
-
)
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
const centerX = x + width / 2
|
|
499
|
-
ctx.fillStyle = textColor
|
|
500
|
-
ctx.fillText(priceText, roundToPhysicalPixel(centerX, dpr), alignToPhysicalPixelCenter(yy, dpr))
|
|
501
|
-
|
|
502
|
-
ctx.restore()
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
export interface AxisTimeLabelOptions {
|
|
506
|
-
x: number
|
|
507
|
-
y: number
|
|
508
|
-
width: number
|
|
509
|
-
height: number
|
|
510
|
-
labelX: number
|
|
511
|
-
timestamp: number
|
|
512
|
-
dpr: number
|
|
513
|
-
bgColor?: string
|
|
514
|
-
textColor?: string
|
|
515
|
-
fontSize?: number
|
|
516
|
-
paddingX?: number
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
export function drawAxisTimeLabel(ctx: CanvasRenderingContext2D, opts: AxisTimeLabelOptions, theme: 'light' | 'dark' = 'light') {
|
|
520
|
-
const colors = getColors(theme)
|
|
521
|
-
const {
|
|
522
|
-
x,
|
|
523
|
-
y,
|
|
524
|
-
width,
|
|
525
|
-
height,
|
|
526
|
-
labelX,
|
|
527
|
-
timestamp,
|
|
528
|
-
dpr,
|
|
529
|
-
fontSize = 12,
|
|
530
|
-
paddingX = 8,
|
|
531
|
-
} = opts
|
|
532
|
-
|
|
533
|
-
const text = formatYMDShanghai(timestamp)
|
|
534
|
-
|
|
535
|
-
ctx.save()
|
|
536
|
-
setCanvasFont(ctx, getFont(fontSize))
|
|
537
|
-
ctx.textBaseline = 'middle'
|
|
538
|
-
ctx.textAlign = 'center'
|
|
539
|
-
|
|
540
|
-
const tw = Math.round(measureTextWidth(ctx, text))
|
|
541
|
-
const rectW = Math.min(width, tw + paddingX * 2)
|
|
542
|
-
const rectH = height
|
|
543
|
-
|
|
544
|
-
const centerX = Math.min(Math.max(labelX, x + rectW / 2), x + width - rectW / 2)
|
|
545
|
-
const centerY = y + height / 2
|
|
546
|
-
|
|
547
|
-
const rectX = centerX - rectW / 2
|
|
548
|
-
const rectY = y
|
|
549
|
-
|
|
550
|
-
ctx.fillStyle = opts.bgColor ?? colors.LABEL.BG
|
|
551
|
-
ctx.fillRect(
|
|
552
|
-
roundToPhysicalPixel(rectX, dpr),
|
|
553
|
-
roundToPhysicalPixel(rectY, dpr),
|
|
554
|
-
roundToPhysicalPixel(rectW, dpr),
|
|
555
|
-
roundToPhysicalPixel(rectH, dpr),
|
|
556
|
-
)
|
|
557
|
-
|
|
558
|
-
ctx.fillStyle = opts.textColor ?? colors.LABEL.TEXT
|
|
559
|
-
ctx.fillText(text, roundToPhysicalPixel(centerX, dpr), alignToPhysicalPixelCenter(centerY, dpr))
|
|
560
|
-
|
|
561
|
-
ctx.restore()
|
|
562
|
-
}
|
|
1
|
+
import type { KLineData } from '../../types/price'
|
|
2
|
+
import { priceToY, yToPrice } from '../priceToY'
|
|
3
|
+
import { alignToPhysicalPixelCenter, roundToPhysicalPixel } from '../../engine/draw/pixelAlign'
|
|
4
|
+
import { formatYMDShanghai, formatMonthOrYear, findMonthBoundaries } from '../../utils/dateFormat'
|
|
5
|
+
import { getColors } from '../../engine/theme/colors'
|
|
6
|
+
import { getFont, setCanvasFont } from '../../engine/theme/fonts'
|
|
7
|
+
|
|
8
|
+
const textWidthCache = new Map<string, number>()
|
|
9
|
+
const TEXT_WIDTH_CACHE_LIMIT = 512
|
|
10
|
+
|
|
11
|
+
function measureTextWidth(ctx: CanvasRenderingContext2D, text: string): number {
|
|
12
|
+
const key = `${ctx.font}\n${text}`
|
|
13
|
+
const cached = textWidthCache.get(key)
|
|
14
|
+
if (cached !== undefined) {
|
|
15
|
+
return cached
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const width = ctx.measureText(text).width
|
|
19
|
+
if (textWidthCache.size >= TEXT_WIDTH_CACHE_LIMIT) {
|
|
20
|
+
textWidthCache.clear()
|
|
21
|
+
}
|
|
22
|
+
textWidthCache.set(key, width)
|
|
23
|
+
return width
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface PriceAxisOptions {
|
|
27
|
+
x: number
|
|
28
|
+
y: number
|
|
29
|
+
width: number
|
|
30
|
+
height: number
|
|
31
|
+
priceRange: { maxPrice: number; minPrice: number }
|
|
32
|
+
yPaddingPx?: number
|
|
33
|
+
dpr: number
|
|
34
|
+
ticks?: number
|
|
35
|
+
bgColor?: string
|
|
36
|
+
textColor?: string
|
|
37
|
+
lineColor?: string
|
|
38
|
+
fontSize?: number
|
|
39
|
+
paddingX?: number
|
|
40
|
+
/** 是否绘制左侧边界竖线(默认 true) */
|
|
41
|
+
drawLeftBorder?: boolean
|
|
42
|
+
/** 是否绘制刻度短线(默认 true) */
|
|
43
|
+
drawTickLines?: boolean
|
|
44
|
+
/** 价格偏移量(用于价格轴平移时同步显示) */
|
|
45
|
+
priceOffset?: number
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 右侧价格轴(固定,不随 translate/scroll 变化) */
|
|
49
|
+
export function drawPriceAxis(ctx: CanvasRenderingContext2D, opts: PriceAxisOptions, theme: 'light' | 'dark' = 'light') {
|
|
50
|
+
const colors = getColors(theme)
|
|
51
|
+
const {
|
|
52
|
+
x,
|
|
53
|
+
y,
|
|
54
|
+
width,
|
|
55
|
+
height,
|
|
56
|
+
priceRange,
|
|
57
|
+
yPaddingPx = 0,
|
|
58
|
+
dpr,
|
|
59
|
+
ticks = 10,
|
|
60
|
+
bgColor = colors.TAG_BG.TRANSPARENT,
|
|
61
|
+
textColor = colors.TEXT.SECONDARY,
|
|
62
|
+
lineColor = colors.BORDER.DARK,
|
|
63
|
+
fontSize = 16,
|
|
64
|
+
drawLeftBorder = true,
|
|
65
|
+
drawTickLines = true,
|
|
66
|
+
priceOffset = 0,
|
|
67
|
+
} = opts
|
|
68
|
+
|
|
69
|
+
const wantPad = yPaddingPx
|
|
70
|
+
const pad = Math.max(0, Math.min(wantPad, Math.floor(height / 2) - 1))
|
|
71
|
+
|
|
72
|
+
const { maxPrice, minPrice } = priceRange
|
|
73
|
+
const range = maxPrice - minPrice
|
|
74
|
+
const step = range === 0 ? 0 : range / (Math.max(2, ticks) - 1)
|
|
75
|
+
|
|
76
|
+
ctx.fillStyle = bgColor
|
|
77
|
+
ctx.fillRect(x, y, width, height)
|
|
78
|
+
|
|
79
|
+
if (drawLeftBorder) {
|
|
80
|
+
ctx.strokeStyle = lineColor
|
|
81
|
+
ctx.lineWidth = 1
|
|
82
|
+
ctx.beginPath()
|
|
83
|
+
ctx.moveTo(alignToPhysicalPixelCenter(x, dpr), y)
|
|
84
|
+
ctx.lineTo(alignToPhysicalPixelCenter(x, dpr), y + height)
|
|
85
|
+
ctx.stroke()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
setCanvasFont(ctx, getFont(fontSize))
|
|
89
|
+
ctx.textBaseline = 'middle'
|
|
90
|
+
ctx.textAlign = 'center'
|
|
91
|
+
ctx.strokeStyle = lineColor
|
|
92
|
+
ctx.fillStyle = textColor
|
|
93
|
+
|
|
94
|
+
const centerX = x + width / 2
|
|
95
|
+
|
|
96
|
+
for (let i = 0; i < Math.max(2, ticks); i++) {
|
|
97
|
+
const p = range === 0 ? maxPrice : maxPrice - step * i
|
|
98
|
+
const yy = Math.round(priceToY(p, maxPrice, minPrice, height, pad, pad) + y)
|
|
99
|
+
|
|
100
|
+
if (drawTickLines) {
|
|
101
|
+
ctx.beginPath()
|
|
102
|
+
const lineY = alignToPhysicalPixelCenter(yy, dpr)
|
|
103
|
+
ctx.moveTo(x, lineY)
|
|
104
|
+
ctx.lineTo(x + 4, lineY)
|
|
105
|
+
ctx.stroke()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const displayPrice = p + priceOffset
|
|
109
|
+
ctx.fillText(displayPrice.toFixed(2), roundToPhysicalPixel(centerX, dpr), alignToPhysicalPixelCenter(yy, dpr))
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface TimeAxisOptions {
|
|
114
|
+
x: number
|
|
115
|
+
y: number
|
|
116
|
+
width: number
|
|
117
|
+
height: number
|
|
118
|
+
data: KLineData[]
|
|
119
|
+
scrollLeft: number
|
|
120
|
+
kWidth: number
|
|
121
|
+
kGap: number
|
|
122
|
+
startIndex: number
|
|
123
|
+
endIndex: number
|
|
124
|
+
dpr: number
|
|
125
|
+
bgColor?: string
|
|
126
|
+
textColor?: string
|
|
127
|
+
lineColor?: string
|
|
128
|
+
fontSize?: number
|
|
129
|
+
/** 左右内边距(逻辑像素),避免月份/年份文字贴边 */
|
|
130
|
+
paddingX?: number
|
|
131
|
+
/** 是否绘制顶部边界线(默认 true,如果主图已有底边框则设为 false 避免重复) */
|
|
132
|
+
drawTopBorder?: boolean
|
|
133
|
+
/** 是否绘制底部边界线(默认 true,如果副图已有下边框则设为 false 避免重复) */
|
|
134
|
+
drawBottomBorder?: boolean
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface LastPriceLineOptions {
|
|
138
|
+
/** 绘图区宽度(逻辑像素) */
|
|
139
|
+
plotWidth: number
|
|
140
|
+
/** 绘图区高度(逻辑像素) */
|
|
141
|
+
plotHeight: number
|
|
142
|
+
/** 当前滚动位置(逻辑像素) */
|
|
143
|
+
scrollLeft: number
|
|
144
|
+
/** 可视范围:用于确定虚线的起止 worldX */
|
|
145
|
+
startIndex: number
|
|
146
|
+
endIndex: number
|
|
147
|
+
/** K线布局 */
|
|
148
|
+
kWidth: number
|
|
149
|
+
kGap: number
|
|
150
|
+
/** 价格范围 */
|
|
151
|
+
priceRange: { maxPrice: number; minPrice: number }
|
|
152
|
+
/** 最新价 */
|
|
153
|
+
lastPrice: number
|
|
154
|
+
/** Y轴 padding(与绘图区一致) */
|
|
155
|
+
yPaddingPx?: number
|
|
156
|
+
dpr: number
|
|
157
|
+
color?: string
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export interface CrosshairPriceLabelOptions {
|
|
161
|
+
x: number
|
|
162
|
+
y: number
|
|
163
|
+
width: number
|
|
164
|
+
height: number
|
|
165
|
+
/** 十字线的 y(相对该 canvas 的逻辑像素坐标) */
|
|
166
|
+
crosshairY: number
|
|
167
|
+
priceRange: { maxPrice: number; minPrice: number }
|
|
168
|
+
yPaddingPx?: number
|
|
169
|
+
dpr: number
|
|
170
|
+
bgColor?: string
|
|
171
|
+
borderColor?: string
|
|
172
|
+
textColor?: string
|
|
173
|
+
fontSize?: number
|
|
174
|
+
paddingX?: number
|
|
175
|
+
/** 价格偏移量(用于价格轴平移时同步显示) */
|
|
176
|
+
priceOffset?: number
|
|
177
|
+
/** 优先显示的价格(如十字线已按 active pane 算好) */
|
|
178
|
+
price?: number
|
|
179
|
+
formatPrice?: (price: number) => string
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface CrosshairTimeLabelOptions {
|
|
183
|
+
x: number
|
|
184
|
+
y: number
|
|
185
|
+
width: number
|
|
186
|
+
height: number
|
|
187
|
+
/** 十字线的 x(相对该 canvas 的逻辑像素坐标) */
|
|
188
|
+
crosshairX: number
|
|
189
|
+
/** 命中的交易日时间戳(毫秒) */
|
|
190
|
+
timestamp: number
|
|
191
|
+
dpr: number
|
|
192
|
+
bgColor?: string
|
|
193
|
+
textColor?: string
|
|
194
|
+
fontSize?: number
|
|
195
|
+
paddingX?: number
|
|
196
|
+
paddingY?: number
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function drawCrosshairTimeLabel(ctx: CanvasRenderingContext2D, opts: CrosshairTimeLabelOptions, theme: 'light' | 'dark' = 'light') {
|
|
200
|
+
const colors = getColors(theme)
|
|
201
|
+
const {
|
|
202
|
+
x,
|
|
203
|
+
y,
|
|
204
|
+
width,
|
|
205
|
+
height,
|
|
206
|
+
crosshairX,
|
|
207
|
+
timestamp,
|
|
208
|
+
dpr,
|
|
209
|
+
fontSize = 16,
|
|
210
|
+
paddingX = 8,
|
|
211
|
+
} = opts
|
|
212
|
+
|
|
213
|
+
const text = formatYMDShanghai(timestamp)
|
|
214
|
+
|
|
215
|
+
ctx.save()
|
|
216
|
+
setCanvasFont(ctx, getFont(fontSize))
|
|
217
|
+
ctx.textBaseline = 'middle'
|
|
218
|
+
ctx.textAlign = 'center'
|
|
219
|
+
|
|
220
|
+
const tw = Math.round(measureTextWidth(ctx, text))
|
|
221
|
+
const rectW = Math.min(width, tw + paddingX * 2)
|
|
222
|
+
const rectH = height
|
|
223
|
+
|
|
224
|
+
const centerX = Math.min(Math.max(crosshairX, x + rectW / 2), x + width - rectW / 2)
|
|
225
|
+
const centerY = y + height / 2
|
|
226
|
+
|
|
227
|
+
const rectX = centerX - rectW / 2
|
|
228
|
+
const rectY = y
|
|
229
|
+
|
|
230
|
+
ctx.fillStyle = colors.LABEL.BG
|
|
231
|
+
ctx.fillRect(
|
|
232
|
+
roundToPhysicalPixel(rectX, dpr),
|
|
233
|
+
roundToPhysicalPixel(rectY, dpr),
|
|
234
|
+
roundToPhysicalPixel(rectW, dpr),
|
|
235
|
+
roundToPhysicalPixel(rectH, dpr),
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
ctx.fillStyle = colors.LABEL.TEXT
|
|
239
|
+
ctx.fillText(text, roundToPhysicalPixel(centerX, dpr), alignToPhysicalPixelCenter(centerY, dpr))
|
|
240
|
+
|
|
241
|
+
ctx.restore()
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function drawCrosshairPriceLabel(ctx: CanvasRenderingContext2D, opts: CrosshairPriceLabelOptions, theme: 'light' | 'dark' = 'light') {
|
|
245
|
+
const colors = getColors(theme)
|
|
246
|
+
const {
|
|
247
|
+
x,
|
|
248
|
+
y,
|
|
249
|
+
width,
|
|
250
|
+
height,
|
|
251
|
+
crosshairY,
|
|
252
|
+
priceRange,
|
|
253
|
+
yPaddingPx = 0,
|
|
254
|
+
dpr,
|
|
255
|
+
bgColor = colors.LABEL.BG,
|
|
256
|
+
borderColor,
|
|
257
|
+
textColor = colors.LABEL.TEXT,
|
|
258
|
+
fontSize = 16,
|
|
259
|
+
priceOffset = 0,
|
|
260
|
+
price,
|
|
261
|
+
formatPrice,
|
|
262
|
+
} = opts
|
|
263
|
+
|
|
264
|
+
const pad = Math.max(0, Math.min(yPaddingPx, Math.floor(height / 2) - 1))
|
|
265
|
+
const { maxPrice, minPrice } = priceRange
|
|
266
|
+
const displayPrice = price ?? (yToPrice(crosshairY - y, maxPrice, minPrice, height, pad, pad) + priceOffset)
|
|
267
|
+
const priceText = formatPrice ? formatPrice(displayPrice) : displayPrice.toFixed(2)
|
|
268
|
+
|
|
269
|
+
ctx.save()
|
|
270
|
+
setCanvasFont(ctx, getFont(fontSize))
|
|
271
|
+
ctx.textBaseline = 'middle'
|
|
272
|
+
ctx.textAlign = 'center'
|
|
273
|
+
|
|
274
|
+
const textH = fontSize + 4
|
|
275
|
+
const rectH = textH
|
|
276
|
+
|
|
277
|
+
const yy = Math.min(Math.max(crosshairY, y + rectH / 2), y + height - rectH / 2)
|
|
278
|
+
const rectY = yy - rectH / 2
|
|
279
|
+
|
|
280
|
+
const rx = x
|
|
281
|
+
const ry = roundToPhysicalPixel(rectY, dpr)
|
|
282
|
+
const rw = width
|
|
283
|
+
const rh = roundToPhysicalPixel(rectH, dpr)
|
|
284
|
+
ctx.fillStyle = bgColor
|
|
285
|
+
ctx.fillRect(rx, ry, rw, rh)
|
|
286
|
+
|
|
287
|
+
if (borderColor) {
|
|
288
|
+
ctx.strokeStyle = borderColor
|
|
289
|
+
ctx.lineWidth = 1
|
|
290
|
+
ctx.strokeRect(
|
|
291
|
+
alignToPhysicalPixelCenter(rx, dpr),
|
|
292
|
+
alignToPhysicalPixelCenter(ry, dpr),
|
|
293
|
+
Math.max(0, rw - 1 / dpr),
|
|
294
|
+
Math.max(0, rh - 1 / dpr)
|
|
295
|
+
)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const centerX = x + width / 2
|
|
299
|
+
ctx.fillStyle = textColor
|
|
300
|
+
ctx.fillText(priceText, roundToPhysicalPixel(centerX, dpr), alignToPhysicalPixelCenter(yy, dpr))
|
|
301
|
+
|
|
302
|
+
ctx.restore()
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** 绘制"最新价水平虚线"(画在 plotCanvas 的 world 坐标系:需在 translate(-scrollLeft,0) 之后调用) */
|
|
306
|
+
export function drawLastPriceDashedLine(ctx: CanvasRenderingContext2D, opts: LastPriceLineOptions, theme: 'light' | 'dark' = 'light') {
|
|
307
|
+
const colors = getColors(theme)
|
|
308
|
+
const {
|
|
309
|
+
plotWidth,
|
|
310
|
+
plotHeight,
|
|
311
|
+
scrollLeft,
|
|
312
|
+
startIndex,
|
|
313
|
+
endIndex,
|
|
314
|
+
kWidth,
|
|
315
|
+
kGap,
|
|
316
|
+
priceRange,
|
|
317
|
+
lastPrice,
|
|
318
|
+
yPaddingPx = 0,
|
|
319
|
+
dpr,
|
|
320
|
+
color = colors.CROSSHAIR.LINE,
|
|
321
|
+
} = opts
|
|
322
|
+
|
|
323
|
+
const { maxPrice, minPrice } = priceRange
|
|
324
|
+
if (!(lastPrice >= minPrice && lastPrice <= maxPrice)) return
|
|
325
|
+
|
|
326
|
+
const pad = Math.max(0, Math.min(yPaddingPx, Math.floor(plotHeight / 2) - 1))
|
|
327
|
+
const y = priceToY(lastPrice, maxPrice, minPrice, plotHeight, pad, pad)
|
|
328
|
+
|
|
329
|
+
const unit = kWidth + kGap
|
|
330
|
+
const startX = kGap + startIndex * unit
|
|
331
|
+
const endX = kGap + endIndex * unit
|
|
332
|
+
|
|
333
|
+
ctx.save()
|
|
334
|
+
ctx.strokeStyle = color
|
|
335
|
+
ctx.lineWidth = 1
|
|
336
|
+
ctx.setLineDash([4, 3])
|
|
337
|
+
ctx.beginPath()
|
|
338
|
+
const yy = alignToPhysicalPixelCenter(y, dpr)
|
|
339
|
+
ctx.moveTo(roundToPhysicalPixel(startX, dpr), yy)
|
|
340
|
+
ctx.lineTo(roundToPhysicalPixel(endX, dpr), yy)
|
|
341
|
+
ctx.stroke()
|
|
342
|
+
ctx.setLineDash([])
|
|
343
|
+
ctx.restore()
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** 底部时间轴(X方向随 scrollLeft 变化) */
|
|
347
|
+
export function drawTimeAxis(ctx: CanvasRenderingContext2D, opts: TimeAxisOptions, theme: 'light' | 'dark' = 'light') {
|
|
348
|
+
const colors = getColors(theme)
|
|
349
|
+
const {
|
|
350
|
+
x,
|
|
351
|
+
y,
|
|
352
|
+
width,
|
|
353
|
+
height,
|
|
354
|
+
data,
|
|
355
|
+
scrollLeft,
|
|
356
|
+
kWidth,
|
|
357
|
+
kGap,
|
|
358
|
+
startIndex,
|
|
359
|
+
endIndex,
|
|
360
|
+
dpr,
|
|
361
|
+
bgColor = colors.TAG_BG.TRANSPARENT,
|
|
362
|
+
textColor = colors.TEXT.SECONDARY,
|
|
363
|
+
lineColor = colors.BORDER.DARK,
|
|
364
|
+
fontSize = 12,
|
|
365
|
+
paddingX = 8,
|
|
366
|
+
drawTopBorder = true,
|
|
367
|
+
drawBottomBorder = true,
|
|
368
|
+
} = opts
|
|
369
|
+
|
|
370
|
+
const physKWidth = Math.round(kWidth * dpr)
|
|
371
|
+
const alignedPhysKWidth = physKWidth % 2 === 0 ? physKWidth + 1 : physKWidth
|
|
372
|
+
const physKGap = Math.round(kGap * dpr)
|
|
373
|
+
const unitPx = alignedPhysKWidth + physKGap
|
|
374
|
+
const startXPx = physKGap
|
|
375
|
+
|
|
376
|
+
const unit = unitPx / dpr
|
|
377
|
+
const startX = startXPx / dpr
|
|
378
|
+
const alignedKWidth = alignedPhysKWidth / dpr
|
|
379
|
+
|
|
380
|
+
ctx.fillStyle = bgColor
|
|
381
|
+
ctx.fillRect(x, y, width, height)
|
|
382
|
+
|
|
383
|
+
if (drawTopBorder) {
|
|
384
|
+
ctx.strokeStyle = lineColor
|
|
385
|
+
ctx.lineWidth = 1
|
|
386
|
+
ctx.beginPath()
|
|
387
|
+
ctx.moveTo(x, alignToPhysicalPixelCenter(y, dpr))
|
|
388
|
+
ctx.lineTo(x + width, alignToPhysicalPixelCenter(y, dpr))
|
|
389
|
+
ctx.stroke()
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (drawBottomBorder) {
|
|
393
|
+
ctx.strokeStyle = lineColor
|
|
394
|
+
ctx.lineWidth = 1
|
|
395
|
+
ctx.beginPath()
|
|
396
|
+
ctx.moveTo(x, alignToPhysicalPixelCenter(y + height, dpr))
|
|
397
|
+
ctx.lineTo(x + width, alignToPhysicalPixelCenter(y + height, dpr))
|
|
398
|
+
ctx.stroke()
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
ctx.textAlign = 'center'
|
|
402
|
+
ctx.textBaseline = 'middle'
|
|
403
|
+
ctx.fillStyle = textColor
|
|
404
|
+
const textY = y + height / 2
|
|
405
|
+
const regularFont = getFont(fontSize)
|
|
406
|
+
const boldFont = getFont(fontSize, { bold: true })
|
|
407
|
+
|
|
408
|
+
const boundaries = findMonthBoundaries(data)
|
|
409
|
+
const visibleBoundaries = boundaries.filter((idx: number) => idx >= startIndex && idx < endIndex)
|
|
410
|
+
|
|
411
|
+
let lastWasYear: boolean | null = null
|
|
412
|
+
|
|
413
|
+
for (const idx of visibleBoundaries) {
|
|
414
|
+
const k = data[idx]
|
|
415
|
+
if (!k) continue
|
|
416
|
+
|
|
417
|
+
const worldX = startX + idx * unit + alignedKWidth / 2
|
|
418
|
+
const screenX = worldX - scrollLeft
|
|
419
|
+
|
|
420
|
+
const minX = paddingX
|
|
421
|
+
const maxX = Math.max(paddingX, width - paddingX)
|
|
422
|
+
|
|
423
|
+
if (screenX >= minX && screenX <= maxX) {
|
|
424
|
+
const drawX = Math.min(Math.max(screenX, minX), maxX)
|
|
425
|
+
const { text, isYear } = formatMonthOrYear(k.timestamp)
|
|
426
|
+
if (lastWasYear !== isYear) {
|
|
427
|
+
setCanvasFont(ctx, isYear ? boldFont : regularFont)
|
|
428
|
+
lastWasYear = isYear
|
|
429
|
+
}
|
|
430
|
+
ctx.fillText(text, roundToPhysicalPixel(drawX, dpr), alignToPhysicalPixelCenter(textY, dpr))
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** ============ 轴标签绘制函数 ============ */
|
|
436
|
+
|
|
437
|
+
export interface AxisPriceLabelOptions {
|
|
438
|
+
x: number
|
|
439
|
+
y: number
|
|
440
|
+
width: number
|
|
441
|
+
height: number
|
|
442
|
+
priceY: number
|
|
443
|
+
price: number
|
|
444
|
+
dpr: number
|
|
445
|
+
bgColor?: string
|
|
446
|
+
borderColor?: string
|
|
447
|
+
textColor?: string
|
|
448
|
+
fontSize?: number
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
export function drawAxisPriceLabel(ctx: CanvasRenderingContext2D, opts: AxisPriceLabelOptions, theme: 'light' | 'dark' = 'light') {
|
|
452
|
+
const colors = getColors(theme)
|
|
453
|
+
const {
|
|
454
|
+
x,
|
|
455
|
+
y,
|
|
456
|
+
width,
|
|
457
|
+
height,
|
|
458
|
+
priceY,
|
|
459
|
+
price,
|
|
460
|
+
dpr,
|
|
461
|
+
bgColor = colors.LABEL.BG,
|
|
462
|
+
borderColor,
|
|
463
|
+
textColor = colors.LABEL.TEXT,
|
|
464
|
+
fontSize = 12,
|
|
465
|
+
} = opts
|
|
466
|
+
|
|
467
|
+
const priceText = price.toFixed(2)
|
|
468
|
+
|
|
469
|
+
ctx.save()
|
|
470
|
+
setCanvasFont(ctx, getFont(fontSize))
|
|
471
|
+
ctx.textBaseline = 'middle'
|
|
472
|
+
ctx.textAlign = 'center'
|
|
473
|
+
|
|
474
|
+
const textH = fontSize + 4
|
|
475
|
+
const rectH = textH
|
|
476
|
+
|
|
477
|
+
const yy = Math.min(Math.max(priceY, y + rectH / 2), y + height - rectH / 2)
|
|
478
|
+
const rectY = yy - rectH / 2
|
|
479
|
+
|
|
480
|
+
const rx = x
|
|
481
|
+
const ry = roundToPhysicalPixel(rectY, dpr)
|
|
482
|
+
const rw = width
|
|
483
|
+
const rh = roundToPhysicalPixel(rectH, dpr)
|
|
484
|
+
ctx.fillStyle = bgColor
|
|
485
|
+
ctx.fillRect(rx, ry, rw, rh)
|
|
486
|
+
|
|
487
|
+
if (borderColor) {
|
|
488
|
+
ctx.strokeStyle = borderColor
|
|
489
|
+
ctx.lineWidth = 1
|
|
490
|
+
ctx.strokeRect(
|
|
491
|
+
alignToPhysicalPixelCenter(rx, dpr),
|
|
492
|
+
alignToPhysicalPixelCenter(ry, dpr),
|
|
493
|
+
Math.max(0, rw - 1 / dpr),
|
|
494
|
+
Math.max(0, rh - 1 / dpr)
|
|
495
|
+
)
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const centerX = x + width / 2
|
|
499
|
+
ctx.fillStyle = textColor
|
|
500
|
+
ctx.fillText(priceText, roundToPhysicalPixel(centerX, dpr), alignToPhysicalPixelCenter(yy, dpr))
|
|
501
|
+
|
|
502
|
+
ctx.restore()
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
export interface AxisTimeLabelOptions {
|
|
506
|
+
x: number
|
|
507
|
+
y: number
|
|
508
|
+
width: number
|
|
509
|
+
height: number
|
|
510
|
+
labelX: number
|
|
511
|
+
timestamp: number
|
|
512
|
+
dpr: number
|
|
513
|
+
bgColor?: string
|
|
514
|
+
textColor?: string
|
|
515
|
+
fontSize?: number
|
|
516
|
+
paddingX?: number
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
export function drawAxisTimeLabel(ctx: CanvasRenderingContext2D, opts: AxisTimeLabelOptions, theme: 'light' | 'dark' = 'light') {
|
|
520
|
+
const colors = getColors(theme)
|
|
521
|
+
const {
|
|
522
|
+
x,
|
|
523
|
+
y,
|
|
524
|
+
width,
|
|
525
|
+
height,
|
|
526
|
+
labelX,
|
|
527
|
+
timestamp,
|
|
528
|
+
dpr,
|
|
529
|
+
fontSize = 12,
|
|
530
|
+
paddingX = 8,
|
|
531
|
+
} = opts
|
|
532
|
+
|
|
533
|
+
const text = formatYMDShanghai(timestamp)
|
|
534
|
+
|
|
535
|
+
ctx.save()
|
|
536
|
+
setCanvasFont(ctx, getFont(fontSize))
|
|
537
|
+
ctx.textBaseline = 'middle'
|
|
538
|
+
ctx.textAlign = 'center'
|
|
539
|
+
|
|
540
|
+
const tw = Math.round(measureTextWidth(ctx, text))
|
|
541
|
+
const rectW = Math.min(width, tw + paddingX * 2)
|
|
542
|
+
const rectH = height
|
|
543
|
+
|
|
544
|
+
const centerX = Math.min(Math.max(labelX, x + rectW / 2), x + width - rectW / 2)
|
|
545
|
+
const centerY = y + height / 2
|
|
546
|
+
|
|
547
|
+
const rectX = centerX - rectW / 2
|
|
548
|
+
const rectY = y
|
|
549
|
+
|
|
550
|
+
ctx.fillStyle = opts.bgColor ?? colors.LABEL.BG
|
|
551
|
+
ctx.fillRect(
|
|
552
|
+
roundToPhysicalPixel(rectX, dpr),
|
|
553
|
+
roundToPhysicalPixel(rectY, dpr),
|
|
554
|
+
roundToPhysicalPixel(rectW, dpr),
|
|
555
|
+
roundToPhysicalPixel(rectH, dpr),
|
|
556
|
+
)
|
|
557
|
+
|
|
558
|
+
ctx.fillStyle = opts.textColor ?? colors.LABEL.TEXT
|
|
559
|
+
ctx.fillText(text, roundToPhysicalPixel(centerX, dpr), alignToPhysicalPixelCenter(centerY, dpr))
|
|
560
|
+
|
|
561
|
+
ctx.restore()
|
|
562
|
+
}
|