@aura-react-comp/charts 0.3.1 → 0.4.0

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 (68) hide show
  1. package/esm/charts/area-chart/index.js +27 -23
  2. package/esm/charts/bar-chart/index.js +17 -21
  3. package/esm/charts/candlestick-chart/index.js +242 -180
  4. package/esm/charts/combo-chart/index.js +8 -20
  5. package/esm/charts/funnel-chart/index.js +133 -108
  6. package/esm/charts/gauge-chart/index.js +102 -105
  7. package/esm/charts/graph-chart/index.js +154 -114
  8. package/esm/charts/heatmap-chart/index.js +179 -135
  9. package/esm/charts/line-chart/index.js +28 -24
  10. package/esm/charts/pie-chart/index.d.ts.map +1 -1
  11. package/esm/charts/pie-chart/index.js +198 -139
  12. package/esm/charts/radar-chart/index.d.ts.map +1 -1
  13. package/esm/charts/radar-chart/index.js +154 -128
  14. package/esm/charts/ring-progress/index.js +81 -82
  15. package/esm/charts/sankey-chart/index.js +171 -141
  16. package/esm/charts/scatter-chart/index.d.ts.map +1 -1
  17. package/esm/charts/scatter-chart/index.js +225 -159
  18. package/esm/charts/sparkline/index.js +81 -66
  19. package/esm/charts/tree-chart/index.js +128 -102
  20. package/esm/charts/treemap-chart/index.js +162 -141
  21. package/esm/charts/waterfall-chart/index.js +165 -121
  22. package/esm/components/Legend.d.ts +3 -2
  23. package/esm/components/Legend.d.ts.map +1 -1
  24. package/esm/components/Legend.js +79 -48
  25. package/esm/components/Tooltip.js +62 -71
  26. package/esm/components/chart/index.d.ts +4 -1
  27. package/esm/components/chart/index.d.ts.map +1 -1
  28. package/esm/components/chart/index.js +80 -102
  29. package/esm/components/chart.less +13 -0
  30. package/esm/core/animation.js +31 -43
  31. package/esm/core/export.d.ts +34 -0
  32. package/esm/core/export.d.ts.map +1 -0
  33. package/esm/core/export.js +390 -0
  34. package/esm/core/force.js +22 -45
  35. package/esm/core/format.js +48 -52
  36. package/esm/core/hierarchy.js +60 -92
  37. package/esm/core/layout.js +32 -50
  38. package/esm/core/sampling.js +30 -53
  39. package/esm/core/sankey.js +35 -55
  40. package/esm/core/scale.js +72 -86
  41. package/esm/core/shape.js +101 -133
  42. package/esm/core/sync.d.ts +41 -0
  43. package/esm/core/sync.d.ts.map +1 -0
  44. package/esm/core/sync.js +120 -0
  45. package/esm/hooks/useChartExport.d.ts +19 -0
  46. package/esm/hooks/useChartExport.d.ts.map +1 -0
  47. package/esm/hooks/useChartExport.js +35 -0
  48. package/esm/hooks/useChartTheme.js +17 -19
  49. package/esm/hooks/useChartTransition.js +58 -73
  50. package/esm/hooks/useElementSize.js +19 -28
  51. package/esm/hooks/usePlotPointer.d.ts +23 -0
  52. package/esm/hooks/usePlotPointer.d.ts.map +1 -0
  53. package/esm/hooks/usePlotPointer.js +94 -0
  54. package/esm/index.d.ts +6 -0
  55. package/esm/index.d.ts.map +1 -1
  56. package/esm/index.js +136 -47
  57. package/esm/internal/CartesianChart.d.ts.map +1 -1
  58. package/esm/internal/CartesianChart.js +666 -485
  59. package/esm/internal/model.js +44 -60
  60. package/esm/scene/canvas.js +100 -107
  61. package/esm/scene/svg.js +106 -99
  62. package/esm/scene/types.js +8 -15
  63. package/esm/style.css +10 -0
  64. package/esm/theme/index.js +143 -171
  65. package/esm/types.d.ts +4 -0
  66. package/esm/types.d.ts.map +1 -1
  67. package/esm/types.js +0 -1
  68. package/package.json +1 -1
