@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,119 @@
1
+ # 数据格式指南
2
+
3
+ 本文档详细说明 QLChart 中使用的各种数据格式及转换规则。
4
+
5
+ ## 数据格式总览
6
+
7
+ | 格式 | 使用场景 | time单位 | 定义位置 |
8
+ |------|---------|---------|---------|
9
+ | `CandlestickRawData` | API返回 / setData输入 | 毫秒 | `types/series.ts` |
10
+ | LWC CandlestickData | 内部图表渲染 | 秒 | lightweight-charts |
11
+ | `RealtimeCandle` | 实时增量推送 | 秒 | `core/RealtimeDataFeed.ts` |
12
+
13
+ ## CandlestickRawData(API格式)
14
+
15
+ API返回和 `setData()` 接受的数据格式,**time 为毫秒级时间戳**:
16
+
17
+ ```typescript
18
+ interface CandlestickRawData {
19
+ /** 毫秒级时间戳(UNIX milliseconds) */
20
+ time: number;
21
+ open: number;
22
+ high: number;
23
+ low: number;
24
+ close: number;
25
+ volume?: number;
26
+ }
27
+ ```
28
+
29
+ **示例:**
30
+
31
+ ```json
32
+ [
33
+ { "time": 1700000000000, "open": 42000, "high": 42500, "low": 41800, "close": 42300, "volume": 1200 },
34
+ { "time": 1700003600000, "open": 42300, "high": 42800, "low": 42100, "close": 42600, "volume": 1500 }
35
+ ]
36
+ ```
37
+
38
+ ### 数据要求
39
+
40
+ 1. **升序排列:** 必须按 time 升序排列
41
+ 2. **时间对齐:** time 应与周期对齐(如1h周期,time应为整点时间戳)
42
+ 3. **OHLC完整性:** open/high/low/close 不可缺失
43
+
44
+ ## LWC 内部格式
45
+
46
+ `transformCandlestickData()` 将 CandlestickRawData 转换为 LWC 内部格式:
47
+
48
+ ```typescript
49
+ // 转换前(毫秒)
50
+ { time: 1700000000000, open: 42000, high: 42500, ... }
51
+
52
+ // 转换后(秒)
53
+ { time: 1700000000, open: 42000, high: 42500, ... } // UTCTimestamp
54
+ ```
55
+
56
+ 转换规则:
57
+ - `time / 1000` → 秒级 UTCTimestamp
58
+ - 数据类型保持不变
59
+
60
+ ## RealtimeCandle(增量格式)
61
+
62
+ 实时数据推送使用秒级时间戳:
63
+
64
+ ```typescript
65
+ interface RealtimeCandle {
66
+ time: number; // 秒级
67
+ open: number;
68
+ high: number;
69
+ low: number;
70
+ close: number;
71
+ volume?: number;
72
+ }
73
+ ```
74
+
75
+ ## 时间戳转换速查
76
+
77
+ ```
78
+ API返回时间戳: 1700000000000 (毫秒)
79
+ ↓ ÷ 1000
80
+ LWC内部时间戳: 1700000000 (秒)
81
+
82
+ RealtimeCandle: 1700000000 (秒)
83
+ ```
84
+
85
+ ## 数据预处理
86
+
87
+ QLChart 支持多种K线类型预处理:
88
+
89
+ | 类型 | 函数 | 说明 |
90
+ |------|------|------|
91
+ | 标准K线 | `transformCandlestickData` | 直接转换 |
92
+ | 成交量K线 | `transformVolumeCandlestickData` | 按成交量加权 |
93
+ | HeikinAshi | `transformToHeikinAshi` | 平均K线 |
94
+ | LineBreak | `transformToLineBreak` | 三线突破 |
95
+
96
+ ## Whitespace 数据
97
+
98
+ `generateFutureWhitespace()` 在K线末尾注入未来时间点,为实时数据预留位置:
99
+
100
+ ```typescript
101
+ const lastTime = 1700000000; // 秒
102
+ const whitespace = generateFutureWhitespace(lastTime, '1h', 100);
103
+ // 生成 1700003600, 1700007200, ... 共100个空白点
104
+ ```
105
+
106
+ 这确保图表在接收实时数据时不会因为时间超出范围而报错。
107
+
108
+ ## 成交量数据
109
+
110
+ `transformVolumeData()` 提取成交量并格式化为 LWC Histogram 数据:
111
+
112
+ ```typescript
113
+ // 输入
114
+ { time: 1700000000000, volume: 1200, close: 42300, open: 42000, ... }
115
+
116
+ // 输出
117
+ { time: 1700000000, value: 1200, color: 'rgba(38, 166, 154, 0.35)' }
118
+ // 绿色(收涨)或 红色(收跌)
119
+ ```
@@ -0,0 +1,30 @@
1
+ # 绘图工具示例
2
+
3
+ ```tsx
4
+ import { QLChartProvider, QLChartPanel } from '@quantlife/qlchart'
5
+
6
+ function App() {
7
+ return (
8
+ <QLChartProvider config={{ fetchFn, defaultTheme: 'dark' }}>
9
+ <QLChartPanel
10
+ pairId="BTCUSDT"
11
+ product="digital_currency"
12
+ showToolbar
13
+ />
14
+ </QLChartProvider>
15
+ )
16
+ }
17
+ ```
18
+
19
+ ## 使用步骤
20
+
21
+ 1. 点击图表右上角的 🔧 绘图按钮展开工具栏
22
+ 2. 选择绘图工具(趋势线、水平线、矩形等)
23
+ 3. 在图表上点击放置锚点
24
+ 4. 按 Esc 取消创建
25
+ 5. 右键菜单可快速切换最近工具
26
+ 6. 右下角属性面板可修改选中绘图的颜色/线宽/样式
27
+
28
+ ## 磁铁吸附
29
+
30
+ 绘图工具支持磁铁吸附功能,开启后锚点会自动吸附到最近的K线收盘价。
@@ -0,0 +1,34 @@
1
+ # 指标属性编辑示例
2
+
3
+ ```tsx
4
+ import { IndicatorPropertyPanel } from '@quantlife/qlchart'
5
+
6
+ function IndicatorSettings({ indicatorKey, currentStyle }) {
7
+ const [style, setStyle] = useState(currentStyle)
8
+
9
+ return (
10
+ <IndicatorPropertyPanel
11
+ indicatorKey={indicatorKey}
12
+ values={style}
13
+ onStyleChange={(key, value) => {
14
+ setStyle(prev => ({ ...prev, [key]: value }))
15
+ }}
16
+ onClose={() => setOpen(false)}
17
+ />
18
+ )
19
+ }
20
+ ```
21
+
22
+ ## MACD属性
23
+
24
+ MACD指标支持修改以下属性:
25
+ - DIF颜色
26
+ - DEA颜色
27
+ - 柱状涨色
28
+ - 柱状跌色
29
+
30
+ ## Volume属性
31
+
32
+ 成交量支持修改:
33
+ - 涨色(默认 rgba(38,166,154,0.35))
34
+ - 跌色(默认 rgba(239,83,80,0.35))
@@ -0,0 +1,24 @@
1
+ # 多副图示例
2
+
3
+ ```tsx
4
+ import { QLChartProvider, QLChartPanel } from '@quantlife/qlchart'
5
+
6
+ function App() {
7
+ return (
8
+ <QLChartProvider config={{ fetchFn, defaultTheme: 'dark' }}>
9
+ <QLChartPanel
10
+ pairId="BTCUSDT"
11
+ product="digital_currency"
12
+ showToolbar
13
+ />
14
+ </QLChartProvider>
15
+ )
16
+ }
17
+ ```
18
+
19
+ 添加MACD和RSI指标后,QLChartPanel会自动通过LWC v5原生pane系统创建副图:
20
+ - pane 0: 主图(K线 + 成交量)
21
+ - pane 1: MACD(DIF/DEA/HIST)
22
+ - pane 2: RSI
23
+
24
+ 所有pane共享同一时间轴,光标自动同步。
@@ -0,0 +1,23 @@
1
+ # 多面板布局示例
2
+
3
+ ```tsx
4
+ import { QLChartProvider, QLChartPanel } from '@quantlife/qlchart'
5
+
6
+ function App() {
7
+ return (
8
+ <QLChartProvider config={{ fetchFn, defaultTheme: 'dark' }}>
9
+ <QLChartLayout mode="2">
10
+ <QLChartPanel pairId="BTCUSDT" product="digital_currency" panelId="p1" panelIndex={0} />
11
+ <QLChartPanel pairId="ETHUSDT" product="digital_currency" panelId="p2" panelIndex={1} />
12
+ </QLChartLayout>
13
+ </QLChartProvider>
14
+ )
15
+ }
16
+ ```
17
+
18
+ ## 效果
19
+
20
+ - 左右分屏对比两个交易对
21
+ - 光标同步移动
22
+ - 点击面板切换活跃面板
23
+ - 绘图工具仅作用于活跃面板
@@ -0,0 +1,147 @@
1
+ # 实时数据更新
2
+
3
+ QLChart 支持通过 WebSocket 或轮询方式接收实时K线数据,并通过 `series.update()` 实现增量更新。
4
+
5
+ ## 数据格式
6
+
7
+ ### RealtimeCandle
8
+
9
+ 实时数据使用秒级时间戳(与 lightweight-charts 内部格式一致):
10
+
11
+ ```typescript
12
+ interface RealtimeCandle {
13
+ time: number; // 秒级时间戳(UNIX seconds)
14
+ open: number;
15
+ high: number;
16
+ low: number;
17
+ close: number;
18
+ volume?: number;
19
+ }
20
+ ```
21
+
22
+ > **重要:** WebSocket 推送的毫秒级时间戳需要转换为秒级:`Math.floor(timestamp / 1000)`
23
+
24
+ ## React 中使用
25
+
26
+ ### 方式一:通过 ref 直接推送
27
+
28
+ ```tsx
29
+ import { useRef, useEffect } from 'react';
30
+ import {
31
+ QLChartProvider,
32
+ QLChartPanel,
33
+ type QLChartPanelRef,
34
+ type RealtimeCandle,
35
+ } from '@quantlife/qlchart';
36
+
37
+ function App() {
38
+ const chartRef = useRef<QLChartPanelRef>(null);
39
+
40
+ useEffect(() => {
41
+ const ws = new WebSocket('wss://api.example.com/ws/kline');
42
+
43
+ ws.onmessage = (event) => {
44
+ const msg = JSON.parse(event.data);
45
+ const candle: RealtimeCandle = {
46
+ time: Math.floor(msg.timestamp / 1000), // ms → s
47
+ open: msg.open,
48
+ high: msg.high,
49
+ low: msg.low,
50
+ close: msg.close,
51
+ volume: msg.volume,
52
+ };
53
+ chartRef.current?.pushRealtimeData(candle);
54
+ };
55
+
56
+ return () => ws.close();
57
+ }, []);
58
+
59
+ return (
60
+ <QLChartProvider config={{ fetchFn: async (params) => { /* ... */ } }}>
61
+ <QLChartPanel
62
+ ref={chartRef}
63
+ pairId="BTCUSDT"
64
+ product="digital_currency"
65
+ />
66
+ </QLChartProvider>
67
+ );
68
+ }
69
+ ```
70
+
71
+ ### 方式二:通过 config.realtimeSubscribe 订阅
72
+
73
+ ```tsx
74
+ const config: QLChartConfig = {
75
+ fetchFn: async (params) => { /* 全量数据获取 */ },
76
+ cacheEnabled: true,
77
+ cacheTTL: 5 * 60 * 1000,
78
+
79
+ realtimeSubscribe: ({ pairId, product, period, onUpdate }) => {
80
+ const ws = new WebSocket(`wss://api.example.com/ws/${pairId}_${period}`);
81
+
82
+ ws.onmessage = (event) => {
83
+ const msg = JSON.parse(event.data);
84
+ onUpdate({
85
+ time: Math.floor(msg.timestamp / 1000),
86
+ open: msg.open,
87
+ high: msg.high,
88
+ low: msg.low,
89
+ close: msg.close,
90
+ volume: msg.volume,
91
+ });
92
+ };
93
+
94
+ // 返回 unsubscribe 函数(组件卸载或切换period时调用)
95
+ return () => ws.close();
96
+ },
97
+ };
98
+ ```
99
+
100
+ ### 方式三:批量推送
101
+
102
+ ```tsx
103
+ // 一次推送多条数据(如断线重连后补偿)
104
+ chartRef.current?.pushRealtimeBatch([
105
+ { time: 1700000000, open: 42000, high: 42100, low: 41900, close: 42050, volume: 100 },
106
+ { time: 1700003600, open: 42050, high: 42200, low: 42000, close: 42150, volume: 120 },
107
+ { time: 1700007200, open: 42150, high: 42300, low: 42100, close: 42280, volume: 90 },
108
+ ]);
109
+ ```
110
+
111
+ ## 纯 JS 中使用(Standalone)
112
+
113
+ ```javascript
114
+ var chart = QLChart.create('container', { theme: 'dark' });
115
+ chart.setData(initialData);
116
+
117
+ // WebSocket 接收实时数据
118
+ var ws = new WebSocket('wss://api.example.com/ws/btcusdt_1h');
119
+ ws.onmessage = function(event) {
120
+ var msg = JSON.parse(event.data);
121
+ chart.update({
122
+ time: Math.floor(msg.timestamp / 1000),
123
+ open: msg.open,
124
+ high: msg.high,
125
+ low: msg.low,
126
+ close: msg.close,
127
+ volume: msg.volume,
128
+ });
129
+ };
130
+ ```
131
+
132
+ ## 增量更新机制
133
+
134
+ QLChart 使用 lightweight-charts 原生的 `series.update()` 方法处理增量数据:
135
+
136
+ | 场景 | 行为 |
137
+ |------|------|
138
+ | `time === 最新K线time` | 更新该K线的 OHLCV |
139
+ | `time > 最新K线time` | 追加新K线 |
140
+ | `time < 最新K线time` | 忽略(LWC自动处理) |
141
+
142
+ ## 注意事项
143
+
144
+ 1. **推送频率:** 建议不超过每秒10次,过高频率可能导致渲染瓶颈
145
+ 2. **切换周期:** 需要重新订阅对应周期的数据源
146
+ 3. **数据连续性:** 断线重连后使用 `pushRealtimeBatch` 补偿缺失数据
147
+ 4. **时间戳单位:** API返回毫秒级,实时推送使用秒级,注意转换
@@ -0,0 +1,333 @@
1
+ # Standalone JS 使用指南
2
+
3
+ QLChart 提供纯 JavaScript 的 Standalone API,无需 React 即可在任何 HTML 页面中使用。
4
+
5
+ ## 安装
6
+
7
+ ### 方式一:本地构建
8
+
9
+ ```bash
10
+ pnpm build:standalone
11
+ ```
12
+
13
+ 生成的文件在 `dist/qlchart.standalone.js`,CSS 已内联到 JS 中。
14
+
15
+ ### 方式二:直接引入
16
+
17
+ ```html
18
+ <script src="path/to/qlchart.standalone.js"></script>
19
+ ```
20
+
21
+ ## 快速开始
22
+
23
+ ```html
24
+ <!DOCTYPE html>
25
+ <html>
26
+ <head>
27
+ <meta charset="UTF-8">
28
+ <title>QLChart Demo</title>
29
+ </head>
30
+ <body>
31
+ <div id="chart" style="width: 100%; height: 400px;"></div>
32
+ <script src="qlchart.standalone.js"></script>
33
+ <script>
34
+ var chart = QLChart.create('chart', {
35
+ theme: 'dark',
36
+ showVolume: true,
37
+ });
38
+
39
+ var data = QLChart.generateMockData(500, 42000);
40
+ chart.setData(data);
41
+ </script>
42
+ </body>
43
+ </html>
44
+ ```
45
+
46
+ ## API 参考
47
+
48
+ ### `QLChart.create(container, options)`
49
+
50
+ 创建图表实例。
51
+
52
+ **参数:**
53
+
54
+ | 参数 | 类型 | 默认值 | 说明 |
55
+ |------|------|--------|------|
56
+ | `container` | `HTMLElement \| string` | — | 容器元素或元素ID |
57
+ | `options.width` | `number \| string` | `'100%'` | 宽度 |
58
+ | `options.height` | `number \| string` | `400` | 高度 |
59
+ | `options.theme` | `'dark' \| 'light'` | `'dark'` | 主题 |
60
+ | `options.seriesType` | `'candlestick' \| 'line' \| 'area' \| 'bar'` | `'candlestick'` | 序列类型 |
61
+ | `options.showVolume` | `boolean` | `true` | 显示成交量 |
62
+ | `options.enableDrawing` | `boolean` | `false` | 启用绘图工具 |
63
+ | `options.period` | `string` | `'1h'` | 初始周期(如 `'1m'` `'5m'` `'1h'` `'1d'`) |
64
+ | `options.onDataReload` | `(period: string) => void` | — | 周期切换时的数据加载回调 |
65
+ | `options.visibleBars` | `number` | `80` | 初始可见K线数量 |
66
+
67
+ **返回:** `StandaloneChart` 实例
68
+
69
+ ---
70
+
71
+ ### `chart.setData(data)`
72
+
73
+ 设置全量K线数据。`time` 使用毫秒级时间戳。
74
+
75
+ ```javascript
76
+ chart.setData([
77
+ { time: 1700000000000, open: 42000, high: 42500, low: 41800, close: 42300, volume: 1200 },
78
+ { time: 1700003600000, open: 42300, high: 42800, low: 42100, close: 42600, volume: 1500 },
79
+ ]);
80
+ ```
81
+
82
+ ---
83
+
84
+ ### `chart.update(data)`
85
+
86
+ 推送增量数据(实时更新)。`time` 使用秒级时间戳。
87
+
88
+ ```javascript
89
+ chart.update({
90
+ time: 1700003600,
91
+ open: 42300, high: 42800, low: 42100, close: 42600, volume: 1500,
92
+ });
93
+ ```
94
+
95
+ ---
96
+
97
+ ### `chart.updateBatch(data)`
98
+
99
+ 批量推送增量数据。
100
+
101
+ ---
102
+
103
+ ### `chart.setPeriod(period)` / `chart.getPeriod()`
104
+
105
+ 切换/获取当前周期。`setPeriod` 会触发 `onDataReload` 回调。
106
+
107
+ ```javascript
108
+ chart.setPeriod('4h');
109
+ var currentPeriod = chart.getPeriod(); // '4h'
110
+ ```
111
+
112
+ ---
113
+
114
+ ### `chart.setTheme(theme)`
115
+
116
+ 切换主题。会自动应用完整的主题预设(含网格线、十字线等),并同步 `data-theme` 属性到容器元素。
117
+
118
+ ```javascript
119
+ chart.setTheme('light');
120
+ ```
121
+
122
+ ---
123
+
124
+ ### `chart.screenshot()`
125
+
126
+ 截图,返回 data URL。
127
+
128
+ ```javascript
129
+ var url = chart.screenshot();
130
+ var link = document.createElement('a');
131
+ link.download = 'chart.png';
132
+ link.href = url;
133
+ link.click();
134
+ ```
135
+
136
+ ---
137
+
138
+ ### `chart.getChart()` / `chart.getMainSeries()`
139
+
140
+ 获取底层 lightweight-charts 的 `IChartApi` 和 `ISeriesApi` 实例,用于高级自定义。
141
+
142
+ ---
143
+
144
+ ### `chart.drawing.*` — 绘图工具API
145
+
146
+ 当 `enableDrawing: true` 时可用,否则为 `null`。
147
+
148
+ #### `chart.drawing.setDrawingTool(tool)`
149
+
150
+ 设置当前绘图工具。
151
+
152
+ ```javascript
153
+ chart.drawing.setDrawingTool('trend-line');
154
+ chart.drawing.setDrawingTool('cursor'); // 切回光标模式
155
+ ```
156
+
157
+ 可用工具类型参见 `QLChart.drawingTools`。
158
+
159
+ #### `chart.drawing.getDrawingTool()`
160
+
161
+ 获取当前激活的工具。
162
+
163
+ #### `chart.drawing.clearDrawings()`
164
+
165
+ 清空所有绘图。
166
+
167
+ #### `chart.drawing.exportDrawings()` / `chart.drawing.importDrawings(data)`
168
+
169
+ 导出/导入绘图数据(用于持久化)。
170
+
171
+ ```javascript
172
+ // 保存
173
+ var saved = chart.drawing.exportDrawings();
174
+
175
+ // 恢复
176
+ chart.drawing.importDrawings(saved);
177
+ ```
178
+
179
+ #### `chart.drawing.setCandleData(data)`
180
+
181
+ 手动设置K线数据给绘图模块(用于磁铁吸附功能)。通常 `setData()` 会自动调用。
182
+
183
+ #### `chart.drawing.destroyDrawing()`
184
+
185
+ 销毁绘图模块。
186
+
187
+ ---
188
+
189
+ ### `QLChart.generateMockData(count, startPrice, period)`
190
+
191
+ 生成模拟K线数据。
192
+
193
+ | 参数 | 类型 | 默认值 | 说明 |
194
+ |------|------|--------|------|
195
+ | `count` | `number` | `500` | K线数量 |
196
+ | `startPrice` | `number` | `42000` | 起始价格 |
197
+ | `period` | `string` | `'1h'` | 周期(影响时间间隔和波动幅度) |
198
+
199
+ ---
200
+
201
+ ### `QLChart.drawingTools`
202
+
203
+ 所有绘图工具配置数组(21种工具),包含 `type`、`label`、`icon`、`category`、`anchorCount`、`priority`。
204
+
205
+ ### `QLChart.drawingCategories`
206
+
207
+ 绘图工具分类标签(中文)。
208
+
209
+ ### `QLChart.getDrawingToolsByCategory()`
210
+
211
+ 按分类获取绘图工具,返回 `Map<category, DrawingToolConfig[]>`。
212
+
213
+ ```javascript
214
+ var categories = QLChart.getDrawingToolsByCategory();
215
+ categories.forEach(function(tools, category) {
216
+ console.log(QLChart.drawingCategories[category] + ':', tools.map(function(t) { return t.label; }));
217
+ });
218
+ ```
219
+
220
+ ---
221
+
222
+ ### `chart.destroy()`
223
+
224
+ 销毁图表实例,释放所有资源(含绘图模块)。
225
+
226
+ ## 示例
227
+
228
+ ### 示例1:基础K线图
229
+
230
+ ```html
231
+ <div id="chart" style="width:100%;height:400px;"></div>
232
+ <script src="qlchart.standalone.js"></script>
233
+ <script>
234
+ var chart = QLChart.create('chart', { theme: 'dark', showVolume: true });
235
+ chart.setData(QLChart.generateMockData(500, 42000));
236
+ </script>
237
+ ```
238
+
239
+ ### 示例2:绘图工具
240
+
241
+ ```javascript
242
+ var chart = QLChart.create('container', {
243
+ enableDrawing: true,
244
+ });
245
+
246
+ chart.setData(QLChart.generateMockData(500, 42000));
247
+
248
+ // 设置绘图工具
249
+ chart.drawing.setDrawingTool('trend-line');
250
+
251
+ // 清空所有绘图
252
+ chart.drawing.clearDrawings();
253
+
254
+ // 持久化
255
+ var saved = chart.drawing.exportDrawings();
256
+ // ... 恢复
257
+ chart.drawing.importDrawings(saved);
258
+ ```
259
+
260
+ ### 示例3:多Panel
261
+
262
+ ```javascript
263
+ var chart1 = QLChart.create('chart1', { period: '15m', showVolume: false });
264
+ var chart2 = QLChart.create('chart2', { period: '1d', showVolume: false });
265
+
266
+ chart1.setData(QLChart.generateMockData(300, 42000, '15m'));
267
+ chart2.setData(QLChart.generateMockData(300, 42000, '1d'));
268
+ ```
269
+
270
+ ### 示例4:周期切换
271
+
272
+ ```javascript
273
+ var chart = QLChart.create('container', {
274
+ period: '1h',
275
+ onDataReload: function(period) {
276
+ chart.setData(QLChart.generateMockData(500, 42000, period));
277
+ },
278
+ });
279
+
280
+ // 切换周期 — 自动触发 onDataReload 回调
281
+ chart.setPeriod('4h');
282
+ ```
283
+
284
+ ### 示例5:主题切换
285
+
286
+ ```javascript
287
+ var chart = QLChart.create('container', { theme: 'dark' });
288
+
289
+ // 切换到亮色主题
290
+ chart.setTheme('light');
291
+
292
+ // 同步页面CSS变量
293
+ document.documentElement.setAttribute('data-theme', 'light');
294
+ ```
295
+
296
+ ## 能力边界与限制
297
+
298
+ Standalone JS 版本提供了核心的图表展示和绘图功能,但相比 React 组件版有一些限制:
299
+
300
+ ### ✅ 支持的功能
301
+
302
+ - K线图、线性图、面积图、柱状图
303
+ - 成交量显示
304
+ - 21种绘图工具(趋势线、水平线、矩形、斐波那契等)
305
+ - 绘图持久化(导出/导入)
306
+ - 多图表实例(多Panel)
307
+ - 周期切换
308
+ - 主题切换(含完整网格线/十字线)
309
+ - 截图
310
+ - 实时数据更新
311
+ - 磁铁吸附
312
+
313
+ ### ❌ 不支持的功能(React版独有)
314
+
315
+ | 功能 | 说明 |
316
+ |------|------|
317
+ | 指标面板 | 需要SeriesManager/PaneManager/IndicatorDataProcessor |
318
+ | K线回放 | 需要ReplayController组件 |
319
+ | 多周期联动 | 需要zustand store驱动 |
320
+ | 光标同步 | 需要ChartSyncManager |
321
+ | 右键菜单 | 需要ChartContextMenu组件 |
322
+ | 绘图属性面板 | 需要DrawingPropertyPanel组件 |
323
+
324
+ 如需以上功能,请使用 React 组件版(`QLChartProvider` + 相关组件)。
325
+
326
+ ## 注意事项
327
+
328
+ - 打包体积约 435KB(gzip 101KB),包含 lightweight-charts + 绘图库
329
+ - CSS 已内联到 JS 中,无需额外引入样式文件
330
+ - 浏览器兼容性:Chrome 88+, Firefox 85+, Safari 14+
331
+ - `time` 字段:`setData()` 使用毫秒级,`update()` 使用秒级
332
+ - 切换K线类型需要销毁重建图表实例(lightweight-charts 限制)
333
+ - 完整 Demo 参见 `demo/standalone-demo.html`