@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.
- package/esm/charts/area-chart/index.js +27 -23
- package/esm/charts/bar-chart/index.js +17 -21
- package/esm/charts/candlestick-chart/index.js +242 -180
- package/esm/charts/combo-chart/index.js +8 -20
- package/esm/charts/funnel-chart/index.js +133 -108
- package/esm/charts/gauge-chart/index.js +102 -105
- package/esm/charts/graph-chart/index.js +154 -114
- package/esm/charts/heatmap-chart/index.js +179 -135
- package/esm/charts/line-chart/index.js +28 -24
- package/esm/charts/pie-chart/index.d.ts.map +1 -1
- package/esm/charts/pie-chart/index.js +198 -139
- package/esm/charts/radar-chart/index.d.ts.map +1 -1
- package/esm/charts/radar-chart/index.js +154 -128
- package/esm/charts/ring-progress/index.js +81 -82
- package/esm/charts/sankey-chart/index.js +171 -141
- package/esm/charts/scatter-chart/index.d.ts.map +1 -1
- package/esm/charts/scatter-chart/index.js +225 -159
- package/esm/charts/sparkline/index.js +81 -66
- package/esm/charts/tree-chart/index.js +128 -102
- package/esm/charts/treemap-chart/index.js +162 -141
- package/esm/charts/waterfall-chart/index.js +165 -121
- package/esm/components/Legend.d.ts +3 -2
- package/esm/components/Legend.d.ts.map +1 -1
- package/esm/components/Legend.js +79 -48
- package/esm/components/Tooltip.js +62 -71
- package/esm/components/chart/index.d.ts +4 -1
- package/esm/components/chart/index.d.ts.map +1 -1
- package/esm/components/chart/index.js +80 -102
- package/esm/components/chart.less +13 -0
- package/esm/core/animation.js +31 -43
- package/esm/core/export.d.ts +34 -0
- package/esm/core/export.d.ts.map +1 -0
- package/esm/core/export.js +390 -0
- package/esm/core/force.js +22 -45
- package/esm/core/format.js +48 -52
- package/esm/core/hierarchy.js +60 -92
- package/esm/core/layout.js +32 -50
- package/esm/core/sampling.js +30 -53
- package/esm/core/sankey.js +35 -55
- package/esm/core/scale.js +72 -86
- package/esm/core/shape.js +101 -133
- package/esm/core/sync.d.ts +41 -0
- package/esm/core/sync.d.ts.map +1 -0
- package/esm/core/sync.js +120 -0
- package/esm/hooks/useChartExport.d.ts +19 -0
- package/esm/hooks/useChartExport.d.ts.map +1 -0
- package/esm/hooks/useChartExport.js +35 -0
- package/esm/hooks/useChartTheme.js +17 -19
- package/esm/hooks/useChartTransition.js +58 -73
- package/esm/hooks/useElementSize.js +19 -28
- package/esm/hooks/usePlotPointer.d.ts +23 -0
- package/esm/hooks/usePlotPointer.d.ts.map +1 -0
- package/esm/hooks/usePlotPointer.js +94 -0
- package/esm/index.d.ts +6 -0
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +136 -47
- package/esm/internal/CartesianChart.d.ts.map +1 -1
- package/esm/internal/CartesianChart.js +666 -485
- package/esm/internal/model.js +44 -60
- package/esm/scene/canvas.js +100 -107
- package/esm/scene/svg.js +106 -99
- package/esm/scene/types.js +8 -15
- package/esm/style.css +10 -0
- package/esm/theme/index.js +143 -171
- package/esm/types.d.ts +4 -0
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js +0 -1
- package/package.json +1 -1
|
@@ -1,32 +1,29 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
import {
|
|
2
|
+
// src/charts/treemap-chart/index.tsx
|
|
3
|
+
import { useMemo, useState } from "react";
|
|
4
|
+
import { classNames, prefixCls } from "@aura-react-comp/shared";
|
|
4
5
|
import { SvgScene } from "../../scene/svg";
|
|
5
6
|
import { formatNumber, formatPercent } from "../../core/format";
|
|
6
7
|
import { roundedRectPath } from "../../core/shape";
|
|
7
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
buildTreemap,
|
|
10
|
+
countLeaves,
|
|
11
|
+
sumValues
|
|
12
|
+
} from "../../core/hierarchy";
|
|
8
13
|
import { mixColor, withAlpha } from "../../theme";
|
|
9
14
|
import { useChartTheme } from "../../hooks/useChartTheme";
|
|
10
15
|
import { useElementSize } from "../../hooks/useElementSize";
|
|
11
16
|
import { useChartTransition } from "../../hooks/useChartTransition";
|
|
12
17
|
import { resolveAnimation, staggerProgress } from "../../core/animation";
|
|
13
18
|
import { Tooltip } from "../../components/Tooltip";
|
|
14
|
-
import { jsx
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 矩形树图。
|
|
18
|
-
*
|
|
19
|
-
* 用面积编码数值,能在有限空间里同时表达「占比」与「层级」。
|
|
20
|
-
* 布局用 squarified 算法:让每块尽量接近正方形,
|
|
21
|
-
* 既放得下文字,也便于人眼比较面积。
|
|
22
|
-
*/
|
|
23
|
-
export function TreemapChart({
|
|
19
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
20
|
+
function TreemapChart({
|
|
24
21
|
data,
|
|
25
22
|
maxDepth = 1,
|
|
26
|
-
label =
|
|
23
|
+
label = "auto",
|
|
27
24
|
showValue = true,
|
|
28
25
|
gap = 2,
|
|
29
|
-
width =
|
|
26
|
+
width = "100%",
|
|
30
27
|
height = 320,
|
|
31
28
|
theme: themeInput,
|
|
32
29
|
palette,
|
|
@@ -35,141 +32,151 @@ export function TreemapChart({
|
|
|
35
32
|
className,
|
|
36
33
|
style,
|
|
37
34
|
ariaLabel,
|
|
38
|
-
emptyText =
|
|
35
|
+
emptyText = "暂无数据"
|
|
39
36
|
}) {
|
|
40
37
|
const theme = useChartTheme(themeInput, palette);
|
|
41
|
-
const {
|
|
42
|
-
ref,
|
|
43
|
-
size
|
|
44
|
-
} = useElementSize();
|
|
38
|
+
const { ref, size } = useElementSize();
|
|
45
39
|
const [activeKey, setActiveKey] = useState(null);
|
|
46
|
-
const animKey = typeof animation ===
|
|
47
|
-
const animConfig = useMemo(
|
|
48
|
-
|
|
49
|
-
|
|
40
|
+
const animKey = typeof animation === "object" ? JSON.stringify(animation) : String(animation);
|
|
41
|
+
const animConfig = useMemo(
|
|
42
|
+
() => resolveAnimation(animation),
|
|
43
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
44
|
+
[animKey]
|
|
45
|
+
);
|
|
50
46
|
const hierarchy = useMemo(() => {
|
|
51
|
-
if (!data)
|
|
52
|
-
cells: [],
|
|
53
|
-
total: 0,
|
|
54
|
-
leaves: 0
|
|
55
|
-
};
|
|
47
|
+
if (!data)
|
|
48
|
+
return { cells: [], total: 0, leaves: 0 };
|
|
56
49
|
const total = sumValues(data);
|
|
57
|
-
if (total <= 0)
|
|
58
|
-
cells: [],
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
50
|
+
if (total <= 0)
|
|
51
|
+
return { cells: [], total: 0, leaves: countLeaves(data) };
|
|
52
|
+
const cells = buildTreemap(
|
|
53
|
+
data,
|
|
54
|
+
{
|
|
55
|
+
x: 0,
|
|
56
|
+
y: 0,
|
|
57
|
+
width: Math.max(0, size.width),
|
|
58
|
+
height: Math.max(0, size.height)
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
maxDepth,
|
|
62
|
+
headerHeight: 18,
|
|
63
|
+
theme,
|
|
64
|
+
colorOf: (depth, index, parentColor) => {
|
|
65
|
+
if (depth === 0)
|
|
66
|
+
return theme.palette[index % theme.palette.length];
|
|
67
|
+
return mixColor(parentColor ?? theme.palette[0], "#ffffff", 0.46);
|
|
68
|
+
}
|
|
75
69
|
}
|
|
76
|
-
|
|
77
|
-
return {
|
|
78
|
-
cells,
|
|
79
|
-
total,
|
|
80
|
-
leaves: countLeaves(data)
|
|
81
|
-
};
|
|
70
|
+
);
|
|
71
|
+
return { cells, total, leaves: countLeaves(data) };
|
|
82
72
|
}, [data, maxDepth, size.width, size.height, theme]);
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
73
|
+
const matrix = useMemo(
|
|
74
|
+
() => [hierarchy.cells.map((cell) => Math.round(cell.width * cell.height))],
|
|
75
|
+
[hierarchy.cells]
|
|
76
|
+
);
|
|
86
77
|
const transition = useChartTransition(matrix, animConfig);
|
|
87
78
|
const progress = transition.progress;
|
|
88
79
|
const scene = useMemo(() => {
|
|
89
|
-
if (!hierarchy.cells.length || size.width <= 0)
|
|
80
|
+
if (!hierarchy.cells.length || size.width <= 0)
|
|
81
|
+
return [];
|
|
90
82
|
const nodes = [];
|
|
91
83
|
hierarchy.cells.forEach((cell, index) => {
|
|
92
|
-
|
|
84
|
+
var _a;
|
|
85
|
+
const local = staggerProgress(
|
|
86
|
+
progress,
|
|
87
|
+
index,
|
|
88
|
+
hierarchy.cells.length,
|
|
89
|
+
animConfig.stagger
|
|
90
|
+
);
|
|
93
91
|
const key = `${cell.depth}-${cell.node.name}-${index}`;
|
|
94
92
|
const isActive = activeKey === key;
|
|
95
93
|
nodes.push({
|
|
96
|
-
kind:
|
|
94
|
+
kind: "path",
|
|
97
95
|
key,
|
|
98
|
-
d: roundedRectPath(
|
|
96
|
+
d: roundedRectPath(
|
|
97
|
+
cell.x + gap / 2,
|
|
98
|
+
cell.y + gap / 2,
|
|
99
|
+
Math.max(0, cell.width - gap),
|
|
100
|
+
Math.max(0, cell.height - gap),
|
|
101
|
+
3
|
|
102
|
+
),
|
|
99
103
|
fill: cell.color,
|
|
100
104
|
// 子层淡一些,形成「父块包着子块」的视觉层次
|
|
101
105
|
opacity: (cell.depth === 0 ? 1 : 0.92) * local,
|
|
102
|
-
stroke: isActive ? theme.text.primary :
|
|
106
|
+
stroke: isActive ? theme.text.primary : void 0,
|
|
103
107
|
strokeWidth: isActive ? 2 : 0,
|
|
104
|
-
pointerEvents:
|
|
108
|
+
pointerEvents: "none"
|
|
105
109
|
});
|
|
106
110
|
const boxWidth = cell.width - gap;
|
|
107
111
|
const boxHeight = cell.height - gap;
|
|
108
|
-
const showLabel = label ===
|
|
109
|
-
if (!showLabel || label === false)
|
|
110
|
-
|
|
112
|
+
const showLabel = label === "always" || label === "auto" && boxWidth >= 56 && boxHeight >= 22 || cell.depth === 0 && boxWidth >= 56 && boxHeight >= 22;
|
|
113
|
+
if (!showLabel || label === false)
|
|
114
|
+
return;
|
|
115
|
+
const hasChildren = Boolean((_a = cell.node.children) == null ? void 0 : _a.length);
|
|
111
116
|
const value = hasChildren ? sumValues(cell.node) : cell.node.value ?? 0;
|
|
112
117
|
const showNumber = showValue && boxHeight >= 40 && boxWidth >= 56;
|
|
113
|
-
// 有子块的格子把名称放在顶部标题条里;叶子居中
|
|
114
118
|
const labelY = hasChildren ? cell.y + 11 : cell.y + (showNumber ? 16 : boxHeight / 2 + gap);
|
|
115
119
|
nodes.push({
|
|
116
|
-
kind:
|
|
120
|
+
kind: "text",
|
|
117
121
|
key: `${key}-label`,
|
|
118
122
|
x: cell.x + 8,
|
|
119
123
|
y: labelY,
|
|
120
124
|
text: cell.node.name,
|
|
121
|
-
fill:
|
|
125
|
+
fill: "#fff",
|
|
122
126
|
fontSize: cell.depth === 0 ? theme.font.size : theme.font.sizeSmall,
|
|
123
|
-
fontWeight: cell.depth === 0 ? theme.font.weightMedium :
|
|
124
|
-
anchor:
|
|
125
|
-
baseline:
|
|
127
|
+
fontWeight: cell.depth === 0 ? theme.font.weightMedium : void 0,
|
|
128
|
+
anchor: "start",
|
|
129
|
+
baseline: "middle",
|
|
126
130
|
opacity: local,
|
|
127
131
|
fontFamily: theme.font.family
|
|
128
132
|
});
|
|
129
|
-
|
|
130
|
-
// 父块的汇总数值放到标题条右端,既不占额外高度又保留信息
|
|
131
133
|
if (hasChildren && showValue && boxWidth >= 130 && boxHeight >= 30) {
|
|
132
134
|
nodes.push({
|
|
133
|
-
kind:
|
|
135
|
+
kind: "text",
|
|
134
136
|
key: `${key}-total`,
|
|
135
137
|
x: cell.x + boxWidth - 8,
|
|
136
138
|
y: labelY,
|
|
137
|
-
text: `${formatNumber(value, {
|
|
138
|
-
|
|
139
|
-
})} · ${formatPercent(value, hierarchy.total, 1)}`,
|
|
140
|
-
fill: withAlpha('#ffffff', 0.85),
|
|
139
|
+
text: `${formatNumber(value, { compact: true })} · ${formatPercent(value, hierarchy.total, 1)}`,
|
|
140
|
+
fill: withAlpha("#ffffff", 0.85),
|
|
141
141
|
fontSize: theme.font.sizeSmall,
|
|
142
|
-
anchor:
|
|
143
|
-
baseline:
|
|
142
|
+
anchor: "end",
|
|
143
|
+
baseline: "middle",
|
|
144
144
|
opacity: local,
|
|
145
145
|
fontFamily: theme.font.family
|
|
146
146
|
});
|
|
147
147
|
}
|
|
148
148
|
if (showNumber && !hasChildren) {
|
|
149
149
|
nodes.push({
|
|
150
|
-
kind:
|
|
150
|
+
kind: "text",
|
|
151
151
|
key: `${key}-value`,
|
|
152
152
|
x: cell.x + 8,
|
|
153
153
|
y: cell.y + 32,
|
|
154
|
-
text: `${formatNumber(value, {
|
|
155
|
-
|
|
156
|
-
})} · ${formatPercent(value, hierarchy.total, 1)}`,
|
|
157
|
-
fill: withAlpha('#ffffff', 0.82),
|
|
154
|
+
text: `${formatNumber(value, { compact: true })} · ${formatPercent(value, hierarchy.total, 1)}`,
|
|
155
|
+
fill: withAlpha("#ffffff", 0.82),
|
|
158
156
|
fontSize: theme.font.sizeSmall,
|
|
159
|
-
anchor:
|
|
160
|
-
baseline:
|
|
157
|
+
anchor: "start",
|
|
158
|
+
baseline: "middle",
|
|
161
159
|
opacity: local,
|
|
162
160
|
fontFamily: theme.font.family
|
|
163
161
|
});
|
|
164
162
|
}
|
|
165
163
|
});
|
|
166
164
|
return nodes;
|
|
167
|
-
}, [
|
|
168
|
-
|
|
169
|
-
|
|
165
|
+
}, [
|
|
166
|
+
hierarchy,
|
|
167
|
+
size.width,
|
|
168
|
+
gap,
|
|
169
|
+
theme,
|
|
170
|
+
progress,
|
|
171
|
+
animConfig.stagger,
|
|
172
|
+
activeKey,
|
|
173
|
+
label,
|
|
174
|
+
showValue
|
|
175
|
+
]);
|
|
170
176
|
const cellFromPointer = (clientX, clientY) => {
|
|
171
177
|
const element = ref.current;
|
|
172
|
-
if (!element)
|
|
178
|
+
if (!element)
|
|
179
|
+
return null;
|
|
173
180
|
const rect = element.getBoundingClientRect();
|
|
174
181
|
const x = clientX - rect.left;
|
|
175
182
|
const y = clientY - rect.top;
|
|
@@ -182,58 +189,72 @@ export function TreemapChart({
|
|
|
182
189
|
return null;
|
|
183
190
|
};
|
|
184
191
|
const activeCell = useMemo(() => {
|
|
185
|
-
if (!activeKey)
|
|
186
|
-
|
|
192
|
+
if (!activeKey)
|
|
193
|
+
return null;
|
|
194
|
+
return hierarchy.cells.find(
|
|
195
|
+
(cell, index) => `${cell.depth}-${cell.node.name}-${index}` === activeKey
|
|
196
|
+
) ?? null;
|
|
187
197
|
}, [activeKey, hierarchy.cells]);
|
|
188
198
|
const tooltipPayload = activeCell ? {
|
|
189
199
|
label: activeCell.node.name,
|
|
190
200
|
row: null,
|
|
191
|
-
series: [
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
compact: true
|
|
198
|
-
}
|
|
199
|
-
|
|
201
|
+
series: [
|
|
202
|
+
{
|
|
203
|
+
key: activeCell.node.name,
|
|
204
|
+
name: "数值",
|
|
205
|
+
color: activeCell.color,
|
|
206
|
+
value: activeCell.node.value ?? sumValues(activeCell.node),
|
|
207
|
+
formatted: `${formatNumber(activeCell.node.value ?? sumValues(activeCell.node), { compact: true })}(${formatPercent(activeCell.node.value ?? sumValues(activeCell.node), hierarchy.total, 1)})`
|
|
208
|
+
}
|
|
209
|
+
]
|
|
200
210
|
} : null;
|
|
201
|
-
const autoAriaLabel = ariaLabel ?? `矩形树图:共 ${hierarchy.leaves} 项,总计 ${formatNumber(hierarchy.total, {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
211
|
+
const autoAriaLabel = ariaLabel ?? `矩形树图:共 ${hierarchy.leaves} 项,总计 ${formatNumber(hierarchy.total, { compact: true })}`;
|
|
212
|
+
return /* @__PURE__ */ jsx(
|
|
213
|
+
"div",
|
|
214
|
+
{
|
|
215
|
+
className: classNames(prefixCls("chart"), className),
|
|
216
|
+
style: { width, height, ...style },
|
|
217
|
+
"data-aura-chart": "treemap",
|
|
218
|
+
children: /* @__PURE__ */ jsxs(
|
|
219
|
+
"div",
|
|
220
|
+
{
|
|
221
|
+
ref,
|
|
222
|
+
className: prefixCls("chart__plot"),
|
|
223
|
+
onPointerMove: (event) => {
|
|
224
|
+
const cell = cellFromPointer(event.clientX, event.clientY);
|
|
225
|
+
setActiveKey(
|
|
226
|
+
cell ? `${cell.depth}-${cell.node.name}-${hierarchy.cells.indexOf(cell)}` : null
|
|
227
|
+
);
|
|
228
|
+
},
|
|
229
|
+
onPointerLeave: () => setActiveKey(null),
|
|
230
|
+
children: [
|
|
231
|
+
!hierarchy.cells.length || size.width === 0 ? /* @__PURE__ */ jsx("div", { className: prefixCls("chart__empty"), children: emptyText }) : /* @__PURE__ */ jsx(
|
|
232
|
+
SvgScene,
|
|
233
|
+
{
|
|
234
|
+
nodes: scene,
|
|
235
|
+
width: size.width,
|
|
236
|
+
height: size.height,
|
|
237
|
+
ariaLabel: autoAriaLabel
|
|
238
|
+
}
|
|
239
|
+
),
|
|
240
|
+
/* @__PURE__ */ jsx(
|
|
241
|
+
Tooltip,
|
|
242
|
+
{
|
|
243
|
+
payload: tooltipPayload,
|
|
244
|
+
x: activeCell ? activeCell.x + activeCell.width / 2 : 0,
|
|
245
|
+
y: activeCell ? activeCell.y + 8 : 0,
|
|
246
|
+
containerWidth: size.width,
|
|
247
|
+
containerHeight: size.height,
|
|
248
|
+
theme,
|
|
249
|
+
config: tooltip
|
|
250
|
+
}
|
|
251
|
+
)
|
|
252
|
+
]
|
|
253
|
+
}
|
|
254
|
+
)
|
|
255
|
+
}
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
export {
|
|
259
|
+
TreemapChart
|
|
260
|
+
};
|