@@ -0,0 +1,120 @@
1
+ 'use client';
2
+ // src/core/sync.tsx
3
+ import {
4
+ createContext,
5
+ useCallback,
6
+ useContext,
7
+ useEffect,
8
+ useId,
9
+ useRef,
10
+ useState
11
+ } from "react";
12
+ import { jsx } from "react/jsx-runtime";
13
+ function isSamePayload(a, b) {
14
+ if (a === b)
15
+ return true;
16
+ if (!a || !b)
17
+ return false;
18
+ return a.sourceId === b.sourceId && a.index === b.index && a.label === b.label;
19
+ }
20
+ function createChartSyncStore() {
21
+ const channels = /* @__PURE__ */ new Map();
22
+ const getOrCreate = (group) => {
23
+ let channel = channels.get(group);
24
+ if (!channel) {
25
+ channel = { state: null, listeners: /* @__PURE__ */ new Set() };
26
+ channels.set(group, channel);
27
+ }
28
+ return channel;
29
+ };
30
+ return {
31
+ subscribe(group, listener) {
32
+ const channel = getOrCreate(group);
33
+ channel.listeners.add(listener);
34
+ return () => {
35
+ channel.listeners.delete(listener);
36
+ if (channel.listeners.size === 0) {
37
+ channels.delete(group);
38
+ }
39
+ };
40
+ },
41
+ publish(group, payload) {
42
+ const channel = channels.get(group);
43
+ if (!channel)
44
+ return;
45
+ if (isSamePayload(channel.state, payload))
46
+ return;
47
+ channel.state = payload;
48
+ channel.listeners.forEach((listener) => listener(payload));
49
+ },
50
+ getSnapshot(group) {
51
+ var _a;
52
+ return ((_a = channels.get(group)) == null ? void 0 : _a.state) ?? null;
53
+ }
54
+ };
55
+ }
56
+ var defaultSyncStore = createChartSyncStore();
57
+ var ChartSyncContext = createContext(defaultSyncStore);
58
+ function ChartSyncProvider({ children }) {
59
+ const storeRef = useRef(null);
60
+ if (!storeRef.current) {
61
+ storeRef.current = createChartSyncStore();
62
+ }
63
+ return /* @__PURE__ */ jsx(ChartSyncContext.Provider, { value: storeRef.current, children });
64
+ }
65
+ ChartSyncProvider.displayName = "ChartSyncProvider";
66
+ function useChartSync(group) {
67
+ const store = useContext(ChartSyncContext);
68
+ const sourceId = useId();
69
+ const [synced, setSynced] = useState(null);
70
+ useEffect(() => {
71
+ if (!group) {
72
+ setSynced(null);
73
+ return void 0;
74
+ }
75
+ const initial = store.getSnapshot(group);
76
+ if (initial && initial.sourceId !== sourceId && initial.index !== null) {
77
+ setSynced(initial);
78
+ } else {
79
+ setSynced(null);
80
+ }
81
+ const unsubscribe = store.subscribe(group, (payload) => {
82
+ if (!payload || payload.sourceId === sourceId)
83
+ return;
84
+ setSynced(payload.index === null ? null : payload);
85
+ });
86
+ return () => {
87
+ unsubscribe();
88
+ const latest = store.getSnapshot(group);
89
+ if (latest && latest.sourceId === sourceId && latest.index !== null) {
90
+ store.publish(group, { sourceId, index: null });
91
+ }
92
+ };
93
+ }, [store, group, sourceId]);
94
+ const emit = useCallback(
95
+ (index, label) => {
96
+ if (!group)
97
+ return;
98
+ if (index === null) {
99
+ const latest = store.getSnapshot(group);
100
+ if (latest && latest.sourceId !== sourceId)
101
+ return;
102
+ }
103
+ store.publish(group, { sourceId, index, label });
104
+ },
105
+ [store, group, sourceId]
106
+ );
107
+ const clear = useCallback(() => {
108
+ if (!group)
109
+ return;
110
+ const latest = store.getSnapshot(group);
111
+ if (latest && latest.sourceId !== sourceId)
112
+ return;
113
+ store.publish(group, { sourceId, index: null });
114
+ }, [store, group, sourceId]);
115
+ return { sourceId, synced, emit, clear };
116
+ }
117
+ export {
118
+ ChartSyncProvider,
119
+ useChartSync
120
+ };
@@ -0,0 +1,19 @@
1
+ /// <reference types="react" />
2
+ import { type DownloadChartOptions, type ExportChartOptions } from '../core/export.js';
3
+ /** useChartExport 返回值 */
4
+ export interface UseChartExportResult<T extends HTMLElement = HTMLDivElement> {
5
+ /** 绑定到图表外层容器的 ref */
6
+ containerRef: React.RefObject<T>;
7
+ /** 导出独立 SVG 字符串 */
8
+ exportSvg: (options?: ExportChartOptions) => string;
9
+ /** 导出 DataURL(支持 png / jpeg / svg) */
10
+ exportDataUrl: (options?: ExportChartOptions) => Promise<string>;
11
+ /** 触发浏览器下载图表文件 */
12
+ download: (options?: DownloadChartOptions) => Promise<string>;
13
+ }
14
+ /**
15
+ * 图表导出轻量 Hook。
16
+ * 将返回的 `containerRef` 挂载到图表外层容器,即可一键调用 SVG / PNG / JPEG 导出与下载。
17
+ */
18
+ export declare function useChartExport<T extends HTMLElement = HTMLDivElement>(): UseChartExportResult<T>;
19
+ //# sourceMappingURL=useChartExport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useChartExport.d.ts","sourceRoot":"","sources":["useChartExport.ts"],"names":[],"mappings":";AACA,OAAO,EAIL,KAAK,oBAAoB,EACzB,KAAK,kBAAkB,EACxB,MAAM,gBAAgB,CAAC;AAExB,yBAAyB;AACzB,MAAM,WAAW,oBAAoB,CAAC,CAAC,SAAS,WAAW,GAAG,cAAc;IAC1E,qBAAqB;IACrB,YAAY,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;IACjC,mBAAmB;IACnB,SAAS,EAAE,CAAC,OAAO,CAAC,EAAE,kBAAkB,KAAK,MAAM,CAAC;IACpD,sCAAsC;IACtC,aAAa,EAAE,CAAC,OAAO,CAAC,EAAE,kBAAkB,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IACjE,kBAAkB;IAClB,QAAQ,EAAE,CAAC,OAAO,CAAC,EAAE,oBAAoB,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CAC/D;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAC5B,CAAC,SAAS,WAAW,GAAG,cAAc,KACnC,oBAAoB,CAAC,CAAC,CAAC,CA0B3B"}
@@ -0,0 +1,35 @@
1
+ 'use client';
2
+ // src/hooks/useChartExport.ts
3
+ import { useCallback, useRef } from "react";
4
+ import {
5
+ downloadChart,
6
+ exportChartDataUrl,
7
+ exportChartSvg
8
+ } from "../core/export";
9
+ function useChartExport() {
10
+ const containerRef = useRef(null);
11
+ const exportSvg = useCallback((options) => {
12
+ const el = containerRef.current;
13
+ if (!el)
14
+ throw new Error("图表导出失败:containerRef 尚未挂载");
15
+ return exportChartSvg(el, options);
16
+ }, []);
17
+ const exportDataUrl = useCallback((options) => {
18
+ const el = containerRef.current;
19
+ if (!el) {
20
+ return Promise.reject(new Error("图表导出失败:containerRef 尚未挂载"));
21
+ }
22
+ return exportChartDataUrl(el, options);
23
+ }, []);
24
+ const download = useCallback((options) => {
25
+ const el = containerRef.current;
26
+ if (!el) {
27
+ return Promise.reject(new Error("图表导出失败:containerRef 尚未挂载"));
28
+ }
29
+ return downloadChart(el, options);
30
+ }, []);
31
+ return { containerRef, exportSvg, exportDataUrl, download };
32
+ }
33
+ export {
34
+ useChartExport
35
+ };
@@ -1,35 +1,33 @@
1
1
  'use client';
