@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,58 @@
1
+ import { useChartStore, selectActivePanelPeriod } from '../../store/useChartStore.js';
2
+ import { usePanelRegistry } from '../../store/usePanelRegistry.js';
3
+ import { PeriodSelector } from './PeriodSelector.js';
4
+ import { TimeBarModule } from '../timebar/TimeBarModule.js';
5
+ import { KlineTypeSelector } from './KlineTypeSelector.js';
6
+ import { IndicatorPanel } from '../indicator/index.js';
7
+ import { useDrawingStore } from '../../store/useDrawingStore.js';
8
+ import { ScreenshotUtil } from '../../plugins/screenshot/ScreenshotUtil.js';
9
+ import { useState, useCallback } from 'react';
10
+
11
+ export function GlobalToolbar() {
12
+ // 活跃面板的周期(用于 PeriodSelector 高亮)— 使用独立 selector 避免内联函数
13
+ const activePeriod = useChartStore(selectActivePanelPeriod);
14
+ const activePanelId = useChartStore((s) => s.activePanelId);
15
+ const activeSeriesType = useChartStore((s) => s.activeSeriesType);
16
+ const drawingToolbarOpen = useDrawingStore((s) => s.drawingToolbarOpen);
17
+ const setDrawingToolbarOpen = useDrawingStore((s) => s.setDrawingToolbarOpen);
18
+ const drawingCount = useDrawingStore((s) => s.drawingCount);
19
+
20
+ const getHandle = usePanelRegistry((s) => s.getHandle);
21
+
22
+ // 获取活跃面板句柄
23
+ const handle = activePanelId ? getHandle(activePanelId) : undefined;
24
+
25
+ const [indicatorPanelOpen, setIndicatorPanelOpen] = useState(false);
26
+
27
+ const handleScreenshot = useCallback(() => {
28
+ if (handle?.chart && handle?.container) {
29
+ ScreenshotUtil.download(handle.chart, handle.container);
30
+ }
31
+ }, [handle]);
32
+
33
+ return (
34
+ <div className="qlchart-toolbar qlchart-global-toolbar" style={{ position: 'relative' }}>
35
+ <PeriodSelector activePeriod={activePeriod} />
36
+ <div className="qlchart-toolbar-separator" />
37
+ <TimeBarModule />
38
+ <div className="qlchart-toolbar-separator" />
39
+ <KlineTypeSelector activeType={activeSeriesType} />
40
+ <div className="qlchart-toolbar-separator" />
41
+ <button className="qlchart-toolbar-btn" onClick={() => setIndicatorPanelOpen(true)}>
42
+ 📊 指标
43
+ </button>
44
+ <IndicatorPanel open={indicatorPanelOpen} onClose={() => setIndicatorPanelOpen(false)} />
45
+ <div className="qlchart-toolbar-separator" />
46
+ <button
47
+ className={`qlchart-toolbar-btn ${drawingToolbarOpen ? 'active' : ''}`}
48
+ onClick={() => setDrawingToolbarOpen(!drawingToolbarOpen)}
49
+ >
50
+ 🔧 绘图{drawingCount > 0 ? `(${drawingCount})` : ''}
51
+ </button>
52
+ <div className="qlchart-toolbar-separator" />
53
+ <button className="qlchart-toolbar-btn" onClick={handleScreenshot} title="截图下载">
54
+ 📸 截图
55
+ </button>
56
+ </div>
57
+ );
58
+ }
@@ -0,0 +1,123 @@
1
+ import { useState, useRef, useEffect, useCallback } from 'react';
2
+ import { createPortal } from 'react-dom';
3
+ import { useChartStore } from '../../store/useChartStore.js';
4
+ import { SeriesType } from '../../types/index.js';
5
+
6
+ const KLINE_TYPES = [
7
+ { label: '蜡烛图', value: SeriesType.Candlestick, group: '标准', icon: '🕯️' },
8
+ { label: '空心蜡烛图', value: SeriesType.HollowCandlestick, group: '标准', icon: '🔥' },
9
+ { label: '美国线', value: SeriesType.Bar, group: '标准', icon: '📊' },
10
+ { label: '折线图', value: SeriesType.Line, group: '标准', icon: '📈' },
11
+ { label: '面积图', value: SeriesType.Area, group: '标准', icon: '🏔️' },
12
+ { label: '基准线图', value: SeriesType.Baseline, group: '标准', icon: '➖' },
13
+ { label: '平均K线', value: SeriesType.HeikinAshi, group: '高级', icon: '⚡' },
14
+ { label: '成交量蜡烛图', value: SeriesType.VolumeCandlestick, group: '高级', icon: '📦' },
15
+ { label: '砖形图', value: SeriesType.Renko, group: '高级', icon: '🧱' },
16
+ { label: '线形反转', value: SeriesType.LineBreak, group: '高级', icon: '🔄' },
17
+ ];
18
+
19
+ const GROUPS = ['标准', '高级'];
20
+
21
+ /** 下拉框最小宽度 */
22
+ const DROPDOWN_MIN_WIDTH = 160;
23
+
24
+ export interface KlineTypeSelectorProps {
25
+ activeType: SeriesType;
26
+ }
27
+
28
+ export function KlineTypeSelector({ activeType }: KlineTypeSelectorProps) {
29
+ const setSeriesType = useChartStore((s) => s.setSeriesType);
30
+ const [open, setOpen] = useState(false);
31
+ const buttonRef = useRef<HTMLButtonElement>(null);
32
+ const dropdownRef = useRef<HTMLDivElement>(null);
33
+ const [dropdownStyle, setDropdownStyle] = useState<React.CSSProperties>({});
34
+
35
+ const activeItem = KLINE_TYPES.find(t => t.value === activeType);
36
+
37
+ /** 计算下拉框定位 */
38
+ const updatePosition = useCallback(() => {
39
+ if (!buttonRef.current) return;
40
+ const rect = buttonRef.current.getBoundingClientRect();
41
+ let left = rect.left;
42
+ // 右边界检测:如果超出视口则向左偏移
43
+ if (left + DROPDOWN_MIN_WIDTH > window.innerWidth) {
44
+ left = window.innerWidth - DROPDOWN_MIN_WIDTH - 8;
45
+ }
46
+ setDropdownStyle({
47
+ position: 'fixed',
48
+ top: rect.bottom,
49
+ left,
50
+ zIndex: 9999,
51
+ });
52
+ }, []);
53
+
54
+ // 打开时计算位置
55
+ useEffect(() => {
56
+ if (!open) return;
57
+ updatePosition();
58
+ }, [open, updatePosition]);
59
+
60
+ // 监听 resize/scroll 更新位置
61
+ useEffect(() => {
62
+ if (!open) return;
63
+ window.addEventListener('resize', updatePosition);
64
+ window.addEventListener('scroll', updatePosition, true);
65
+ return () => {
66
+ window.removeEventListener('resize', updatePosition);
67
+ window.removeEventListener('scroll', updatePosition, true);
68
+ };
69
+ }, [open, updatePosition]);
70
+
71
+ // 点击外部关闭(适配 Portal 模式:同时检查按钮和下拉框)
72
+ useEffect(() => {
73
+ if (!open) return;
74
+ const handleClick = (e: MouseEvent) => {
75
+ const target = e.target as Node;
76
+ if (
77
+ buttonRef.current?.contains(target) ||
78
+ dropdownRef.current?.contains(target)
79
+ ) {
80
+ return;
81
+ }
82
+ setOpen(false);
83
+ };
84
+ document.addEventListener('mousedown', handleClick);
85
+ return () => document.removeEventListener('mousedown', handleClick);
86
+ }, [open]);
87
+
88
+ return (
89
+ <div className="qlchart-kline-type-selector">
90
+ <button
91
+ ref={buttonRef}
92
+ className={`qlchart-toolbar-btn ${open ? 'active' : ''}`}
93
+ onClick={() => setOpen(!open)}
94
+ title={activeItem?.label ?? '选择K线类型'}
95
+ >
96
+ {activeItem?.icon ?? '📊'} {activeItem?.label ?? 'K线类型'} ▾
97
+ </button>
98
+ {open && createPortal(
99
+ <div className="qlchart-kline-type-dropdown" style={dropdownStyle} ref={dropdownRef}>
100
+ {GROUPS.map(group => (
101
+ <div key={group} className="qlchart-kline-type-group">
102
+ <div className="qlchart-kline-type-group-label">{group}</div>
103
+ {KLINE_TYPES.filter(t => t.group === group).map(item => (
104
+ <button
105
+ key={item.value}
106
+ className={`qlchart-kline-type-option ${activeType === item.value ? 'active' : ''}`}
107
+ onClick={() => {
108
+ setSeriesType(item.value);
109
+ setOpen(false);
110
+ }}
111
+ >
112
+ <span className="qlchart-kline-type-icon">{item.icon}</span>
113
+ <span>{item.label}</span>
114
+ </button>
115
+ ))}
116
+ </div>
117
+ ))}
118
+ </div>,
119
+ document.body
120
+ )}
121
+ </div>
122
+ );
123
+ }
@@ -0,0 +1,45 @@
1
+ import { useCallback } from 'react';
2
+ import type { LayoutMode } from '../../types/chart.js';
3
+ import { useChartStore } from '../../store/useChartStore.js';
4
+
5
+ export interface LayoutSwitcherProps {
6
+ currentMode?: LayoutMode;
7
+ onModeChange?: (mode: LayoutMode) => void;
8
+ }
9
+
10
+ const LAYOUT_OPTIONS: { mode: LayoutMode; icon: string; label: string }[] = [
11
+ { mode: '1', icon: '◻', label: '单图' },
12
+ { mode: '2h', icon: '◻◻', label: '左右分屏' },
13
+ { mode: '2v', icon: '◻/◻', label: '上下分屏' },
14
+ { mode: '4', icon: '◻◻/◻◻', label: '四分屏' },
15
+ ];
16
+
17
+ export function LayoutSwitcher({ currentMode, onModeChange }: LayoutSwitcherProps) {
18
+ const storeMode = useChartStore((s) => s.layoutMode);
19
+ const setLayoutMode = useChartStore((s) => s.setLayoutMode);
20
+
21
+ const activeMode = currentMode ?? storeMode;
22
+
23
+ const handleModeChange = useCallback(
24
+ (mode: LayoutMode) => {
25
+ setLayoutMode(mode);
26
+ onModeChange?.(mode);
27
+ },
28
+ [setLayoutMode, onModeChange],
29
+ );
30
+
31
+ return (
32
+ <div className="qlchart-layout-switcher">
33
+ {LAYOUT_OPTIONS.map(({ mode, icon, label }) => (
34
+ <button
35
+ key={mode}
36
+ className={`qlchart-layout-btn ${activeMode === mode ? 'active' : ''}`}
37
+ onClick={() => handleModeChange(mode)}
38
+ title={label}
39
+ >
40
+ <span className="qlchart-layout-icon">{icon}</span>
41
+ </button>
42
+ ))}
43
+ </div>
44
+ );
45
+ }
@@ -0,0 +1,35 @@
1
+ import { useChartStore } from '../../store/useChartStore.js';
2
+
3
+ const PERIODS = [
4
+ { label: '1m', value: '1m' },
5
+ { label: '5m', value: '5m' },
6
+ { label: '15m', value: '15m' },
7
+ { label: '30m', value: '30m' },
8
+ { label: '1h', value: '1h' },
9
+ { label: '4h', value: '4h' },
10
+ { label: '1d', value: '1d' },
11
+ { label: '1w', value: '1w' },
12
+ { label: '1M', value: '1M' },
13
+ ];
14
+
15
+ export interface PeriodSelectorProps {
16
+ activePeriod: string;
17
+ }
18
+
19
+ export function PeriodSelector({ activePeriod }: PeriodSelectorProps) {
20
+ const setActivePanelPeriod = useChartStore((s) => s.setActivePanelPeriod);
21
+
22
+ return (
23
+ <div className="qlchart-toolbar-group">
24
+ {PERIODS.map((p) => (
25
+ <button
26
+ key={p.value}
27
+ className={`qlchart-toolbar-btn ${activePeriod === p.value ? 'active' : ''}`}
28
+ onClick={() => setActivePanelPeriod(p.value)}
29
+ >
30
+ {p.label}
31
+ </button>
32
+ ))}
33
+ </div>
34
+ );
35
+ }
@@ -0,0 +1,71 @@
1
+ import type { ThemePreset, SeriesType } from '../../types/index.js';
2
+ import { PeriodSelector } from './PeriodSelector.js';
3
+ import { TimeBarModule } from '../timebar/TimeBarModule.js';
4
+ import { KlineTypeSelector } from './KlineTypeSelector.js';
5
+ import { useState, useCallback } from 'react';
6
+ import { IndicatorPanel } from '../indicator/index.js';
7
+ import { useDrawingStore } from '../../store/useDrawingStore.js';
8
+ import { ScreenshotUtil } from '../../plugins/screenshot/ScreenshotUtil.js';
9
+
10
+ export interface QLToolbarProps {
11
+ period: string;
12
+ seriesType: SeriesType;
13
+ theme: ThemePreset;
14
+ chartManager?: { getChart: () => any } | null;
15
+ chartRef?: React.RefObject<any>;
16
+ containerRef?: React.RefObject<HTMLElement>;
17
+ }
18
+
19
+ export function QLToolbar({ period, seriesType, theme, chartManager, chartRef, containerRef }: QLToolbarProps) {
20
+ const [indicatorPanelOpen, setIndicatorPanelOpen] = useState(false);
21
+ const drawingToolbarOpen = useDrawingStore((s) => s.drawingToolbarOpen);
22
+ const setDrawingToolbarOpen = useDrawingStore((s) => s.setDrawingToolbarOpen);
23
+ const drawingCount = useDrawingStore((s) => s.drawingCount);
24
+
25
+ const handleOpenIndicator = useCallback(() => {
26
+ setIndicatorPanelOpen(true);
27
+ }, []);
28
+
29
+ const handleCloseIndicator = useCallback(() => {
30
+ setIndicatorPanelOpen(false);
31
+ }, []);
32
+
33
+ const handleScreenshot = useCallback(() => {
34
+ if (chartRef?.current && containerRef?.current) {
35
+ ScreenshotUtil.download(chartRef.current, containerRef.current);
36
+ }
37
+ }, [chartRef, containerRef]);
38
+
39
+ return (
40
+ <div className="qlchart-toolbar">
41
+ <PeriodSelector activePeriod={period} />
42
+ <div className="qlchart-toolbar-separator" />
43
+ <TimeBarModule />
44
+ <div className="qlchart-toolbar-separator" />
45
+ <KlineTypeSelector activeType={seriesType} />
46
+ <div className="qlchart-toolbar-separator" />
47
+ <button
48
+ className="qlchart-toolbar-btn"
49
+ onClick={handleOpenIndicator}
50
+ >
51
+ 📊 指标
52
+ </button>
53
+ <IndicatorPanel open={indicatorPanelOpen} onClose={handleCloseIndicator} />
54
+ <div className="qlchart-toolbar-separator" />
55
+ <button
56
+ className={`qlchart-toolbar-btn ${drawingToolbarOpen ? 'active' : ''}`}
57
+ onClick={() => setDrawingToolbarOpen(!drawingToolbarOpen)}
58
+ >
59
+ 🔧 绘图{drawingCount > 0 ? `(${drawingCount})` : ''}
60
+ </button>
61
+ <div className="qlchart-toolbar-separator" />
62
+ <button
63
+ className="qlchart-toolbar-btn"
64
+ onClick={handleScreenshot}
65
+ title="截图下载"
66
+ >
67
+ 📸 截图
68
+ </button>
69
+ </div>
70
+ );
71
+ }
@@ -0,0 +1,89 @@
1
+ import { useState } from 'react';
2
+ import type { UTCTimestamp } from 'lightweight-charts';
3
+
4
+ interface TimeRangeSelectorProps {
5
+ chartManager?: { getChart: () => any } | null;
6
+ }
7
+
8
+ const TIME_RANGES = [
9
+ { label: '5D', days: 5 },
10
+ { label: '1M', days: 30 },
11
+ { label: '3M', days: 90 },
12
+ { label: '6M', days: 180 },
13
+ { label: 'YTD', days: -1 },
14
+ { label: '1Y', days: 365 },
15
+ { label: '2Y', days: 730 },
16
+ { label: '5Y', days: 1825 },
17
+ { label: 'Max', days: 0 },
18
+ ];
19
+
20
+ export function TimeRangeSelector({ chartManager }: TimeRangeSelectorProps) {
21
+ const [showDatePicker, setShowDatePicker] = useState(false);
22
+ const [startDate, setStartDate] = useState('');
23
+ const [endDate, setEndDate] = useState('');
24
+
25
+ const handleClick = (days: number) => {
26
+ const chart = chartManager?.getChart();
27
+ if (!chart) return;
28
+
29
+ if (days === 0) {
30
+ chart.timeScale().fitContent();
31
+ return;
32
+ }
33
+
34
+ const endTime = Date.now();
35
+ let startTime: number;
36
+
37
+ if (days === -1) {
38
+ // YTD
39
+ startTime = new Date(new Date().getFullYear(), 0, 1).getTime();
40
+ } else {
41
+ startTime = endTime - days * 24 * 60 * 60 * 1000;
42
+ }
43
+
44
+ chart.timeScale().setVisibleRange({
45
+ from: (startTime / 1000) as UTCTimestamp,
46
+ to: (endTime / 1000) as UTCTimestamp,
47
+ });
48
+ };
49
+
50
+ const applyDateRange = () => {
51
+ const chart = chartManager?.getChart();
52
+ if (!chart || !startDate || !endDate) return;
53
+
54
+ chart.timeScale().setVisibleRange({
55
+ from: (new Date(startDate).getTime() / 1000) as UTCTimestamp,
56
+ to: (new Date(endDate).getTime() / 1000) as UTCTimestamp,
57
+ });
58
+ setShowDatePicker(false);
59
+ };
60
+
61
+ return (
62
+ <div className="qlchart-toolbar-group">
63
+ {TIME_RANGES.map((range) => (
64
+ <button
65
+ key={range.label}
66
+ className="qlchart-toolbar-btn"
67
+ onClick={() => handleClick(range.days)}
68
+ >
69
+ {range.label}
70
+ </button>
71
+ ))}
72
+ <button
73
+ className="qlchart-toolbar-btn"
74
+ onClick={() => setShowDatePicker(!showDatePicker)}
75
+ title="日期范围"
76
+ >
77
+ 📅
78
+ </button>
79
+ {showDatePicker && (
80
+ <div className="qlchart-daterange-picker">
81
+ <input type="date" value={startDate} onChange={(e) => setStartDate(e.target.value)} />
82
+ <span>~</span>
83
+ <input type="date" value={endDate} onChange={(e) => setEndDate(e.target.value)} />
84
+ <button onClick={applyDateRange}>应用</button>
85
+ </div>
86
+ )}
87
+ </div>
88
+ );
89
+ }
@@ -0,0 +1,67 @@
1
+ import { Dialog, Transition } from '@headlessui/react';
2
+ import { Fragment, type ReactNode } from 'react';
3
+ import { createPortal } from 'react-dom';
4
+
5
+ export interface ModalProps {
6
+ open: boolean;
7
+ onClose: () => void;
8
+ title?: string;
9
+ children: ReactNode;
10
+ width?: number;
11
+ }
12
+
13
+ /**
14
+ * QLChart 统一 Modal 组件
15
+ * 基于 @headlessui/react Dialog + 现有 CSS 变量系统
16
+ *
17
+ * 特性:
18
+ * - 自动 Portal 到 document.body
19
+ * - 焦点陷阱(Focus Trap)
20
+ * - 过渡动画
21
+ * - ARIA 无障碍
22
+ * - 点击遮罩关闭
23
+ */
24
+ export function Modal({ open, onClose, title, children, width = 320 }: ModalProps) {
25
+ return createPortal(
26
+ <Transition show={open} as={Fragment}>
27
+ <Dialog onClose={onClose} className="qlchart-modal-root">
28
+ {/* 遮罩层 */}
29
+ <Transition.Child
30
+ as={Fragment}
31
+ enter="qlchart-modal-enter"
32
+ enterFrom="qlchart-modal-overlay-enter-from"
33
+ enterTo="qlchart-modal-overlay-enter-to"
34
+ leave="qlchart-modal-leave"
35
+ leaveFrom="qlchart-modal-overlay-leave-from"
36
+ leaveTo="qlchart-modal-overlay-leave-to"
37
+ >
38
+ <div className="qlchart-modal-overlay" aria-hidden="true" />
39
+ </Transition.Child>
40
+
41
+ {/* Modal 内容 */}
42
+ <Transition.Child
43
+ as={Fragment}
44
+ enter="qlchart-modal-enter"
45
+ enterFrom="qlchart-modal-content-enter-from"
46
+ enterTo="qlchart-modal-content-enter-to"
47
+ leave="qlchart-modal-leave"
48
+ leaveFrom="qlchart-modal-content-leave-from"
49
+ leaveTo="qlchart-modal-content-leave-to"
50
+ >
51
+ <Dialog.Panel
52
+ className="qlchart-modal-panel"
53
+ style={{ width: `${width}px`, maxWidth: '90vw' }}
54
+ >
55
+ {title && (
56
+ <Dialog.Title className="qlchart-modal-title">
57
+ {title}
58
+ </Dialog.Title>
59
+ )}
60
+ {children}
61
+ </Dialog.Panel>
62
+ </Transition.Child>
63
+ </Dialog>
64
+ </Transition>,
65
+ document.body,
66
+ );
67
+ }
@@ -0,0 +1,95 @@
1
+ import {
2
+ createChart,
3
+ ColorType,
4
+ type IChartApi,
5
+ type ChartOptions as LWCChartOptions,
6
+ type DeepPartial,
7
+ } from 'lightweight-charts';
8
+ import { EventManager, ChartEvent } from './EventManager.js';
9
+ import { formatFullTime, formatTickTime } from '../utils/timeFormatter.js';
10
+
11
+ export interface ChartManagerCreateOptions {
12
+ width?: number;
13
+ height?: number;
14
+ theme?: DeepPartial<LWCChartOptions>;
15
+ }
16
+
17
+ /**
18
+ * 图表管理器
19
+ * 封装 LWC IChartApi 的生命周期
20
+ */
21
+ export class ChartManager {
22
+ private chart: IChartApi | null = null;
23
+ private eventManager: EventManager;
24
+ private container: HTMLElement | null = null;
25
+
26
+ constructor(eventManager: EventManager) {
27
+ this.eventManager = eventManager;
28
+ }
29
+
30
+ /** 创建图表实例 */
31
+ create(container: HTMLElement, options?: ChartManagerCreateOptions): IChartApi {
32
+ // 如果已有实例先销毁
33
+ if (this.chart) {
34
+ this.dispose();
35
+ }
36
+
37
+ this.container = container;
38
+ this.chart = createChart(container, {
39
+ width: options?.width ?? container.clientWidth,
40
+ height: options?.height ?? container.clientHeight,
41
+ layout: {
42
+ background: { type: ColorType.Solid, color: '#131722' },
43
+ textColor: '#d1d4dc',
44
+ },
45
+ grid: {
46
+ vertLines: { color: '#1e222d' },
47
+ horzLines: { color: '#1e222d' },
48
+ },
49
+ timeScale: {
50
+ timeVisible: true,
51
+ secondsVisible: true,
52
+ tickMarkFormatter: formatTickTime,
53
+ },
54
+ localization: {
55
+ timeFormatter: formatFullTime,
56
+ dateFormat: 'yyyy-MM-dd HH:mm:ss',
57
+ },
58
+ ...options?.theme,
59
+ });
60
+
61
+ this.eventManager.emit(ChartEvent.ChartCreated, { chart: this.chart });
62
+ return this.chart;
63
+ }
64
+
65
+ /** 销毁图表实例 */
66
+ dispose(): void {
67
+ if (this.chart) {
68
+ this.chart.remove();
69
+ this.chart = null;
70
+ this.container = null;
71
+ this.eventManager.emit(ChartEvent.ChartDisposed);
72
+ }
73
+ }
74
+
75
+ /** 更新图表选项 */
76
+ updateOptions(options: DeepPartial<LWCChartOptions>): void {
77
+ this.chart?.applyOptions(options);
78
+ }
79
+
80
+ /** 获取图表实例 */
81
+ getChart(): IChartApi | null {
82
+ return this.chart;
83
+ }
84
+
85
+ /** 调整图表大小 */
86
+ resize(width: number, height: number): void {
87
+ this.chart?.applyOptions({ width, height });
88
+ }
89
+
90
+ /** 截图 */
91
+ screenshot(): string | null {
92
+ if (!this.chart) return null;
93
+ return this.chart.takeScreenshot()?.toDataURL() ?? null;
94
+ }
95
+ }