@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,114 @@
1
+ """QLChartHTML单元测试"""
2
+
3
+ import json
4
+ import pytest
5
+ from qlchart import QLChartHTML
6
+
7
+
8
+ class TestQLChartHTML:
9
+ """QLChartHTML单元测试"""
10
+
11
+ def setup_method(self):
12
+ self.chart = QLChartHTML("TestChart")
13
+ self.sample_ohlcv = [
14
+ {"time": 1704067200, "open": 42000, "high": 42500, "low": 41800, "close": 42300, "volume": 100},
15
+ {"time": 1704153600, "open": 42300, "high": 43000, "low": 42200, "close": 42800, "volume": 120},
16
+ {"time": 1704240000, "open": 42800, "high": 43500, "low": 42600, "close": 43200, "volume": 150},
17
+ ]
18
+
19
+ def _make_long_data(self, n=30):
20
+ """生成较长的K线数据用于MA/BOLL测试"""
21
+ return [
22
+ {"time": i * 86400 + 1704067200, "open": 42000 + i, "high": 42500 + i,
23
+ "low": 41800 + i, "close": 42200 + i, "volume": 100}
24
+ for i in range(n)
25
+ ]
26
+
27
+ def test_add_candlestick(self):
28
+ result = self.chart.add_candlestick(self.sample_ohlcv)
29
+ assert result is self.chart
30
+ assert len(self.chart._series_list) == 1
31
+ assert self.chart._series_list[0]["type"] == "candlestick"
32
+
33
+ def test_add_line(self):
34
+ line_data = [{"time": 1704067200, "value": 42000}]
35
+ result = self.chart.add_line(line_data, color="#FF0000", name="Test")
36
+ assert len(self.chart._series_list) == 1
37
+ assert self.chart._series_list[0]["type"] == "line"
38
+
39
+ def test_add_ma(self):
40
+ data = self._make_long_data(30)
41
+ result = self.chart.add_ma(data, period=5)
42
+ assert len(self.chart._series_list) == 1
43
+ assert self.chart._series_list[0]["type"] == "line"
44
+ # MA5需要至少5条数据,30条数据应产出26个MA点
45
+ assert len(self.chart._series_list[0]["data"]) == 26
46
+
47
+ def test_add_boll(self):
48
+ data = self._make_long_data(30)
49
+ result = self.chart.add_boll(data, period=20)
50
+ assert len(self.chart._series_list) == 3 # upper + middle + lower
51
+
52
+ def test_add_signals(self):
53
+ signals = [
54
+ {"time": 1704153600, "type": "buy", "text": "买入"},
55
+ {"time": 1704240000, "type": "sell", "text": "卖出"},
56
+ ]
57
+ result = self.chart.add_signals(signals)
58
+ assert len(self.chart._markers) == 2
59
+ assert self.chart._markers[0]["shape"] == "arrowUp"
60
+ assert self.chart._markers[1]["shape"] == "arrowDown"
61
+
62
+ def test_add_volume_colored(self):
63
+ result = self.chart.add_volume_colored(self.sample_ohlcv)
64
+ hist_data = self.chart._series_list[0]
65
+ assert hist_data["type"] == "histogram"
66
+ # 第一根K线收涨(42300>42000),颜色为绿
67
+ assert hist_data["data"][0]["color"] == "#26a69a"
68
+ # 第二根K线收涨(42800>42300),颜色为绿
69
+ assert hist_data["data"][1]["color"] == "#26a69a"
70
+
71
+ def test_to_html_contains_lwc(self):
72
+ self.chart.add_candlestick(self.sample_ohlcv)
73
+ html = self.chart.to_html()
74
+ assert "lightweight-charts" in html
75
+ assert "createChart" in html
76
+ assert "TestChart" in html
77
+
78
+ def test_to_html_dark_theme(self):
79
+ self.chart.set_theme("dark").add_candlestick(self.sample_ohlcv)
80
+ html = self.chart.to_html()
81
+ assert "#1e1e1e" in html
82
+
83
+ def test_to_html_light_theme(self):
84
+ self.chart.set_theme("light").add_candlestick(self.sample_ohlcv)
85
+ html = self.chart.to_html()
86
+ assert "#ffffff" in html
87
+
88
+ def test_timestamp_conversion(self):
89
+ # 毫秒时间戳应自动转换为秒
90
+ data = [{"time": 1704067200000, "open": 42000, "high": 42500,
91
+ "low": 41800, "close": 42300}]
92
+ self.chart.add_candlestick(data)
93
+ transformed = self.chart._series_list[0]["data"]
94
+ assert transformed[0]["time"] == 1704067200
95
+
96
+ def test_chain_calls(self):
97
+ data = self._make_long_data(30)
98
+ html = (self.chart
99
+ .add_candlestick(data)
100
+ .add_volume_colored(data)
101
+ .add_ma(data, period=5)
102
+ .set_theme("dark")
103
+ .to_html())
104
+ assert isinstance(html, str)
105
+ # candlestick + volume_colored(histogram) + ma(line) = 3
106
+ assert len(self.chart._series_list) == 3
107
+
108
+ def test_save_file(self, tmp_path):
109
+ self.chart.add_candlestick(self.sample_ohlcv)
110
+ filepath = str(tmp_path / "test_chart.html")
111
+ self.chart.save(filepath)
112
+ with open(filepath) as f:
113
+ content = f.read()
114
+ assert "lightweight-charts" in content
Binary file
@@ -0,0 +1,30 @@
1
+ import type { ChartDataResponse } from '../types/indicator.js';
2
+ import { IndicatorDataProcessor } from '../core/IndicatorDataProcessor.js';
3
+
4
+ /**
5
+ * 图表数据API封装
6
+ */
7
+
8
+ /** fetchFn参数 */
9
+ export interface ChartDataParams {
10
+ pairId: string;
11
+ product: string;
12
+ period: string;
13
+ }
14
+
15
+ /** 获取图表数据(v2格式) */
16
+ export async function fetchChartData(
17
+ apiBaseUrl: string,
18
+ params: ChartDataParams,
19
+ ): Promise<ChartDataResponse> {
20
+ const res = await fetch(
21
+ `${apiBaseUrl}/api/chart/data?${new URLSearchParams(params as unknown as Record<string, string>)}`,
22
+ );
23
+ if (!res.ok) throw new Error(`Failed to fetch chart data: ${res.statusText}`);
24
+ return res.json();
25
+ }
26
+
27
+ /** 判断fetchFn返回的是否为v2格式 */
28
+ export function isV2Format(response: unknown): response is ChartDataResponse {
29
+ return IndicatorDataProcessor.isV2Format(response);
30
+ }
@@ -0,0 +1,27 @@
1
+ import type { IndicatorListResponse } from '../types/indicator.js';
2
+
3
+ /**
4
+ * 指标API封装
5
+ */
6
+
7
+ /** 获取指标列表 */
8
+ export async function fetchIndicatorList(apiBaseUrl: string): Promise<IndicatorListResponse> {
9
+ const res = await fetch(`${apiBaseUrl}/api/indicators/list`);
10
+ if (!res.ok) throw new Error(`Failed to fetch indicator list: ${res.statusText}`);
11
+ return res.json();
12
+ }
13
+
14
+ /** 获取指标列表(带fallback默认列表) */
15
+ export async function fetchIndicatorListWithFallback(
16
+ apiBaseUrl?: string,
17
+ ): Promise<IndicatorListResponse> {
18
+ if (!apiBaseUrl) {
19
+ return { indicators: [] };
20
+ }
21
+ try {
22
+ return await fetchIndicatorList(apiBaseUrl);
23
+ } catch (err) {
24
+ console.warn('[QLChart] Failed to fetch indicator list, using empty list:', err);
25
+ return { indicators: [] };
26
+ }
27
+ }
@@ -0,0 +1,64 @@
1
+ import { useState, useCallback, useEffect, useRef } from 'react';
2
+ import { PriceScaleMode } from 'lightweight-charts';
3
+ import type { IChartApi } from 'lightweight-charts';
4
+
5
+ export interface ChartFunctionMenuProps {
6
+ chart: IChartApi | null;
7
+ }
8
+
9
+ export function ChartFunctionMenu({ chart }: ChartFunctionMenuProps) {
10
+ const [open, setOpen] = useState(false);
11
+ const [yAxisMode, setYAxisMode] = useState<PriceScaleMode>(PriceScaleMode.Normal);
12
+ const wrapperRef = useRef<HTMLDivElement>(null);
13
+
14
+ const handleReset = useCallback(() => {
15
+ if (!chart) return;
16
+ chart.priceScale('right').applyOptions({ autoScale: true });
17
+ chart.timeScale().fitContent();
18
+ setOpen(false);
19
+ }, [chart]);
20
+
21
+ const handleToggleYAxis = useCallback(() => {
22
+ if (!chart) return;
23
+ const nextMode = yAxisMode === PriceScaleMode.Normal
24
+ ? PriceScaleMode.Percentage
25
+ : PriceScaleMode.Normal;
26
+ chart.priceScale('right').applyOptions({ mode: nextMode });
27
+ setYAxisMode(nextMode);
28
+ setOpen(false);
29
+ }, [chart, yAxisMode]);
30
+
31
+ // 点击外部收起
32
+ useEffect(() => {
33
+ if (!open) return;
34
+ const handleClickOutside = (e: MouseEvent) => {
35
+ if (wrapperRef.current && !wrapperRef.current.contains(e.target as Node)) {
36
+ setOpen(false);
37
+ }
38
+ };
39
+ document.addEventListener('mousedown', handleClickOutside);
40
+ return () => document.removeEventListener('mousedown', handleClickOutside);
41
+ }, [open]);
42
+
43
+ return (
44
+ <div className="qlchart-function-menu-wrapper" ref={wrapperRef}>
45
+ {open && (
46
+ <div className="qlchart-function-menu-popup">
47
+ <button className="qlchart-fm-item" onClick={handleReset}>
48
+ 📐 重置坐标
49
+ </button>
50
+ <button className="qlchart-fm-item" onClick={handleToggleYAxis}>
51
+ {yAxisMode === PriceScaleMode.Normal ? '% 切换百分比' : '# 切换常规'}
52
+ </button>
53
+ </div>
54
+ )}
55
+ <button
56
+ className="qlchart-function-menu-trigger"
57
+ onClick={() => setOpen(!open)}
58
+ title="图表功能"
59
+ >
60
+
61
+ </button>
62
+ </div>
63
+ );
64
+ }
@@ -0,0 +1,116 @@
1
+ import { useRef, useEffect, useState } from 'react';
2
+ import {
3
+ createChart,
4
+ type IChartApi,
5
+ ColorType,
6
+ } from 'lightweight-charts';
7
+
8
+ interface PaneChartPanelProps {
9
+ /** 主图chart实例,用于时间轴同步 */
10
+ mainChart: IChartApi | null;
11
+ /** 主题配置 */
12
+ themeConfig: {
13
+ bg: string;
14
+ text: string;
15
+ grid: string;
16
+ };
17
+ /** paneChart回调,创建后通知父组件 */
18
+ onChartCreated?: (chart: IChartApi) => void;
19
+ }
20
+
21
+ /**
22
+ * 副图面板组件
23
+ * 独立的Chart实例,时间轴与主图同步
24
+ */
25
+ export function PaneChartPanel({
26
+ mainChart,
27
+ themeConfig,
28
+ onChartCreated,
29
+ }: PaneChartPanelProps) {
30
+ const containerRef = useRef<HTMLDivElement>(null);
31
+ const chartRef = useRef<IChartApi | null>(null);
32
+
33
+ useEffect(() => {
34
+ const container = containerRef.current;
35
+ if (!container) return;
36
+
37
+ const chart = createChart(container, {
38
+ width: container.clientWidth,
39
+ height: container.clientHeight,
40
+ layout: {
41
+ background: { type: ColorType.Solid, color: themeConfig.bg },
42
+ textColor: themeConfig.text,
43
+ },
44
+ grid: {
45
+ vertLines: { color: themeConfig.grid },
46
+ horzLines: { color: themeConfig.grid },
47
+ },
48
+ timeScale: {
49
+ timeVisible: true,
50
+ secondsVisible: false,
51
+ visible: false, // 副图不显示独立的时间轴
52
+ },
53
+ rightPriceScale: {
54
+ borderVisible: true,
55
+ },
56
+ });
57
+
58
+ chartRef.current = chart;
59
+ onChartCreated?.(chart);
60
+
61
+ // 时间轴同步:监听主图 visibleRange 变化
62
+ if (mainChart) {
63
+ const timeScale = mainChart.timeScale();
64
+ timeScale.subscribeVisibleLogicalRangeChange((range) => {
65
+ if (range && chartRef.current) {
66
+ chartRef.current.timeScale().setVisibleLogicalRange(range);
67
+ }
68
+ });
69
+
70
+ // 同步当前可见范围
71
+ const currentRange = timeScale.getVisibleLogicalRange();
72
+ if (currentRange) {
73
+ chart.timeScale().setVisibleLogicalRange(currentRange);
74
+ }
75
+ }
76
+
77
+ const observer = new ResizeObserver((entries) => {
78
+ for (const entry of entries) {
79
+ const { width, height } = entry.contentRect;
80
+ if (width > 0 && height > 0) {
81
+ chart.applyOptions({ width, height });
82
+ }
83
+ }
84
+ });
85
+ observer.observe(container);
86
+
87
+ return () => {
88
+ observer.disconnect();
89
+ chart.remove();
90
+ chartRef.current = null;
91
+ };
92
+ // eslint-disable-next-line react-hooks/exhaustive-deps
93
+ }, [mainChart]);
94
+
95
+ // 主题更新
96
+ useEffect(() => {
97
+ chartRef.current?.applyOptions({
98
+ layout: {
99
+ background: { type: ColorType.Solid, color: themeConfig.bg },
100
+ textColor: themeConfig.text,
101
+ },
102
+ grid: {
103
+ vertLines: { color: themeConfig.grid },
104
+ horzLines: { color: themeConfig.grid },
105
+ },
106
+ });
107
+ }, [themeConfig]);
108
+
109
+ return (
110
+ <div
111
+ ref={containerRef}
112
+ className="qlchart-pane-chart"
113
+ style={{ width: '100%', height: '100%' }}
114
+ />
115
+ );
116
+ }
@@ -0,0 +1,66 @@
1
+ import { useCallback } from 'react';
2
+
3
+ export interface PaneDividerProps {
4
+ /** pane索引 */
5
+ index: number;
6
+ /** 拖拽回调,deltaRatio为高度比例变化量(正=下方变大,负=上方变大) */
7
+ onDrag: (deltaRatio: number) => void;
8
+ }
9
+
10
+ /**
11
+ * PaneDivider - 可拖拽的pane分割线组件
12
+ * 用于调整主图/副图的高度比例
13
+ */
14
+ export function PaneDivider({ index, onDrag }: PaneDividerProps) {
15
+ const handleMouseDown = useCallback((e: React.MouseEvent) => {
16
+ e.preventDefault();
17
+ e.stopPropagation();
18
+
19
+ const startY = e.clientY;
20
+ const container = e.currentTarget.parentElement;
21
+ const containerHeight = container?.clientHeight ?? 600;
22
+
23
+ const handleMouseMove = (e: MouseEvent) => {
24
+ const deltaY = e.clientY - startY;
25
+ const deltaRatio = deltaY / containerHeight;
26
+ onDrag(deltaRatio);
27
+ };
28
+
29
+ const handleMouseUp = () => {
30
+ document.removeEventListener('mousemove', handleMouseMove);
31
+ document.removeEventListener('mouseup', handleMouseUp);
32
+ document.body.style.cursor = '';
33
+ };
34
+
35
+ document.body.style.cursor = 'ns-resize';
36
+ document.addEventListener('mousemove', handleMouseMove);
37
+ document.addEventListener('mouseup', handleMouseUp);
38
+ }, [onDrag]);
39
+
40
+ return (
41
+ <div
42
+ className="qlchart-pane-divider"
43
+ data-pane-index={index}
44
+ onMouseDown={handleMouseDown}
45
+ style={{
46
+ height: '4px',
47
+ width: '100%',
48
+ cursor: 'ns-resize',
49
+ background: 'var(--ant-color-border, #1e222d)',
50
+ flexShrink: 0,
51
+ position: 'relative',
52
+ }}
53
+ >
54
+ <div style={{
55
+ position: 'absolute',
56
+ top: '1px',
57
+ left: '50%',
58
+ transform: 'translateX(-50%)',
59
+ width: '32px',
60
+ height: '2px',
61
+ background: 'var(--ant-color-text-quaternary, #555)',
62
+ borderRadius: '1px',
63
+ }} />
64
+ </div>
65
+ );
66
+ }
@@ -0,0 +1,151 @@
1
+ import { useMemo, useRef, useEffect, useCallback, useState } from 'react';
2
+ import type { LayoutMode, PanelConfig } from '../types/chart.js';
3
+ import { QLChartPanel } from './QLChartPanel.js';
4
+ import { useChartStore } from '../store/useChartStore.js';
5
+ import { useCrosshairSync } from '../hooks/useCrosshairSync.js';
6
+ import { GlobalToolbar } from './toolbar/GlobalToolbar.js';
7
+ import { DrawingModule } from './drawing/DrawingModule.js';
8
+ import type { IChartApi } from 'lightweight-charts';
9
+
10
+ export interface QLChartLayoutProps {
11
+ /** 布局模式 */
12
+ mode?: LayoutMode;
13
+ /** 面板配置 */
14
+ panels?: PanelConfig[];
15
+ /** 是否开启光标联动 */
16
+ crosshairSync?: boolean;
17
+ /** 布局变化回调 */
18
+ onLayoutChange?: (mode: LayoutMode) => void;
19
+ }
20
+
21
+ /** 布局模式对应的 CSS Grid 配置 */
22
+ const GRID_CONFIG: Record<LayoutMode, { columns: string; rows: string; count: number }> = {
23
+ '1': { columns: '1fr', rows: '1fr', count: 1 },
24
+ '2h': { columns: '1fr 1fr', rows: '1fr', count: 2 },
25
+ '2v': { columns: '1fr', rows: '1fr 1fr', count: 2 },
26
+ '4': { columns: '1fr 1fr', rows: '1fr 1fr', count: 4 },
27
+ };
28
+
29
+ export function QLChartLayout({
30
+ mode: modeProp,
31
+ panels: panelsProp,
32
+ crosshairSync = true,
33
+ onLayoutChange,
34
+ }: QLChartLayoutProps) {
35
+ const storeMode = useChartStore((s) => s.layoutMode);
36
+ const storePanels = useChartStore((s) => s.panels);
37
+ const activePairId = useChartStore((s) => s.activePairId);
38
+ const activeProduct = useChartStore((s) => s.activeProduct);
39
+
40
+ const mode = modeProp ?? storeMode;
41
+ const panels = panelsProp ?? storePanels;
42
+ const gridConfig = GRID_CONFIG[mode];
43
+
44
+ // Bug③修复:chartRefs改为useState驱动,chart创建时触发重渲染和effect重新执行
45
+ const [chartInstances, setChartInstances] = useState<(IChartApi | null)[]>([]);
46
+
47
+ // Register chart instance from QLChartPanel
48
+ const handleChartCreated = useCallback((chart: IChartApi | null, index: number) => {
49
+ setChartInstances(prev => {
50
+ const next = [...prev];
51
+ while (next.length <= index) next.push(null);
52
+ next[index] = chart;
53
+ return next;
54
+ });
55
+ }, []);
56
+
57
+ // Crosshair sync — 使用useState驱动的chartInstances
58
+ const chartRefsForSync = useRef<(IChartApi | null)[]>(chartInstances);
59
+ chartRefsForSync.current = chartInstances;
60
+
61
+ const isSingleMode = mode === '1';
62
+
63
+ // 确保有足够的 panel 配置
64
+ const effectivePanels = useMemo(() => {
65
+ const result: PanelConfig[] = [];
66
+ for (let i = 0; i < gridConfig.count; i++) {
67
+ if (panels[i]) {
68
+ result.push(panels[i]);
69
+ } else {
70
+ result.push({
71
+ id: `panel_${i}`,
72
+ pairId: activePairId ?? 'default',
73
+ product: activeProduct ?? 'BTC',
74
+ period: '1h',
75
+ seriesType: 'candlestick' as any,
76
+ showToolbar: true,
77
+ });
78
+ }
79
+ }
80
+ return result;
81
+ }, [panels, gridConfig.count, activePairId, activeProduct]);
82
+
83
+ const panelIdList = effectivePanels.map(p => p.id);
84
+ useCrosshairSync(
85
+ crosshairSync ? chartRefsForSync : { current: [] },
86
+ crosshairSync ? panelIdList : [],
87
+ );
88
+
89
+ // 当 panels 变化时,自动设置 activePanelId
90
+ useEffect(() => {
91
+ const store = useChartStore.getState();
92
+ if ((!store.activePanelId || !effectivePanels.some(p => p.id === store.activePanelId)) && effectivePanels.length > 0) {
93
+ store.setActivePanel(effectivePanels[0].id);
94
+ }
95
+ }, [effectivePanels]);
96
+
97
+ // Resize charts on layout change
98
+ useEffect(() => {
99
+ // Trigger resize after layout change
100
+ const timer = setTimeout(() => {
101
+ window.dispatchEvent(new Event('resize'));
102
+ }, 50);
103
+ return () => clearTimeout(timer);
104
+ }, [mode]);
105
+
106
+ return (
107
+ <div className="qlchart-layout-wrapper" style={{ display: 'flex', flexDirection: 'column', width: '100%', height: '100%' }}>
108
+ {/* 多图模式才显示全局工具栏 */}
109
+ {!isSingleMode && <GlobalToolbar />}
110
+
111
+ {/* 图表区域 */}
112
+ <div style={{ display: 'flex', flex: 1, overflow: 'hidden' }}>
113
+ {/* 多面板模式:全局绘图Sidebar */}
114
+ {!isSingleMode && (
115
+ <div className="qlchart-global-drawing-sidebar">
116
+ <DrawingModule />
117
+ </div>
118
+ )}
119
+
120
+ {/* 图表 Grid */}
121
+ <div
122
+ className="qlchart-layout"
123
+ style={{
124
+ display: 'grid',
125
+ gridTemplateColumns: gridConfig.columns,
126
+ gridTemplateRows: gridConfig.rows,
127
+ gap: '4px',
128
+ flex: 1,
129
+ overflow: 'hidden',
130
+ }}
131
+ >
132
+ {effectivePanels.map((panel, index) => (
133
+ <div key={panel.id} className="qlchart-layout-cell">
134
+ <QLChartPanel
135
+ pairId={panel.pairId}
136
+ product={panel.product}
137
+ period={panel.period}
138
+ seriesType={panel.seriesType}
139
+ showToolbar={isSingleMode ? (panel.showToolbar ?? true) : false}
140
+ panelId={panel.id}
141
+ panelIndex={index}
142
+ onChartCreated={handleChartCreated}
143
+ isSinglePanel={isSingleMode}
144
+ />
145
+ </div>
146
+ ))}
147
+ </div>
148
+ </div>
149
+ </div>
150
+ );
151
+ }