2
- import { useEffect, useState } from 'react';
2
+ // src/hooks/useChartTheme.ts
3
+ import { useEffect, useState } from "react";
3
4
  import { resolveTheme } from "../theme";
4
-
5
- /**
6
- * 解析图表主题,并在主题属性变化时重新读取。
7
- *
8
- * 为什么需要订阅:Aura 的主题是 `document.documentElement` 上的双轴属性
9
- * (`data-mode`=明暗 × `data-palette`=色板,见 ui/theme/tokens.css)。
10
- * 图表颜色若是挂载时读一次,用户点切换后图表会停在旧配色 ——
11
- * 视觉上和整站脱节。旧的 `data-theme` 键继续监听以兼容历史用法。
12
- */
13
- export function useChartTheme(input, palette) {
5
+ function useChartTheme(input, palette) {
14
6
  const [theme, setTheme] = useState(() => resolveTheme(input));
15
7
  useEffect(() => {
16
8
  const update = () => {
17
9
  const next = resolveTheme(input);
18
- setTheme(palette && palette.length ? {
19
- ...next,
20
- palette
21
- } : next);
10
+ setTheme(palette && palette.length ? { ...next, palette } : next);
22
11
  };
23
12
  update();
24
- if (typeof MutationObserver === 'undefined' || typeof document === 'undefined') {
25
- return undefined;
13
+ if (typeof MutationObserver === "undefined" || typeof document === "undefined") {
14
+ return void 0;
26
15
  }
27
16
  const observer = new MutationObserver(update);
28
17
  observer.observe(document.documentElement, {
29
18
  attributes: true,
30
- attributeFilter: ['data-mode', 'data-palette', 'data-theme', 'class', 'style']
19
+ attributeFilter: [
20
+ "data-mode",
21
+ "data-palette",
22
+ "data-theme",
23
+ "class",
24
+ "style"
25
+ ]
31
26
  });
32
27
  return () => observer.disconnect();
33
28
  }, [input, palette]);
34
29
  return theme;
35
- }
30
+ }
31
+ export {
32
+ useChartTheme
33
+ };
@@ -1,120 +1,105 @@
1
1
  'use client';
2
- import { useEffect, useReducer, useRef } from 'react';
2
+ // src/hooks/useChartTransition.ts
3
+ import { useEffect, useReducer, useRef } from "react";
3
4
  import { hashMatrix } from "../core/animation";
4
5
  function prefersReducedMotion() {
5
- if (typeof window === 'undefined' || !window.matchMedia) return false;
6
- return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
6
+ if (typeof window === "undefined" || !window.matchMedia)
7
+ return false;
8
+ return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
7
9
  }
8
-
9
- /**
10
- * 图表动画状态机。
11
- *
12
- * 只做两件事,且两者互斥,避免叠加出双重缓动:
13
- * · **首次挂载** → 推进 `progress`(0→1),数值保持目标值,
14
- * 由各图表用 staggerProgress 把 progress 映射成逐图元的位置;
15
- * · **数据更新** → `progress` 恒为 1,改为在「当前显示值 → 新值」之间插值。
16
- *
17
- * 之前每次数据变化都重播入场,是「生硬」观感的主要来源:
18
- * 用户改一个数字,整张图却从头长一遍。
19
- */
20
- export function useChartTransition(matrix, animation) {
10
+ function useChartTransition(matrix, animation) {
21
11
  const hash = hashMatrix(matrix);
22
12
  const displayRef = useRef(matrix);
23
13
  const progressRef = useRef(0);
24
14
  const mountedRef = useRef(false);
25
- const [, forceRender] = useReducer(tick => tick + 1, 0);
15
+ const [, forceRender] = useReducer((tick) => tick + 1, 0);
26
16
  useEffect(() => {
27
- /** 直接落到终态:无动画、时长非法、命中「减少动态效果」偏好、环境无 rAF */
28
17
  const settle = () => {
29
18
  displayRef.current = matrix;
30
19
  progressRef.current = 1;
31
20
  mountedRef.current = true;
32
21
  forceRender();
33
22
  };
34
-
35
- // 页面不可见时不做动画:后台标签页的 rAF 会被浏览器节流甚至暂停,
36
- // 此时推进动画既无意义,还可能让图表停在起始帧
37
- const hidden = typeof document !== 'undefined' && document.visibilityState === 'hidden';
38
- if (!animation.enabled || animation.duration <= 0 || prefersReducedMotion() || hidden || typeof requestAnimationFrame === 'undefined') {
23
+ const hidden = typeof document !== "undefined" && document.visibilityState === "hidden";
24
+ if (!animation.enabled || animation.duration <= 0 || prefersReducedMotion() || hidden || typeof requestAnimationFrame === "undefined") {
39
25
  settle();
40
- return undefined;
26
+ return void 0;
41
27
  }
42
28
  const entering = !mountedRef.current;
43
29
  mountedRef.current = true;
44
30
  const from = entering ? matrix : displayRef.current;
45
31
  const to = matrix;
46
-
47
- /**
48
- * 形状变化(行数或列数不同)时不做插值。
49
- * 典型场景是 dataZoom 切换窗口:点数量变了,新旧数据之间没有对应关系,
50
- * 硬插值会让图「从 0 长出来」,比直接切换更难看。
51
- */
52
- const sameShape = from.length === to.length && to.every((row, index) => row.length === (from[index]?.length ?? -1));
32
+ const sameShape = from.length === to.length && to.every((row, index) => {
33
+ var _a;
34
+ return row.length === (((_a = from[index]) == null ? void 0 : _a.length) ?? -1);
35
+ });
53
36
  if (entering) {
54
37
  progressRef.current = 0;
55
38
  } else if (!sameShape) {
56
39
  displayRef.current = to;
57
40
  progressRef.current = 1;
58
41
  forceRender();
59
- return undefined;
42
+ return void 0;
60
43
  }
61
44
  let frame = 0;
62
45
  let cancelled = false;
63
- /**
64
- * 时间基准用 Date.now()(墙钟)。
65
- *
66
- * 试过 rAF 回调的时间戳与 performance.now():浏览器里两者同源,
67
- * 但 jsdom 等测试环境的时钟是冻结的,每帧拿到同一个值,
68
- * 进度会永远停在 0 —— 动画在测试里完全不推进,也就无法断言。
69
- * 600ms 量级用 1ms 精度完全够。
70
- */
71
46
  const startAt = Date.now() + animation.delay;
72
47
  const tick = () => {
73
- if (cancelled) return;
74
- const raw = Math.max(0, Math.min(1, (Date.now() - startAt) / animation.duration));
48
+ if (cancelled)
49
+ return;
50
+ const raw = Math.max(
51
+ 0,
52
+ Math.min(1, (Date.now() - startAt) / animation.duration)
53
+ );
75
54
  const eased = animation.ease(raw);
76
55
  if (entering) {
77
- // 入场:只推进进度,数值就是目标值
78
56
  progressRef.current = eased;
79
57
  displayRef.current = to;
80
58
  } else {
81
- // 更新:逐格插值。null 不参与插值(缺失就是缺失)
82
- displayRef.current = to.map((row, rowIndex) => row.map((target, columnIndex) => {
83
- const previous = from[rowIndex]?.[columnIndex] ?? target ?? 0;
84
- if (target === null) return previous === null ? null : previous;
85
- if (previous === null) return target;
86
- return previous + (target - previous) * eased;
87
- }));
59
+ displayRef.current = to.map(
60
+ (row, rowIndex) => row.map((target, columnIndex) => {
61
+ var _a;
62
+ const previous = ((_a = from[rowIndex]) == null ? void 0 : _a[columnIndex]) ?? target ?? 0;
63
+ if (target === null)
64
+ return previous === null ? null : previous;
65
+ if (previous === null)
66
+ return target;
67
+ return previous + (target - previous) * eased;
68
+ })
69
+ );
88
70
  progressRef.current = 1;
89
71
  }
90
72
  forceRender();
91
- if (raw < 1) frame = requestAnimationFrame(tick);
73
+ if (raw < 1)
74
+ frame = requestAnimationFrame(tick);
92
75
  };
93
76
  frame = requestAnimationFrame(tick);
94
-
95
- /**
96
- * 兜底:rAF 在某些环境不保证持续触发(无头浏览器、后台标签页、
97
- * 窗口最小化、某些低功耗模式)。只靠 rAF,图表会永远停在起始帧 ——
98
- * 表现为「所有图元贴在基线上」,比没有动画严重得多。
99
- * 因此设一个略长于时长的定时器强制落到终态。
100
- */
101
- const failsafe = setTimeout(() => {
102
- if (cancelled) return;
103
- cancelled = true;
104
- progressRef.current = 1;
105
- displayRef.current = to;
106
- forceRender();
107
- }, animation.delay + animation.duration + 100);
77
+ const failsafe = setTimeout(
78
+ () => {
79
+ if (cancelled)
80
+ return;
81
+ cancelled = true;
82
+ progressRef.current = 1;
83
+ displayRef.current = to;
84
+ forceRender();
85
+ },
86
+ animation.delay + animation.duration + 100
87
+ );
108
88
  return () => {
109
89
  cancelled = true;
110
90
  cancelAnimationFrame(frame);
111
91
  clearTimeout(failsafe);
112
92
  };
113
- // hash 变化 = 数据变了;其余依赖变化 = 动画参数变了
114
- // eslint-disable-next-line react-hooks/exhaustive-deps
115
- }, [hash, animation.enabled, animation.duration, animation.easing, animation.stagger, animation.delay]);
116
- return {
117
- values: displayRef.current,
118
- progress: progressRef.current
119
- };
120
- }
93
+ }, [
94
+ hash,
95
+ animation.enabled,
96
+ animation.duration,
97
+ animation.easing,
98
+ animation.stagger,
99
+ animation.delay
100
+ ]);
101
+ return { values: displayRef.current, progress: progressRef.current };
102
+ }
103
+ export {
104
+ useChartTransition
105
+ };
@@ -1,47 +1,38 @@
1
1
  'use client';
