@aura-react-comp/charts 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +90 -2
- package/esm/charts/area-chart/index.d.ts +19 -0
- package/esm/charts/area-chart/index.d.ts.map +1 -0
- package/esm/charts/area-chart/index.js +30 -0
- package/esm/charts/bar-chart/index.d.ts +16 -0
- package/esm/charts/bar-chart/index.d.ts.map +1 -0
- package/esm/charts/bar-chart/index.js +27 -0
- package/esm/charts/candlestick-chart/index.d.ts +47 -0
- package/esm/charts/candlestick-chart/index.d.ts.map +1 -0
- package/esm/charts/candlestick-chart/index.js +346 -0
- package/esm/charts/combo-chart/index.d.ts +17 -0
- package/esm/charts/combo-chart/index.d.ts.map +1 -0
- package/esm/charts/combo-chart/index.js +25 -0
- package/esm/charts/funnel-chart/index.d.ts +35 -0
- package/esm/charts/funnel-chart/index.d.ts.map +1 -0
- package/esm/charts/funnel-chart/index.js +199 -0
- package/esm/charts/gauge-chart/index.d.ts +41 -0
- package/esm/charts/gauge-chart/index.d.ts.map +1 -0
- package/esm/charts/gauge-chart/index.js +216 -0
- package/esm/charts/graph-chart/index.d.ts +35 -0
- package/esm/charts/graph-chart/index.d.ts.map +1 -0
- package/esm/charts/graph-chart/index.js +223 -0
- package/esm/charts/heatmap-chart/index.d.ts +47 -0
- package/esm/charts/heatmap-chart/index.d.ts.map +1 -0
- package/esm/charts/heatmap-chart/index.js +280 -0
- package/esm/charts/line-chart/index.d.ts +23 -0
- package/esm/charts/line-chart/index.d.ts.map +1 -0
- package/esm/charts/line-chart/index.js +32 -0
- package/esm/charts/pie-chart/index.d.ts +38 -0
- package/esm/charts/pie-chart/index.d.ts.map +1 -0
- package/esm/charts/pie-chart/index.js +271 -0
- package/esm/charts/radar-chart/index.d.ts +44 -0
- package/esm/charts/radar-chart/index.d.ts.map +1 -0
- package/esm/charts/radar-chart/index.js +262 -0
- package/esm/charts/ring-progress/index.d.ts +38 -0
- package/esm/charts/ring-progress/index.d.ts.map +1 -0
- package/esm/charts/ring-progress/index.js +136 -0
- package/esm/charts/sankey-chart/index.d.ts +34 -0
- package/esm/charts/sankey-chart/index.d.ts.map +1 -0
- package/esm/charts/sankey-chart/index.js +247 -0
- package/esm/charts/scatter-chart/index.d.ts +34 -0
- package/esm/charts/scatter-chart/index.d.ts.map +1 -0
- package/esm/charts/scatter-chart/index.js +304 -0
- package/esm/charts/sparkline/index.d.ts +31 -0
- package/esm/charts/sparkline/index.d.ts.map +1 -0
- package/esm/charts/sparkline/index.js +151 -0
- package/esm/charts/tree-chart/index.d.ts +34 -0
- package/esm/charts/tree-chart/index.d.ts.map +1 -0
- package/esm/charts/tree-chart/index.js +199 -0
- package/esm/charts/treemap-chart/index.d.ts +35 -0
- package/esm/charts/treemap-chart/index.d.ts.map +1 -0
- package/esm/charts/treemap-chart/index.js +238 -0
- package/esm/charts/waterfall-chart/index.d.ts +41 -0
- package/esm/charts/waterfall-chart/index.d.ts.map +1 -0
- package/esm/charts/waterfall-chart/index.js +280 -0
- package/esm/components/Legend.d.ts +24 -0
- package/esm/components/Legend.d.ts.map +1 -0
- package/esm/components/Legend.js +49 -0
- package/esm/components/Tooltip.d.ts +24 -0
- package/esm/components/Tooltip.d.ts.map +1 -0
- package/esm/components/Tooltip.js +84 -0
- package/esm/components/chart/index.d.ts +71 -0
- package/esm/components/chart/index.d.ts.map +1 -0
- package/esm/components/chart/index.js +141 -0
- package/esm/components/chart.less +223 -0
- package/esm/core/animation.d.ts +49 -0
- package/esm/core/animation.d.ts.map +1 -0
- package/esm/core/animation.js +92 -0
- package/esm/core/force.d.ts +43 -0
- package/esm/core/force.d.ts.map +1 -0
- package/esm/core/force.js +133 -0
- package/esm/core/format.d.ts +34 -0
- package/esm/core/format.d.ts.map +1 -0
- package/esm/core/format.js +80 -0
- package/esm/core/hierarchy.d.ts +77 -0
- package/esm/core/hierarchy.d.ts.map +1 -0
- package/esm/core/hierarchy.js +232 -0
- package/esm/core/layout.d.ts +67 -0
- package/esm/core/layout.d.ts.map +1 -0
- package/esm/core/layout.js +120 -0
- package/esm/core/sampling.d.ts +32 -0
- package/esm/core/sampling.d.ts.map +1 -0
- package/esm/core/sampling.js +123 -0
- package/esm/core/sankey.d.ts +67 -0
- package/esm/core/sankey.d.ts.map +1 -0
- package/esm/core/sankey.js +135 -0
- package/esm/core/scale.d.ts +98 -0
- package/esm/core/scale.d.ts.map +1 -0
- package/esm/core/scale.js +206 -0
- package/esm/core/shape.d.ts +60 -0
- package/esm/core/shape.d.ts.map +1 -0
- package/esm/core/shape.js +245 -0
- package/esm/global.d.ts +6 -0
- package/esm/hooks/useChartTheme.d.ts +10 -0
- package/esm/hooks/useChartTheme.d.ts.map +1 -0
- package/esm/hooks/useChartTheme.js +33 -0
- package/esm/hooks/useChartTransition.d.ts +20 -0
- package/esm/hooks/useChartTransition.d.ts.map +1 -0
- package/esm/hooks/useChartTransition.js +119 -0
- package/esm/hooks/useElementSize.d.ts +17 -0
- package/esm/hooks/useElementSize.d.ts.map +1 -0
- package/esm/hooks/useElementSize.js +46 -0
- package/esm/index.d.ts +62 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +47 -0
- package/esm/internal/CartesianChart.d.ts +16 -0
- package/esm/internal/CartesianChart.d.ts.map +1 -0
- package/esm/internal/CartesianChart.js +810 -0
- package/esm/internal/model.d.ts +67 -0
- package/esm/internal/model.d.ts.map +1 -0
- package/esm/internal/model.js +145 -0
- package/esm/scene/canvas.d.ts +18 -0
- package/esm/scene/canvas.d.ts.map +1 -0
- package/esm/scene/canvas.js +137 -0
- package/esm/scene/svg.d.ts +12 -0
- package/esm/scene/svg.d.ts.map +1 -0
- package/esm/scene/svg.js +115 -0
- package/esm/scene/types.d.ts +89 -0
- package/esm/scene/types.d.ts.map +1 -0
- package/esm/scene/types.js +20 -0
- package/esm/style.css +196 -0
- package/esm/theme/index.d.ts +127 -0
- package/esm/theme/index.d.ts.map +1 -0
- package/esm/theme/index.js +297 -0
- package/esm/types.d.ts +168 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/types.js +1 -0
- package/package.json +64 -3
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import React, { useMemo, useState } from 'react';
|
|
2
|
+
import { classNames, prefixCls } from '@aura-react-comp/shared';
|
|
3
|
+
import { SvgScene } from "../../scene/svg";
|
|
4
|
+
import { formatNumber } from "../../core/format";
|
|
5
|
+
import { layoutTree, maxDepth, sumValues, treeLinkPath } from "../../core/hierarchy";
|
|
6
|
+
import { withAlpha } from "../../theme";
|
|
7
|
+
import { useChartTheme } from "../../hooks/useChartTheme";
|
|
8
|
+
import { useElementSize } from "../../hooks/useElementSize";
|
|
9
|
+
import { useChartTransition } from "../../hooks/useChartTransition";
|
|
10
|
+
import { resolveAnimation, staggerProgress } from "../../core/animation";
|
|
11
|
+
import { Tooltip } from "../../components/Tooltip";
|
|
12
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
14
|
+
/**
|
|
15
|
+
* 树图(横向展开)。
|
|
16
|
+
*
|
|
17
|
+
* 与矩形树图的分工:矩形树图用面积编码数值、回答「谁占得多」;
|
|
18
|
+
* 树图只表达层级结构与从属关系,节点大小恒定。
|
|
19
|
+
*
|
|
20
|
+
* 纵向位置按叶子顺序均分,父节点取子节点均值 —— 这样连线不交叉,
|
|
21
|
+
* 同一父节点下的兄弟会自然聚成一簇。
|
|
22
|
+
*/
|
|
23
|
+
export function TreeChart({
|
|
24
|
+
data,
|
|
25
|
+
nodeSize = 4,
|
|
26
|
+
showValue = false,
|
|
27
|
+
labelSpace = 96,
|
|
28
|
+
width = '100%',
|
|
29
|
+
height = 360,
|
|
30
|
+
theme: themeInput,
|
|
31
|
+
palette,
|
|
32
|
+
tooltip = true,
|
|
33
|
+
animation = true,
|
|
34
|
+
className,
|
|
35
|
+
style,
|
|
36
|
+
ariaLabel,
|
|
37
|
+
emptyText = '暂无数据'
|
|
38
|
+
}) {
|
|
39
|
+
const theme = useChartTheme(themeInput, palette);
|
|
40
|
+
const {
|
|
41
|
+
ref,
|
|
42
|
+
size
|
|
43
|
+
} = useElementSize();
|
|
44
|
+
const [activeIndex, setActiveIndex] = useState(null);
|
|
45
|
+
const animKey = typeof animation === 'object' ? JSON.stringify(animation) : String(animation);
|
|
46
|
+
const animConfig = useMemo(() => resolveAnimation(animation),
|
|
47
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
48
|
+
[animKey]);
|
|
49
|
+
const layout = useMemo(() => {
|
|
50
|
+
if (!data) return {
|
|
51
|
+
nodes: [],
|
|
52
|
+
depth: 0
|
|
53
|
+
};
|
|
54
|
+
return layoutTree(data, {
|
|
55
|
+
x: 12,
|
|
56
|
+
y: 16,
|
|
57
|
+
// 右侧留出文字空间,否则最深一层的标签会被裁掉
|
|
58
|
+
width: Math.max(0, size.width - 24 - labelSpace),
|
|
59
|
+
height: Math.max(0, size.height - 32)
|
|
60
|
+
});
|
|
61
|
+
}, [data, size.width, size.height, labelSpace]);
|
|
62
|
+
|
|
63
|
+
// 用叶子数驱动:结构变化时节点位置平滑过渡
|
|
64
|
+
const matrix = useMemo(() => [layout.nodes.map(item => Math.round(item.y))], [layout.nodes]);
|
|
65
|
+
const transition = useChartTransition(matrix, animConfig);
|
|
66
|
+
const progress = transition.progress;
|
|
67
|
+
const scene = useMemo(() => {
|
|
68
|
+
if (!layout.nodes.length || size.width <= 0) return [];
|
|
69
|
+
const nodes = [];
|
|
70
|
+
const depthTotal = Math.max(1, maxDepth(data ?? {
|
|
71
|
+
name: ''
|
|
72
|
+
}));
|
|
73
|
+
|
|
74
|
+
// 先画连线,保证节点压在线上
|
|
75
|
+
layout.nodes.forEach((item, index) => {
|
|
76
|
+
if (item.parent < 0) return;
|
|
77
|
+
const parent = layout.nodes[item.parent];
|
|
78
|
+
const local = staggerProgress(progress, item.depth, depthTotal, animConfig.stagger);
|
|
79
|
+
const animatedParent = {
|
|
80
|
+
...parent,
|
|
81
|
+
y: transition.values[0]?.[item.parent] ?? parent.y
|
|
82
|
+
};
|
|
83
|
+
const animatedChild = {
|
|
84
|
+
...item,
|
|
85
|
+
y: transition.values[0]?.[index] ?? item.y
|
|
86
|
+
};
|
|
87
|
+
nodes.push({
|
|
88
|
+
kind: 'path',
|
|
89
|
+
key: `link-${index}`,
|
|
90
|
+
d: treeLinkPath(animatedParent, animatedChild, nodeSize),
|
|
91
|
+
fill: 'none',
|
|
92
|
+
stroke: withAlpha(theme.axis.line, 0.9),
|
|
93
|
+
strokeWidth: 1.2,
|
|
94
|
+
opacity: local,
|
|
95
|
+
pointerEvents: 'none'
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
layout.nodes.forEach((item, index) => {
|
|
99
|
+
const y = transition.values[0]?.[index] ?? item.y;
|
|
100
|
+
const isActive = activeIndex === index;
|
|
101
|
+
const hasChildren = Boolean(item.node.children?.length);
|
|
102
|
+
const local = staggerProgress(progress, item.depth, depthTotal, animConfig.stagger);
|
|
103
|
+
const color = hasChildren ? theme.emphasis : item.node.color ?? theme.palette[item.depth % theme.palette.length];
|
|
104
|
+
nodes.push({
|
|
105
|
+
kind: 'circle',
|
|
106
|
+
key: `node-${index}`,
|
|
107
|
+
cx: item.x,
|
|
108
|
+
cy: y,
|
|
109
|
+
r: isActive ? nodeSize + 2 : nodeSize,
|
|
110
|
+
fill: hasChildren ? theme.tooltip.background : color,
|
|
111
|
+
stroke: color,
|
|
112
|
+
strokeWidth: hasChildren ? 2 : 0,
|
|
113
|
+
opacity: local,
|
|
114
|
+
pointerEvents: 'none'
|
|
115
|
+
});
|
|
116
|
+
nodes.push({
|
|
117
|
+
kind: 'text',
|
|
118
|
+
key: `label-${index}`,
|
|
119
|
+
x: item.x + nodeSize + 8,
|
|
120
|
+
y,
|
|
121
|
+
text: showValue && item.node.value !== undefined ? `${item.node.name} ${formatNumber(item.node.value, {
|
|
122
|
+
compact: true
|
|
123
|
+
})}` : item.node.name,
|
|
124
|
+
fill: hasChildren ? theme.text.primary : theme.text.secondary,
|
|
125
|
+
fontSize: theme.font.size,
|
|
126
|
+
fontWeight: hasChildren ? theme.font.weightMedium : undefined,
|
|
127
|
+
anchor: 'start',
|
|
128
|
+
baseline: 'middle',
|
|
129
|
+
opacity: local,
|
|
130
|
+
fontFamily: theme.font.family
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
return nodes;
|
|
134
|
+
}, [layout, size.width, data, theme, progress, animConfig.stagger, activeIndex, nodeSize, showValue, transition.values]);
|
|
135
|
+
const nodeFromPointer = (clientX, clientY) => {
|
|
136
|
+
const element = ref.current;
|
|
137
|
+
if (!element) return null;
|
|
138
|
+
const rect = element.getBoundingClientRect();
|
|
139
|
+
const x = clientX - rect.left;
|
|
140
|
+
const y = clientY - rect.top;
|
|
141
|
+
let nearest = null;
|
|
142
|
+
let min = 18; // 命中半径,节点本身只有 4px,必须放宽
|
|
143
|
+
layout.nodes.forEach((item, index) => {
|
|
144
|
+
const distance = Math.hypot(item.x - x, item.y - y);
|
|
145
|
+
if (distance < min) {
|
|
146
|
+
min = distance;
|
|
147
|
+
nearest = index;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
return nearest;
|
|
151
|
+
};
|
|
152
|
+
const active = activeIndex !== null ? layout.nodes[activeIndex] : null;
|
|
153
|
+
const tooltipPayload = active ? {
|
|
154
|
+
label: active.node.name,
|
|
155
|
+
row: null,
|
|
156
|
+
series: [{
|
|
157
|
+
key: 'value',
|
|
158
|
+
name: active.node.children?.length ? '合计' : '数值',
|
|
159
|
+
color: theme.emphasis,
|
|
160
|
+
value: sumValues(active.node),
|
|
161
|
+
formatted: formatNumber(sumValues(active.node), {
|
|
162
|
+
compact: true
|
|
163
|
+
})
|
|
164
|
+
}]
|
|
165
|
+
} : null;
|
|
166
|
+
const autoAriaLabel = ariaLabel ?? `树图:${layout.nodes.length} 个节点`;
|
|
167
|
+
return /*#__PURE__*/_jsx("div", {
|
|
168
|
+
className: classNames(prefixCls('chart'), className),
|
|
169
|
+
style: {
|
|
170
|
+
width,
|
|
171
|
+
height,
|
|
172
|
+
...style
|
|
173
|
+
},
|
|
174
|
+
"data-aura-chart": "tree",
|
|
175
|
+
children: /*#__PURE__*/_jsxs("div", {
|
|
176
|
+
ref: ref,
|
|
177
|
+
className: prefixCls('chart__plot'),
|
|
178
|
+
onPointerMove: event => setActiveIndex(nodeFromPointer(event.clientX, event.clientY)),
|
|
179
|
+
onPointerLeave: () => setActiveIndex(null),
|
|
180
|
+
children: [!layout.nodes.length || size.width === 0 ? /*#__PURE__*/_jsx("div", {
|
|
181
|
+
className: prefixCls('chart__empty'),
|
|
182
|
+
children: emptyText
|
|
183
|
+
}) : /*#__PURE__*/_jsx(SvgScene, {
|
|
184
|
+
nodes: scene,
|
|
185
|
+
width: size.width,
|
|
186
|
+
height: size.height,
|
|
187
|
+
ariaLabel: autoAriaLabel
|
|
188
|
+
}), /*#__PURE__*/_jsx(Tooltip, {
|
|
189
|
+
payload: tooltipPayload,
|
|
190
|
+
x: active ? active.x : 0,
|
|
191
|
+
y: active ? active.y - 12 : 0,
|
|
192
|
+
containerWidth: size.width,
|
|
193
|
+
containerHeight: size.height,
|
|
194
|
+
theme: theme,
|
|
195
|
+
config: tooltip
|
|
196
|
+
})]
|
|
197
|
+
})
|
|
198
|
+
});
|
|
199
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type HierarchyNode } from '../../core/hierarchy';
|
|
3
|
+
import { type ChartThemeInput } from '../../theme';
|
|
4
|
+
import type { ChartAnimationInput, TooltipConfig } from '../../types';
|
|
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,238 @@
|
|
|
1
|
+
import React, { useMemo, useState } from 'react';
|
|
2
|
+
import { classNames, prefixCls } from '@aura-react-comp/shared';
|
|
3
|
+
import { SvgScene } from "../../scene/svg";
|
|
4
|
+
import { formatNumber, formatPercent } from "../../core/format";
|
|
5
|
+
import { roundedRectPath } from "../../core/shape";
|
|
6
|
+
import { buildTreemap, countLeaves, sumValues } from "../../core/hierarchy";
|
|
7
|
+
import { mixColor, 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
|
+
* 布局用 squarified 算法:让每块尽量接近正方形,
|
|
20
|
+
* 既放得下文字,也便于人眼比较面积。
|
|
21
|
+
*/
|
|
22
|
+
export function TreemapChart({
|
|
23
|
+
data,
|
|
24
|
+
maxDepth = 1,
|
|
25
|
+
label = 'auto',
|
|
26
|
+
showValue = true,
|
|
27
|
+
gap = 2,
|
|
28
|
+
width = '100%',
|
|
29
|
+
height = 320,
|
|
30
|
+
theme: themeInput,
|
|
31
|
+
palette,
|
|
32
|
+
tooltip = true,
|
|
33
|
+
animation = true,
|
|
34
|
+
className,
|
|
35
|
+
style,
|
|
36
|
+
ariaLabel,
|
|
37
|
+
emptyText = '暂无数据'
|
|
38
|
+
}) {
|
|
39
|
+
const theme = useChartTheme(themeInput, palette);
|
|
40
|
+
const {
|
|
41
|
+
ref,
|
|
42
|
+
size
|
|
43
|
+
} = useElementSize();
|
|
44
|
+
const [activeKey, setActiveKey] = useState(null);
|
|
45
|
+
const animKey = typeof animation === 'object' ? JSON.stringify(animation) : String(animation);
|
|
46
|
+
const animConfig = useMemo(() => resolveAnimation(animation),
|
|
47
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
48
|
+
[animKey]);
|
|
49
|
+
const hierarchy = useMemo(() => {
|
|
50
|
+
if (!data) return {
|
|
51
|
+
cells: [],
|
|
52
|
+
total: 0,
|
|
53
|
+
leaves: 0
|
|
54
|
+
};
|
|
55
|
+
const total = sumValues(data);
|
|
56
|
+
if (total <= 0) return {
|
|
57
|
+
cells: [],
|
|
58
|
+
total: 0,
|
|
59
|
+
leaves: countLeaves(data)
|
|
60
|
+
};
|
|
61
|
+
const cells = buildTreemap(data, {
|
|
62
|
+
x: 0,
|
|
63
|
+
y: 0,
|
|
64
|
+
width: Math.max(0, size.width),
|
|
65
|
+
height: Math.max(0, size.height)
|
|
66
|
+
}, {
|
|
67
|
+
maxDepth,
|
|
68
|
+
headerHeight: 18,
|
|
69
|
+
theme,
|
|
70
|
+
colorOf: (depth, index, parentColor) => {
|
|
71
|
+
if (depth === 0) return theme.palette[index % theme.palette.length];
|
|
72
|
+
// 子层用父色向白混合,保持「同一父族颜色相近」的直觉
|
|
73
|
+
return mixColor(parentColor ?? theme.palette[0], '#ffffff', 0.46);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
return {
|
|
77
|
+
cells,
|
|
78
|
+
total,
|
|
79
|
+
leaves: countLeaves(data)
|
|
80
|
+
};
|
|
81
|
+
}, [data, maxDepth, size.width, size.height, theme]);
|
|
82
|
+
|
|
83
|
+
// 用各格面积驱动动画:数据变化时格子平滑伸缩
|
|
84
|
+
const matrix = useMemo(() => [hierarchy.cells.map(cell => Math.round(cell.width * cell.height))], [hierarchy.cells]);
|
|
85
|
+
const transition = useChartTransition(matrix, animConfig);
|
|
86
|
+
const progress = transition.progress;
|
|
87
|
+
const scene = useMemo(() => {
|
|
88
|
+
if (!hierarchy.cells.length || size.width <= 0) return [];
|
|
89
|
+
const nodes = [];
|
|
90
|
+
hierarchy.cells.forEach((cell, index) => {
|
|
91
|
+
const local = staggerProgress(progress, index, hierarchy.cells.length, animConfig.stagger);
|
|
92
|
+
const key = `${cell.depth}-${cell.node.name}-${index}`;
|
|
93
|
+
const isActive = activeKey === key;
|
|
94
|
+
nodes.push({
|
|
95
|
+
kind: 'path',
|
|
96
|
+
key,
|
|
97
|
+
d: roundedRectPath(cell.x + gap / 2, cell.y + gap / 2, Math.max(0, cell.width - gap), Math.max(0, cell.height - gap), 3),
|
|
98
|
+
fill: cell.color,
|
|
99
|
+
// 子层淡一些,形成「父块包着子块」的视觉层次
|
|
100
|
+
opacity: (cell.depth === 0 ? 1 : 0.92) * local,
|
|
101
|
+
stroke: isActive ? theme.text.primary : undefined,
|
|
102
|
+
strokeWidth: isActive ? 2 : 0,
|
|
103
|
+
pointerEvents: 'none'
|
|
104
|
+
});
|
|
105
|
+
const boxWidth = cell.width - gap;
|
|
106
|
+
const boxHeight = cell.height - gap;
|
|
107
|
+
const showLabel = label === 'always' || label === 'auto' && boxWidth >= 56 && boxHeight >= 22 || cell.depth === 0 && boxWidth >= 56 && boxHeight >= 22;
|
|
108
|
+
if (!showLabel || label === false) return;
|
|
109
|
+
const hasChildren = Boolean(cell.node.children?.length);
|
|
110
|
+
const value = hasChildren ? sumValues(cell.node) : cell.node.value ?? 0;
|
|
111
|
+
const showNumber = showValue && boxHeight >= 40 && boxWidth >= 56;
|
|
112
|
+
// 有子块的格子把名称放在顶部标题条里;叶子居中
|
|
113
|
+
const labelY = hasChildren ? cell.y + 11 : cell.y + (showNumber ? 16 : boxHeight / 2 + gap);
|
|
114
|
+
nodes.push({
|
|
115
|
+
kind: 'text',
|
|
116
|
+
key: `${key}-label`,
|
|
117
|
+
x: cell.x + 8,
|
|
118
|
+
y: labelY,
|
|
119
|
+
text: cell.node.name,
|
|
120
|
+
fill: '#fff',
|
|
121
|
+
fontSize: cell.depth === 0 ? theme.font.size : theme.font.sizeSmall,
|
|
122
|
+
fontWeight: cell.depth === 0 ? theme.font.weightMedium : undefined,
|
|
123
|
+
anchor: 'start',
|
|
124
|
+
baseline: 'middle',
|
|
125
|
+
opacity: local,
|
|
126
|
+
fontFamily: theme.font.family
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// 父块的汇总数值放到标题条右端,既不占额外高度又保留信息
|
|
130
|
+
if (hasChildren && showValue && boxWidth >= 130 && boxHeight >= 30) {
|
|
131
|
+
nodes.push({
|
|
132
|
+
kind: 'text',
|
|
133
|
+
key: `${key}-total`,
|
|
134
|
+
x: cell.x + boxWidth - 8,
|
|
135
|
+
y: labelY,
|
|
136
|
+
text: `${formatNumber(value, {
|
|
137
|
+
compact: true
|
|
138
|
+
})} · ${formatPercent(value, hierarchy.total, 1)}`,
|
|
139
|
+
fill: withAlpha('#ffffff', 0.85),
|
|
140
|
+
fontSize: theme.font.sizeSmall,
|
|
141
|
+
anchor: 'end',
|
|
142
|
+
baseline: 'middle',
|
|
143
|
+
opacity: local,
|
|
144
|
+
fontFamily: theme.font.family
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
if (showNumber && !hasChildren) {
|
|
148
|
+
nodes.push({
|
|
149
|
+
kind: 'text',
|
|
150
|
+
key: `${key}-value`,
|
|
151
|
+
x: cell.x + 8,
|
|
152
|
+
y: cell.y + 32,
|
|
153
|
+
text: `${formatNumber(value, {
|
|
154
|
+
compact: true
|
|
155
|
+
})} · ${formatPercent(value, hierarchy.total, 1)}`,
|
|
156
|
+
fill: withAlpha('#ffffff', 0.82),
|
|
157
|
+
fontSize: theme.font.sizeSmall,
|
|
158
|
+
anchor: 'start',
|
|
159
|
+
baseline: 'middle',
|
|
160
|
+
opacity: local,
|
|
161
|
+
fontFamily: theme.font.family
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
return nodes;
|
|
166
|
+
}, [hierarchy, size.width, gap, theme, progress, animConfig.stagger, activeKey, label, showValue]);
|
|
167
|
+
|
|
168
|
+
/** 命中检测:从后往前找第一个包含点的格子(深层画在上层) */
|
|
169
|
+
const cellFromPointer = (clientX, clientY) => {
|
|
170
|
+
const element = ref.current;
|
|
171
|
+
if (!element) return null;
|
|
172
|
+
const rect = element.getBoundingClientRect();
|
|
173
|
+
const x = clientX - rect.left;
|
|
174
|
+
const y = clientY - rect.top;
|
|
175
|
+
for (let index = hierarchy.cells.length - 1; index >= 0; index -= 1) {
|
|
176
|
+
const cell = hierarchy.cells[index];
|
|
177
|
+
if (x >= cell.x && x <= cell.x + cell.width && y >= cell.y && y <= cell.y + cell.height) {
|
|
178
|
+
return cell;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return null;
|
|
182
|
+
};
|
|
183
|
+
const activeCell = useMemo(() => {
|
|
184
|
+
if (!activeKey) return null;
|
|
185
|
+
return hierarchy.cells.find((cell, index) => `${cell.depth}-${cell.node.name}-${index}` === activeKey) ?? null;
|
|
186
|
+
}, [activeKey, hierarchy.cells]);
|
|
187
|
+
const tooltipPayload = activeCell ? {
|
|
188
|
+
label: activeCell.node.name,
|
|
189
|
+
row: null,
|
|
190
|
+
series: [{
|
|
191
|
+
key: activeCell.node.name,
|
|
192
|
+
name: '数值',
|
|
193
|
+
color: activeCell.color,
|
|
194
|
+
value: activeCell.node.value ?? sumValues(activeCell.node),
|
|
195
|
+
formatted: `${formatNumber(activeCell.node.value ?? sumValues(activeCell.node), {
|
|
196
|
+
compact: true
|
|
197
|
+
})}(${formatPercent(activeCell.node.value ?? sumValues(activeCell.node), hierarchy.total, 1)})`
|
|
198
|
+
}]
|
|
199
|
+
} : null;
|
|
200
|
+
const autoAriaLabel = ariaLabel ?? `矩形树图:共 ${hierarchy.leaves} 项,总计 ${formatNumber(hierarchy.total, {
|
|
201
|
+
compact: true
|
|
202
|
+
})}`;
|
|
203
|
+
return /*#__PURE__*/_jsx("div", {
|
|
204
|
+
className: classNames(prefixCls('chart'), className),
|
|
205
|
+
style: {
|
|
206
|
+
width,
|
|
207
|
+
height,
|
|
208
|
+
...style
|
|
209
|
+
},
|
|
210
|
+
"data-aura-chart": "treemap",
|
|
211
|
+
children: /*#__PURE__*/_jsxs("div", {
|
|
212
|
+
ref: ref,
|
|
213
|
+
className: prefixCls('chart__plot'),
|
|
214
|
+
onPointerMove: event => {
|
|
215
|
+
const cell = cellFromPointer(event.clientX, event.clientY);
|
|
216
|
+
setActiveKey(cell ? `${cell.depth}-${cell.node.name}-${hierarchy.cells.indexOf(cell)}` : null);
|
|
217
|
+
},
|
|
218
|
+
onPointerLeave: () => setActiveKey(null),
|
|
219
|
+
children: [!hierarchy.cells.length || size.width === 0 ? /*#__PURE__*/_jsx("div", {
|
|
220
|
+
className: prefixCls('chart__empty'),
|
|
221
|
+
children: emptyText
|
|
222
|
+
}) : /*#__PURE__*/_jsx(SvgScene, {
|
|
223
|
+
nodes: scene,
|
|
224
|
+
width: size.width,
|
|
225
|
+
height: size.height,
|
|
226
|
+
ariaLabel: autoAriaLabel
|
|
227
|
+
}), /*#__PURE__*/_jsx(Tooltip, {
|
|
228
|
+
payload: tooltipPayload,
|
|
229
|
+
x: activeCell ? activeCell.x + activeCell.width / 2 : 0,
|
|
230
|
+
y: activeCell ? activeCell.y + 8 : 0,
|
|
231
|
+
containerWidth: size.width,
|
|
232
|
+
containerHeight: size.height,
|
|
233
|
+
theme: theme,
|
|
234
|
+
config: tooltip
|
|
235
|
+
})]
|
|
236
|
+
})
|
|
237
|
+
});
|
|
238
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type ChartThemeInput } from '../../theme';
|
|
3
|
+
import type { TooltipConfig } from '../../types';
|
|
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"}
|