@quantlife/qlchart 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (321) hide show
  1. package/.idea/QLChart.iml +12 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/vcs.xml +6 -0
  4. package/README.md +75 -0
  5. package/demo/App.css +213 -0
  6. package/demo/App.tsx +46 -0
  7. package/demo/components/ControlPanel.tsx +13 -0
  8. package/demo/components/DemoNav.tsx +27 -0
  9. package/demo/index.html +16 -0
  10. package/demo/main.tsx +10 -0
  11. package/demo/pages/BasicChartDemo.tsx +61 -0
  12. package/demo/pages/DrawingDemo.tsx +22 -0
  13. package/demo/pages/IndicatorDemo.tsx +22 -0
  14. package/demo/pages/LayoutDemo.tsx +35 -0
  15. package/demo/pages/MultiPeriodDemo.tsx +31 -0
  16. package/demo/pages/ReplayDemo.tsx +195 -0
  17. package/demo/pages/SaveDemo.tsx +27 -0
  18. package/demo/pages/ThemeDemo.tsx +29 -0
  19. package/demo/standalone-demo.html +597 -0
  20. package/demo/vite.config.demo.ts +17 -0
  21. package/dist/index.d.ts +1973 -0
  22. package/dist/qlchart.js +23169 -0
  23. package/dist/style.css +1 -0
  24. package/doc/api/indicator-data-processor.md +35 -0
  25. package/doc/api-reference/.nojekyll +1 -0
  26. package/doc/api-reference/assets/hierarchy.js +1 -0
  27. package/doc/api-reference/assets/highlight.css +43 -0
  28. package/doc/api-reference/assets/icons.js +18 -0
  29. package/doc/api-reference/assets/icons.svg +1 -0
  30. package/doc/api-reference/assets/main.js +60 -0
  31. package/doc/api-reference/assets/navigation.js +1 -0
  32. package/doc/api-reference/assets/search.js +1 -0
  33. package/doc/api-reference/assets/style.css +1611 -0
  34. package/doc/api-reference/classes/ChartManager.html +16 -0
  35. package/doc/api-reference/classes/DataManager.html +13 -0
  36. package/doc/api-reference/classes/DrawingAdapter.html +64 -0
  37. package/doc/api-reference/classes/DrawingPersistence.html +21 -0
  38. package/doc/api-reference/classes/EventManager.html +12 -0
  39. package/doc/api-reference/classes/HollowCandlestickSeries.html +22 -0
  40. package/doc/api-reference/classes/IndicatorRenderer.html +20 -0
  41. package/doc/api-reference/classes/KlineReplay.html +31 -0
  42. package/doc/api-reference/classes/MockDataService.html +13 -0
  43. package/doc/api-reference/classes/MockIndicatorService.html +4 -0
  44. package/doc/api-reference/classes/OverlayIndicator.html +11 -0
  45. package/doc/api-reference/classes/PaneIndicator.html +16 -0
  46. package/doc/api-reference/classes/PaneManager.html +24 -0
  47. package/doc/api-reference/classes/RealtimeDataFeed.html +22 -0
  48. package/doc/api-reference/classes/RenkoSeries.html +22 -0
  49. package/doc/api-reference/classes/ScreenshotUtil.html +10 -0
  50. package/doc/api-reference/classes/SeriesManager.html +30 -0
  51. package/doc/api-reference/classes/ThemeManager.html +18 -0
  52. package/doc/api-reference/enums/ChartEvent.html +12 -0
  53. package/doc/api-reference/enums/IndicatorType.html +4 -0
  54. package/doc/api-reference/enums/SeriesType.html +13 -0
  55. package/doc/api-reference/functions/ChartFunctionMenu.html +1 -0
  56. package/doc/api-reference/functions/DrawingModule.html +8 -0
  57. package/doc/api-reference/functions/IndicatorPanel.html +2 -0
  58. package/doc/api-reference/functions/IndicatorTag.html +2 -0
  59. package/doc/api-reference/functions/KlineTypeSelector.html +1 -0
  60. package/doc/api-reference/functions/LayoutSwitcher.html +1 -0
  61. package/doc/api-reference/functions/PeriodSelector.html +1 -0
  62. package/doc/api-reference/functions/QLChartLayout.html +1 -0
  63. package/doc/api-reference/functions/QLChartPanel.html +10 -0
  64. package/doc/api-reference/functions/QLChartProvider.html +2 -0
  65. package/doc/api-reference/functions/QLToolbar.html +1 -0
  66. package/doc/api-reference/functions/ReplayController.html +1 -0
  67. package/doc/api-reference/functions/TimeBarModule.html +4 -0
  68. package/doc/api-reference/functions/TimeRangeSelector.html +1 -0
  69. package/doc/api-reference/functions/createIndicatorConfig.html +2 -0
  70. package/doc/api-reference/functions/getToolConfig.html +2 -0
  71. package/doc/api-reference/functions/getToolsByCategory.html +2 -0
  72. package/doc/api-reference/functions/getToolsByPriority.html +2 -0
  73. package/doc/api-reference/functions/mapLibTypeToOurs.html +2 -0
  74. package/doc/api-reference/functions/mapToolTypeToLib.html +3 -0
  75. package/doc/api-reference/functions/transformCandlestickData.html +3 -0
  76. package/doc/api-reference/functions/transformIndicatorData.html +2 -0
  77. package/doc/api-reference/functions/transformVolumeData.html +3 -0
  78. package/doc/api-reference/functions/useChart.html +4 -0
  79. package/doc/api-reference/functions/useChartStore.html +8 -0
  80. package/doc/api-reference/functions/useCrosshairSync.html +8 -0
  81. package/doc/api-reference/functions/useDrawingModule.html +1 -0
  82. package/doc/api-reference/functions/useDrawingStore.html +8 -0
  83. package/doc/api-reference/functions/useIndicatorStore.html +8 -0
  84. package/doc/api-reference/functions/useQLChartConfig.html +2 -0
  85. package/doc/api-reference/functions/useReplayStore.html +8 -0
  86. package/doc/api-reference/functions/useTheme.html +2 -0
  87. package/doc/api-reference/functions/useTimeBarStore.html +8 -0
  88. package/doc/api-reference/index.html +1 -0
  89. package/doc/api-reference/interfaces/CandlestickData.html +7 -0
  90. package/doc/api-reference/interfaces/CandlestickRawData.html +8 -0
  91. package/doc/api-reference/interfaces/ChartFunctionMenuProps.html +2 -0
  92. package/doc/api-reference/interfaces/ChartManagerCreateOptions.html +4 -0
  93. package/doc/api-reference/interfaces/ChartOptions.html +8 -0
  94. package/doc/api-reference/interfaces/ChartRequestParams.html +8 -0
  95. package/doc/api-reference/interfaces/ChartResponse.html +5 -0
  96. package/doc/api-reference/interfaces/ChartState.html +24 -0
  97. package/doc/api-reference/interfaces/ChartThemeOptions.html +5 -0
  98. package/doc/api-reference/interfaces/CrosshairData.html +5 -0
  99. package/doc/api-reference/interfaces/DrawingModuleProps.html +5 -0
  100. package/doc/api-reference/interfaces/DrawingState.html +48 -0
  101. package/doc/api-reference/interfaces/HistogramData.html +5 -0
  102. package/doc/api-reference/interfaces/HollowCandlestickData.html +14 -0
  103. package/doc/api-reference/interfaces/IndicatorConfig.html +11 -0
  104. package/doc/api-reference/interfaces/IndicatorDataPoint.html +4 -0
  105. package/doc/api-reference/interfaces/IndicatorDataResponse.html +5 -0
  106. package/doc/api-reference/interfaces/IndicatorDefinition.html +9 -0
  107. package/doc/api-reference/interfaces/IndicatorPanelProps.html +3 -0
  108. package/doc/api-reference/interfaces/IndicatorParamDef.html +8 -0
  109. package/doc/api-reference/interfaces/IndicatorRawData.html +4 -0
  110. package/doc/api-reference/interfaces/IndicatorState.html +19 -0
  111. package/doc/api-reference/interfaces/IndicatorTagProps.html +2 -0
  112. package/doc/api-reference/interfaces/KlineReplayOptions.html +4 -0
  113. package/doc/api-reference/interfaces/LayoutSwitcherProps.html +3 -0
  114. package/doc/api-reference/interfaces/LineData.html +4 -0
  115. package/doc/api-reference/interfaces/MockDataConfig.html +8 -0
  116. package/doc/api-reference/interfaces/MockIndicatorConfig.html +5 -0
  117. package/doc/api-reference/interfaces/PairInfo.html +6 -0
  118. package/doc/api-reference/interfaces/PaneInfo.html +6 -0
  119. package/doc/api-reference/interfaces/PanelConfig.html +9 -0
  120. package/doc/api-reference/interfaces/PersistenceConfig.html +12 -0
  121. package/doc/api-reference/interfaces/QLChartConfig.html +18 -0
  122. package/doc/api-reference/interfaces/QLChartLayoutProps.html +9 -0
  123. package/doc/api-reference/interfaces/QLChartPanelProps.html +13 -0
  124. package/doc/api-reference/interfaces/QLChartPanelRef.html +14 -0
  125. package/doc/api-reference/interfaces/QLToolbarProps.html +7 -0
  126. package/doc/api-reference/interfaces/RealtimeCandle.html +9 -0
  127. package/doc/api-reference/interfaces/RealtimeSubscribeFn.html +2 -0
  128. package/doc/api-reference/interfaces/RenkoData.html +16 -0
  129. package/doc/api-reference/interfaces/ReplayControllerProps.html +2 -0
  130. package/doc/api-reference/interfaces/ReplayState.html +19 -0
  131. package/doc/api-reference/interfaces/ThemeConfig.html +14 -0
  132. package/doc/api-reference/interfaces/TimeBarModuleProps.html +5 -0
  133. package/doc/api-reference/interfaces/TimeBarState.html +13 -0
  134. package/doc/api-reference/interfaces/UseChartReturn.html +4 -0
  135. package/doc/api-reference/interfaces/UseDrawingModuleOptions.html +11 -0
  136. package/doc/api-reference/interfaces/UseDrawingModuleReturn.html +6 -0
  137. package/doc/api-reference/interfaces/UseThemeReturn.html +5 -0
  138. package/doc/api-reference/types/EventHandler.html +2 -0
  139. package/doc/api-reference/types/FetchFn.html +2 -0
  140. package/doc/api-reference/types/LayoutMode.html +2 -0
  141. package/doc/api-reference/types/MarketTrend.html +2 -0
  142. package/doc/api-reference/types/ThemePreset.html +2 -0
  143. package/doc/api-reference/variables/BUILTIN_INDICATORS.html +2 -0
  144. package/doc/api-reference/variables/CATEGORY_LABELS.html +2 -0
  145. package/doc/api-reference/variables/DRAWING_TOOLS.html +3 -0
  146. package/doc/api-reference/variables/MARKET_PRESETS.html +1 -0
  147. package/doc/api-reference/variables/PAIR_PRESETS.html +1 -0
  148. package/doc/api-reference/variables/darkPreset.html +1 -0
  149. package/doc/api-reference/variables/lightPreset.html +1 -0
  150. package/doc/components/drawing-module.md +24 -0
  151. package/doc/components/indicator-list-panel.md +24 -0
  152. package/doc/components/indicator-panel.md +17 -0
  153. package/doc/components/pane-divider.md +25 -0
  154. package/doc/components/qlchart-layout.md +30 -0
  155. package/doc/components/qlchart-panel.md +93 -0
  156. package/doc/components/qlchart-provider.md +73 -0
  157. package/doc/components/qltoolbar.md +17 -0
  158. package/doc/components/replay-controller.md +23 -0
  159. package/doc/components/timebar-module.md +13 -0
  160. package/doc/core/chart-manager.md +14 -0
  161. package/doc/core/data-manager.md +33 -0
  162. package/doc/core/event-manager.md +26 -0
  163. package/doc/core/pane-manager.md +13 -0
  164. package/doc/core/series-manager.md +19 -0
  165. package/doc/core/theme-manager.md +21 -0
  166. package/doc/examples/basic-chart.md +24 -0
  167. package/doc/examples/data-format-guide.md +119 -0
  168. package/doc/examples/drawing-tools.md +30 -0
  169. package/doc/examples/indicator-properties.md +34 -0
  170. package/doc/examples/multi-pane.md +24 -0
  171. package/doc/examples/multi-panel.md +23 -0
  172. package/doc/examples/realtime-data.md +147 -0
  173. package/doc/examples/standalone-js.md +333 -0
  174. package/doc/guide/architecture.md +87 -0
  175. package/doc/guide/data-flow.md +310 -0
  176. package/doc/guide/deployment.md +59 -0
  177. package/doc/guide/drawing-properties.md +40 -0
  178. package/doc/guide/getting-started.md +94 -0
  179. package/doc/guide/pane-system.md +47 -0
  180. package/doc/guide/theme-switching.md +58 -0
  181. package/doc/hooks/use-chart.md +20 -0
  182. package/doc/hooks/use-crosshair-sync.md +14 -0
  183. package/doc/hooks/use-drawing-module.md +43 -0
  184. package/doc/hooks/use-theme.md +15 -0
  185. package/doc/index.md +33 -0
  186. package/doc/plugins/drawing/overview.md +36 -0
  187. package/doc/plugins/drawing/persistence.md +42 -0
  188. package/doc/plugins/drawing/tool-registry.md +29 -0
  189. package/doc/plugins/hollow-candlestick.md +18 -0
  190. package/doc/plugins/indicators.md +28 -0
  191. package/doc/plugins/renko.md +17 -0
  192. package/doc/plugins/replay.md +21 -0
  193. package/doc/plugins/screenshot.md +20 -0
  194. package/docs/api.md +94 -0
  195. package/package.json +54 -0
  196. package/python/qlchart/__init__.py +9 -0
  197. package/python/qlchart/__pycache__/__init__.cpython-311.pyc +0 -0
  198. package/python/qlchart/__pycache__/chart.cpython-311.pyc +0 -0
  199. package/python/qlchart/chart.py +333 -0
  200. package/python/qlchart/templates/chart_template.html +304 -0
  201. package/python/requirements.txt +1 -0
  202. package/python/setup.py +18 -0
  203. package/python/tests/__init__.py +1 -0
  204. package/python/tests/__pycache__/__init__.cpython-311.pyc +0 -0
  205. package/python/tests/__pycache__/test_chart.cpython-311-pytest-8.3.3.pyc +0 -0
  206. package/python/tests/test_chart.py +114 -0
  207. package/quantlife-qlchart-0.0.1.tgz +0 -0
  208. package/src/api/chartApi.ts +30 -0
  209. package/src/api/indicatorApi.ts +27 -0
  210. package/src/components/ChartFunctionMenu.tsx +64 -0
  211. package/src/components/PaneChartPanel.tsx +116 -0
  212. package/src/components/PaneDivider.tsx +66 -0
  213. package/src/components/QLChartLayout.tsx +151 -0
  214. package/src/components/QLChartPanel.tsx +560 -0
  215. package/src/components/QLChartProvider.tsx +90 -0
  216. package/src/components/context-menu/ChartContextMenu.tsx +139 -0
  217. package/src/components/context-menu/index.ts +2 -0
  218. package/src/components/drawing/DrawingModule.tsx +36 -0
  219. package/src/components/drawing/DrawingPropertyPanel.tsx +347 -0
  220. package/src/components/drawing/DrawingToolbar.tsx +305 -0
  221. package/src/components/drawing/index.ts +5 -0
  222. package/src/components/index.ts +43 -0
  223. package/src/components/indicator/IndicatorListPanel.tsx +94 -0
  224. package/src/components/indicator/IndicatorModal.tsx +171 -0
  225. package/src/components/indicator/IndicatorPanel.tsx +9 -0
  226. package/src/components/indicator/IndicatorPropertyPanel.tsx +130 -0
  227. package/src/components/indicator/IndicatorTag.tsx +173 -0
  228. package/src/components/indicator/index.ts +4 -0
  229. package/src/components/replay/ReplayController.css +97 -0
  230. package/src/components/replay/ReplayController.tsx +138 -0
  231. package/src/components/timebar/TimeBarModule.tsx +30 -0
  232. package/src/components/timebar/TimeRangeSelector.tsx +96 -0
  233. package/src/components/timebar/index.ts +3 -0
  234. package/src/components/toolbar/GlobalToolbar.tsx +58 -0
  235. package/src/components/toolbar/KlineTypeSelector.tsx +123 -0
  236. package/src/components/toolbar/LayoutSwitcher.tsx +45 -0
  237. package/src/components/toolbar/PeriodSelector.tsx +35 -0
  238. package/src/components/toolbar/QLToolbar.tsx +71 -0
  239. package/src/components/toolbar/TimeRangeSelector.tsx +89 -0
  240. package/src/components/ui/Modal.tsx +67 -0
  241. package/src/core/ChartManager.ts +95 -0
  242. package/src/core/DataManager.ts +427 -0
  243. package/src/core/EventManager.ts +63 -0
  244. package/src/core/IndicatorDataProcessor.ts +104 -0
  245. package/src/core/PaneManager.ts +121 -0
  246. package/src/core/RealtimeDataFeed.ts +110 -0
  247. package/src/core/SeriesManager.ts +210 -0
  248. package/src/core/ThemeManager.ts +59 -0
  249. package/src/core/index.ts +10 -0
  250. package/src/css.d.ts +4 -0
  251. package/src/hooks/useChart.ts +62 -0
  252. package/src/hooks/useCrosshairSync.ts +109 -0
  253. package/src/hooks/useDrawingModule.ts +475 -0
  254. package/src/hooks/useTheme.ts +31 -0
  255. package/src/index.ts +170 -0
  256. package/src/mock/MockDataService.ts +102 -0
  257. package/src/mock/MockIndicatorService.ts +40 -0
  258. package/src/mock/index.ts +5 -0
  259. package/src/mock/presets.ts +16 -0
  260. package/src/plugins/drawing/DrawingAdapter.ts +1762 -0
  261. package/src/plugins/drawing/DrawingPersistence.ts +273 -0
  262. package/src/plugins/drawing/DrawingPropertyTemplates.ts +327 -0
  263. package/src/plugins/drawing/DrawingSharedService.ts +125 -0
  264. package/src/plugins/drawing/DrawingToolRegistry.ts +684 -0
  265. package/src/plugins/drawing/TextLabelOverlay.ts +101 -0
  266. package/src/plugins/drawing/colorUtils.ts +53 -0
  267. package/src/plugins/drawing/index.ts +10 -0
  268. package/src/plugins/drawing/lineStyleMap.ts +46 -0
  269. package/src/plugins/drawing/migration.ts +105 -0
  270. package/src/plugins/drawing/patterns/PatternDefinitions.ts +57 -0
  271. package/src/plugins/drawing/patterns/index.ts +2 -0
  272. package/src/plugins/drawing/periodUtils.ts +51 -0
  273. package/src/plugins/indicators/AutoIndicatorRenderer.ts +204 -0
  274. package/src/plugins/indicators/IndicatorRenderer.ts +350 -0
  275. package/src/plugins/indicators/OverlayIndicator.ts +114 -0
  276. package/src/plugins/indicators/PaneIndicator.ts +137 -0
  277. package/src/plugins/indicators/index.ts +4 -0
  278. package/src/plugins/replay/KlineReplay.ts +163 -0
  279. package/src/plugins/replay/index.ts +2 -0
  280. package/src/plugins/screenshot/ScreenshotUtil.ts +123 -0
  281. package/src/plugins/screenshot/index.ts +1 -0
  282. package/src/plugins/series/HollowCandlestickSeries.ts +111 -0
  283. package/src/plugins/series/RenkoSeries.ts +104 -0
  284. package/src/plugins/series/VolumeCandlestickSeries.ts +127 -0
  285. package/src/plugins/series/index.ts +6 -0
  286. package/src/standalone.ts +386 -0
  287. package/src/store/useChartStore.ts +101 -0
  288. package/src/store/useDrawingStore.ts +135 -0
  289. package/src/store/useIndicatorStore.ts +100 -0
  290. package/src/store/usePanelRegistry.ts +50 -0
  291. package/src/store/useReplayStore.ts +42 -0
  292. package/src/store/useTimeBarStore.ts +34 -0
  293. package/src/styles/chart.css +312 -0
  294. package/src/styles/components.css +184 -0
  295. package/src/styles/context-menu.css +60 -0
  296. package/src/styles/drawing.css +524 -0
  297. package/src/styles/indicator-modal.css +216 -0
  298. package/src/styles/indicator-tag.css +210 -0
  299. package/src/styles/pane-chart.css +9 -0
  300. package/src/styles/responsive.css +71 -0
  301. package/src/styles/themes/dark.css +63 -0
  302. package/src/styles/themes/light.css +61 -0
  303. package/src/styles/toolbar.css +129 -0
  304. package/src/types/api.ts +36 -0
  305. package/src/types/chart.ts +44 -0
  306. package/src/types/drawing.ts +265 -0
  307. package/src/types/index.ts +40 -0
  308. package/src/types/indicator.ts +344 -0
  309. package/src/types/series.ts +53 -0
  310. package/src/types/theme.ts +48 -0
  311. package/src/utils/dataTransformer.ts +63 -0
  312. package/src/utils/heikinAshi.ts +41 -0
  313. package/src/utils/index.ts +3 -0
  314. package/src/utils/lineBreak.ts +88 -0
  315. package/src/utils/themePresets.ts +69 -0
  316. package/src/utils/timeFormatter.ts +29 -0
  317. package/src/utils/timeScaleUtils.ts +68 -0
  318. package/tsconfig.json +21 -0
  319. package/typedoc.json +10 -0
  320. package/vite.config.standalone.ts +31 -0
  321. package/vite.config.ts +24 -0