2
- import { useCallback, useEffect, useRef, useState } from 'react';
3
- /**
4
- * 监听元素尺寸。
5
- *
6
- * 图表必须能跟随容器变化(响应式栅格里容器宽度是被父元素决定的),
7
- * 因此不用 window.resize(弹窗/抽屉里不会触发),而用 ResizeObserver。
8
- */
9
- export function useElementSize() {
2
+ // src/hooks/useElementSize.ts
3
+ import { useCallback, useEffect, useRef, useState } from "react";
4
+ function useElementSize() {
10
5
  const ref = useRef(null);
11
- const [size, setSize] = useState({
12
- width: 0,
13
- height: 0
14
- });
6
+ const [size, setSize] = useState({ width: 0, height: 0 });
15
7
  const measure = useCallback(() => {
16
8
  const element = ref.current;
17
- if (!element) return;
9
+ if (!element)
10
+ return;
18
11
  const rect = element.getBoundingClientRect();
19
- setSize(prev => {
12
+ setSize((prev) => {
20
13
  const next = {
21
14
  width: Math.round(rect.width),
22
15
  height: Math.round(rect.height)
23
16
  };
24
- // 尺寸未变时不触发渲染,避免 ResizeObserver 抖动导致的重绘风暴
25
17
  return prev.width === next.width && prev.height === next.height ? prev : next;
26
18
  });
27
19
  }, []);
28
20
  useEffect(() => {
29
21
  measure();
30
- if (typeof ResizeObserver === 'undefined') {
31
- // jsdom 等环境没有 ResizeObserver,退化为一次性测量
32
- if (typeof window !== 'undefined') {
33
- window.addEventListener('resize', measure);
34
- return () => window.removeEventListener('resize', measure);
22
+ if (typeof ResizeObserver === "undefined") {
23
+ if (typeof window !== "undefined") {
24
+ window.addEventListener("resize", measure);
25
+ return () => window.removeEventListener("resize", measure);
35
26
  }
36
- return undefined;
27
+ return void 0;
37
28
  }
38
29
  const observer = new ResizeObserver(measure);
39
- if (ref.current) observer.observe(ref.current);
30
+ if (ref.current)
31
+ observer.observe(ref.current);
40
32
  return () => observer.disconnect();
41
33
  }, [measure]);
42
- return {
43
- ref,
44
- size,
45
- measure
46
- };
47
- }
34
+ return { ref, size, measure };
35
+ }
36
+ export {
37
+ useElementSize
38
+ };
@@ -0,0 +1,23 @@
1
+ export interface UsePlotPointerOptions {
2
+ /** 命中更新回调,传入屏幕坐标 (clientX, clientY) */
3
+ onMove: (clientX: number, clientY: number) => void;
4
+ /** 离开 / 触摸结束清理回调 */
5
+ onLeave: () => void;
6
+ /** 是否禁用交互(例如空数据或关闭 tooltip) */
7
+ disabled?: boolean;
8
+ }
9
+ /**
10
+ * 统一处理桌面指针与移动端触摸交互:
11
+ * - `pointermove` 跳过 `touch`(避免与触摸事件重复触发);
12
+ * - `touchstart` 立即响应,`touchmove` 经 `requestAnimationFrame` 按帧合流节流;
13
+ * - `touchend` / `touchcancel` 与卸载时自动取消挂起帧并优雅收尾。
14
+ */
15
+ export declare function usePlotPointer({ onMove, onLeave, disabled, }: UsePlotPointerOptions): {
16
+ onPointerMove: (event: React.PointerEvent<HTMLDivElement>) => void;
17
+ onPointerLeave: () => void;
18
+ onTouchStart: (event: React.TouchEvent<HTMLDivElement>) => void;
19
+ onTouchMove: (event: React.TouchEvent<HTMLDivElement>) => void;
20
+ onTouchEnd: () => void;
21
+ onTouchCancel: () => void;
22
+ };
23
+ //# sourceMappingURL=usePlotPointer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePlotPointer.d.ts","sourceRoot":"","sources":["usePlotPointer.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,qBAAqB;IACpC,uCAAuC;IACvC,MAAM,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACnD,oBAAoB;IACpB,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,+BAA+B;IAC/B,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,EAC7B,MAAM,EACN,OAAO,EACP,QAAgB,GACjB,EAAE,qBAAqB;2BAoBZ,kBAAkB,CAAC,cAAc,CAAC;;0BAalC,gBAAgB,CAAC,cAAc,CAAC;yBAWhC,gBAAgB,CAAC,cAAc,CAAC;;;EAqC3C"}
@@ -0,0 +1,94 @@
1
+ 'use client';
2
+ // src/hooks/usePlotPointer.ts
3
+ import { useCallback, useEffect, useRef } from "react";
4
+ function usePlotPointer({
5
+ onMove,
6
+ onLeave,
7
+ disabled = false
8
+ }) {
9
+ const moveRef = useRef(onMove);
10
+ moveRef.current = onMove;
11
+ const leaveRef = useRef(onLeave);
12
+ leaveRef.current = onLeave;
13
+ const rafRef = useRef(null);
14
+ const pendingRef = useRef(null);
15
+ const cancelFrame = useCallback(() => {
16
+ if (rafRef.current !== null && typeof cancelAnimationFrame === "function") {
17
+ cancelAnimationFrame(rafRef.current);
18
+ }
19
+ rafRef.current = null;
20
+ pendingRef.current = null;
21
+ }, []);
22
+ useEffect(() => cancelFrame, [cancelFrame]);
23
+ const onPointerMove = useCallback(
24
+ (event) => {
25
+ if (disabled || event.pointerType === "touch")
26
+ return;
27
+ moveRef.current(event.clientX, event.clientY);
28
+ },
29
+ [disabled]
30
+ );
31
+ const onPointerLeave = useCallback(() => {
32
+ if (disabled)
33
+ return;
34
+ leaveRef.current();
35
+ }, [disabled]);
36
+ const onTouchStart = useCallback(
37
+ (event) => {
38
+ var _a;
39
+ if (disabled)
40
+ return;
41
+ const touch = (_a = event.touches) == null ? void 0 : _a[0];
42
+ if (!touch)
43
+ return;
44
+ cancelFrame();
45
+ moveRef.current(touch.clientX, touch.clientY);
46
+ },
47
+ [disabled, cancelFrame]
48
+ );
49
+ const onTouchMove = useCallback(
50
+ (event) => {
51
+ var _a;
52
+ if (disabled)
53
+ return;
54
+ const touch = (_a = event.touches) == null ? void 0 : _a[0];
55
+ if (!touch)
56
+ return;
57
+ pendingRef.current = { x: touch.clientX, y: touch.clientY };
58
+ if (rafRef.current !== null)
59
+ return;
60
+ if (typeof requestAnimationFrame === "function") {
61
+ rafRef.current = requestAnimationFrame(() => {
62
+ rafRef.current = null;
63
+ const next = pendingRef.current;
64
+ pendingRef.current = null;
65
+ if (next)
66
+ moveRef.current(next.x, next.y);
67
+ });
68
+ } else {
69
+ const next = pendingRef.current;
70
+ pendingRef.current = null;
71
+ if (next)
72
+ moveRef.current(next.x, next.y);
73
+ }
74
+ },
75
+ [disabled]
76
+ );
77
+ const onTouchEnd = useCallback(() => {
78
+ cancelFrame();
79
+ if (disabled)
80
+ return;
81
+ leaveRef.current();
82
+ }, [disabled, cancelFrame]);
83
+ return {
84
+ onPointerMove,
85
+ onPointerLeave,
86
+ onTouchStart,
87
+ onTouchMove,
88
+ onTouchEnd,
89
+ onTouchCancel: onTouchEnd
90
+ };
91
+ }
92
+ export {
93
+ usePlotPointer
94
+ };
package/esm/index.d.ts CHANGED
@@ -47,6 +47,12 @@ export { GraphChart } from './charts/graph-chart/index.js';
47
47
  export type { GraphChartProps } from './charts/graph-chart/index.js';
48
48
  export { Tooltip } from './components/Tooltip.js';
49
49
  export { useElementSize } from './hooks/useElementSize.js';
50
+ export { ChartSyncProvider, useChartSync } from './core/sync.js';
51
+ export type { ChartSyncPayload, ChartSyncProviderProps, UseChartSyncResult, } from './core/sync.js';
52
+ export { exportChartSvg, exportChartDataUrl, downloadChart, } from './core/export.js';
53
+ export type { ChartExportFormat, ExportChartOptions, DownloadChartOptions, } from './core/export.js';
54
+ export { useChartExport } from './hooks/useChartExport.js';
55
+ export type { UseChartExportResult } from './hooks/useChartExport.js';
50
56
  export { Chart } from './components/chart/index.js';
51
57
  export type { ChartProps, ChartOption, ChartOptionSeries, } from './components/chart/index.js';
52
58
  export type { BaseChartProps, ChartDataRow, SeriesConfig, SeriesType, AxisConfig, LegendConfig, TooltipConfig, TooltipPayload, TooltipSeriesItem, ChartRenderer, ChartAnimationInput, } from './types.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,YAAY,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC1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