@aura-react-comp/charts 0.0.0-stage → 0.3.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 (129) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +90 -2
  3. package/esm/charts/area-chart/index.d.ts +19 -0
  4. package/esm/charts/area-chart/index.d.ts.map +1 -0
  5. package/esm/charts/area-chart/index.js +31 -0
  6. package/esm/charts/bar-chart/index.d.ts +16 -0
  7. package/esm/charts/bar-chart/index.d.ts.map +1 -0
  8. package/esm/charts/bar-chart/index.js +28 -0
  9. package/esm/charts/candlestick-chart/index.d.ts +47 -0
  10. package/esm/charts/candlestick-chart/index.d.ts.map +1 -0
  11. package/esm/charts/candlestick-chart/index.js +347 -0
  12. package/esm/charts/combo-chart/index.d.ts +17 -0
  13. package/esm/charts/combo-chart/index.d.ts.map +1 -0
  14. package/esm/charts/combo-chart/index.js +26 -0
  15. package/esm/charts/funnel-chart/index.d.ts +35 -0
  16. package/esm/charts/funnel-chart/index.d.ts.map +1 -0
  17. package/esm/charts/funnel-chart/index.js +200 -0
  18. package/esm/charts/gauge-chart/index.d.ts +41 -0
  19. package/esm/charts/gauge-chart/index.d.ts.map +1 -0
  20. package/esm/charts/gauge-chart/index.js +217 -0
  21. package/esm/charts/graph-chart/index.d.ts +35 -0
  22. package/esm/charts/graph-chart/index.d.ts.map +1 -0
  23. package/esm/charts/graph-chart/index.js +224 -0
  24. package/esm/charts/heatmap-chart/index.d.ts +47 -0
  25. package/esm/charts/heatmap-chart/index.d.ts.map +1 -0
  26. package/esm/charts/heatmap-chart/index.js +281 -0
  27. package/esm/charts/line-chart/index.d.ts +23 -0
  28. package/esm/charts/line-chart/index.d.ts.map +1 -0
  29. package/esm/charts/line-chart/index.js +33 -0
  30. package/esm/charts/pie-chart/index.d.ts +38 -0
  31. package/esm/charts/pie-chart/index.d.ts.map +1 -0
  32. package/esm/charts/pie-chart/index.js +272 -0
  33. package/esm/charts/radar-chart/index.d.ts +44 -0
  34. package/esm/charts/radar-chart/index.d.ts.map +1 -0
  35. package/esm/charts/radar-chart/index.js +263 -0
  36. package/esm/charts/ring-progress/index.d.ts +38 -0
  37. package/esm/charts/ring-progress/index.d.ts.map +1 -0
  38. package/esm/charts/ring-progress/index.js +137 -0
  39. package/esm/charts/sankey-chart/index.d.ts +34 -0
  40. package/esm/charts/sankey-chart/index.d.ts.map +1 -0
  41. package/esm/charts/sankey-chart/index.js +248 -0
  42. package/esm/charts/scatter-chart/index.d.ts +34 -0
  43. package/esm/charts/scatter-chart/index.d.ts.map +1 -0
  44. package/esm/charts/scatter-chart/index.js +305 -0
  45. package/esm/charts/sparkline/index.d.ts +31 -0
  46. package/esm/charts/sparkline/index.d.ts.map +1 -0
  47. package/esm/charts/sparkline/index.js +152 -0
  48. package/esm/charts/tree-chart/index.d.ts +34 -0
  49. package/esm/charts/tree-chart/index.d.ts.map +1 -0
  50. package/esm/charts/tree-chart/index.js +200 -0
  51. package/esm/charts/treemap-chart/index.d.ts +35 -0
  52. package/esm/charts/treemap-chart/index.d.ts.map +1 -0
  53. package/esm/charts/treemap-chart/index.js +239 -0
  54. package/esm/charts/waterfall-chart/index.d.ts +41 -0
  55. package/esm/charts/waterfall-chart/index.d.ts.map +1 -0
  56. package/esm/charts/waterfall-chart/index.js +281 -0
  57. package/esm/components/Legend.d.ts +24 -0
  58. package/esm/components/Legend.d.ts.map +1 -0
  59. package/esm/components/Legend.js +50 -0
  60. package/esm/components/Tooltip.d.ts +24 -0
  61. package/esm/components/Tooltip.d.ts.map +1 -0
  62. package/esm/components/Tooltip.js +85 -0
  63. package/esm/components/chart/index.d.ts +71 -0
  64. package/esm/components/chart/index.d.ts.map +1 -0
  65. package/esm/components/chart/index.js +142 -0
  66. package/esm/components/chart.less +223 -0
  67. package/esm/core/animation.d.ts +49 -0
  68. package/esm/core/animation.d.ts.map +1 -0
  69. package/esm/core/animation.js +93 -0
  70. package/esm/core/force.d.ts +43 -0
  71. package/esm/core/force.d.ts.map +1 -0
  72. package/esm/core/force.js +134 -0
  73. package/esm/core/format.d.ts +34 -0
  74. package/esm/core/format.d.ts.map +1 -0
  75. package/esm/core/format.js +81 -0
  76. package/esm/core/hierarchy.d.ts +77 -0
  77. package/esm/core/hierarchy.d.ts.map +1 -0
  78. package/esm/core/hierarchy.js +233 -0
  79. package/esm/core/layout.d.ts +67 -0
  80. package/esm/core/layout.d.ts.map +1 -0
  81. package/esm/core/layout.js +121 -0
  82. package/esm/core/sampling.d.ts +32 -0
  83. package/esm/core/sampling.d.ts.map +1 -0
  84. package/esm/core/sampling.js +124 -0
  85. package/esm/core/sankey.d.ts +67 -0
  86. package/esm/core/sankey.d.ts.map +1 -0
  87. package/esm/core/sankey.js +136 -0
  88. package/esm/core/scale.d.ts +98 -0
  89. package/esm/core/scale.d.ts.map +1 -0
  90. package/esm/core/scale.js +207 -0
  91. package/esm/core/shape.d.ts +60 -0
  92. package/esm/core/shape.d.ts.map +1 -0
  93. package/esm/core/shape.js +246 -0
  94. package/esm/global.d.ts +6 -0
  95. package/esm/hooks/useChartTheme.d.ts +11 -0
  96. package/esm/hooks/useChartTheme.d.ts.map +1 -0
  97. package/esm/hooks/useChartTheme.js +35 -0
  98. package/esm/hooks/useChartTransition.d.ts +20 -0
  99. package/esm/hooks/useChartTransition.d.ts.map +1 -0
  100. package/esm/hooks/useChartTransition.js +120 -0
  101. package/esm/hooks/useElementSize.d.ts +17 -0
  102. package/esm/hooks/useElementSize.d.ts.map +1 -0
  103. package/esm/hooks/useElementSize.js +47 -0
  104. package/esm/index.d.ts +62 -0
  105. package/esm/index.d.ts.map +1 -0
  106. package/esm/index.js +48 -0
  107. package/esm/internal/CartesianChart.d.ts +16 -0
  108. package/esm/internal/CartesianChart.d.ts.map +1 -0
  109. package/esm/internal/CartesianChart.js +811 -0
  110. package/esm/internal/model.d.ts +67 -0
  111. package/esm/internal/model.d.ts.map +1 -0
  112. package/esm/internal/model.js +146 -0
  113. package/esm/scene/canvas.d.ts +18 -0
  114. package/esm/scene/canvas.d.ts.map +1 -0
  115. package/esm/scene/canvas.js +138 -0
  116. package/esm/scene/svg.d.ts +12 -0
  117. package/esm/scene/svg.d.ts.map +1 -0
  118. package/esm/scene/svg.js +116 -0
  119. package/esm/scene/types.d.ts +89 -0
  120. package/esm/scene/types.d.ts.map +1 -0
  121. package/esm/scene/types.js +21 -0
  122. package/esm/style.css +196 -0
  123. package/esm/theme/index.d.ts +127 -0
  124. package/esm/theme/index.d.ts.map +1 -0
  125. package/esm/theme/index.js +300 -0
  126. package/esm/types.d.ts +168 -0
  127. package/esm/types.d.ts.map +1 -0
  128. package/esm/types.js +2 -0
  129. package/package.json +64 -3