@@ -0,0 +1,273 @@
1
+ import type { DrawingPersistData } from '../../types/drawing.js';
2
+
3
+ /** 存储Key前缀 */
4
+ const STORAGE_PREFIX = 'qlchart_dw_';
5
+ /** 存储数据版本(兼容未来格式变更) */
6
+ const STORAGE_VERSION = 1;
7
+ /** 自动保存debounce时间(ms) */
8
+ const AUTO_SAVE_DELAY = 300;
9
+ /** localStorage容量警告阈值(4MB) */
10
+ const STORAGE_WARN_SIZE = 4 * 1024 * 1024;
11
+
12
+ /** 存储数据包装 */
13
+ interface StorageEnvelope {
14
+ version: number;
15
+ drawings: DrawingPersistData[];
16
+ updatedAt: number;
17
+ }
18
+
19
+ /** 持久化配置 */
20
+ export interface PersistenceConfig {
21
+ /** 交易对ID(核心标识) */
22
+ pairId: string;
23
+ /** 产品类型 */
24
+ product: string;
25
+ /** 周期(保留用于UI层,不参与持久化key) */
26
+ period?: string;
27
+ /** 图表实例ID(保留用于内部标识,不参与持久化key) */
28
+ chartId?: string;
29
+ /** API基础URL,有值则使用远程模式 */
30
+ apiBaseUrl?: string;
31
+ }
32
+
33
+ /** 持久化管理器 */
34
+ export class DrawingPersistence {
35
+ private storageKey: string;
36
+ private pairId: string;
37
+ private product: string;
38
+ private mode: 'local' | 'remote';
39
+ private apiBaseUrl?: string;
40
+ private debounceTimer: ReturnType<typeof setTimeout> | null = null;
41
+
42
+ constructor(config: PersistenceConfig) {
43
+ this.pairId = config.pairId;
44
+ this.product = config.product;
45
+ this.mode = config.apiBaseUrl ? 'remote' : 'local';
46
+ this.apiBaseUrl = config.apiBaseUrl;
47
+ this.storageKey = this.buildKey(config);
48
+ }
49
+
50
+ /** 保存画图数据 */
51
+ async save(drawings: DrawingPersistData[]): Promise<void> {
52
+ if (this.mode === 'local') {
53
+ await this.localSave(drawings);
54
+ } else {
55
+ await this.remoteSave(drawings);
56
+ }
57
+ }
58
+
59
+ /** 加载画图数据 */
60
+ async load(): Promise<DrawingPersistData[]> {
61
+ if (this.mode === 'local') {
62
+ return this.localLoad();
63
+ } else {
64
+ return this.remoteLoad();
65
+ }
66
+ }
67
+
68
+ /** 删除指定画图 */
69
+ async remove(id: string): Promise<void> {
70
+ if (this.mode === 'local') {
71
+ await this.localRemove(id);
72
+ } else {
73
+ await this.remoteRemove(id);
74
+ }
75
+ }
76
+
77
+ /** 更新单条绘图(增量更新) */
78
+ async update(drawing: DrawingPersistData): Promise<void> {
79
+ if (this.mode === 'local') {
80
+ // local模式下通过load→修改→save实现
81
+ const drawings = await this.localLoad();
82
+ const idx = drawings.findIndex(d => d.id === drawing.id);
83
+ if (idx >= 0) {
84
+ drawings[idx] = drawing;
85
+ await this.localSave(drawings);
86
+ }
87
+ } else {
88
+ await this.remoteUpdate(drawing);
89
+ }
90
+ }
91
+
92
+ /** 清空所有画图 */
93
+ async clear(): Promise<void> {
94
+ if (this.mode === 'local') {
95
+ await this.localClear();
96
+ } else {
97
+ await this.remoteClear();
98
+ }
99
+ }
100
+
101
+ /** 导出为JSON字符串(备份用) */
102
+ exportJSON(drawings: DrawingPersistData[]): string {
103
+ const envelope: StorageEnvelope = {
104
+ version: STORAGE_VERSION,
105
+ drawings,
106
+ updatedAt: Date.now(),
107
+ };
108
+ return JSON.stringify(envelope, null, 2);
109
+ }
110
+
111
+ /** 从JSON字符串导入 */
112
+ async importJSON(json: string): Promise<void> {
113
+ try {
114
+ const envelope: StorageEnvelope = JSON.parse(json);
115
+ if (envelope.version !== STORAGE_VERSION) {
116
+ throw new Error(`Unsupported storage version: ${envelope.version}`);
117
+ }
118
+ await this.save(envelope.drawings ?? []);
119
+ } catch (err) {
120
+ console.error('[QLChart] Failed to import drawings:', err);
121
+ throw err;
122
+ }
123
+ }
124
+
125
+ /** 自动保存(debounce) */
126
+ autoSave(drawings: DrawingPersistData[]): void {
127
+ if (this.debounceTimer) clearTimeout(this.debounceTimer);
128
+ this.debounceTimer = setTimeout(() => {
129
+ this.save(drawings);
130
+ }, AUTO_SAVE_DELAY);
131
+ }
132
+
133
+ /** 销毁 */
134
+ dispose(): void {
135
+ if (this.debounceTimer) {
136
+ clearTimeout(this.debounceTimer);
137
+ this.debounceTimer = null;
138
+ }
139
+ }
140
+
141
+ // ── Private ──
142
+
143
+ /** 构建存储Key(仅使用 pairId + product,移除 chartId 和 period) */
144
+ private buildKey(config: PersistenceConfig): string {
145
+ const raw = `${config.pairId}_${config.product}`;
146
+ const hash = this.simpleHash(raw);
147
+ return `${STORAGE_PREFIX}${hash}`;
148
+ }
149
+
150
+ /** 简单hash函数 */
151
+ private simpleHash(str: string): string {
152
+ let hash = 0;
153
+ for (let i = 0; i < str.length; i++) {
154
+ const char = str.charCodeAt(i);
155
+ hash = ((hash << 5) - hash) + char;
156
+ hash |= 0;
157
+ }
158
+ return Math.abs(hash).toString(36);
159
+ }
160
+
161
+ // ── Local Storage ──
162
+
163
+ private async localSave(drawings: DrawingPersistData[]): Promise<void> {
164
+ const envelope: StorageEnvelope = {
165
+ version: STORAGE_VERSION,
166
+ drawings,
167
+ updatedAt: Date.now(),
168
+ };
169
+ const json = JSON.stringify(envelope);
170
+ localStorage.setItem(this.storageKey, json);
171
+ this.checkStorageSize();
172
+ }
173
+
174
+ private async localLoad(): Promise<DrawingPersistData[]> {
175
+ const json = localStorage.getItem(this.storageKey);
176
+ if (!json) return [];
177
+ try {
178
+ const envelope: StorageEnvelope = JSON.parse(json);
179
+ if (envelope.version !== STORAGE_VERSION) {
180
+ return [];
181
+ }
182
+ return envelope.drawings ?? [];
183
+ } catch {
184
+ return [];
185
+ }
186
+ }
187
+
188
+ private async localRemove(id: string): Promise<void> {
189
+ const drawings = await this.localLoad();
190
+ const filtered = drawings.filter(d => d.id !== id);
191
+ await this.localSave(filtered);
192
+ }
193
+
194
+ private async localClear(): Promise<void> {
195
+ localStorage.removeItem(this.storageKey);
196
+ }
197
+
198
+ /** 检查localStorage容量 */
199
+ private checkStorageSize(): void {
200
+ let totalSize = 0;
201
+ for (let i = 0; i < localStorage.length; i++) {
202
+ const key = localStorage.key(i);
203
+ if (key?.startsWith(STORAGE_PREFIX)) {
204
+ totalSize += (localStorage.getItem(key) ?? '').length;
205
+ }
206
+ }
207
+ if (totalSize > STORAGE_WARN_SIZE) {
208
+ console.warn(
209
+ `[QLChart] Drawing storage size: ${(totalSize / 1024 / 1024).toFixed(1)}MB, ` +
210
+ `approaching localStorage limit. Consider exporting old drawings.`
211
+ );
212
+ }
213
+ }
214
+
215
+ // ── Remote Storage (RESTful API) ──
216
+
217
+ /** 批量保存(全量覆盖) */
218
+ private async remoteSave(drawings: DrawingPersistData[]): Promise<void> {
219
+ await fetch(`${this.apiBaseUrl}/api/chart/drawing/save`, {
220
+ method: 'POST',
221
+ headers: { 'Content-Type': 'application/json' },
222
+ body: JSON.stringify({
223
+ pair_id: this.pairId,
224
+ product: this.product,
225
+ drawings: drawings,
226
+ }),
227
+ });
228
+ }
229
+
230
+ /** 按交易对加载全部绘图 */
231
+ private async remoteLoad(): Promise<DrawingPersistData[]> {
232
+ const res = await fetch(
233
+ `${this.apiBaseUrl}/api/chart/drawing/list?pair_id=${this.pairId}&product=${this.product}`,
234
+ { method: 'GET' },
235
+ );
236
+ const json = await res.json();
237
+ return json.data ?? [];
238
+ }
239
+
240
+ /** 删除单条绘图 */
241
+ private async remoteRemove(id: string): Promise<void> {
242
+ await fetch(`${this.apiBaseUrl}/api/chart/drawing/delete`, {
243
+ method: 'DELETE',
244
+ headers: { 'Content-Type': 'application/json' },
245
+ body: JSON.stringify({ id }),
246
+ });
247
+ }
248
+
249
+ /** 更新单条绘图(增量更新) */
250
+ private async remoteUpdate(drawing: DrawingPersistData): Promise<void> {
251
+ await fetch(`${this.apiBaseUrl}/api/chart/drawing/update`, {
252
+ method: 'PUT',
253
+ headers: { 'Content-Type': 'application/json' },
254
+ body: JSON.stringify(drawing),
255
+ });
256
+ }
257
+
258
+ /** 清空指定交易对的所有绘图 */
259
+ private async remoteClear(): Promise<void> {
260
+ await fetch(`${this.apiBaseUrl}/api/chart/drawing/clear`, {
261
+ method: 'DELETE',
262
+ headers: { 'Content-Type': 'application/json' },
263
+ body: JSON.stringify({
264
+ pair_id: this.pairId,
265
+ product: this.product,
266
+ }),
267
+ });
268
+ }
269
+ }
270
+
271
+ // 导出常量供 migration.ts 使用
272
+ export { STORAGE_PREFIX, STORAGE_VERSION };
273
+ export type { StorageEnvelope };
@@ -0,0 +1,327 @@
1
+ import { COMMON_FIELDS, type DrawingToolProperties, type DrawingToolType } from '../../types/drawing.js';
2
+
3
+ /**
4
+ * 按工具类型定义属性模板
5
+ * 每种绘图工具有不同的可修改属性
6
+ */
7
+ export const DRAWING_PROPERTY_TEMPLATES: Record<string, DrawingToolProperties> = {
8
+ // ── 线条类 ──
9
+ 'trend-line': {
10
+ toolType: 'trend-line' as DrawingToolType,
11
+ fields: [
12
+ COMMON_FIELDS.lineColor(),
13
+ COMMON_FIELDS.lineWidth(),
14
+ COMMON_FIELDS.lineStyle(),
15
+ COMMON_FIELDS.extendLeft(),
16
+ COMMON_FIELDS.extendRight(),
17
+ COMMON_FIELDS.showLabels(),
18
+ COMMON_FIELDS.text(),
19
+ ],
20
+ },
21
+ 'horizontal-line': {
22
+ toolType: 'horizontal-line' as DrawingToolType,
23
+ fields: [
24
+ COMMON_FIELDS.lineColor(),
25
+ COMMON_FIELDS.lineWidth(),
26
+ COMMON_FIELDS.lineStyle(),
27
+ COMMON_FIELDS.showLabels(),
28
+ ],
29
+ },
30
+ 'vertical-line': {
31
+ toolType: 'vertical-line' as DrawingToolType,
32
+ fields: [
33
+ COMMON_FIELDS.lineColor(),
34
+ COMMON_FIELDS.lineWidth(),
35
+ COMMON_FIELDS.lineStyle(),
36
+ ],
37
+ },
38
+ 'ray': {
39
+ toolType: 'ray' as DrawingToolType,
40
+ fields: [
41
+ COMMON_FIELDS.lineColor(),
42
+ COMMON_FIELDS.lineWidth(),
43
+ COMMON_FIELDS.lineStyle(),
44
+ COMMON_FIELDS.showLabels(),
45
+ ],
46
+ },
47
+ 'cross-line': {
48
+ toolType: 'cross-line' as DrawingToolType,
49
+ fields: [
50
+ COMMON_FIELDS.lineColor(),
51
+ COMMON_FIELDS.lineWidth(),
52
+ ],
53
+ },
54
+
55
+ // ── 形状类(有填充) ──
56
+ 'rectangle': {
57
+ toolType: 'rectangle' as DrawingToolType,
58
+ fields: [
59
+ COMMON_FIELDS.lineColor(),
60
+ COMMON_FIELDS.lineWidth(),
61
+ COMMON_FIELDS.fillColor(),
62
+ COMMON_FIELDS.fillOpacity(),
63
+ COMMON_FIELDS.showLabels(),
64
+ ],
65
+ },
66
+ 'circle': {
67
+ toolType: 'circle' as DrawingToolType,
68
+ fields: [
69
+ COMMON_FIELDS.lineColor(),
70
+ COMMON_FIELDS.lineWidth(),
71
+ COMMON_FIELDS.fillColor(),
72
+ COMMON_FIELDS.fillOpacity(),
73
+ ],
74
+ },
75
+ 'ellipse': {
76
+ toolType: 'ellipse' as DrawingToolType,
77
+ fields: [
78
+ COMMON_FIELDS.lineColor(),
79
+ COMMON_FIELDS.lineWidth(),
80
+ COMMON_FIELDS.fillColor(),
81
+ COMMON_FIELDS.fillOpacity(),
82
+ ],
83
+ },
84
+ 'triangle': {
85
+ toolType: 'triangle' as DrawingToolType,
86
+ fields: [
87
+ COMMON_FIELDS.lineColor(),
88
+ COMMON_FIELDS.lineWidth(),
89
+ COMMON_FIELDS.fillColor(),
90
+ COMMON_FIELDS.fillOpacity(),
91
+ ],
92
+ },
93
+
94
+ // ── 通道类 ──
95
+ 'parallel-channel': {
96
+ toolType: 'parallel-channel' as DrawingToolType,
97
+ fields: [
98
+ COMMON_FIELDS.lineColor(),
99
+ COMMON_FIELDS.lineWidth(),
100
+ COMMON_FIELDS.fillColor(),
101
+ COMMON_FIELDS.fillOpacity(),
102
+ COMMON_FIELDS.showLabels(),
103
+ ],
104
+ },
105
+
106
+ // ── 斐波那契类 ──
107
+ 'fib-retracement': {
108
+ toolType: 'fib-retracement' as DrawingToolType,
109
+ fields: [
110
+ COMMON_FIELDS.lineColor(),
111
+ COMMON_FIELDS.lineWidth(),
112
+ COMMON_FIELDS.fillColor(),
113
+ COMMON_FIELDS.fillOpacity(0.1),
114
+ COMMON_FIELDS.showLabels(),
115
+ COMMON_FIELDS.labelColor(),
116
+ COMMON_FIELDS.fibLevels(), // ★ Req3
117
+ ],
118
+ },
119
+ 'fib-extension': {
120
+ toolType: 'fib-extension' as DrawingToolType,
121
+ fields: [
122
+ COMMON_FIELDS.lineColor(),
123
+ COMMON_FIELDS.lineWidth(),
124
+ COMMON_FIELDS.fillOpacity(0.1),
125
+ COMMON_FIELDS.showLabels(),
126
+ COMMON_FIELDS.labelColor(),
127
+ COMMON_FIELDS.fibLevels(), // ★ Req3
128
+ ],
129
+ },
130
+ 'fib-fan': {
131
+ toolType: 'fib-fan' as DrawingToolType,
132
+ fields: [
133
+ COMMON_FIELDS.lineColor(),
134
+ COMMON_FIELDS.lineWidth(),
135
+ COMMON_FIELDS.showLabels(),
136
+ COMMON_FIELDS.fibLevels([ // ★ Req3,扇形使用速度比率
137
+ { value: 0.382, label: '38.2%', visible: true },
138
+ { value: 0.5, label: '50%', visible: true },
139
+ { value: 0.618, label: '61.8%', visible: true },
140
+ { value: 1, label: '100%', visible: true },
141
+ { value: 1.618, label: '161.8%', visible: true },
142
+ ]),
143
+ ],
144
+ },
145
+
146
+ // ── 标注类 ──
147
+ 'path': {
148
+ toolType: 'path' as DrawingToolType,
149
+ fields: [
150
+ COMMON_FIELDS.lineColor(),
151
+ COMMON_FIELDS.lineWidth(),
152
+ COMMON_FIELDS.lineStyle(),
153
+ ],
154
+ },
155
+ 'brush': {
156
+ toolType: 'brush' as DrawingToolType,
157
+ fields: [
158
+ COMMON_FIELDS.lineColor(),
159
+ COMMON_FIELDS.lineWidth(),
160
+ ],
161
+ },
162
+ 'arrow': {
163
+ toolType: 'arrow' as DrawingToolType,
164
+ fields: [
165
+ COMMON_FIELDS.lineColor(),
166
+ COMMON_FIELDS.lineWidth(),
167
+ COMMON_FIELDS.showLabels(),
168
+ ],
169
+ },
170
+ 'callout': {
171
+ toolType: 'callout' as DrawingToolType,
172
+ fields: [
173
+ COMMON_FIELDS.lineColor(),
174
+ COMMON_FIELDS.lineWidth(),
175
+ COMMON_FIELDS.fillColor(),
176
+ COMMON_FIELDS.fillOpacity(),
177
+ COMMON_FIELDS.text(),
178
+ COMMON_FIELDS.labelColor(),
179
+ ],
180
+ },
181
+
182
+ // ── 仓位类 ──
183
+ 'long-position': {
184
+ toolType: 'long-position' as DrawingToolType,
185
+ fields: [
186
+ COMMON_FIELDS.lineColor('#26a69a'),
187
+ COMMON_FIELDS.lineWidth(),
188
+ COMMON_FIELDS.fillColor('#26a69a'),
189
+ COMMON_FIELDS.fillOpacity(0.15),
190
+ COMMON_FIELDS.showLabels(),
191
+ ],
192
+ },
193
+ 'short-position': {
194
+ toolType: 'short-position' as DrawingToolType,
195
+ fields: [
196
+ COMMON_FIELDS.lineColor('#ef5350'),
197
+ COMMON_FIELDS.lineWidth(),
198
+ COMMON_FIELDS.fillColor('#ef5350'),
199
+ COMMON_FIELDS.fillOpacity(0.15),
200
+ COMMON_FIELDS.showLabels(),
201
+ ],
202
+ },
203
+ // ── 新增工具属性模板 ──
204
+ 'date-price-range': {
205
+ toolType: 'date-price-range' as DrawingToolType,
206
+ fields: [
207
+ COMMON_FIELDS.lineColor(),
208
+ COMMON_FIELDS.lineWidth(),
209
+ COMMON_FIELDS.fillColor(),
210
+ COMMON_FIELDS.fillOpacity(0.1),
211
+ COMMON_FIELDS.showLabels(),
212
+ COMMON_FIELDS.labelColor(),
213
+ ],
214
+ },
215
+ 'fib-circles': {
216
+ toolType: 'fib-circles' as DrawingToolType,
217
+ fields: [
218
+ COMMON_FIELDS.lineColor(),
219
+ COMMON_FIELDS.lineWidth(),
220
+ COMMON_FIELDS.fillOpacity(0.1),
221
+ COMMON_FIELDS.showLabels(),
222
+ COMMON_FIELDS.labelColor(),
223
+ COMMON_FIELDS.fibLevels(), // ★ Req3:替换原有的 fibRatios()
224
+ ],
225
+ },
226
+ 'fib-time-zone': {
227
+ toolType: 'fib-time-zone' as DrawingToolType,
228
+ fields: [
229
+ COMMON_FIELDS.lineColor(),
230
+ COMMON_FIELDS.lineWidth(),
231
+ COMMON_FIELDS.fillOpacity(0.1),
232
+ COMMON_FIELDS.showLabels(),
233
+ COMMON_FIELDS.labelColor(),
234
+ COMMON_FIELDS.fibLevels([ // ★ Req3,时区使用斐波那契数列
235
+ { value: 1, label: '1', visible: true },
236
+ { value: 2, label: '2', visible: true },
237
+ { value: 3, label: '3', visible: true },
238
+ { value: 5, label: '5', visible: true },
239
+ { value: 8, label: '8', visible: true },
240
+ { value: 13, label: '13', visible: true },
241
+ { value: 21, label: '21', visible: true },
242
+ { value: 34, label: '34', visible: false },
243
+ { value: 55, label: '55', visible: false },
244
+ { value: 89, label: '89', visible: false },
245
+ ]),
246
+ ],
247
+ },
248
+ 'rotated-rectangle': {
249
+ toolType: 'rotated-rectangle' as DrawingToolType,
250
+ fields: [
251
+ COMMON_FIELDS.lineColor(),
252
+ COMMON_FIELDS.lineWidth(),
253
+ COMMON_FIELDS.fillColor(),
254
+ COMMON_FIELDS.fillOpacity(),
255
+ ],
256
+ },
257
+ // ── 形态工具属性模板 ──
258
+ 'pattern-abcd': {
259
+ toolType: 'pattern-abcd' as DrawingToolType,
260
+ fields: [
261
+ COMMON_FIELDS.lineColor(),
262
+ COMMON_FIELDS.lineWidth(),
263
+ COMMON_FIELDS.showLabels(),
264
+ COMMON_FIELDS.labelColor(),
265
+ ],
266
+ },
267
+ 'pattern-xabcd': {
268
+ toolType: 'pattern-xabcd' as DrawingToolType,
269
+ fields: [
270
+ COMMON_FIELDS.lineColor(),
271
+ COMMON_FIELDS.lineWidth(),
272
+ COMMON_FIELDS.fillOpacity(0.1),
273
+ COMMON_FIELDS.showLabels(),
274
+ COMMON_FIELDS.labelColor(),
275
+ ],
276
+ },
277
+ 'pattern-head-shoulders': {
278
+ toolType: 'pattern-head-shoulders' as DrawingToolType,
279
+ fields: [
280
+ COMMON_FIELDS.lineColor(),
281
+ COMMON_FIELDS.lineWidth(),
282
+ COMMON_FIELDS.fillColor(),
283
+ COMMON_FIELDS.fillOpacity(0.1),
284
+ COMMON_FIELDS.showLabels(),
285
+ ],
286
+ },
287
+ 'pattern-triangle': {
288
+ toolType: 'pattern-triangle' as DrawingToolType,
289
+ fields: [
290
+ COMMON_FIELDS.lineColor(),
291
+ COMMON_FIELDS.lineWidth(),
292
+ COMMON_FIELDS.fillColor(),
293
+ COMMON_FIELDS.fillOpacity(),
294
+ COMMON_FIELDS.showLabels(),
295
+ ],
296
+ },
297
+ 'pattern-cypher': {
298
+ toolType: 'pattern-cypher' as DrawingToolType,
299
+ fields: [
300
+ COMMON_FIELDS.lineColor(),
301
+ COMMON_FIELDS.lineWidth(),
302
+ COMMON_FIELDS.fillOpacity(0.1),
303
+ COMMON_FIELDS.showLabels(),
304
+ COMMON_FIELDS.labelColor(),
305
+ ],
306
+ },
307
+ };
308
+
309
+ /**
310
+ * 获取工具的属性模板
311
+ * 如果没有预定义模板,返回通用模板
312
+ */
313
+ export function getPropertyTemplate(toolType: string): DrawingToolProperties {
314
+ const template = DRAWING_PROPERTY_TEMPLATES[toolType];
315
+ if (template) return template;
316
+
317
+ // 通用fallback
318
+ return {
319
+ toolType: toolType as DrawingToolType,
320
+ fields: [
321
+ COMMON_FIELDS.lineColor(),
322
+ COMMON_FIELDS.lineWidth(),
323
+ COMMON_FIELDS.fillColor(),
324
+ COMMON_FIELDS.fillOpacity(),
325
+ ],
326
+ };
327
+ }