@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,200 @@
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 { formatNumber } from "../../core/format";
6
+ import { layoutTree, maxDepth, sumValues, treeLinkPath } from "../../core/hierarchy";
7
+ import { withAlpha } from "../../theme";
8
+ import { useChartTheme } from "../../hooks/useChartTheme";
9
+ import { useElementSize } from "../../hooks/useElementSize";
10
+ import { useChartTransition } from "../../hooks/useChartTransition";
11
+ import { resolveAnimation, staggerProgress } from "../../core/animation";
12
+ import { Tooltip } from "../../components/Tooltip";
13
+ import { jsx as _jsx } from "react/jsx-runtime";
14
+ import { jsxs as _jsxs } from "react/jsx-runtime";
15
+ /**
16
+ * 树图(横向展开)。
17
+ *
18
+ * 与矩形树图的分工:矩形树图用面积编码数值、回答「谁占得多」;
19
+ * 树图只表达层级结构与从属关系,节点大小恒定。
20
+ *
21
+ * 纵向位置按叶子顺序均分,父节点取子节点均值 —— 这样连线不交叉,
22
+ * 同一父节点下的兄弟会自然聚成一簇。
23
+ */
24
+ export function TreeChart({
25
+ data,
26
+ nodeSize = 4,
27
+ showValue = false,
28
+ labelSpace = 96,
29
+ width = '100%',
30
+ height = 360,
31
+ theme: themeInput,
32
+ palette,
33
+ tooltip = true,
34
+ animation = true,
35
+ className,
36
+ style,
37
+ ariaLabel,
38
+ emptyText = '暂无数据'
39
+ }) {
40
+ const theme = useChartTheme(themeInput, palette);
41
+ const {
42
+ ref,
43
+ size
44
+ } = useElementSize();
45
+ const [activeIndex, setActiveIndex] = useState(null);
46
+ const animKey = typeof animation === 'object' ? JSON.stringify(animation) : String(animation);
47
+ const animConfig = useMemo(() => resolveAnimation(animation),
48
+ // eslint-disable-next-line react-hooks/exhaustive-deps
49
+ [animKey]);
50
+ const layout = useMemo(() => {
51
+ if (!data) return {
52
+ nodes: [],
53
+ depth: 0
54
+ };
55
+ return layoutTree(data, {
56
+ x: 12,
57
+ y: 16,
58
+ // 右侧留出文字空间,否则最深一层的标签会被裁掉
59
+ width: Math.max(0, size.width - 24 - labelSpace),
60
+ height: Math.max(0, size.height - 32)
61
+ });
62
+ }, [data, size.width, size.height, labelSpace]);
63
+
64
+ // 用叶子数驱动:结构变化时节点位置平滑过渡
65
+ const matrix = useMemo(() => [layout.nodes.map(item => Math.round(item.y))], [layout.nodes]);
66
+ const transition = useChartTransition(matrix, animConfig);
67
+ const progress = transition.progress;
68
+ const scene = useMemo(() => {
69
+ if (!layout.nodes.length || size.width <= 0) return [];
70
+ const nodes = [];
71
+ const depthTotal = Math.max(1, maxDepth(data ?? {
72
+ name: ''
73
+ }));
74
+
75
+ // 先画连线,保证节点压在线上
76
+ layout.nodes.forEach((item, index) => {
77
+ if (item.parent < 0) return;
78
+ const parent = layout.nodes[item.parent];
79
+ const local = staggerProgress(progress, item.depth, depthTotal, animConfig.stagger);
80
+ const animatedParent = {
81
+ ...parent,
82
+ y: transition.values[0]?.[item.parent] ?? parent.y
83
+ };
84
+ const animatedChild = {
85
+ ...item,
86
+ y: transition.values[0]?.[index] ?? item.y
87
+ };
88
+ nodes.push({
89
+ kind: 'path',
90
+ key: `link-${index}`,
91
+ d: treeLinkPath(animatedParent, animatedChild, nodeSize),
92
+ fill: 'none',
93
+ stroke: withAlpha(theme.axis.line, 0.9),
94
+ strokeWidth: 1.2,
95
+ opacity: local,
96
+ pointerEvents: 'none'
97
+ });
98
+ });
99
+ layout.nodes.forEach((item, index) => {
100
+ const y = transition.values[0]?.[index] ?? item.y;
101
+ const isActive = activeIndex === index;
102
+ const hasChildren = Boolean(item.node.children?.length);
103
+ const local = staggerProgress(progress, item.depth, depthTotal, animConfig.stagger);
104
+ const color = hasChildren ? theme.emphasis : item.node.color ?? theme.palette[item.depth % theme.palette.length];
105
+ nodes.push({
106
+ kind: 'circle',
107
+ key: `node-${index}`,
108
+ cx: item.x,
109
+ cy: y,
110
+ r: isActive ? nodeSize + 2 : nodeSize,
111
+ fill: hasChildren ? theme.tooltip.background : color,
112
+ stroke: color,
113
+ strokeWidth: hasChildren ? 2 : 0,
114
+ opacity: local,
115
+ pointerEvents: 'none'
116
+ });
117
+ nodes.push({
118
+ kind: 'text',
119
+ key: `label-${index}`,
120
+ x: item.x + nodeSize + 8,
121
+ y,
122
+ text: showValue && item.node.value !== undefined ? `${item.node.name} ${formatNumber(item.node.value, {
123
+ compact: true
124
+ })}` : item.node.name,
125
+ fill: hasChildren ? theme.text.primary : theme.text.secondary,
126
+ fontSize: theme.font.size,
127
+ fontWeight: hasChildren ? theme.font.weightMedium : undefined,
128
+ anchor: 'start',
129
+ baseline: 'middle',
130
+ opacity: local,
131
+ fontFamily: theme.font.family
132
+ });
133
+ });
134
+ return nodes;
135
+ }, [layout, size.width, data, theme, progress, animConfig.stagger, activeIndex, nodeSize, showValue, transition.values]);
136
+ const nodeFromPointer = (clientX, clientY) => {
137
+ const element = ref.current;
138
+ if (!element) return null;
139
+ const rect = element.getBoundingClientRect();
140
+ const x = clientX - rect.left;
141
+ const y = clientY - rect.top;
142
+ let nearest = null;
143
+ let min = 18; // 命中半径,节点本身只有 4px,必须放宽
144
+ layout.nodes.forEach((item, index) => {
145
+ const distance = Math.hypot(item.x - x, item.y - y);
146
+ if (distance < min) {
147
+ min = distance;
148
+ nearest = index;
149
+ }
150
+ });
151
+ return nearest;
152
+ };
153
+ const active = activeIndex !== null ? layout.nodes[activeIndex] : null;
154
+ const tooltipPayload = active ? {
155
+ label: active.node.name,
156
+ row: null,
157
+ series: [{
158
+ key: 'value',
159
+ name: active.node.children?.length ? '合计' : '数值',
160
+ color: theme.emphasis,
161
+ value: sumValues(active.node),
162
+ formatted: formatNumber(sumValues(active.node), {
163
+ compact: true
164
+ })
165
+ }]
166
+ } : null;
167
+ const autoAriaLabel = ariaLabel ?? `树图:${layout.nodes.length} 个节点`;
168
+ return /*#__PURE__*/_jsx("div", {
169
+ className: classNames(prefixCls('chart'), className),
170
+ style: {
171
+ width,
172
+ height,
173
+ ...style
174
+ },
175
+ "data-aura-chart": "tree",
176
+ children: /*#__PURE__*/_jsxs("div", {
177
+ ref: ref,
178
+ className: prefixCls('chart__plot'),
179
+ onPointerMove: event => setActiveIndex(nodeFromPointer(event.clientX, event.clientY)),
180
+ onPointerLeave: () => setActiveIndex(null),
181
+ children: [!layout.nodes.length || size.width === 0 ? /*#__PURE__*/_jsx("div", {
182
+ className: prefixCls('chart__empty'),
183
+ children: emptyText
184
+ }) : /*#__PURE__*/_jsx(SvgScene, {
185
+ nodes: scene,
186
+ width: size.width,
187
+ height: size.height,
188
+ ariaLabel: autoAriaLabel
189
+ }), /*#__PURE__*/_jsx(Tooltip, {
190
+ payload: tooltipPayload,
191
+ x: active ? active.x : 0,
192
+ y: active ? active.y - 12 : 0,
193
+ containerWidth: size.width,
194
+ containerHeight: size.height,
195
+ theme: theme,
196
+ config: tooltip
197
+ })]
198
+ })
199
+ });
200
+ }
@@ -0,0 +1,35 @@
1
+ import React from 'react';
2
+ import { type HierarchyNode } from '../../core/hierarchy.js';
3
+ import { type ChartThemeInput } from '../../theme/index.js';
4
+ import type { ChartAnimationInput, TooltipConfig } from '../../types.js';
5
+ export interface TreemapChartProps {
6
+ /** 根节点;只渲染它的子孙 */
7
+ data?: HierarchyNode;
8
+ /** 展开层数,1 表示只画根的直接子节点,2 表示再往里一层 */
9
+ maxDepth?: number;
10
+ /** 标签策略:auto 时按格子尺寸决定 */
11
+ label?: 'auto' | 'always' | false;
12
+ /** 是否在标签下方显示数值 */
13
+ showValue?: boolean;
14
+ /** 格子间隙(像素) */
15
+ gap?: number;
16
+ width?: number | string;
17
+ height?: number;
18
+ theme?: ChartThemeInput;
19
+ palette?: string[];
20
+ tooltip?: boolean | TooltipConfig;
21
+ animation?: ChartAnimationInput;
22
+ className?: string;
23
+ style?: React.CSSProperties;
24
+ ariaLabel?: string;
25
+ emptyText?: string;
26
+ }
27
+ /**
28
+ * 矩形树图。
29
+ *
30
+ * 用面积编码数值,能在有限空间里同时表达「占比」与「层级」。
31
+ * 布局用 squarified 算法:让每块尽量接近正方形,
32
+ * 既放得下文字,也便于人眼比较面积。
33
+ */
34
+ export declare function TreemapChart({ data, maxDepth, label, showValue, gap, width, height, theme: themeInput, palette, tooltip, animation, className, style, ariaLabel, emptyText, }: TreemapChartProps): React.JSX.Element;
35
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA4B,MAAM,OAAO,CAAC;AAMjD,OAAO,EAIL,KAAK,aAAa,EAEnB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAuB,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AAMxE,OAAO,KAAK,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,6BAA6B,CAAC;AAErC,MAAM,WAAW,iBAAiB;IAChC,kBAAkB;IAClB,IAAI,CAAC,EAAE,aAAa,CAAC;IACrB,mCAAmC;IACnC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,yBAAyB;IACzB,KAAK,CAAC,EAAE,MAAM,GAAG,QAAQ,GAAG,KAAK,CAAC;IAClC,kBAAkB;IAClB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,eAAe;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,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,OAAO,CAAC,EAAE,OAAO,GAAG,aAAa,CAAC;IAClC,SAAS,CAAC,EAAE,mBAAmB,CAAC;IAChC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,EAC3B,IAAI,EACJ,QAAY,EACZ,KAAc,EACd,SAAgB,EAChB,GAAO,EACP,KAAc,EACd,MAAY,EACZ,KAAK,EAAE,UAAU,EACjB,OAAO,EACP,OAAc,EACd,SAAgB,EAChB,SAAS,EACT,KAAK,EACL,SAAS,EACT,SAAkB,GACnB,EAAE,iBAAiB,qBAkQnB"}
@@ -0,0 +1,239 @@
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 { formatNumber, formatPercent } from "../../core/format";
6
+ import { roundedRectPath } from "../../core/shape";
7
+ import { buildTreemap, countLeaves, sumValues } from "../../core/hierarchy";
8
+ import { mixColor, withAlpha } from "../../theme";
9
+ import { useChartTheme } from "../../hooks/useChartTheme";
10
+ import { useElementSize } from "../../hooks/useElementSize";
11
+ import { useChartTransition } from "../../hooks/useChartTransition";
12
+ import { resolveAnimation, staggerProgress } from "../../core/animation";
13
+ import { Tooltip } from "../../components/Tooltip";
14
+ import { jsx as _jsx } from "react/jsx-runtime";
15
+ import { jsxs as _jsxs } from "react/jsx-runtime";
16
+ /**
17
+ * 矩形树图。
18
+ *
19
+ * 用面积编码数值,能在有限空间里同时表达「占比」与「层级」。
20
+ * 布局用 squarified 算法:让每块尽量接近正方形,
21
+ * 既放得下文字,也便于人眼比较面积。
22
+ */
23
+ export function TreemapChart({
24
+ data,
25
+ maxDepth = 1,
26
+ label = 'auto',
27
+ showValue = true,
28
+ gap = 2,
29
+ width = '100%',
30
+ height = 320,
31
+ theme: themeInput,
32
+ palette,
33
+ tooltip = true,
34
+ animation = true,
35
+ className,
36
+ style,
37
+ ariaLabel,
38
+ emptyText = '暂无数据'
39
+ }) {
40
+ const theme = useChartTheme(themeInput, palette);
41
+ const {
42
+ ref,
43
+ size
44
+ } = useElementSize();
45
+ const [activeKey, setActiveKey] = useState(null);
46
+ const animKey = typeof animation === 'object' ? JSON.stringify(animation) : String(animation);
47
+ const animConfig = useMemo(() => resolveAnimation(animation),
48
+ // eslint-disable-next-line react-hooks/exhaustive-deps
49
+ [animKey]);
50
+ const hierarchy = useMemo(() => {
51
+ if (!data) return {
52
+ cells: [],
53
+ total: 0,
54
+ leaves: 0
55
+ };
56
+ const total = sumValues(data);
57
+ if (total <= 0) return {
58
+ cells: [],
59
+ total: 0,
60
+ leaves: countLeaves(data)
61
+ };
62
+ const cells = buildTreemap(data, {
63
+ x: 0,
64
+ y: 0,
65
+ width: Math.max(0, size.width),
66
+ height: Math.max(0, size.height)
67
+ }, {
68
+ maxDepth,
69
+ headerHeight: 18,
70
+ theme,
71
+ colorOf: (depth, index, parentColor) => {
72
+ if (depth === 0) return theme.palette[index % theme.palette.length];
73
+ // 子层用父色向白混合,保持「同一父族颜色相近」的直觉
74
+ return mixColor(parentColor ?? theme.palette[0], '#ffffff', 0.46);
75
+ }
76
+ });
77
+ return {
78
+ cells,
79
+ total,
80
+ leaves: countLeaves(data)
81
+ };
82
+ }, [data, maxDepth, size.width, size.height, theme]);
83
+
84
+ // 用各格面积驱动动画:数据变化时格子平滑伸缩
85
+ const matrix = useMemo(() => [hierarchy.cells.map(cell => Math.round(cell.width * cell.height))], [hierarchy.cells]);
86
+ const transition = useChartTransition(matrix, animConfig);
87
+ const progress = transition.progress;
88
+ const scene = useMemo(() => {
89
+ if (!hierarchy.cells.length || size.width <= 0) return [];
90
+ const nodes = [];
91
+ hierarchy.cells.forEach((cell, index) => {
92
+ const local = staggerProgress(progress, index, hierarchy.cells.length, animConfig.stagger);
93
+ const key = `${cell.depth}-${cell.node.name}-${index}`;
94
+ const isActive = activeKey === key;
95
+ nodes.push({
96
+ kind: 'path',
97
+ key,
98
+ d: roundedRectPath(cell.x + gap / 2, cell.y + gap / 2, Math.max(0, cell.width - gap), Math.max(0, cell.height - gap), 3),
99
+ fill: cell.color,
100
+ // 子层淡一些,形成「父块包着子块」的视觉层次
101
+ opacity: (cell.depth === 0 ? 1 : 0.92) * local,
102
+ stroke: isActive ? theme.text.primary : undefined,
103
+ strokeWidth: isActive ? 2 : 0,
104
+ pointerEvents: 'none'
105
+ });
106
+ const boxWidth = cell.width - gap;
107
+ const boxHeight = cell.height - gap;
108
+ const showLabel = label === 'always' || label === 'auto' && boxWidth >= 56 && boxHeight >= 22 || cell.depth === 0 && boxWidth >= 56 && boxHeight >= 22;
109
+ if (!showLabel || label === false) return;
110
+ const hasChildren = Boolean(cell.node.children?.length);
111
+ const value = hasChildren ? sumValues(cell.node) : cell.node.value ?? 0;
112
+ const showNumber = showValue && boxHeight >= 40 && boxWidth >= 56;
113
+ // 有子块的格子把名称放在顶部标题条里;叶子居中
114
+ const labelY = hasChildren ? cell.y + 11 : cell.y + (showNumber ? 16 : boxHeight / 2 + gap);
115
+ nodes.push({
116
+ kind: 'text',
117
+ key: `${key}-label`,
118
+ x: cell.x + 8,
119
+ y: labelY,
120
+ text: cell.node.name,
121
+ fill: '#fff',
122
+ fontSize: cell.depth === 0 ? theme.font.size : theme.font.sizeSmall,
123
+ fontWeight: cell.depth === 0 ? theme.font.weightMedium : undefined,
124
+ anchor: 'start',
125
+ baseline: 'middle',
126
+ opacity: local,
127
+ fontFamily: theme.font.family
128
+ });
129
+
130
+ // 父块的汇总数值放到标题条右端,既不占额外高度又保留信息
131
+ if (hasChildren && showValue && boxWidth >= 130 && boxHeight >= 30) {
132
+ nodes.push({
133
+ kind: 'text',
134
+ key: `${key}-total`,
135
+ x: cell.x + boxWidth - 8,
136
+ y: labelY,
137
+ text: `${formatNumber(value, {
138
+ compact: true
139
+ })} · ${formatPercent(value, hierarchy.total, 1)}`,
140
+ fill: withAlpha('#ffffff', 0.85),
141
+ fontSize: theme.font.sizeSmall,
142
+ anchor: 'end',
143
+ baseline: 'middle',
144
+ opacity: local,
145
+ fontFamily: theme.font.family
146
+ });
147
+ }
148
+ if (showNumber && !hasChildren) {
149
+ nodes.push({
150
+ kind: 'text',
151
+ key: `${key}-value`,
152
+ x: cell.x + 8,
153
+ y: cell.y + 32,
154
+ text: `${formatNumber(value, {
155
+ compact: true
156
+ })} · ${formatPercent(value, hierarchy.total, 1)}`,
157
+ fill: withAlpha('#ffffff', 0.82),
158
+ fontSize: theme.font.sizeSmall,
159
+ anchor: 'start',
160
+ baseline: 'middle',
161
+ opacity: local,
162
+ fontFamily: theme.font.family
163
+ });
164
+ }
165
+ });
166
+ return nodes;
167
+ }, [hierarchy, size.width, gap, theme, progress, animConfig.stagger, activeKey, label, showValue]);
168
+
169
+ /** 命中检测:从后往前找第一个包含点的格子(深层画在上层) */
170
+ const cellFromPointer = (clientX, clientY) => {
171
+ const element = ref.current;
172
+ if (!element) return null;
173
+ const rect = element.getBoundingClientRect();
174
+ const x = clientX - rect.left;
175
+ const y = clientY - rect.top;
176
+ for (let index = hierarchy.cells.length - 1; index >= 0; index -= 1) {
177
+ const cell = hierarchy.cells[index];
178
+ if (x >= cell.x && x <= cell.x + cell.width && y >= cell.y && y <= cell.y + cell.height) {
179
+ return cell;
180
+ }
181
+ }
182
+ return null;
183
+ };
184
+ const activeCell = useMemo(() => {
185
+ if (!activeKey) return null;
186
+ return hierarchy.cells.find((cell, index) => `${cell.depth}-${cell.node.name}-${index}` === activeKey) ?? null;
187
+ }, [activeKey, hierarchy.cells]);
188
+ const tooltipPayload = activeCell ? {
189
+ label: activeCell.node.name,
190
+ row: null,
191
+ series: [{
192
+ key: activeCell.node.name,
193
+ name: '数值',
194
+ color: activeCell.color,
195
+ value: activeCell.node.value ?? sumValues(activeCell.node),
196
+ formatted: `${formatNumber(activeCell.node.value ?? sumValues(activeCell.node), {
197
+ compact: true
198
+ })}(${formatPercent(activeCell.node.value ?? sumValues(activeCell.node), hierarchy.total, 1)})`
199
+ }]
200
+ } : null;
201
+ const autoAriaLabel = ariaLabel ?? `矩形树图:共 ${hierarchy.leaves} 项,总计 ${formatNumber(hierarchy.total, {
202
+ compact: true
203
+ })}`;
204
+ return /*#__PURE__*/_jsx("div", {
205
+ className: classNames(prefixCls('chart'), className),
206
+ style: {
207
+ width,
208
+ height,
209
+ ...style
210
+ },
211
+ "data-aura-chart": "treemap",
212
+ children: /*#__PURE__*/_jsxs("div", {
213
+ ref: ref,
214
+ className: prefixCls('chart__plot'),
215
+ onPointerMove: event => {
216
+ const cell = cellFromPointer(event.clientX, event.clientY);
217
+ setActiveKey(cell ? `${cell.depth}-${cell.node.name}-${hierarchy.cells.indexOf(cell)}` : null);
218
+ },
219
+ onPointerLeave: () => setActiveKey(null),
220
+ children: [!hierarchy.cells.length || size.width === 0 ? /*#__PURE__*/_jsx("div", {
221
+ className: prefixCls('chart__empty'),
222
+ children: emptyText
223
+ }) : /*#__PURE__*/_jsx(SvgScene, {
224
+ nodes: scene,
225
+ width: size.width,
226
+ height: size.height,
227
+ ariaLabel: autoAriaLabel
228
+ }), /*#__PURE__*/_jsx(Tooltip, {
229
+ payload: tooltipPayload,
230
+ x: activeCell ? activeCell.x + activeCell.width / 2 : 0,
231
+ y: activeCell ? activeCell.y + 8 : 0,
232
+ containerWidth: size.width,
233
+ containerHeight: size.height,
234
+ theme: theme,
235
+ config: tooltip
236
+ })]
237
+ })
238
+ });
239
+ }
@@ -0,0 +1,41 @@
1
+ import React from 'react';
2
+ import { type ChartThemeInput } from '../../theme/index.js';
3
+ import type { TooltipConfig } from '../../types.js';
4
+ export interface WaterfallItem {
5
+ name: string;
6
+ /** 增量值:正为增加,负为减少 */
7
+ value: number;
8
+ /** 是否为汇总项(从 0 画到累计值) */
9
+ isTotal?: boolean;
10
+ color?: string;
11
+ }
12
+ export interface WaterfallChartProps {
13
+ data: WaterfallItem[];
14
+ width?: number | string;
15
+ height?: number;
16
+ /** 增加项颜色,默认主题色板首色 */
17
+ positiveColor?: string;
18
+ /** 减少项颜色,默认 Aura 错误色 */
19
+ negativeColor?: string;
20
+ /** 汇总项颜色 */
21
+ totalColor?: string;
22
+ /** 是否显示相邻柱之间的连接线 */
23
+ connector?: boolean;
24
+ theme?: ChartThemeInput;
25
+ palette?: string[];
26
+ tooltip?: boolean | TooltipConfig;
27
+ animation?: boolean;
28
+ className?: string;
29
+ style?: React.CSSProperties;
30
+ ariaLabel?: string;
31
+ emptyText?: string;
32
+ }
33
+ /**
34
+ * 瀑布图。
35
+ *
36
+ * 用柱子的「起点 + 终点」表达增减过程,最后落到总计。
37
+ * 与堆叠柱的区别:堆叠柱的每段都从 0 起算的相对量,瀑布图的每一段都是**增量**,
38
+ * 位置取决于此前所有项的累计。
39
+ */
40
+ export declare function WaterfallChart({ data, width, height, positiveColor, negativeColor, totalColor, connector, theme: themeInput, palette, tooltip, animation, className, style, ariaLabel, emptyText, }: WaterfallChartProps): React.JSX.Element;
41
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA4B,MAAM,OAAO,CAAC;AAYjD,OAAO,EAAW,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AAM5D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,6BAA6B,CAAC;AAErC,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,oBAAoB;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,wBAAwB;IACxB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,aAAa,EAAE,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,qBAAqB;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,wBAAwB;IACxB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,YAAY;IACZ,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,oBAAoB;IACpB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,KAAK,CAAC,EAAE,eAAe,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,OAAO,GAAG,aAAa,CAAC;IAClC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAWD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,EAC7B,IAAS,EACT,KAAc,EACd,MAAY,EACZ,aAAa,EACb,aAAyB,EACzB,UAAU,EACV,SAAgB,EAChB,KAAK,EAAE,UAAU,EACjB,OAAO,EACP,OAAc,EACd,SAAgB,EAChB,SAAS,EACT,KAAK,EACL,SAAS,EACT,SAAkB,GACnB,EAAE,mBAAmB,qBAsTrB"}