@@ -0,0 +1,281 @@
1
+ 'use client';
2
+ import React, { useMemo, useState } from 'react';
3
+ import { classNames, prefixCls } from '@aura-react-comp/shared';
4
+ import { SvgScene } from "../../scene/svg";
5
+ import { createBandScale, createLinearScale, linearTicks } from "../../core/scale";
6
+ import { computeLayout, suggestTickCount } from "../../core/layout";
7
+ import { formatNumber } from "../../core/format";
8
+ import { roundedRectPath } from "../../core/shape";
9
+ import { colorAt } from "../../theme";
10
+ import { useChartTheme } from "../../hooks/useChartTheme";
11
+ import { useElementSize } from "../../hooks/useElementSize";
12
+ import { useChartTransition } from "../../hooks/useChartTransition";
13
+ import { resolveAnimation, staggerProgress } from "../../core/animation";
14
+ import { Tooltip } from "../../components/Tooltip";
15
+ import { jsx as _jsx } from "react/jsx-runtime";
16
+ import { jsxs as _jsxs } from "react/jsx-runtime";
17
+ /**
18
+ * 瀑布图。
19
+ *
20
+ * 用柱子的「起点 + 终点」表达增减过程,最后落到总计。
21
+ * 与堆叠柱的区别:堆叠柱的每段都从 0 起算的相对量,瀑布图的每一段都是**增量**,
22
+ * 位置取决于此前所有项的累计。
23
+ */
24
+ export function WaterfallChart({
25
+ data = [],
26
+ width = '100%',
27
+ height = 320,
28
+ positiveColor,
29
+ negativeColor = '#ef4444',
30
+ totalColor,
31
+ connector = true,
32
+ theme: themeInput,
33
+ palette,
34
+ tooltip = true,
35
+ animation = true,
36
+ className,
37
+ style,
38
+ ariaLabel,
39
+ emptyText = '暂无数据'
40
+ }) {
41
+ const theme = useChartTheme(themeInput, palette);
42
+ const {
43
+ ref,
44
+ size
45
+ } = useElementSize();
46
+ const [activeIndex, setActiveIndex] = useState(null);
47
+ const rows = useMemo(() => {
48
+ const up = positiveColor ?? colorAt(theme, 0);
49
+ const total = totalColor ?? colorAt(theme, 1);
50
+ let running = 0;
51
+ return data.map(item => {
52
+ if (item.isTotal) {
53
+ const row = {
54
+ name: item.name,
55
+ value: running,
56
+ start: 0,
57
+ end: running,
58
+ isTotal: true,
59
+ color: item.color ?? total
60
+ };
61
+ return row;
62
+ }
63
+ const start = running;
64
+ running += item.value;
65
+ return {
66
+ name: item.name,
67
+ value: item.value,
68
+ start,
69
+ end: running,
70
+ isTotal: false,
71
+ color: item.color ?? (item.value >= 0 ? up : negativeColor)
72
+ };
73
+ });
74
+ }, [data, positiveColor, negativeColor, totalColor, theme]);
75
+ const domain = useMemo(() => {
76
+ if (!rows.length) return [0, 1];
77
+ let min = 0;
78
+ let max = 0;
79
+ for (const row of rows) {
80
+ min = Math.min(min, row.start, row.end);
81
+ max = Math.max(max, row.start, row.end);
82
+ }
83
+ return min === max ? [0, max + 1] : [min, max];
84
+ }, [rows]);
85
+ const yTicks = useMemo(() => linearTicks(domain[0], domain[1], suggestTickCount(Math.max(60, height - 48))), [domain, height]);
86
+ const yLabels = useMemo(() => yTicks.map(tick => formatNumber(tick)), [yTicks]);
87
+ const layout = useMemo(() => computeLayout({
88
+ width: Math.max(size.width, 0),
89
+ height: Math.max(size.height, 0),
90
+ yLabels,
91
+ xLabels: rows.map(row => row.name),
92
+ theme
93
+ }), [size.width, size.height, yLabels, rows, theme]);
94
+ const {
95
+ plot
96
+ } = layout;
97
+ const xScale = useMemo(() => createBandScale({
98
+ domain: rows.map(row => row.name),
99
+ range: [plot.x, plot.x + plot.width],
100
+ paddingInner: 0.36,
101
+ paddingOuter: 0.12
102
+ }), [rows, plot.x, plot.width]);
103
+ const yScale = useMemo(() => createLinearScale({
104
+ domain,
105
+ range: [plot.y + plot.height, plot.y]
106
+ }), [domain, plot.y, plot.height]);
107
+ const animKey = typeof animation === 'object' ? JSON.stringify(animation) : String(animation);
108
+ const animConfig = useMemo(() => resolveAnimation(animation),
109
+ // eslint-disable-next-line react-hooks/exhaustive-deps
110
+ [animKey]);
111
+
112
+ // 每根柱子用 [起点, 终点] 两个数描述,插值时两端一起平滑移动
113
+ const valueMatrix = useMemo(() => rows.map(row => [row.start, row.end]), [rows]);
114
+ const transition = useChartTransition(valueMatrix, animConfig);
115
+ const progress = transition.progress;
116
+ const scene = useMemo(() => {
117
+ if (!rows.length || plot.width <= 0) return [];
118
+ const nodes = [];
119
+ yTicks.forEach((tick, index) => {
120
+ nodes.push({
121
+ kind: 'line',
122
+ key: `grid-${index}`,
123
+ x1: plot.x,
124
+ y1: yScale.scale(tick),
125
+ x2: plot.x + plot.width,
126
+ y2: yScale.scale(tick),
127
+ stroke: theme.grid.line,
128
+ strokeWidth: theme.grid.lineWidth,
129
+ strokeDasharray: theme.grid.dash.join(' ') || undefined
130
+ });
131
+ nodes.push({
132
+ kind: 'text',
133
+ key: `yl-${index}`,
134
+ x: plot.x - theme.axis.tickSize - 4,
135
+ y: yScale.scale(tick),
136
+ text: yLabels[index],
137
+ fill: theme.axis.label,
138
+ fontSize: theme.font.size,
139
+ anchor: 'end',
140
+ baseline: 'middle',
141
+ fontFamily: theme.font.family
142
+ });
143
+ });
144
+
145
+ // 零基线(负值场景下必须显式画出)
146
+ if (domain[0] < 0) {
147
+ nodes.push({
148
+ kind: 'line',
149
+ key: 'zero',
150
+ x1: plot.x,
151
+ y1: yScale.scale(0),
152
+ x2: plot.x + plot.width,
153
+ y2: yScale.scale(0),
154
+ stroke: theme.axis.line,
155
+ strokeWidth: 1
156
+ });
157
+ }
158
+ const barWidth = Math.min(xScale.bandWidth, 56);
159
+ rows.forEach((row, index) => {
160
+ const cx = xScale.center(row.name);
161
+ if (cx === undefined) return;
162
+ // 逐柱错峰 + 从各自起点生长(不是整排从底部弹起)
163
+ const local = staggerProgress(progress, index, rows.length, animConfig.stagger);
164
+ const animatedStart = transition.values[index]?.[0] ?? row.start;
165
+ const animatedEnd = transition.values[index]?.[1] ?? row.end;
166
+ const drawnEnd = animatedStart + (animatedEnd - animatedStart) * local;
167
+ const top = yScale.scale(drawnEnd);
168
+ const bottom = yScale.scale(animatedStart);
169
+ const y = Math.min(top, bottom);
170
+ const h = Math.max(2, Math.abs(bottom - top));
171
+ const isActive = activeIndex === index;
172
+ nodes.push({
173
+ kind: 'path',
174
+ key: `bar-${index}`,
175
+ d: roundedRectPath(cx - barWidth / 2, y, barWidth, h, 3, {
176
+ tl: true,
177
+ tr: true
178
+ }),
179
+ fill: row.color,
180
+ opacity: activeIndex === null || isActive ? 1 : 0.55,
181
+ pointerEvents: 'none'
182
+ });
183
+
184
+ // 连接线:从前一根柱的终点水平引到本柱起点
185
+ if (connector && index > 0) {
186
+ const prev = rows[index - 1];
187
+ const prevCx = xScale.center(prev.name);
188
+ if (prevCx !== undefined) {
189
+ const linkY = yScale.scale(prev.end);
190
+ nodes.push({
191
+ kind: 'line',
192
+ key: `link-${index}`,
193
+ x1: prevCx + barWidth / 2,
194
+ y1: linkY,
195
+ x2: cx - barWidth / 2,
196
+ y2: linkY,
197
+ stroke: theme.axis.line,
198
+ strokeWidth: 1,
199
+ strokeDasharray: '3 3',
200
+ pointerEvents: 'none'
201
+ });
202
+ }
203
+ }
204
+ nodes.push({
205
+ kind: 'text',
206
+ key: `xlabel-${index}`,
207
+ x: cx,
208
+ y: plot.y + plot.height + theme.axis.tickSize + theme.font.size,
209
+ text: row.name,
210
+ fill: theme.axis.label,
211
+ fontSize: theme.font.size,
212
+ anchor: 'middle',
213
+ fontFamily: theme.font.family
214
+ });
215
+ });
216
+ return nodes;
217
+ }, [rows, plot, yTicks, yLabels, yScale, xScale, theme, progress, activeIndex, domain, connector, transition.values, animConfig.stagger]);
218
+ const indexFromPointer = clientX => {
219
+ const element = ref.current;
220
+ if (!element || !rows.length) return null;
221
+ const rect = element.getBoundingClientRect();
222
+ const localX = clientX - rect.left;
223
+ let nearest = null;
224
+ let min = Infinity;
225
+ rows.forEach((row, index) => {
226
+ const cx = xScale.center(row.name);
227
+ if (cx === undefined) return;
228
+ const distance = Math.abs(cx - localX);
229
+ if (distance < min) {
230
+ min = distance;
231
+ nearest = index;
232
+ }
233
+ });
234
+ return nearest;
235
+ };
236
+ const active = activeIndex !== null ? rows[activeIndex] : null;
237
+ const tooltipPayload = active ? {
238
+ label: active.name,
239
+ row: null,
240
+ series: [{
241
+ key: active.name,
242
+ name: active.isTotal ? '累计' : '增减',
243
+ color: active.color,
244
+ value: active.isTotal ? active.end : active.value,
245
+ formatted: formatNumber(active.isTotal ? active.end : active.value)
246
+ }]
247
+ } : null;
248
+ const autoAriaLabel = ariaLabel ?? `瀑布图:${rows.map(row => row.name).join('、')}`;
249
+ return /*#__PURE__*/_jsx("div", {
250
+ className: classNames(prefixCls('chart'), className),
251
+ style: {
252
+ width,
253
+ height,
254
+ ...style
255
+ },
256
+ "data-aura-chart": "waterfall",
257
+ children: /*#__PURE__*/_jsxs("div", {
258
+ ref: ref,
259
+ className: prefixCls('chart__plot'),
260
+ onPointerMove: event => setActiveIndex(indexFromPointer(event.clientX)),
261
+ onPointerLeave: () => setActiveIndex(null),
262
+ children: [rows.length === 0 || size.width === 0 ? /*#__PURE__*/_jsx("div", {
263
+ className: prefixCls('chart__empty'),
264
+ children: emptyText
265
+ }) : /*#__PURE__*/_jsx(SvgScene, {
266
+ nodes: scene,
267
+ width: size.width,
268
+ height: size.height,
269
+ ariaLabel: autoAriaLabel
270
+ }), /*#__PURE__*/_jsx(Tooltip, {
271
+ payload: tooltipPayload,
272
+ x: active ? xScale.center(active.name) ?? 0 : 0,
273
+ y: active ? yScale.scale(Math.max(active.start, active.end)) - 10 : 0,
274
+ containerWidth: size.width,
275
+ containerHeight: size.height,
276
+ theme: theme,
277
+ config: tooltip
278
+ })]
279
+ })
280
+ });
281
+ }
@@ -0,0 +1,24 @@
1
+ import React from 'react';
2
+ import type { ChartTheme } from '../theme/index.js';
3
+ import type { LegendConfig } from '../types.js';
4
+ export interface LegendItem {
5
+ key: string;
6
+ name: string;
7
+ color: string;
8
+ visible: boolean;
9
+ }
10
+ export interface LegendProps {
11
+ items: LegendItem[];
12
+ theme: ChartTheme;
13
+ config?: boolean | LegendConfig;
14
+ onToggle: (key: string) => void;
15
+ }
16
+ /**
17
+ * 图例。
18
+ *
19
+ * 刻意用 DOM 而不是画进 SVG:
20
+ * · 可以用原生 button,键盘可达、可聚焦,天然符合无障碍要求;
21
+ * · 换行、省略号交给 CSS,省去手写文本换行计算。
22
+ */
23
+ export declare function Legend({ items, theme, config, onToggle }: LegendProps): React.JSX.Element | null;
24
+ //# sourceMappingURL=Legend.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Legend.d.ts","sourceRoot":"","sources":["Legend.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,UAAU,EAAE,CAAC;IACpB,KAAK,EAAE,UAAU,CAAC;IAClB,MAAM,CAAC,EAAE,OAAO,GAAG,YAAY,CAAC;IAChC,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CACjC;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,WAAW,4BA8CrE"}
@@ -0,0 +1,50 @@
1
+ 'use client';
2
+ import React from 'react';
3
+ import { classNames, prefixCls } from '@aura-react-comp/shared';
4
+ import { jsx as _jsx } from "react/jsx-runtime";
5
+ import { jsxs as _jsxs } from "react/jsx-runtime";
6
+ /**
7
+ * 图例。
8
+ *
9
+ * 刻意用 DOM 而不是画进 SVG:
10
+ * · 可以用原生 button,键盘可达、可聚焦,天然符合无障碍要求;
11
+ * · 换行、省略号交给 CSS,省去手写文本换行计算。
12
+ */
13
+ export function Legend({
14
+ items,
15
+ theme,
16
+ config,
17
+ onToggle
18
+ }) {
19
+ if (config === false || items.length === 0) return null;
20
+ const options = typeof config === 'object' ? config : {};
21
+ const {
22
+ position = 'top',
23
+ align = 'left'
24
+ } = options;
25
+ if (options.show === false) return null;
26
+ return /*#__PURE__*/_jsx("div", {
27
+ className: classNames(prefixCls('chart__legend'), position === 'bottom' && prefixCls('chart__legend--bottom'), align !== 'left' && prefixCls(`chart__legend--${align}`)),
28
+ role: "group",
29
+ "aria-label": "\u56FE\u4F8B",
30
+ children: items.map(item => /*#__PURE__*/_jsxs("button", {
31
+ type: "button",
32
+ className: classNames(prefixCls('chart__legend-item'), !item.visible && prefixCls('chart__legend-item--inactive')),
33
+ "aria-pressed": item.visible,
34
+ title: `${item.visible ? '隐藏' : '显示'} ${item.name}`,
35
+ onClick: () => onToggle(item.key),
36
+ children: [/*#__PURE__*/_jsx("span", {
37
+ className: prefixCls('chart__legend-dot'),
38
+ style: {
39
+ background: item.color,
40
+ opacity: item.visible ? 1 : 0.5
41
+ }
42
+ }), /*#__PURE__*/_jsx("span", {
43
+ style: {
44
+ color: item.visible ? theme.legend.text : theme.legend.textInactive
45
+ },
46
+ children: item.name
47
+ })]
48
+ }, item.key))
49
+ });
50
+ }
@@ -0,0 +1,24 @@
1
+ import React from 'react';
2
+ import type { ChartTheme } from '../theme/index.js';
3
+ import type { TooltipConfig, TooltipPayload } from '../types.js';
4
+ export interface TooltipProps {
5
+ payload: TooltipPayload | null;
6
+ /** 相对容器的像素位置 */
7
+ x: number;
8
+ y: number;
9
+ /** 容器宽度,用于防止 tooltip 溢出右边界 */
10
+ containerWidth: number;
11
+ /** 容器高度;传入后会在上方放不下时自动翻到下方 */
12
+ containerHeight?: number;
13
+ theme: ChartTheme;
14
+ config?: boolean | TooltipConfig;
15
+ }
16
+ /**
17
+ * 悬浮提示。
18
+ *
19
+ * 渲染为绝对定位的 DOM 而非 SVG foreignObject:
20
+ * · foreignObject 在部分导出/打印场景下会失效;
21
+ * · DOM 能自然继承字体与换行,样式维护成本更低。
22
+ */
23
+ export declare function Tooltip({ payload, x, y, containerWidth, containerHeight, theme, config, }: TooltipProps): React.JSX.Element | null;
24
+ //# sourceMappingURL=Tooltip.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Tooltip.d.ts","sourceRoot":"","sources":["Tooltip.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE9D,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,cAAc,GAAG,IAAI,CAAC;IAC/B,gBAAgB;IAChB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,8BAA8B;IAC9B,cAAc,EAAE,MAAM,CAAC;IACvB,6BAA6B;IAC7B,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,KAAK,EAAE,UAAU,CAAC;IAClB,MAAM,CAAC,EAAE,OAAO,GAAG,aAAa,CAAC;CAClC;AAOD;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,EACtB,OAAO,EACP,CAAC,EACD,CAAC,EACD,cAAc,EACd,eAAe,EACf,KAAK,EACL,MAAM,GACP,EAAE,YAAY,4BAoEd"}
@@ -0,0 +1,85 @@
1
+ 'use client';
2
+ import React from 'react';
3
+ import { classNames, prefixCls } from '@aura-react-comp/shared';
4
+ import { jsx as _jsx } from "react/jsx-runtime";
5
+ import { jsxs as _jsxs } from "react/jsx-runtime";
6
+ import { Fragment as _Fragment } from "react/jsx-runtime";
7
+ const TOOLTIP_MAX_WIDTH = 280;
8
+ const EDGE_GAP = 8;
9
+ /** tooltip 与锚点之间的间隙 */
10
+ const HEAD_GAP = 12;
11
+
12
+ /**
13
+ * 悬浮提示。
14
+ *
15
+ * 渲染为绝对定位的 DOM 而非 SVG foreignObject:
16
+ * · foreignObject 在部分导出/打印场景下会失效;
17
+ * · DOM 能自然继承字体与换行,样式维护成本更低。
18
+ */
19
+ export function Tooltip({
20
+ payload,
21
+ x,
22
+ y,
23
+ containerWidth,
24
+ containerHeight,
25
+ theme,
26
+ config
27
+ }) {
28
+ if (!payload || config === false) return null;
29
+ const options = typeof config === 'object' ? config : {};
30
+ if (options.show === false) return null;
31
+ const custom = options.formatter?.(payload);
32
+ if (custom === null) return null;
33
+
34
+ // 水平收敛:按「最大宽度的一半」留足空间,避免贴边被裁
35
+ const half = TOOLTIP_MAX_WIDTH / 2;
36
+ const clampedX = Math.max(Math.min(x, containerWidth - half - EDGE_GAP), Math.min(half + EDGE_GAP, Math.max(0, containerWidth / 2)));
37
+
38
+ /**
39
+ * 垂直翻转:默认浮在锚点上方,上方放不下就翻到下方。
40
+ *
41
+ * 高度估算而非测量——测量要等一帧,会看到 tooltip 先闪错位置。
42
+ * 标题一行 + 每行数据约 18px,估得略大为好(宁可提前翻转)。
43
+ */
44
+ const estimatedHeight = 30 + payload.series.length * 18;
45
+ const fitsAbove = y - estimatedHeight - EDGE_GAP >= 0;
46
+ const placeBelow = Boolean(containerHeight) && !fitsAbove;
47
+ const top = placeBelow ? y + 12 : Math.max(y - HEAD_GAP, 0);
48
+ return /*#__PURE__*/_jsxs("div", {
49
+ className: classNames(prefixCls('chart__tooltip')),
50
+ style: {
51
+ left: clampedX,
52
+ top,
53
+ transform: placeBelow ? 'translate(-50%, 0)' : 'translate(-50%, -100%)',
54
+ background: theme.tooltip.background,
55
+ borderColor: theme.tooltip.border,
56
+ color: theme.tooltip.text,
57
+ boxShadow: theme.tooltip.shadow,
58
+ borderRadius: theme.tooltip.radius
59
+ },
60
+ role: "tooltip",
61
+ children: [/*#__PURE__*/_jsx("div", {
62
+ className: prefixCls('chart__tooltip-title'),
63
+ children: payload.label
64
+ }), custom ?? /*#__PURE__*/_jsx(_Fragment, {
65
+ children: payload.series.map(item => /*#__PURE__*/_jsxs("div", {
66
+ className: prefixCls('chart__tooltip-item'),
67
+ children: [/*#__PURE__*/_jsxs("span", {
68
+ className: prefixCls('chart__tooltip-label'),
69
+ children: [/*#__PURE__*/_jsx("span", {
70
+ style: {
71
+ width: 8,
72
+ height: 8,
73
+ borderRadius: 2,
74
+ background: item.color,
75
+ flex: '0 0 auto'
76
+ }
77
+ }), item.name]
78
+ }), /*#__PURE__*/_jsx("span", {
79
+ className: prefixCls('chart__tooltip-value'),
80
+ children: item.formatted
81
+ })]
82
+ }, item.key))
83
+ })]
84
+ });
85
+ }
@@ -0,0 +1,71 @@
1
+ import React from 'react';
2
+ import type { ChartThemeInput } from '../../theme/index.js';
3
+ /** ECharts 风格的系列配置(支持子集) */
4
+ export interface ChartOptionSeries {
5
+ type?: 'line' | 'bar' | 'scatter' | 'area' | 'pie';
6
+ name?: string;
7
+ data?: (number | null)[];
8
+ /** 堆叠分组名 */
9
+ stack?: string;
10
+ /** 折线平滑 */
11
+ smooth?: boolean;
12
+ /** 面积填充(折线图用) */
13
+ areaStyle?: Record<string, unknown> | boolean;
14
+ /** 单系列颜色 */
15
+ itemStyle?: {
16
+ color?: string;
17
+ };
18
+ }
19
+ export interface ChartOption {
20
+ xAxis?: {
21
+ data?: (string | number)[];
22
+ name?: string;
23
+ type?: 'category' | 'value';
24
+ };
25
+ yAxis?: {
26
+ name?: string;
27
+ min?: number;
28
+ max?: number;
29
+ };
30
+ series?: ChartOptionSeries[];
31
+ /** 色板,等价于 palette */
32
+ color?: string[];
33
+ legend?: {
34
+ show?: boolean;
35
+ top?: number | string;
36
+ };
37
+ tooltip?: {
38
+ show?: boolean;
39
+ trigger?: 'axis' | 'item';
40
+ };
41
+ grid?: {
42
+ show?: boolean;
43
+ };
44
+ title?: {
45
+ text?: string;
46
+ subtext?: string;
47
+ };
48
+ }
49
+ export interface ChartProps {
50
+ option: ChartOption;
51
+ width?: number | string;
52
+ height?: number;
53
+ theme?: ChartThemeInput;
54
+ palette?: string[];
55
+ animation?: boolean;
56
+ className?: string;
57
+ style?: React.CSSProperties;
58
+ }
59
+ /**
60
+ * 配置式图表(兼容 ECharts 风格的 option)。
61
+ *
62
+ * 存在意义有两个:
63
+ * 1. 迁移成本 —— 已有 ECharts 配置的项目可以低成本试用;
64
+ * 2. **AI 友好** —— 让模型生成 JSON 配置比生成 JSX 组合可靠得多,
65
+ * 尤其在需要动态拼装系列时。
66
+ *
67
+ * 明确只支持常用子集(直角坐标系的 line / bar / area / scatter 与 pie)。
68
+ * 不做「假装全支持」:未识别的系列类型会被跳过,而不是静默画错。
69
+ */
70
+ export declare function Chart({ option, width, height, theme, palette, animation, className, style, }: ChartProps): React.JSX.Element;
71
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAkB,MAAM,OAAO,CAAC;AAGvC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAOnD,4BAA4B;AAC5B,MAAM,WAAW,iBAAiB;IAChC,IAAI,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS,GAAG,MAAM,GAAG,KAAK,CAAC;IACnD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,CAAC,MAAM,GAAG,IAAI,CAAC,EAAE,CAAC;IACzB,YAAY;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW;IACX,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,iBAAiB;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC;IAC9C,YAAY;IACZ,SAAS,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAChC;AAED,MAAM,WAAW,WAAW;IAC1B,KAAK,CAAC,EAAE;QACN,IAAI,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;QAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,IAAI,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC;KAC7B,CAAC;IACF,KAAK,CAAC,EAAE;QACN,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,GAAG,CAAC,EAAE,MAAM,CAAC;KACd,CAAC;IACF,MAAM,CAAC,EAAE,iBAAiB,EAAE,CAAC;IAC7B,qBAAqB;IACrB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,MAAM,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,OAAO,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IACnD,OAAO,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,OAAO,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IACxD,IAAI,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;IAC1B,KAAK,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAC7C;AAED,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,WAAW,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,eAAe,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;CAC7B;AAID;;;;;;;;;;GAUG;AACH,wBAAgB,KAAK,CAAC,EACpB,MAAM,EACN,KAAc,EACd,MAAY,EACZ,KAAK,EACL,OAAO,EACP,SAAgB,EAChB,SAAS,EACT,KAAK,GACN,EAAE,UAAU,qBAiIZ"}