@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.
- package/.idea/QLChart.iml +12 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/README.md +75 -0
- package/demo/App.css +213 -0
- package/demo/App.tsx +46 -0
- package/demo/components/ControlPanel.tsx +13 -0
- package/demo/components/DemoNav.tsx +27 -0
- package/demo/index.html +16 -0
- package/demo/main.tsx +10 -0
- package/demo/pages/BasicChartDemo.tsx +61 -0
- package/demo/pages/DrawingDemo.tsx +22 -0
- package/demo/pages/IndicatorDemo.tsx +22 -0
- package/demo/pages/LayoutDemo.tsx +35 -0
- package/demo/pages/MultiPeriodDemo.tsx +31 -0
- package/demo/pages/ReplayDemo.tsx +195 -0
- package/demo/pages/SaveDemo.tsx +27 -0
- package/demo/pages/ThemeDemo.tsx +29 -0
- package/demo/standalone-demo.html +597 -0
- package/demo/vite.config.demo.ts +17 -0
- package/dist/index.d.ts +1973 -0
- package/dist/qlchart.js +23169 -0
- package/dist/style.css +1 -0
- package/doc/api/indicator-data-processor.md +35 -0
- package/doc/api-reference/.nojekyll +1 -0
- package/doc/api-reference/assets/hierarchy.js +1 -0
- package/doc/api-reference/assets/highlight.css +43 -0
- package/doc/api-reference/assets/icons.js +18 -0
- package/doc/api-reference/assets/icons.svg +1 -0
- package/doc/api-reference/assets/main.js +60 -0
- package/doc/api-reference/assets/navigation.js +1 -0
- package/doc/api-reference/assets/search.js +1 -0
- package/doc/api-reference/assets/style.css +1611 -0
- package/doc/api-reference/classes/ChartManager.html +16 -0
- package/doc/api-reference/classes/DataManager.html +13 -0
- package/doc/api-reference/classes/DrawingAdapter.html +64 -0
- package/doc/api-reference/classes/DrawingPersistence.html +21 -0
- package/doc/api-reference/classes/EventManager.html +12 -0
- package/doc/api-reference/classes/HollowCandlestickSeries.html +22 -0
- package/doc/api-reference/classes/IndicatorRenderer.html +20 -0
- package/doc/api-reference/classes/KlineReplay.html +31 -0
- package/doc/api-reference/classes/MockDataService.html +13 -0
- package/doc/api-reference/classes/MockIndicatorService.html +4 -0
- package/doc/api-reference/classes/OverlayIndicator.html +11 -0
- package/doc/api-reference/classes/PaneIndicator.html +16 -0
- package/doc/api-reference/classes/PaneManager.html +24 -0
- package/doc/api-reference/classes/RealtimeDataFeed.html +22 -0
- package/doc/api-reference/classes/RenkoSeries.html +22 -0
- package/doc/api-reference/classes/ScreenshotUtil.html +10 -0
- package/doc/api-reference/classes/SeriesManager.html +30 -0
- package/doc/api-reference/classes/ThemeManager.html +18 -0
- package/doc/api-reference/enums/ChartEvent.html +12 -0
- package/doc/api-reference/enums/IndicatorType.html +4 -0
- package/doc/api-reference/enums/SeriesType.html +13 -0
- package/doc/api-reference/functions/ChartFunctionMenu.html +1 -0
- package/doc/api-reference/functions/DrawingModule.html +8 -0
- package/doc/api-reference/functions/IndicatorPanel.html +2 -0
- package/doc/api-reference/functions/IndicatorTag.html +2 -0
- package/doc/api-reference/functions/KlineTypeSelector.html +1 -0
- package/doc/api-reference/functions/LayoutSwitcher.html +1 -0
- package/doc/api-reference/functions/PeriodSelector.html +1 -0
- package/doc/api-reference/functions/QLChartLayout.html +1 -0
- package/doc/api-reference/functions/QLChartPanel.html +10 -0
- package/doc/api-reference/functions/QLChartProvider.html +2 -0
- package/doc/api-reference/functions/QLToolbar.html +1 -0
- package/doc/api-reference/functions/ReplayController.html +1 -0
- package/doc/api-reference/functions/TimeBarModule.html +4 -0
- package/doc/api-reference/functions/TimeRangeSelector.html +1 -0
- package/doc/api-reference/functions/createIndicatorConfig.html +2 -0
- package/doc/api-reference/functions/getToolConfig.html +2 -0
- package/doc/api-reference/functions/getToolsByCategory.html +2 -0
- package/doc/api-reference/functions/getToolsByPriority.html +2 -0
- package/doc/api-reference/functions/mapLibTypeToOurs.html +2 -0
- package/doc/api-reference/functions/mapToolTypeToLib.html +3 -0
- package/doc/api-reference/functions/transformCandlestickData.html +3 -0
- package/doc/api-reference/functions/transformIndicatorData.html +2 -0
- package/doc/api-reference/functions/transformVolumeData.html +3 -0
- package/doc/api-reference/functions/useChart.html +4 -0
- package/doc/api-reference/functions/useChartStore.html +8 -0
- package/doc/api-reference/functions/useCrosshairSync.html +8 -0
- package/doc/api-reference/functions/useDrawingModule.html +1 -0
- package/doc/api-reference/functions/useDrawingStore.html +8 -0
- package/doc/api-reference/functions/useIndicatorStore.html +8 -0
- package/doc/api-reference/functions/useQLChartConfig.html +2 -0
- package/doc/api-reference/functions/useReplayStore.html +8 -0
- package/doc/api-reference/functions/useTheme.html +2 -0
- package/doc/api-reference/functions/useTimeBarStore.html +8 -0
- package/doc/api-reference/index.html +1 -0
- package/doc/api-reference/interfaces/CandlestickData.html +7 -0
- package/doc/api-reference/interfaces/CandlestickRawData.html +8 -0
- package/doc/api-reference/interfaces/ChartFunctionMenuProps.html +2 -0
- package/doc/api-reference/interfaces/ChartManagerCreateOptions.html +4 -0
- package/doc/api-reference/interfaces/ChartOptions.html +8 -0
- package/doc/api-reference/interfaces/ChartRequestParams.html +8 -0
- package/doc/api-reference/interfaces/ChartResponse.html +5 -0
- package/doc/api-reference/interfaces/ChartState.html +24 -0
- package/doc/api-reference/interfaces/ChartThemeOptions.html +5 -0
- package/doc/api-reference/interfaces/CrosshairData.html +5 -0
- package/doc/api-reference/interfaces/DrawingModuleProps.html +5 -0
- package/doc/api-reference/interfaces/DrawingState.html +48 -0
- package/doc/api-reference/interfaces/HistogramData.html +5 -0
- package/doc/api-reference/interfaces/HollowCandlestickData.html +14 -0
- package/doc/api-reference/interfaces/IndicatorConfig.html +11 -0
- package/doc/api-reference/interfaces/IndicatorDataPoint.html +4 -0
- package/doc/api-reference/interfaces/IndicatorDataResponse.html +5 -0
- package/doc/api-reference/interfaces/IndicatorDefinition.html +9 -0
- package/doc/api-reference/interfaces/IndicatorPanelProps.html +3 -0
- package/doc/api-reference/interfaces/IndicatorParamDef.html +8 -0
- package/doc/api-reference/interfaces/IndicatorRawData.html +4 -0
- package/doc/api-reference/interfaces/IndicatorState.html +19 -0
- package/doc/api-reference/interfaces/IndicatorTagProps.html +2 -0
- package/doc/api-reference/interfaces/KlineReplayOptions.html +4 -0
- package/doc/api-reference/interfaces/LayoutSwitcherProps.html +3 -0
- package/doc/api-reference/interfaces/LineData.html +4 -0
- package/doc/api-reference/interfaces/MockDataConfig.html +8 -0
- package/doc/api-reference/interfaces/MockIndicatorConfig.html +5 -0
- package/doc/api-reference/interfaces/PairInfo.html +6 -0
- package/doc/api-reference/interfaces/PaneInfo.html +6 -0
- package/doc/api-reference/interfaces/PanelConfig.html +9 -0
- package/doc/api-reference/interfaces/PersistenceConfig.html +12 -0
- package/doc/api-reference/interfaces/QLChartConfig.html +18 -0
- package/doc/api-reference/interfaces/QLChartLayoutProps.html +9 -0
- package/doc/api-reference/interfaces/QLChartPanelProps.html +13 -0
- package/doc/api-reference/interfaces/QLChartPanelRef.html +14 -0
- package/doc/api-reference/interfaces/QLToolbarProps.html +7 -0
- package/doc/api-reference/interfaces/RealtimeCandle.html +9 -0
- package/doc/api-reference/interfaces/RealtimeSubscribeFn.html +2 -0
- package/doc/api-reference/interfaces/RenkoData.html +16 -0
- package/doc/api-reference/interfaces/ReplayControllerProps.html +2 -0
- package/doc/api-reference/interfaces/ReplayState.html +19 -0
- package/doc/api-reference/interfaces/ThemeConfig.html +14 -0
- package/doc/api-reference/interfaces/TimeBarModuleProps.html +5 -0
- package/doc/api-reference/interfaces/TimeBarState.html +13 -0
- package/doc/api-reference/interfaces/UseChartReturn.html +4 -0
- package/doc/api-reference/interfaces/UseDrawingModuleOptions.html +11 -0
- package/doc/api-reference/interfaces/UseDrawingModuleReturn.html +6 -0
- package/doc/api-reference/interfaces/UseThemeReturn.html +5 -0
- package/doc/api-reference/types/EventHandler.html +2 -0
- package/doc/api-reference/types/FetchFn.html +2 -0
- package/doc/api-reference/types/LayoutMode.html +2 -0
- package/doc/api-reference/types/MarketTrend.html +2 -0
- package/doc/api-reference/types/ThemePreset.html +2 -0
- package/doc/api-reference/variables/BUILTIN_INDICATORS.html +2 -0
- package/doc/api-reference/variables/CATEGORY_LABELS.html +2 -0
- package/doc/api-reference/variables/DRAWING_TOOLS.html +3 -0
- package/doc/api-reference/variables/MARKET_PRESETS.html +1 -0
- package/doc/api-reference/variables/PAIR_PRESETS.html +1 -0
- package/doc/api-reference/variables/darkPreset.html +1 -0
- package/doc/api-reference/variables/lightPreset.html +1 -0
- package/doc/components/drawing-module.md +24 -0
- package/doc/components/indicator-list-panel.md +24 -0
- package/doc/components/indicator-panel.md +17 -0
- package/doc/components/pane-divider.md +25 -0
- package/doc/components/qlchart-layout.md +30 -0
- package/doc/components/qlchart-panel.md +93 -0
- package/doc/components/qlchart-provider.md +73 -0
- package/doc/components/qltoolbar.md +17 -0
- package/doc/components/replay-controller.md +23 -0
- package/doc/components/timebar-module.md +13 -0
- package/doc/core/chart-manager.md +14 -0
- package/doc/core/data-manager.md +33 -0
- package/doc/core/event-manager.md +26 -0
- package/doc/core/pane-manager.md +13 -0
- package/doc/core/series-manager.md +19 -0
- package/doc/core/theme-manager.md +21 -0
- package/doc/examples/basic-chart.md +24 -0
- package/doc/examples/data-format-guide.md +119 -0
- package/doc/examples/drawing-tools.md +30 -0
- package/doc/examples/indicator-properties.md +34 -0
- package/doc/examples/multi-pane.md +24 -0
- package/doc/examples/multi-panel.md +23 -0
- package/doc/examples/realtime-data.md +147 -0
- package/doc/examples/standalone-js.md +333 -0
- package/doc/guide/architecture.md +87 -0
- package/doc/guide/data-flow.md +310 -0
- package/doc/guide/deployment.md +59 -0
- package/doc/guide/drawing-properties.md +40 -0
- package/doc/guide/getting-started.md +94 -0
- package/doc/guide/pane-system.md +47 -0
- package/doc/guide/theme-switching.md +58 -0
- package/doc/hooks/use-chart.md +20 -0
- package/doc/hooks/use-crosshair-sync.md +14 -0
- package/doc/hooks/use-drawing-module.md +43 -0
- package/doc/hooks/use-theme.md +15 -0
- package/doc/index.md +33 -0
- package/doc/plugins/drawing/overview.md +36 -0
- package/doc/plugins/drawing/persistence.md +42 -0
- package/doc/plugins/drawing/tool-registry.md +29 -0
- package/doc/plugins/hollow-candlestick.md +18 -0
- package/doc/plugins/indicators.md +28 -0
- package/doc/plugins/renko.md +17 -0
- package/doc/plugins/replay.md +21 -0
- package/doc/plugins/screenshot.md +20 -0
- package/docs/api.md +94 -0
- package/package.json +54 -0
- package/python/qlchart/__init__.py +9 -0
- package/python/qlchart/__pycache__/__init__.cpython-311.pyc +0 -0
- package/python/qlchart/__pycache__/chart.cpython-311.pyc +0 -0
- package/python/qlchart/chart.py +333 -0
- package/python/qlchart/templates/chart_template.html +304 -0
- package/python/requirements.txt +1 -0
- package/python/setup.py +18 -0
- package/python/tests/__init__.py +1 -0
- package/python/tests/__pycache__/__init__.cpython-311.pyc +0 -0
- package/python/tests/__pycache__/test_chart.cpython-311-pytest-8.3.3.pyc +0 -0
- package/python/tests/test_chart.py +114 -0
- package/quantlife-qlchart-0.0.1.tgz +0 -0
- package/src/api/chartApi.ts +30 -0
- package/src/api/indicatorApi.ts +27 -0
- package/src/components/ChartFunctionMenu.tsx +64 -0
- package/src/components/PaneChartPanel.tsx +116 -0
- package/src/components/PaneDivider.tsx +66 -0
- package/src/components/QLChartLayout.tsx +151 -0
- package/src/components/QLChartPanel.tsx +560 -0
- package/src/components/QLChartProvider.tsx +90 -0
- package/src/components/context-menu/ChartContextMenu.tsx +139 -0
- package/src/components/context-menu/index.ts +2 -0
- package/src/components/drawing/DrawingModule.tsx +36 -0
- package/src/components/drawing/DrawingPropertyPanel.tsx +347 -0
- package/src/components/drawing/DrawingToolbar.tsx +305 -0
- package/src/components/drawing/index.ts +5 -0
- package/src/components/index.ts +43 -0
- package/src/components/indicator/IndicatorListPanel.tsx +94 -0
- package/src/components/indicator/IndicatorModal.tsx +171 -0
- package/src/components/indicator/IndicatorPanel.tsx +9 -0
- package/src/components/indicator/IndicatorPropertyPanel.tsx +130 -0
- package/src/components/indicator/IndicatorTag.tsx +173 -0
- package/src/components/indicator/index.ts +4 -0
- package/src/components/replay/ReplayController.css +97 -0
- package/src/components/replay/ReplayController.tsx +138 -0
- package/src/components/timebar/TimeBarModule.tsx +30 -0
- package/src/components/timebar/TimeRangeSelector.tsx +96 -0
- package/src/components/timebar/index.ts +3 -0
- package/src/components/toolbar/GlobalToolbar.tsx +58 -0
- package/src/components/toolbar/KlineTypeSelector.tsx +123 -0
- package/src/components/toolbar/LayoutSwitcher.tsx +45 -0
- package/src/components/toolbar/PeriodSelector.tsx +35 -0
- package/src/components/toolbar/QLToolbar.tsx +71 -0
- package/src/components/toolbar/TimeRangeSelector.tsx +89 -0
- package/src/components/ui/Modal.tsx +67 -0
- package/src/core/ChartManager.ts +95 -0
- package/src/core/DataManager.ts +427 -0
- package/src/core/EventManager.ts +63 -0
- package/src/core/IndicatorDataProcessor.ts +104 -0
- package/src/core/PaneManager.ts +121 -0
- package/src/core/RealtimeDataFeed.ts +110 -0
- package/src/core/SeriesManager.ts +210 -0
- package/src/core/ThemeManager.ts +59 -0
- package/src/core/index.ts +10 -0
- package/src/css.d.ts +4 -0
- package/src/hooks/useChart.ts +62 -0
- package/src/hooks/useCrosshairSync.ts +109 -0
- package/src/hooks/useDrawingModule.ts +475 -0
- package/src/hooks/useTheme.ts +31 -0
- package/src/index.ts +170 -0
- package/src/mock/MockDataService.ts +102 -0
- package/src/mock/MockIndicatorService.ts +40 -0
- package/src/mock/index.ts +5 -0
- package/src/mock/presets.ts +16 -0
- package/src/plugins/drawing/DrawingAdapter.ts +1762 -0
- package/src/plugins/drawing/DrawingPersistence.ts +273 -0
- package/src/plugins/drawing/DrawingPropertyTemplates.ts +327 -0
- package/src/plugins/drawing/DrawingSharedService.ts +125 -0
- package/src/plugins/drawing/DrawingToolRegistry.ts +684 -0
- package/src/plugins/drawing/TextLabelOverlay.ts +101 -0
- package/src/plugins/drawing/colorUtils.ts +53 -0
- package/src/plugins/drawing/index.ts +10 -0
- package/src/plugins/drawing/lineStyleMap.ts +46 -0
- package/src/plugins/drawing/migration.ts +105 -0
- package/src/plugins/drawing/patterns/PatternDefinitions.ts +57 -0
- package/src/plugins/drawing/patterns/index.ts +2 -0
- package/src/plugins/drawing/periodUtils.ts +51 -0
- package/src/plugins/indicators/AutoIndicatorRenderer.ts +204 -0
- package/src/plugins/indicators/IndicatorRenderer.ts +350 -0
- package/src/plugins/indicators/OverlayIndicator.ts +114 -0
- package/src/plugins/indicators/PaneIndicator.ts +137 -0
- package/src/plugins/indicators/index.ts +4 -0
- package/src/plugins/replay/KlineReplay.ts +163 -0
- package/src/plugins/replay/index.ts +2 -0
- package/src/plugins/screenshot/ScreenshotUtil.ts +123 -0
- package/src/plugins/screenshot/index.ts +1 -0
- package/src/plugins/series/HollowCandlestickSeries.ts +111 -0
- package/src/plugins/series/RenkoSeries.ts +104 -0
- package/src/plugins/series/VolumeCandlestickSeries.ts +127 -0
- package/src/plugins/series/index.ts +6 -0
- package/src/standalone.ts +386 -0
- package/src/store/useChartStore.ts +101 -0
- package/src/store/useDrawingStore.ts +135 -0
- package/src/store/useIndicatorStore.ts +100 -0
- package/src/store/usePanelRegistry.ts +50 -0
- package/src/store/useReplayStore.ts +42 -0
- package/src/store/useTimeBarStore.ts +34 -0
- package/src/styles/chart.css +312 -0
- package/src/styles/components.css +184 -0
- package/src/styles/context-menu.css +60 -0
- package/src/styles/drawing.css +524 -0
- package/src/styles/indicator-modal.css +216 -0
- package/src/styles/indicator-tag.css +210 -0
- package/src/styles/pane-chart.css +9 -0
- package/src/styles/responsive.css +71 -0
- package/src/styles/themes/dark.css +63 -0
- package/src/styles/themes/light.css +61 -0
- package/src/styles/toolbar.css +129 -0
- package/src/types/api.ts +36 -0
- package/src/types/chart.ts +44 -0
- package/src/types/drawing.ts +265 -0
- package/src/types/index.ts +40 -0
- package/src/types/indicator.ts +344 -0
- package/src/types/series.ts +53 -0
- package/src/types/theme.ts +48 -0
- package/src/utils/dataTransformer.ts +63 -0
- package/src/utils/heikinAshi.ts +41 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/lineBreak.ts +88 -0
- package/src/utils/themePresets.ts +69 -0
- package/src/utils/timeFormatter.ts +29 -0
- package/src/utils/timeScaleUtils.ts +68 -0
- package/tsconfig.json +21 -0
- package/typedoc.json +10 -0
- package/vite.config.standalone.ts +31 -0
- 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
|
+
}
|