@octanejs/recharts 0.1.16 → 0.1.18
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/package.json +4 -4
- package/src/animation/CSSTransitionAnimate.tsrx +142 -0
- package/src/cartesian/Area.tsrx +1096 -0
- package/src/cartesian/Area.tsrx.d.ts +340 -0
- package/src/cartesian/AreaRevealShape.tsrx +202 -0
- package/src/cartesian/CartesianGrid.tsrx +643 -0
- package/src/cartesian/CartesianGrid.tsrx.d.ts +219 -0
- package/src/cartesian/ErrorBar.tsrx +322 -0
- package/src/cartesian/ErrorBar.tsrx.d.ts +131 -0
- package/src/cartesian/Funnel.tsrx +833 -0
- package/src/cartesian/Funnel.tsrx.d.ts +274 -0
- package/src/cartesian/ReferenceLine.tsrx +452 -0
- package/src/cartesian/ReferenceLine.tsrx.d.ts +178 -0
- package/src/cartesian/Scatter.tsrx +1076 -0
- package/src/cartesian/Scatter.tsrx.d.ts +351 -0
- package/src/cartesian/ZAxis.tsrx +148 -0
- package/src/cartesian/ZAxis.tsrx.d.ts +102 -0
- package/src/chart/AreaChart.tsrx +22 -0
- package/src/chart/AreaChart.tsrx.d.ts +12 -0
- package/src/chart/ComposedChart.tsrx +22 -0
- package/src/chart/ComposedChart.tsrx.d.ts +12 -0
- package/src/chart/FunnelChart.tsrx +22 -0
- package/src/chart/FunnelChart.tsrx.d.ts +12 -0
- package/src/chart/PieChart.tsrx +31 -0
- package/src/chart/PieChart.tsrx.d.ts +31 -0
- package/src/chart/PolarChart.tsrx +121 -0
- package/src/chart/RadarChart.tsrx +53 -0
- package/src/chart/RadarChart.tsrx.d.ts +31 -0
- package/src/chart/RadialBarChart.tsrx +31 -0
- package/src/chart/RadialBarChart.tsrx.d.ts +31 -0
- package/src/chart/Sankey.tsrx +1542 -0
- package/src/chart/Sankey.tsrx.d.ts +253 -0
- package/src/chart/ScatterChart.tsrx +22 -0
- package/src/chart/ScatterChart.tsrx.d.ts +12 -0
- package/src/chart/SunburstChart.tsrx +495 -0
- package/src/chart/SunburstChart.tsrx.d.ts +146 -0
- package/src/chart/types.ts +19 -0
- package/src/component/Cursor.tsrx +157 -0
- package/src/component/DefaultLegendContent.tsrx +288 -0
- package/src/component/DefaultLegendContent.tsrx.d.ts +125 -0
- package/src/component/DefaultTooltipContent.tsrx +215 -0
- package/src/component/DefaultTooltipContent.tsrx.d.ts +87 -0
- package/src/component/Legend.tsrx +302 -0
- package/src/component/Legend.tsrx.d.ts +88 -0
- package/src/component/ResponsiveContainer.ts +1 -22
- package/src/component/ResponsiveContainer.tsrx +171 -0
- package/src/component/ResponsiveContainer.tsrx.d.ts +81 -0
- package/src/component/Tooltip.tsrx +425 -0
- package/src/component/Tooltip.tsrx.d.ts +246 -0
- package/src/component/TooltipBoundingBox.tsrx +135 -0
- package/src/context/CellsContext.ts +12 -0
- package/src/context/legendPayloadContext.tsrx +11 -0
- package/src/context/useTooltipAxis.ts +20 -0
- package/src/index.d.ts +236 -0
- package/src/index.ts +67 -0
- package/src/polar/Pie.tsrx +1224 -0
- package/src/polar/Pie.tsrx.d.ts +436 -0
- package/src/polar/PolarAngleAxis.tsrx +554 -0
- package/src/polar/PolarAngleAxis.tsrx.d.ts +215 -0
- package/src/polar/PolarGrid.tsrx +317 -0
- package/src/polar/PolarGrid.tsrx.d.ts +117 -0
- package/src/polar/PolarRadiusAxis.tsrx +463 -0
- package/src/polar/PolarRadiusAxis.tsrx.d.ts +209 -0
- package/src/polar/Radar.tsrx +696 -0
- package/src/polar/Radar.tsrx.d.ts +215 -0
- package/src/polar/RadialBar.tsrx +887 -0
- package/src/polar/RadialBar.tsrx.d.ts +340 -0
- package/src/state/ReportPolarOptions.tsrx +11 -0
- package/src/state/selectors/areaSelectors.ts +257 -0
- package/src/state/selectors/funnelSelectors.ts +86 -0
- package/src/state/selectors/pieSelectors.ts +134 -0
- package/src/state/selectors/polarGridSelectors.ts +43 -0
- package/src/state/selectors/polarScaleSelectors.ts +159 -0
- package/src/state/selectors/polarSelectors.ts +198 -0
- package/src/state/selectors/radarSelectors.ts +228 -0
- package/src/state/selectors/radialBarSelectors.ts +511 -0
- package/src/state/selectors/scatterSelectors.ts +168 -0
- package/src/util/FunnelUtils.tsrx +19 -0
- package/src/util/RadialBarUtils.tsrx +29 -0
- package/src/util/ScatterUtils.tsrx +34 -0
- package/src/util/cursor/getCursorPoints.ts +45 -0
- package/src/util/cursor/getCursorRectangle.ts +28 -0
- package/src/util/cursor/getRadialCursorPoints.ts +31 -0
- package/src/util/payload/getUniqPayload.ts +28 -0
- package/src/util/scale/CartesianScaleHelper.ts +71 -0
- package/src/util/tooltip/translate.ts +168 -0
- package/src/util/types.ts +3 -0
- package/src/util/useElementOffset.ts +147 -0
|
@@ -0,0 +1,1542 @@
|
|
|
1
|
+
import * as React from 'octane';
|
|
2
|
+
import {
|
|
3
|
+
MouseEvent,
|
|
4
|
+
ReactElement,
|
|
5
|
+
ReactNode,
|
|
6
|
+
SVGProps,
|
|
7
|
+
useCallback,
|
|
8
|
+
useMemo,
|
|
9
|
+
useState,
|
|
10
|
+
} from 'octane';
|
|
11
|
+
import maxBy from 'es-toolkit/compat/maxBy';
|
|
12
|
+
import sumBy from 'es-toolkit/compat/sumBy';
|
|
13
|
+
import get from 'es-toolkit/compat/get';
|
|
14
|
+
import { Surface } from '../container/Surface';
|
|
15
|
+
import { Layer } from '../container/Layer';
|
|
16
|
+
import { Props as RectangleProps, Rectangle } from '../shape/Rectangle';
|
|
17
|
+
import { getValueByDataKey } from '../util/ChartUtils';
|
|
18
|
+
import {
|
|
19
|
+
Coordinate,
|
|
20
|
+
DataKey,
|
|
21
|
+
EventThrottlingProps,
|
|
22
|
+
Margin,
|
|
23
|
+
Percent,
|
|
24
|
+
SankeyLink,
|
|
25
|
+
SankeyNode,
|
|
26
|
+
} from '../util/types';
|
|
27
|
+
import {
|
|
28
|
+
ReportChartMargin,
|
|
29
|
+
ReportChartSize,
|
|
30
|
+
useChartHeight,
|
|
31
|
+
useChartWidth,
|
|
32
|
+
} from '../context/chartLayoutContext';
|
|
33
|
+
import { TooltipPortalContext } from '../context/tooltipPortalContext';
|
|
34
|
+
import { RechartsWrapper } from './RechartsWrapper';
|
|
35
|
+
import { RechartsStoreProvider } from '../state/RechartsStoreProvider';
|
|
36
|
+
import { useAppDispatch } from '../state/hooks';
|
|
37
|
+
import {
|
|
38
|
+
mouseLeaveItem,
|
|
39
|
+
setActiveClickItemIndex,
|
|
40
|
+
setActiveMouseOverItemIndex,
|
|
41
|
+
TooltipIndex,
|
|
42
|
+
TooltipPayloadConfiguration,
|
|
43
|
+
TooltipPayloadSearcher,
|
|
44
|
+
} from '../state/tooltipSlice';
|
|
45
|
+
import { SetTooltipEntrySettings } from '../state/SetTooltipEntrySettings';
|
|
46
|
+
import { ChartOptions } from '../state/optionsSlice';
|
|
47
|
+
import { ReportEventSettings } from '../state/ReportEventSettings';
|
|
48
|
+
import { SetComputedData } from '../context/chartDataContext';
|
|
49
|
+
import {
|
|
50
|
+
svgPropertiesNoEvents,
|
|
51
|
+
svgPropertiesNoEventsFromUnknown,
|
|
52
|
+
} from '../util/svgPropertiesNoEvents';
|
|
53
|
+
import { RequiresDefaultProps, resolveDefaultProps } from '../util/resolveDefaultProps';
|
|
54
|
+
import { isPositiveNumber } from '../util/isWellBehavedNumber';
|
|
55
|
+
import { isNotNil, noop } from '../util/DataUtils';
|
|
56
|
+
import { WithIdRequired } from '../util/useUniqueId';
|
|
57
|
+
import { RegisterGraphicalItemId } from '../context/RegisterGraphicalItemId';
|
|
58
|
+
import { GraphicalItemId } from '../state/graphicalItemsSlice';
|
|
59
|
+
import { initialEventSettingsState } from '../state/eventSettingsSlice';
|
|
60
|
+
|
|
61
|
+
const interpolationGenerator = (a: number, b: number) => {
|
|
62
|
+
const ka = +a;
|
|
63
|
+
const kb = b - ka;
|
|
64
|
+
return (animationElapsedTime: number) => ka + kb * animationElapsedTime;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const centerY = (node: SankeyNode) => node.y + node.dy / 2;
|
|
68
|
+
|
|
69
|
+
const cubicValue = (
|
|
70
|
+
start: number,
|
|
71
|
+
control1: number,
|
|
72
|
+
control2: number,
|
|
73
|
+
end: number,
|
|
74
|
+
t: number,
|
|
75
|
+
): number => {
|
|
76
|
+
const inverseT = 1 - t;
|
|
77
|
+
return inverseT ** 3 * start + 3 * inverseT ** 2 * t * control1 +
|
|
78
|
+
3 * inverseT * t ** 2 * control2 +
|
|
79
|
+
t ** 3 * end;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// TODO why is this not reading dataKey?
|
|
83
|
+
const getValue = (entry: LinkDataItem | SankeyNode | undefined): number =>
|
|
84
|
+
entry && entry.value || 0;
|
|
85
|
+
|
|
86
|
+
const getSumOfIds = (links: ReadonlyArray<LinkDataItem>, ids: number[]): number => ids.reduce(
|
|
87
|
+
(result, id) => result + getValue(links[id]),
|
|
88
|
+
0,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const getSumWithWeightedSource = (
|
|
92
|
+
tree: ReadonlyArray<SankeyNode>,
|
|
93
|
+
links: ReadonlyArray<LinkDataItemDy>,
|
|
94
|
+
ids: number[],
|
|
95
|
+
) => ids.reduce((result, id) => {
|
|
96
|
+
const link = links[id];
|
|
97
|
+
if (link == null) {
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
const sourceNode = tree[link.source];
|
|
101
|
+
if (sourceNode == null) {
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return result + centerY(sourceNode) * getValue(links[id]);
|
|
106
|
+
}, 0);
|
|
107
|
+
|
|
108
|
+
const getSumWithWeightedTarget = (
|
|
109
|
+
tree: ReadonlyArray<SankeyNode>,
|
|
110
|
+
links: ReadonlyArray<LinkDataItemDy>,
|
|
111
|
+
ids: ReadonlyArray<number>,
|
|
112
|
+
): number => ids.reduce((result: number, id: number) => {
|
|
113
|
+
const link = links[id];
|
|
114
|
+
if (link == null) {
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
const targetNode = tree[link.target];
|
|
118
|
+
if (targetNode == null) {
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return result + centerY(targetNode) * getValue(links[id]);
|
|
123
|
+
}, 0);
|
|
124
|
+
|
|
125
|
+
const ascendingY = (a: { y: number }, b: { y: number }) => a.y - b.y;
|
|
126
|
+
|
|
127
|
+
const searchTargetsAndSources = (links: LinkDataItem[], id: number) => {
|
|
128
|
+
const sourceNodes: number[] = [];
|
|
129
|
+
const sourceLinks: number[] = [];
|
|
130
|
+
const targetNodes: number[] = [];
|
|
131
|
+
const targetLinks: number[] = [];
|
|
132
|
+
|
|
133
|
+
for (let i = 0, len = links.length; i < len; i++) {
|
|
134
|
+
const link = links[i];
|
|
135
|
+
|
|
136
|
+
if (link?.source === id) {
|
|
137
|
+
targetNodes.push(link.target);
|
|
138
|
+
targetLinks.push(i);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (link?.target === id) {
|
|
142
|
+
sourceNodes.push(link.source);
|
|
143
|
+
sourceLinks.push(i);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return { sourceNodes, sourceLinks, targetLinks, targetNodes };
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const updateDepthOfTargets = (tree: SankeyNode[], curNode: SankeyNode) => {
|
|
151
|
+
const { targetNodes } = curNode;
|
|
152
|
+
|
|
153
|
+
for (let i = 0, len = targetNodes.length; i < len; i++) {
|
|
154
|
+
const targetNode = targetNodes[i];
|
|
155
|
+
if (targetNode == null) {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const target = tree[targetNode];
|
|
159
|
+
|
|
160
|
+
if (target) {
|
|
161
|
+
const newDepth = curNode.depth + 1;
|
|
162
|
+
/*
|
|
163
|
+
* Only recurse when the depth grows. Depth is the longest path to a node, so once it stops increasing
|
|
164
|
+
* the subtree is already up to date; without this guard every path through the graph is walked
|
|
165
|
+
* separately, which explodes combinatorially on dense graphs.
|
|
166
|
+
*/
|
|
167
|
+
if (newDepth > target.depth) {
|
|
168
|
+
target.depth = newDepth;
|
|
169
|
+
updateDepthOfTargets(tree, target);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const getNodesTree = (
|
|
176
|
+
{ nodes, links }: SankeyData,
|
|
177
|
+
width: number,
|
|
178
|
+
nodeWidth: number,
|
|
179
|
+
align: 'left' | 'justify',
|
|
180
|
+
): { tree: SankeyNode[]; maxDepth: number } => {
|
|
181
|
+
const tree = nodes.map((entry: SankeyNode, index: number) => {
|
|
182
|
+
const result = searchTargetsAndSources(links, index);
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
...entry,
|
|
186
|
+
...result,
|
|
187
|
+
value: Math.max(
|
|
188
|
+
getSumOfIds(links, result.sourceLinks),
|
|
189
|
+
getSumOfIds(links, result.targetLinks),
|
|
190
|
+
),
|
|
191
|
+
depth: 0,
|
|
192
|
+
};
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
for (let i = 0, len = tree.length; i < len; i++) {
|
|
196
|
+
const node = tree[i];
|
|
197
|
+
|
|
198
|
+
if (node != null && !node.sourceNodes.length) {
|
|
199
|
+
updateDepthOfTargets(tree, node);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const maxDepth = maxBy(tree, (entry: SankeyNode) => entry.depth)?.depth ?? 0;
|
|
203
|
+
|
|
204
|
+
if (maxDepth >= 1) {
|
|
205
|
+
const childWidth = (width - nodeWidth) / maxDepth;
|
|
206
|
+
for (let i = 0, len = tree.length; i < len; i++) {
|
|
207
|
+
const node = tree[i];
|
|
208
|
+
if (node == null) {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (!node.targetNodes.length) {
|
|
213
|
+
if (align === 'justify') {
|
|
214
|
+
node.depth = maxDepth;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
node.x = node.depth * childWidth;
|
|
218
|
+
node.dx = nodeWidth;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return { tree, maxDepth };
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const getDepthTree = (tree: SankeyNode[]): SankeyNode[][] => {
|
|
226
|
+
const result: SankeyNode[][] = [];
|
|
227
|
+
|
|
228
|
+
for (let i = 0, len = tree.length; i < len; i++) {
|
|
229
|
+
const node = tree[i];
|
|
230
|
+
if (node == null) {
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (!result[node.depth]) {
|
|
235
|
+
result[node.depth] = [];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
result[node.depth]?.push(node);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return result;
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
type LinkDataItemDy = LinkDataItem & { dy: number; sy?: number; ty?: number };
|
|
245
|
+
|
|
246
|
+
type SankeyVerticalAlign = 'justify' | 'top';
|
|
247
|
+
|
|
248
|
+
const updateYOfTree = (
|
|
249
|
+
depthTree: SankeyNode[][],
|
|
250
|
+
height: number,
|
|
251
|
+
nodePadding: number,
|
|
252
|
+
links: LinkDataItem[],
|
|
253
|
+
verticalAlign: SankeyVerticalAlign,
|
|
254
|
+
): Array<LinkDataItemDy> => {
|
|
255
|
+
let yRatio: number = Math.min(
|
|
256
|
+
...depthTree.map((nodes) => {
|
|
257
|
+
const value = sumBy(nodes, getValue);
|
|
258
|
+
return value === 0 ? Infinity : (height - (nodes.length - 1) * nodePadding) / value;
|
|
259
|
+
}),
|
|
260
|
+
);
|
|
261
|
+
if (yRatio === Infinity) {
|
|
262
|
+
yRatio = 0;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
for (let d = 0, maxDepth = depthTree.length; d < maxDepth; d++) {
|
|
266
|
+
const nodes = depthTree[d];
|
|
267
|
+
if (nodes == null) {
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (verticalAlign === 'top') {
|
|
272
|
+
let currentY = 0;
|
|
273
|
+
for (let i = 0, len = nodes.length; i < len; i++) {
|
|
274
|
+
const node = nodes[i];
|
|
275
|
+
if (node == null) {
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
node.dy = node.value * yRatio;
|
|
280
|
+
node.y = currentY;
|
|
281
|
+
currentY += node.dy + nodePadding;
|
|
282
|
+
}
|
|
283
|
+
} else {
|
|
284
|
+
for (let i = 0, len = nodes.length; i < len; i++) {
|
|
285
|
+
const node = nodes[i];
|
|
286
|
+
if (node == null) {
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
node.y = i;
|
|
291
|
+
node.dy = node.value * yRatio;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return links.map(
|
|
297
|
+
(link: LinkDataItem): LinkDataItemDy => ({ ...link, dy: getValue(link) * yRatio }),
|
|
298
|
+
);
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const resolveCollisions = (
|
|
302
|
+
depthTree: SankeyNode[][],
|
|
303
|
+
height: number,
|
|
304
|
+
nodePadding: number,
|
|
305
|
+
sort = true,
|
|
306
|
+
) => {
|
|
307
|
+
for (let i = 0, len = depthTree.length; i < len; i++) {
|
|
308
|
+
const nodes = depthTree[i];
|
|
309
|
+
if (nodes == null) {
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
const n = nodes.length;
|
|
313
|
+
|
|
314
|
+
// Sort by the value of y
|
|
315
|
+
if (sort) {
|
|
316
|
+
nodes.sort(ascendingY);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
let y0 = 0;
|
|
320
|
+
for (let j = 0; j < n; j++) {
|
|
321
|
+
const node = nodes[j];
|
|
322
|
+
if (node == null) {
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
const dy = y0 - node.y;
|
|
326
|
+
|
|
327
|
+
if (dy > 0) {
|
|
328
|
+
node.y += dy;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
y0 = node.y + node.dy + nodePadding;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
y0 = height + nodePadding;
|
|
335
|
+
for (let j = n - 1; j >= 0; j--) {
|
|
336
|
+
const node = nodes[j];
|
|
337
|
+
if (node == null) {
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const dy = node.y + node.dy + nodePadding - y0;
|
|
341
|
+
|
|
342
|
+
if (dy > 0) {
|
|
343
|
+
node.y -= dy;
|
|
344
|
+
y0 = node.y;
|
|
345
|
+
} else {
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const relaxLeftToRight = (
|
|
353
|
+
tree: ReadonlyArray<SankeyNode>,
|
|
354
|
+
depthTree: SankeyNode[][],
|
|
355
|
+
links: ReadonlyArray<LinkDataItemDy>,
|
|
356
|
+
alpha: number,
|
|
357
|
+
) => {
|
|
358
|
+
for (let i = 0, maxDepth = depthTree.length; i < maxDepth; i++) {
|
|
359
|
+
const nodes = depthTree[i];
|
|
360
|
+
if (nodes == null) {
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
for (let j = 0, len = nodes.length; j < len; j++) {
|
|
365
|
+
const node = nodes[j];
|
|
366
|
+
if (node == null) {
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (node.sourceLinks.length) {
|
|
371
|
+
const sourceSum = getSumOfIds(links, node.sourceLinks);
|
|
372
|
+
const weightedSum = getSumWithWeightedSource(tree, links, node.sourceLinks);
|
|
373
|
+
const y =
|
|
374
|
+
sourceSum === 0 ? centerY(node) : weightedSum / sourceSum;
|
|
375
|
+
|
|
376
|
+
node.y += (y - centerY(node)) * alpha;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
const relaxRightToLeft = (
|
|
383
|
+
tree: ReadonlyArray<SankeyNode>,
|
|
384
|
+
/*
|
|
385
|
+
* depthTree array is modified by this method!
|
|
386
|
+
*/
|
|
387
|
+
depthTree: SankeyNode[][],
|
|
388
|
+
links: ReadonlyArray<LinkDataItemDy>,
|
|
389
|
+
alpha: number,
|
|
390
|
+
) => {
|
|
391
|
+
for (let i = depthTree.length - 1; i >= 0; i--) {
|
|
392
|
+
const nodes = depthTree[i];
|
|
393
|
+
if (nodes == null) {
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
for (let j = 0, len = nodes.length; j < len; j++) {
|
|
398
|
+
const node = nodes[j];
|
|
399
|
+
if (node == null) {
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (node.targetLinks.length) {
|
|
404
|
+
const targetSum = getSumOfIds(links, node.targetLinks);
|
|
405
|
+
const weightedSum = getSumWithWeightedTarget(tree, links, node.targetLinks);
|
|
406
|
+
const y =
|
|
407
|
+
targetSum === 0 ? centerY(node) : weightedSum / targetSum;
|
|
408
|
+
|
|
409
|
+
node.y += (y - centerY(node)) * alpha;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
const updateYOfLinks = (tree: SankeyNode[], links: LinkDataItemDy[]): void => {
|
|
416
|
+
for (let i = 0, len = tree.length; i < len; i++) {
|
|
417
|
+
const node = tree[i];
|
|
418
|
+
if (node == null) {
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
let sy = 0;
|
|
422
|
+
let ty = 0;
|
|
423
|
+
|
|
424
|
+
node.targetLinks.sort((a, b) => {
|
|
425
|
+
const targetA = links[a]?.target;
|
|
426
|
+
const targetB = links[b]?.target;
|
|
427
|
+
if (targetA == null || targetB == null) {
|
|
428
|
+
return 0;
|
|
429
|
+
}
|
|
430
|
+
const yA = tree[targetA]?.y;
|
|
431
|
+
const yB = tree[targetB]?.y;
|
|
432
|
+
if (yA == null || yB == null) {
|
|
433
|
+
return 0;
|
|
434
|
+
}
|
|
435
|
+
return yA - yB;
|
|
436
|
+
});
|
|
437
|
+
node.sourceLinks.sort((a, b) => {
|
|
438
|
+
const sourceA = links[a]?.source;
|
|
439
|
+
const sourceB = links[b]?.source;
|
|
440
|
+
if (sourceA == null || sourceB == null) {
|
|
441
|
+
return 0;
|
|
442
|
+
}
|
|
443
|
+
const yA = tree[sourceA]?.y;
|
|
444
|
+
const yB = tree[sourceB]?.y;
|
|
445
|
+
if (yA == null || yB == null) {
|
|
446
|
+
return 0;
|
|
447
|
+
}
|
|
448
|
+
return yA - yB;
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
for (let j = 0, tLen = node.targetLinks.length; j < tLen; j++) {
|
|
452
|
+
const targetLink = node.targetLinks[j];
|
|
453
|
+
if (targetLink == null) {
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
const link = links[targetLink];
|
|
457
|
+
|
|
458
|
+
if (link) {
|
|
459
|
+
link.sy = sy;
|
|
460
|
+
sy += link.dy;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
for (let j = 0, sLen = node.sourceLinks.length; j < sLen; j++) {
|
|
465
|
+
const sourceLink = node.sourceLinks[j];
|
|
466
|
+
if (sourceLink == null) {
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
const link = links[sourceLink];
|
|
470
|
+
|
|
471
|
+
if (link) {
|
|
472
|
+
link.ty = ty;
|
|
473
|
+
ty += link.dy;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
const getLinkYAtX = (
|
|
480
|
+
sourceNode: SankeyNode,
|
|
481
|
+
targetNode: SankeyNode,
|
|
482
|
+
link: LinkDataItemDy,
|
|
483
|
+
x: number,
|
|
484
|
+
): number => {
|
|
485
|
+
const sourceX = sourceNode.x + sourceNode.dx;
|
|
486
|
+
const targetX = targetNode.x;
|
|
487
|
+
const progress =
|
|
488
|
+
targetX === sourceX ? 0 : (x - sourceX) / (targetX - sourceX);
|
|
489
|
+
const boundedProgress = Math.min(Math.max(progress, 0), 1);
|
|
490
|
+
const sourceY = sourceNode.y + (link.sy ?? 0) + link.dy / 2;
|
|
491
|
+
const targetY = targetNode.y + (link.ty ?? 0) + link.dy / 2;
|
|
492
|
+
|
|
493
|
+
return cubicValue(sourceY, sourceY, targetY, targetY, boundedProgress);
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
const resolveNodeLinkCollisions = (
|
|
497
|
+
tree: SankeyNode[],
|
|
498
|
+
depthTree: SankeyNode[][],
|
|
499
|
+
links: LinkDataItemDy[],
|
|
500
|
+
height: number,
|
|
501
|
+
nodePadding: number,
|
|
502
|
+
) => {
|
|
503
|
+
const depthByNode = new Map<SankeyNode, number>();
|
|
504
|
+
|
|
505
|
+
for (let depth = 0; depth < depthTree.length; depth++) {
|
|
506
|
+
const nodes = depthTree[depth];
|
|
507
|
+
if (nodes == null) {
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
for (const node of nodes) {
|
|
512
|
+
depthByNode.set(node, depth);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
for (let depth = 0; depth < depthTree.length; depth++) {
|
|
517
|
+
const nodes = depthTree[depth];
|
|
518
|
+
if (nodes == null || nodes.length === 0) {
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
const depthX =
|
|
523
|
+
nodes[0] == null ? undefined : nodes[0].x + nodes[0].dx / 2;
|
|
524
|
+
if (depthX == null) {
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
const fixedObstacles = links.flatMap((link) => {
|
|
529
|
+
const sourceNode = tree[link.source];
|
|
530
|
+
const targetNode = tree[link.target];
|
|
531
|
+
if (sourceNode == null || targetNode == null) {
|
|
532
|
+
return [];
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
const sourceDepth = depthByNode.get(sourceNode);
|
|
536
|
+
const targetDepth = depthByNode.get(targetNode);
|
|
537
|
+
if (sourceDepth == null || targetDepth == null) {
|
|
538
|
+
return [];
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
if (
|
|
542
|
+
depth <= Math.min(sourceDepth, targetDepth) || depth >= Math.max(sourceDepth, targetDepth)
|
|
543
|
+
) {
|
|
544
|
+
return [];
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
const y = getLinkYAtX(sourceNode, targetNode, link, depthX) - link.dy / 2;
|
|
548
|
+
return [
|
|
549
|
+
{ y, dy: link.dy, fixed: true as const },
|
|
550
|
+
];
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
const containedNodeObstacles = fixedObstacles.filter(
|
|
554
|
+
(obstacle) => nodes.some(
|
|
555
|
+
(node) => node.y >= obstacle.y && node.y + node.dy <= obstacle.y + obstacle.dy,
|
|
556
|
+
),
|
|
557
|
+
);
|
|
558
|
+
|
|
559
|
+
if (containedNodeObstacles.length === 0) {
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
type CollisionItem =
|
|
564
|
+
{ node: SankeyNode; fixed: false } | { y: number; dy: number; fixed: true };
|
|
565
|
+
const getItemY = (item: CollisionItem): number => (item.fixed ? item.y : item.node.y);
|
|
566
|
+
const getItemHeight = (item: CollisionItem): number => (item.fixed ? item.dy : item.node.dy);
|
|
567
|
+
let items: CollisionItem[] = [
|
|
568
|
+
...nodes.map((node) => ({ node, fixed: false as const })),
|
|
569
|
+
...containedNodeObstacles,
|
|
570
|
+
].sort((a, b) => getItemY(a) - getItemY(b));
|
|
571
|
+
|
|
572
|
+
let nextY = 0;
|
|
573
|
+
for (const item of items) {
|
|
574
|
+
if (item.fixed) {
|
|
575
|
+
nextY = Math.max(nextY, item.y + item.dy + nodePadding);
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
if (item.node.y < nextY) {
|
|
580
|
+
item.node.y = nextY;
|
|
581
|
+
}
|
|
582
|
+
nextY = item.node.y + item.node.dy + nodePadding;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
items = items.sort((a, b) => getItemY(a) - getItemY(b));
|
|
586
|
+
|
|
587
|
+
let previousY = height + nodePadding;
|
|
588
|
+
for (let i = items.length - 1; i >= 0; i--) {
|
|
589
|
+
const item = items[i];
|
|
590
|
+
if (item == null) {
|
|
591
|
+
continue;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
if (item.fixed) {
|
|
595
|
+
previousY = Math.min(previousY, item.y - nodePadding);
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
// This backward pass only keeps moved nodes within the chart bounds.
|
|
600
|
+
// It intentionally does not iterate again against fixed obstacles above,
|
|
601
|
+
// because this fix is scoped to the fully-contained skipped-depth case.
|
|
602
|
+
const dy = item.node.y + getItemHeight(item) + nodePadding - previousY;
|
|
603
|
+
if (dy > 0) {
|
|
604
|
+
item.node.y -= dy;
|
|
605
|
+
}
|
|
606
|
+
previousY = item.node.y;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
|
|
611
|
+
export const computeData = ({
|
|
612
|
+
data,
|
|
613
|
+
width,
|
|
614
|
+
height,
|
|
615
|
+
iterations,
|
|
616
|
+
nodeWidth,
|
|
617
|
+
nodePadding,
|
|
618
|
+
sort,
|
|
619
|
+
verticalAlign,
|
|
620
|
+
align,
|
|
621
|
+
}: {
|
|
622
|
+
data: SankeyData;
|
|
623
|
+
width: number;
|
|
624
|
+
height: number;
|
|
625
|
+
iterations: number;
|
|
626
|
+
nodeWidth: number;
|
|
627
|
+
nodePadding: number;
|
|
628
|
+
sort: boolean;
|
|
629
|
+
verticalAlign: SankeyVerticalAlign;
|
|
630
|
+
align: 'left' | 'justify';
|
|
631
|
+
}): {
|
|
632
|
+
nodes: ReadonlyArray<SankeyNode>;
|
|
633
|
+
links: ReadonlyArray<SankeyLink>;
|
|
634
|
+
} => {
|
|
635
|
+
const { links } = data;
|
|
636
|
+
const { tree } = getNodesTree(data, width, nodeWidth, align);
|
|
637
|
+
const depthTree = getDepthTree(tree);
|
|
638
|
+
const linksWithDy: Array<LinkDataItemDy> = updateYOfTree(
|
|
639
|
+
depthTree,
|
|
640
|
+
height,
|
|
641
|
+
nodePadding,
|
|
642
|
+
links,
|
|
643
|
+
verticalAlign,
|
|
644
|
+
);
|
|
645
|
+
|
|
646
|
+
resolveCollisions(depthTree, height, nodePadding, sort);
|
|
647
|
+
|
|
648
|
+
if (verticalAlign === 'justify') {
|
|
649
|
+
let alpha = 1;
|
|
650
|
+
for (let i = 1; i <= iterations; i++) {
|
|
651
|
+
relaxRightToLeft(tree, depthTree, linksWithDy, alpha *= 0.99);
|
|
652
|
+
|
|
653
|
+
resolveCollisions(depthTree, height, nodePadding, sort);
|
|
654
|
+
|
|
655
|
+
relaxLeftToRight(tree, depthTree, linksWithDy, alpha);
|
|
656
|
+
|
|
657
|
+
resolveCollisions(depthTree, height, nodePadding, sort);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
updateYOfLinks(tree, linksWithDy);
|
|
662
|
+
resolveNodeLinkCollisions(tree, depthTree, linksWithDy, height, nodePadding);
|
|
663
|
+
updateYOfLinks(tree, linksWithDy);
|
|
664
|
+
|
|
665
|
+
// @ts-expect-error updateYOfLinks modifies the links array to add sy and ty in place
|
|
666
|
+
const newLinks: ReadonlyArray<SankeyLink> = linksWithDy;
|
|
667
|
+
|
|
668
|
+
return { nodes: tree, links: newLinks };
|
|
669
|
+
};
|
|
670
|
+
|
|
671
|
+
const getNodeCoordinateOfTooltip = (item: NodeProps) => {
|
|
672
|
+
return { x: +item.x + +item.width / 2, y: +item.y + +item.height / 2 };
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
const getLinkCoordinateOfTooltip = (item: LinkProps): Coordinate | undefined => {
|
|
676
|
+
return 'sourceX' in item
|
|
677
|
+
? {
|
|
678
|
+
x: (item.sourceX + item.targetX) / 2,
|
|
679
|
+
y: (item.sourceY + item.targetY) / 2,
|
|
680
|
+
}
|
|
681
|
+
: undefined;
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
type SankeyTooltipPayload = { payload: SankeyNode | SankeyLink; name: unknown; value: unknown };
|
|
685
|
+
|
|
686
|
+
const getPayloadOfTooltip = (
|
|
687
|
+
item: { payload: SankeyNode | SankeyLink },
|
|
688
|
+
type: SankeyElementType,
|
|
689
|
+
nameKey: DataKey<SankeyLink | SankeyNode, string> | undefined,
|
|
690
|
+
): SankeyTooltipPayload | undefined => {
|
|
691
|
+
const { payload } = item;
|
|
692
|
+
if (type === 'node') {
|
|
693
|
+
return {
|
|
694
|
+
payload,
|
|
695
|
+
name: getValueByDataKey(payload, nameKey, ''),
|
|
696
|
+
value: getValueByDataKey(payload, 'value'),
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
if ('source' in payload && payload.source && payload.target) {
|
|
700
|
+
// @ts-expect-error we're sending number indexes as source and target, but getValueByDataKey expects objects
|
|
701
|
+
const sourceName = getValueByDataKey(payload.source, nameKey, '');
|
|
702
|
+
// @ts-expect-error we're sending number indexes as source and target, but getValueByDataKey expects objects
|
|
703
|
+
const targetName = getValueByDataKey(payload.target, nameKey, '');
|
|
704
|
+
|
|
705
|
+
return {
|
|
706
|
+
payload,
|
|
707
|
+
name: `${sourceName} - ${targetName}`,
|
|
708
|
+
value: getValueByDataKey(payload, 'value'),
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
return undefined;
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
export const sankeyPayloadSearcher: TooltipPayloadSearcher = (
|
|
716
|
+
_: unknown,
|
|
717
|
+
activeIndex: TooltipIndex,
|
|
718
|
+
computedData?: unknown,
|
|
719
|
+
nameKey,
|
|
720
|
+
): SankeyTooltipPayload | undefined => {
|
|
721
|
+
if (activeIndex == null || typeof activeIndex !== 'string') {
|
|
722
|
+
return undefined;
|
|
723
|
+
}
|
|
724
|
+
if (computedData == null || typeof computedData !== 'object') {
|
|
725
|
+
return undefined;
|
|
726
|
+
}
|
|
727
|
+
const splitIndex = activeIndex.split('-');
|
|
728
|
+
const [targetType, index] = splitIndex;
|
|
729
|
+
const item = get(computedData, `${targetType}s[${index}]`);
|
|
730
|
+
if (item) {
|
|
731
|
+
// @ts-expect-error nameKey type does not match SankeyElementType
|
|
732
|
+
const payload = getPayloadOfTooltip(item, targetType as SankeyElementType, nameKey);
|
|
733
|
+
return payload;
|
|
734
|
+
}
|
|
735
|
+
return undefined;
|
|
736
|
+
};
|
|
737
|
+
|
|
738
|
+
const options: ChartOptions = {
|
|
739
|
+
chartName: 'Sankey',
|
|
740
|
+
defaultTooltipEventType: 'item',
|
|
741
|
+
validateTooltipEventTypes: ['item'],
|
|
742
|
+
tooltipPayloadSearcher: sankeyPayloadSearcher,
|
|
743
|
+
eventEmitter: undefined,
|
|
744
|
+
};
|
|
745
|
+
|
|
746
|
+
const SetSankeyTooltipEntrySettings = React.memo(({
|
|
747
|
+
dataKey,
|
|
748
|
+
nameKey,
|
|
749
|
+
stroke,
|
|
750
|
+
strokeWidth,
|
|
751
|
+
fill,
|
|
752
|
+
name,
|
|
753
|
+
data,
|
|
754
|
+
id,
|
|
755
|
+
}: Pick<
|
|
756
|
+
InternalSankeyProps,
|
|
757
|
+
'dataKey' | 'nameKey' | 'stroke' | 'strokeWidth' | 'fill' | 'name' | 'data' | 'id'
|
|
758
|
+
>) => {
|
|
759
|
+
const tooltipEntrySettings: TooltipPayloadConfiguration = {
|
|
760
|
+
dataDefinedOnItem: data,
|
|
761
|
+
getPosition: noop,
|
|
762
|
+
settings: {
|
|
763
|
+
stroke,
|
|
764
|
+
strokeWidth,
|
|
765
|
+
fill,
|
|
766
|
+
dataKey,
|
|
767
|
+
name,
|
|
768
|
+
nameKey,
|
|
769
|
+
hide: false,
|
|
770
|
+
type: undefined,
|
|
771
|
+
color: fill,
|
|
772
|
+
unit: '',
|
|
773
|
+
graphicalItemId: id,
|
|
774
|
+
},
|
|
775
|
+
};
|
|
776
|
+
return <SetTooltipEntrySettings tooltipEntrySettings={tooltipEntrySettings} />;
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
interface LinkDataItem {
|
|
780
|
+
source: number;
|
|
781
|
+
target: number;
|
|
782
|
+
value: number;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
export interface NodeProps extends Omit<SVGProps<SVGRectElement>, 'height' | 'width'> {
|
|
786
|
+
height: number;
|
|
787
|
+
width: number;
|
|
788
|
+
payload: SankeyNode;
|
|
789
|
+
index: number;
|
|
790
|
+
x: number;
|
|
791
|
+
y: number;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
export interface LinkProps extends SVGProps<SVGPathElement> {
|
|
795
|
+
sourceX: number;
|
|
796
|
+
targetX: number;
|
|
797
|
+
sourceY: number;
|
|
798
|
+
targetY: number;
|
|
799
|
+
sourceControlX: number;
|
|
800
|
+
targetControlX: number;
|
|
801
|
+
sourceRelativeY: number;
|
|
802
|
+
targetRelativeY: number;
|
|
803
|
+
linkWidth: number;
|
|
804
|
+
index: number;
|
|
805
|
+
// payload is SankeyLink except source and target are now Node objects instead of numbers
|
|
806
|
+
payload: Omit<SankeyLink, 'source' | 'target'> & { source: SankeyNode; target: SankeyNode };
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
export interface SankeyData {
|
|
810
|
+
nodes: any[];
|
|
811
|
+
links: LinkDataItem[];
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
// TODO: improve types - NodeOptions uses SankeyNode, LinkOptions uses LinkProps. Standardize.
|
|
815
|
+
export type SankeyNodeOptions =
|
|
816
|
+
ReactElement<SVGProps<SVGRectElement>> | ((props: NodeProps) => ReactNode) | RectangleProps;
|
|
817
|
+
|
|
818
|
+
type SankeyLinkOptions =
|
|
819
|
+
| ReactElement<SVGProps<SVGPathElement>>
|
|
820
|
+
| ((props: LinkProps) => ReactElement<SVGProps<SVGPathElement>>)
|
|
821
|
+
| SVGProps<SVGPathElement>;
|
|
822
|
+
|
|
823
|
+
interface SankeyProps extends EventThrottlingProps {
|
|
824
|
+
/**
|
|
825
|
+
* Name represents each sector in the tooltip.
|
|
826
|
+
* This allows you to extract the name from the data:
|
|
827
|
+
*
|
|
828
|
+
* - `string`: the name of the field in the data object;
|
|
829
|
+
* - `number`: the index of the field in the data;
|
|
830
|
+
* - `function`: a function that receives the data object and returns the name.
|
|
831
|
+
*
|
|
832
|
+
* @defaultValue name
|
|
833
|
+
*/
|
|
834
|
+
nameKey?: DataKey<any>;
|
|
835
|
+
/**
|
|
836
|
+
* dataKey prop in Sankey defines which key in the link objects represents the value of the link _in Tooltip only_.
|
|
837
|
+
*
|
|
838
|
+
* Unlike other charts where dataKey is used to extract values from the data array, in Sankey charts,
|
|
839
|
+
* the value of each link is directly taken from the 'value' property of the link objects.
|
|
840
|
+
*
|
|
841
|
+
* @default 'value'
|
|
842
|
+
*/
|
|
843
|
+
dataKey?: DataKey<any>;
|
|
844
|
+
/**
|
|
845
|
+
* The width of chart container.
|
|
846
|
+
* Can be a number or a percent string like "100%".
|
|
847
|
+
*
|
|
848
|
+
* @see {@link https://recharts.github.io/en-US/guide/sizes/ Chart sizing guide}
|
|
849
|
+
*/
|
|
850
|
+
width?: number | Percent;
|
|
851
|
+
/**
|
|
852
|
+
* The height of chart container.
|
|
853
|
+
* Can be a number or a percent string like "100%".
|
|
854
|
+
*
|
|
855
|
+
* @see {@link https://recharts.github.io/en-US/guide/sizes/ Chart sizing guide}
|
|
856
|
+
*/
|
|
857
|
+
height?: number | Percent;
|
|
858
|
+
/**
|
|
859
|
+
* The source data, including the array of nodes, and the relationships, represented by links.
|
|
860
|
+
*
|
|
861
|
+
* Note that Sankey requires a specific data structure.
|
|
862
|
+
* Each node should have a unique index in the nodes array, and each link should reference these nodes by their indices.
|
|
863
|
+
* This is different from other chart types in Recharts, which accept arbitrary data.
|
|
864
|
+
*
|
|
865
|
+
* @example
|
|
866
|
+
* nodes: [
|
|
867
|
+
* { name: 'Visit' },
|
|
868
|
+
* { name: 'Direct-Favourite' },
|
|
869
|
+
* { name: 'Page-Click' },
|
|
870
|
+
* { name: 'Detail-Favourite' },
|
|
871
|
+
* { name: 'Lost' },
|
|
872
|
+
* ],
|
|
873
|
+
* links: [
|
|
874
|
+
* { source: 0, target: 1, value: 3728.3 },
|
|
875
|
+
* { source: 0, target: 2, value: 354170 },
|
|
876
|
+
* { source: 2, target: 3, value: 62429 },
|
|
877
|
+
* { source: 2, target: 4, value: 291741 },
|
|
878
|
+
* ],
|
|
879
|
+
*/
|
|
880
|
+
data: SankeyData;
|
|
881
|
+
/**
|
|
882
|
+
* The padding between the nodes
|
|
883
|
+
* @default 10
|
|
884
|
+
*/
|
|
885
|
+
nodePadding?: number;
|
|
886
|
+
/**
|
|
887
|
+
* The width of node
|
|
888
|
+
* @default 10
|
|
889
|
+
*/
|
|
890
|
+
nodeWidth?: number;
|
|
891
|
+
/**
|
|
892
|
+
* The curvature of width
|
|
893
|
+
* @default 0.5
|
|
894
|
+
*/
|
|
895
|
+
linkCurvature?: number;
|
|
896
|
+
/**
|
|
897
|
+
* The number of the iterations between the links
|
|
898
|
+
* @default 32
|
|
899
|
+
*/
|
|
900
|
+
iterations?: number;
|
|
901
|
+
/**
|
|
902
|
+
* If set an object, the option is the configuration of nodes.
|
|
903
|
+
* If set a React element, the option is the custom react element of drawing the nodes.
|
|
904
|
+
*
|
|
905
|
+
* @example <Sankey node={MyCustomComponent} />
|
|
906
|
+
* @example <Sankey node={{stroke: #77c878, strokeWidth: 2}} />
|
|
907
|
+
*/
|
|
908
|
+
node?: SankeyNodeOptions;
|
|
909
|
+
/**
|
|
910
|
+
* If set an object, the option is the configuration of links.
|
|
911
|
+
* If set a React element, the option is the custom react element of drawing the links.
|
|
912
|
+
*
|
|
913
|
+
* @example <Sankey link={MyCustomComponent} />
|
|
914
|
+
* @example <Sankey link={{ fill: #77c878 }} />
|
|
915
|
+
*/
|
|
916
|
+
link?: SankeyLinkOptions;
|
|
917
|
+
style?: React.CSSProperties;
|
|
918
|
+
className?: string;
|
|
919
|
+
children?: ReactNode;
|
|
920
|
+
/**
|
|
921
|
+
* Empty space around the container.
|
|
922
|
+
*
|
|
923
|
+
* @defaultValue {"top":5,"right":5,"bottom":5,"left":5}
|
|
924
|
+
*/
|
|
925
|
+
margin?: Partial<Margin>;
|
|
926
|
+
/**
|
|
927
|
+
* The customized event handler of click on the area in this group
|
|
928
|
+
*/
|
|
929
|
+
onClick?: (item: NodeProps | LinkProps, type: SankeyElementType, e: MouseEvent<
|
|
930
|
+
SVGGraphicsElement
|
|
931
|
+
>) => void;
|
|
932
|
+
/**
|
|
933
|
+
* The customized event handler of mouseenter on the area in this group
|
|
934
|
+
*/
|
|
935
|
+
onMouseEnter?: (item: NodeProps | LinkProps, type: SankeyElementType, e: MouseEvent<
|
|
936
|
+
SVGGraphicsElement
|
|
937
|
+
>) => void;
|
|
938
|
+
/**
|
|
939
|
+
* The customized event handler of mouseleave on the area in this group
|
|
940
|
+
*/
|
|
941
|
+
onMouseLeave?: (item: NodeProps | LinkProps, type: SankeyElementType, e: MouseEvent<
|
|
942
|
+
SVGGraphicsElement
|
|
943
|
+
>) => void;
|
|
944
|
+
/**
|
|
945
|
+
* Whether to sort the nodes on the y axis, or to display them as user-defined.
|
|
946
|
+
* @default true
|
|
947
|
+
*/
|
|
948
|
+
sort?: boolean;
|
|
949
|
+
/**
|
|
950
|
+
* Controls the vertical spacing of nodes within a depth. 'justify' distributes nodes evenly and balances link paths, while 'top' positions the group starting from the top edge of the chart.
|
|
951
|
+
* @default 'justify'
|
|
952
|
+
*/
|
|
953
|
+
verticalAlign?: SankeyVerticalAlign;
|
|
954
|
+
/**
|
|
955
|
+
* If set to 'justify', the start nodes will be aligned to the left edge of the chart and the end nodes will be aligned to the right edge of the chart. If set to 'left', the start nodes will be aligned to the left edge of the chart.
|
|
956
|
+
* @default 'justify'
|
|
957
|
+
*/
|
|
958
|
+
align?: 'left' | 'justify';
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
export type Props = Omit<SVGProps<SVGSVGElement>, keyof SankeyProps> & SankeyProps;
|
|
962
|
+
|
|
963
|
+
export type SankeyElementType = 'node' | 'link';
|
|
964
|
+
|
|
965
|
+
function renderLinkItem(option: SankeyLinkOptions | undefined, props: LinkProps) {
|
|
966
|
+
if (React.isValidElement(option)) {
|
|
967
|
+
return React.cloneElement(option, props);
|
|
968
|
+
}
|
|
969
|
+
if (typeof option === 'function') {
|
|
970
|
+
return option(props);
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
const {
|
|
974
|
+
sourceX,
|
|
975
|
+
sourceY,
|
|
976
|
+
sourceControlX,
|
|
977
|
+
targetX,
|
|
978
|
+
targetY,
|
|
979
|
+
targetControlX,
|
|
980
|
+
linkWidth,
|
|
981
|
+
...others
|
|
982
|
+
} = props;
|
|
983
|
+
|
|
984
|
+
return <path
|
|
985
|
+
className="recharts-sankey-link"
|
|
986
|
+
d={`
|
|
987
|
+
M${sourceX},${sourceY}
|
|
988
|
+
C${sourceControlX},${sourceY} ${targetControlX},${targetY} ${targetX},${targetY}
|
|
989
|
+
`}
|
|
990
|
+
fill="none"
|
|
991
|
+
stroke="#333"
|
|
992
|
+
strokeWidth={linkWidth}
|
|
993
|
+
strokeOpacity="0.2"
|
|
994
|
+
{...svgPropertiesNoEvents(others)}
|
|
995
|
+
/>;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
const buildLinkProps = ({
|
|
999
|
+
link,
|
|
1000
|
+
nodes,
|
|
1001
|
+
left,
|
|
1002
|
+
top,
|
|
1003
|
+
i,
|
|
1004
|
+
linkContent,
|
|
1005
|
+
linkCurvature,
|
|
1006
|
+
}: {
|
|
1007
|
+
link: SankeyLink;
|
|
1008
|
+
nodes: ReadonlyArray<SankeyNode>;
|
|
1009
|
+
top: number;
|
|
1010
|
+
left: number;
|
|
1011
|
+
linkContent: SankeyLinkOptions | undefined;
|
|
1012
|
+
i: number;
|
|
1013
|
+
linkCurvature: number;
|
|
1014
|
+
}): LinkProps | undefined => {
|
|
1015
|
+
const { sy: sourceRelativeY, ty: targetRelativeY, dy: linkWidth } = link;
|
|
1016
|
+
const sourceNode = nodes[link.source];
|
|
1017
|
+
const targetNode = nodes[link.target];
|
|
1018
|
+
if (sourceNode == null || targetNode == null) {
|
|
1019
|
+
return undefined;
|
|
1020
|
+
}
|
|
1021
|
+
const sourceX = sourceNode.x + sourceNode.dx + left;
|
|
1022
|
+
const targetX = targetNode.x + left;
|
|
1023
|
+
const interpolationFunc = interpolationGenerator(sourceX, targetX);
|
|
1024
|
+
const sourceControlX = interpolationFunc(linkCurvature);
|
|
1025
|
+
const targetControlX = interpolationFunc(1 - linkCurvature);
|
|
1026
|
+
const sourceY = sourceNode.y + sourceRelativeY + linkWidth / 2 + top;
|
|
1027
|
+
const targetY = targetNode.y + targetRelativeY + linkWidth / 2 + top;
|
|
1028
|
+
|
|
1029
|
+
const linkProps: LinkProps = {
|
|
1030
|
+
sourceX,
|
|
1031
|
+
// @ts-expect-error the linkContent from below is contributing unknown props
|
|
1032
|
+
targetX,
|
|
1033
|
+
sourceY,
|
|
1034
|
+
// @ts-expect-error the linkContent from below is contributing unknown props
|
|
1035
|
+
targetY,
|
|
1036
|
+
sourceControlX,
|
|
1037
|
+
targetControlX,
|
|
1038
|
+
sourceRelativeY,
|
|
1039
|
+
targetRelativeY,
|
|
1040
|
+
linkWidth,
|
|
1041
|
+
index: i,
|
|
1042
|
+
payload: { ...link, source: sourceNode, target: targetNode },
|
|
1043
|
+
...svgPropertiesNoEventsFromUnknown(linkContent),
|
|
1044
|
+
};
|
|
1045
|
+
|
|
1046
|
+
return linkProps;
|
|
1047
|
+
};
|
|
1048
|
+
|
|
1049
|
+
function SankeyLinkElement({
|
|
1050
|
+
graphicalItemId,
|
|
1051
|
+
props,
|
|
1052
|
+
i,
|
|
1053
|
+
linkContent,
|
|
1054
|
+
onMouseEnter,
|
|
1055
|
+
onMouseLeave,
|
|
1056
|
+
onClick,
|
|
1057
|
+
dataKey,
|
|
1058
|
+
}: {
|
|
1059
|
+
graphicalItemId: GraphicalItemId;
|
|
1060
|
+
props: LinkProps;
|
|
1061
|
+
i: number;
|
|
1062
|
+
linkContent: SankeyLinkOptions | undefined;
|
|
1063
|
+
onMouseEnter: (linkProps: LinkProps, e: MouseEvent<SVGGraphicsElement>) => void;
|
|
1064
|
+
onMouseLeave: (linkProps: LinkProps, e: MouseEvent<SVGGraphicsElement>) => void;
|
|
1065
|
+
onClick: (linkProps: LinkProps, e: MouseEvent<SVGGraphicsElement>) => void;
|
|
1066
|
+
dataKey: DataKey<any>;
|
|
1067
|
+
}) {
|
|
1068
|
+
const activeCoordinate = getLinkCoordinateOfTooltip(props);
|
|
1069
|
+
const activeIndex = `link-${i}`;
|
|
1070
|
+
|
|
1071
|
+
const dispatch = useAppDispatch();
|
|
1072
|
+
|
|
1073
|
+
const events = {
|
|
1074
|
+
onMouseEnter: (e: MouseEvent<SVGGraphicsElement>) => {
|
|
1075
|
+
dispatch(
|
|
1076
|
+
setActiveMouseOverItemIndex({
|
|
1077
|
+
activeIndex,
|
|
1078
|
+
activeDataKey: dataKey,
|
|
1079
|
+
activeCoordinate,
|
|
1080
|
+
activeGraphicalItemId: graphicalItemId,
|
|
1081
|
+
}),
|
|
1082
|
+
);
|
|
1083
|
+
onMouseEnter(props, e);
|
|
1084
|
+
},
|
|
1085
|
+
onMouseLeave: (e: MouseEvent<SVGGraphicsElement>) => {
|
|
1086
|
+
dispatch(mouseLeaveItem());
|
|
1087
|
+
onMouseLeave(props, e);
|
|
1088
|
+
},
|
|
1089
|
+
onClick: (e: MouseEvent<SVGGraphicsElement>) => {
|
|
1090
|
+
dispatch(
|
|
1091
|
+
setActiveClickItemIndex({
|
|
1092
|
+
activeIndex,
|
|
1093
|
+
activeDataKey: dataKey,
|
|
1094
|
+
activeCoordinate,
|
|
1095
|
+
activeGraphicalItemId: graphicalItemId,
|
|
1096
|
+
}),
|
|
1097
|
+
);
|
|
1098
|
+
onClick(props, e);
|
|
1099
|
+
},
|
|
1100
|
+
};
|
|
1101
|
+
|
|
1102
|
+
return <Layer {...events}>{renderLinkItem(linkContent, props)}</Layer>;
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
function AllSankeyLinkElements({
|
|
1106
|
+
graphicalItemId,
|
|
1107
|
+
modifiedLinks,
|
|
1108
|
+
links,
|
|
1109
|
+
linkContent,
|
|
1110
|
+
onMouseEnter,
|
|
1111
|
+
onMouseLeave,
|
|
1112
|
+
onClick,
|
|
1113
|
+
dataKey,
|
|
1114
|
+
}: {
|
|
1115
|
+
graphicalItemId: GraphicalItemId;
|
|
1116
|
+
modifiedLinks: ReadonlyArray<LinkProps>;
|
|
1117
|
+
links: ReadonlyArray<SankeyLink>;
|
|
1118
|
+
linkContent: SankeyLinkOptions | undefined;
|
|
1119
|
+
onMouseEnter: (linkProps: LinkProps, e: MouseEvent<SVGGraphicsElement>) => void;
|
|
1120
|
+
onMouseLeave: (linkProps: LinkProps, e: MouseEvent<SVGGraphicsElement>) => void;
|
|
1121
|
+
onClick: (linkProps: LinkProps, e: MouseEvent<SVGGraphicsElement>) => void;
|
|
1122
|
+
dataKey: DataKey<any>;
|
|
1123
|
+
}) {
|
|
1124
|
+
return <Layer className="recharts-sankey-links" key="recharts-sankey-links">{links.map((
|
|
1125
|
+
link: SankeyLink,
|
|
1126
|
+
i: number,
|
|
1127
|
+
) => {
|
|
1128
|
+
const linkProps = modifiedLinks[i];
|
|
1129
|
+
if (linkProps == null) {
|
|
1130
|
+
return null;
|
|
1131
|
+
}
|
|
1132
|
+
return <SankeyLinkElement
|
|
1133
|
+
graphicalItemId={graphicalItemId}
|
|
1134
|
+
key={`link-${link.source}-${link.target}-${link.value}`}
|
|
1135
|
+
props={linkProps}
|
|
1136
|
+
linkContent={linkContent}
|
|
1137
|
+
i={i}
|
|
1138
|
+
onMouseEnter={onMouseEnter}
|
|
1139
|
+
onMouseLeave={onMouseLeave}
|
|
1140
|
+
onClick={onClick}
|
|
1141
|
+
dataKey={dataKey}
|
|
1142
|
+
/>;
|
|
1143
|
+
})}</Layer>;
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
function renderNodeItem(option: SankeyNodeOptions | undefined, props: NodeProps) {
|
|
1147
|
+
if (React.isValidElement(option)) {
|
|
1148
|
+
return React.cloneElement(option, props);
|
|
1149
|
+
}
|
|
1150
|
+
if (typeof option === 'function') {
|
|
1151
|
+
return option(props);
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
return (
|
|
1155
|
+
// @ts-expect-error recharts radius is not compatible with SVG radius
|
|
1156
|
+
<Rectangle
|
|
1157
|
+
className="recharts-sankey-node"
|
|
1158
|
+
fill="#0088fe"
|
|
1159
|
+
fillOpacity="0.8"
|
|
1160
|
+
{...svgPropertiesNoEvents(props)}
|
|
1161
|
+
/>
|
|
1162
|
+
);
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
const buildNodeProps = ({
|
|
1166
|
+
node,
|
|
1167
|
+
nodeContent,
|
|
1168
|
+
top,
|
|
1169
|
+
left,
|
|
1170
|
+
i,
|
|
1171
|
+
}: {
|
|
1172
|
+
node: SankeyNode;
|
|
1173
|
+
nodeContent: SankeyNodeOptions | undefined;
|
|
1174
|
+
top: number;
|
|
1175
|
+
left: number;
|
|
1176
|
+
i: number;
|
|
1177
|
+
}) => {
|
|
1178
|
+
const { x, y, dx, dy } = node;
|
|
1179
|
+
// @ts-expect-error nodeContent is passing in unknown props
|
|
1180
|
+
const nodeProps: NodeProps = {
|
|
1181
|
+
...svgPropertiesNoEventsFromUnknown(nodeContent),
|
|
1182
|
+
x: x + left,
|
|
1183
|
+
y: y + top,
|
|
1184
|
+
width: dx,
|
|
1185
|
+
height: dy,
|
|
1186
|
+
index: i,
|
|
1187
|
+
payload: node,
|
|
1188
|
+
};
|
|
1189
|
+
return nodeProps;
|
|
1190
|
+
};
|
|
1191
|
+
|
|
1192
|
+
function NodeElement({
|
|
1193
|
+
graphicalItemId,
|
|
1194
|
+
props,
|
|
1195
|
+
nodeContent,
|
|
1196
|
+
i,
|
|
1197
|
+
onMouseEnter,
|
|
1198
|
+
onMouseLeave,
|
|
1199
|
+
onClick,
|
|
1200
|
+
dataKey,
|
|
1201
|
+
}: {
|
|
1202
|
+
graphicalItemId: GraphicalItemId;
|
|
1203
|
+
props: NodeProps;
|
|
1204
|
+
nodeContent: SankeyNodeOptions | undefined;
|
|
1205
|
+
i: number;
|
|
1206
|
+
onMouseEnter: (nodeProps: NodeProps, e: MouseEvent<SVGGraphicsElement>) => void;
|
|
1207
|
+
onMouseLeave: (nodeProps: NodeProps, e: MouseEvent<SVGGraphicsElement>) => void;
|
|
1208
|
+
onClick: (nodeProps: NodeProps, e: MouseEvent<SVGGraphicsElement>) => void;
|
|
1209
|
+
dataKey: DataKey<any>;
|
|
1210
|
+
}) {
|
|
1211
|
+
const dispatch = useAppDispatch();
|
|
1212
|
+
|
|
1213
|
+
const activeCoordinate = getNodeCoordinateOfTooltip(props);
|
|
1214
|
+
const activeIndex = `node-${i}`;
|
|
1215
|
+
|
|
1216
|
+
const events = {
|
|
1217
|
+
onMouseEnter: (e: MouseEvent<SVGGraphicsElement>) => {
|
|
1218
|
+
dispatch(
|
|
1219
|
+
setActiveMouseOverItemIndex({
|
|
1220
|
+
activeIndex,
|
|
1221
|
+
activeDataKey: dataKey,
|
|
1222
|
+
activeCoordinate,
|
|
1223
|
+
activeGraphicalItemId: graphicalItemId,
|
|
1224
|
+
}),
|
|
1225
|
+
);
|
|
1226
|
+
onMouseEnter(props, e);
|
|
1227
|
+
},
|
|
1228
|
+
onMouseLeave: (e: MouseEvent<SVGGraphicsElement>) => {
|
|
1229
|
+
dispatch(mouseLeaveItem());
|
|
1230
|
+
onMouseLeave(props, e);
|
|
1231
|
+
},
|
|
1232
|
+
onClick: (e: MouseEvent<SVGGraphicsElement>) => {
|
|
1233
|
+
dispatch(
|
|
1234
|
+
setActiveClickItemIndex({
|
|
1235
|
+
activeIndex,
|
|
1236
|
+
activeDataKey: dataKey,
|
|
1237
|
+
activeCoordinate,
|
|
1238
|
+
activeGraphicalItemId: graphicalItemId,
|
|
1239
|
+
}),
|
|
1240
|
+
);
|
|
1241
|
+
onClick(props, e);
|
|
1242
|
+
},
|
|
1243
|
+
};
|
|
1244
|
+
|
|
1245
|
+
return <Layer {...events}>{renderNodeItem(nodeContent, props)}</Layer>;
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
function AllNodeElements({
|
|
1249
|
+
graphicalItemId,
|
|
1250
|
+
modifiedNodes,
|
|
1251
|
+
nodeContent,
|
|
1252
|
+
onMouseEnter,
|
|
1253
|
+
onMouseLeave,
|
|
1254
|
+
onClick,
|
|
1255
|
+
dataKey,
|
|
1256
|
+
}: {
|
|
1257
|
+
graphicalItemId: GraphicalItemId;
|
|
1258
|
+
modifiedNodes: ReadonlyArray<NodeProps>;
|
|
1259
|
+
nodeContent: SankeyNodeOptions | undefined;
|
|
1260
|
+
onMouseEnter: (nodeProps: NodeProps, e: MouseEvent<SVGGraphicsElement>) => void;
|
|
1261
|
+
onMouseLeave: (nodeProps: NodeProps, e: MouseEvent<SVGGraphicsElement>) => void;
|
|
1262
|
+
onClick: (nodeProps: NodeProps, e: MouseEvent<SVGGraphicsElement>) => void;
|
|
1263
|
+
dataKey: DataKey<any>;
|
|
1264
|
+
}) {
|
|
1265
|
+
return <Layer className="recharts-sankey-nodes" key="recharts-sankey-nodes">{modifiedNodes.map((
|
|
1266
|
+
modifiedNode,
|
|
1267
|
+
i,
|
|
1268
|
+
) => {
|
|
1269
|
+
return <NodeElement
|
|
1270
|
+
graphicalItemId={graphicalItemId}
|
|
1271
|
+
key={`node-${modifiedNode.index}-${modifiedNode.x}-${modifiedNode.y}`}
|
|
1272
|
+
props={modifiedNode}
|
|
1273
|
+
nodeContent={nodeContent}
|
|
1274
|
+
i={i}
|
|
1275
|
+
onMouseEnter={onMouseEnter}
|
|
1276
|
+
onMouseLeave={onMouseLeave}
|
|
1277
|
+
onClick={onClick}
|
|
1278
|
+
dataKey={dataKey}
|
|
1279
|
+
/>;
|
|
1280
|
+
})}</Layer>;
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
export const sankeyDefaultProps = {
|
|
1284
|
+
align: 'justify',
|
|
1285
|
+
dataKey: 'value',
|
|
1286
|
+
iterations: 32,
|
|
1287
|
+
linkCurvature: 0.5,
|
|
1288
|
+
margin: { top: 5, right: 5, bottom: 5, left: 5 },
|
|
1289
|
+
nameKey: 'name',
|
|
1290
|
+
nodePadding: 10,
|
|
1291
|
+
nodeWidth: 10,
|
|
1292
|
+
sort: true,
|
|
1293
|
+
verticalAlign: 'justify',
|
|
1294
|
+
...initialEventSettingsState,
|
|
1295
|
+
} as const satisfies Partial<Props>;
|
|
1296
|
+
|
|
1297
|
+
type PropsWithResolvedDefaults = RequiresDefaultProps<Props, typeof sankeyDefaultProps>;
|
|
1298
|
+
|
|
1299
|
+
type InternalSankeyProps = WithIdRequired<PropsWithResolvedDefaults>;
|
|
1300
|
+
|
|
1301
|
+
function SankeyImpl(props: InternalSankeyProps) {
|
|
1302
|
+
const { className, style, children, id, ...others } = props;
|
|
1303
|
+
const {
|
|
1304
|
+
link,
|
|
1305
|
+
dataKey,
|
|
1306
|
+
node,
|
|
1307
|
+
onMouseEnter,
|
|
1308
|
+
onMouseLeave,
|
|
1309
|
+
onClick,
|
|
1310
|
+
data,
|
|
1311
|
+
iterations,
|
|
1312
|
+
nodeWidth,
|
|
1313
|
+
nodePadding,
|
|
1314
|
+
sort,
|
|
1315
|
+
linkCurvature,
|
|
1316
|
+
margin,
|
|
1317
|
+
verticalAlign,
|
|
1318
|
+
align,
|
|
1319
|
+
} = props;
|
|
1320
|
+
|
|
1321
|
+
const attrs = svgPropertiesNoEvents(others);
|
|
1322
|
+
|
|
1323
|
+
const width = useChartWidth();
|
|
1324
|
+
const height = useChartHeight();
|
|
1325
|
+
|
|
1326
|
+
const { links, modifiedLinks, modifiedNodes } = useMemo(() => {
|
|
1327
|
+
if (!data || !width || !height || width <= 0 || height <= 0) {
|
|
1328
|
+
return { nodes: [], links: [], modifiedLinks: [], modifiedNodes: [] };
|
|
1329
|
+
}
|
|
1330
|
+
const contentWidth = width - (margin.left ?? 0) - (margin.right ?? 0);
|
|
1331
|
+
const contentHeight = height - (margin.top ?? 0) - (margin.bottom ?? 0);
|
|
1332
|
+
const computed = computeData({
|
|
1333
|
+
data,
|
|
1334
|
+
width: contentWidth,
|
|
1335
|
+
height: contentHeight,
|
|
1336
|
+
iterations,
|
|
1337
|
+
nodeWidth,
|
|
1338
|
+
nodePadding,
|
|
1339
|
+
sort,
|
|
1340
|
+
verticalAlign,
|
|
1341
|
+
align,
|
|
1342
|
+
});
|
|
1343
|
+
|
|
1344
|
+
const top = margin.top || 0;
|
|
1345
|
+
const left = margin.left || 0;
|
|
1346
|
+
const newModifiedLinks = computed.links.map((l, i) => {
|
|
1347
|
+
return buildLinkProps({
|
|
1348
|
+
link: l,
|
|
1349
|
+
nodes: computed.nodes,
|
|
1350
|
+
i,
|
|
1351
|
+
top,
|
|
1352
|
+
left,
|
|
1353
|
+
linkContent: link,
|
|
1354
|
+
linkCurvature,
|
|
1355
|
+
});
|
|
1356
|
+
}).filter(isNotNil);
|
|
1357
|
+
|
|
1358
|
+
const newModifiedNodes = computed.nodes.map((n, i) => {
|
|
1359
|
+
return buildNodeProps({
|
|
1360
|
+
node: n,
|
|
1361
|
+
nodeContent: node,
|
|
1362
|
+
i,
|
|
1363
|
+
top,
|
|
1364
|
+
left,
|
|
1365
|
+
});
|
|
1366
|
+
});
|
|
1367
|
+
|
|
1368
|
+
return {
|
|
1369
|
+
nodes: computed.nodes,
|
|
1370
|
+
links: computed.links,
|
|
1371
|
+
modifiedLinks: newModifiedLinks,
|
|
1372
|
+
modifiedNodes: newModifiedNodes,
|
|
1373
|
+
};
|
|
1374
|
+
}, [
|
|
1375
|
+
data,
|
|
1376
|
+
width,
|
|
1377
|
+
height,
|
|
1378
|
+
margin,
|
|
1379
|
+
iterations,
|
|
1380
|
+
nodeWidth,
|
|
1381
|
+
nodePadding,
|
|
1382
|
+
sort,
|
|
1383
|
+
link,
|
|
1384
|
+
node,
|
|
1385
|
+
linkCurvature,
|
|
1386
|
+
align,
|
|
1387
|
+
verticalAlign,
|
|
1388
|
+
]);
|
|
1389
|
+
|
|
1390
|
+
const handleMouseEnter = useCallback((
|
|
1391
|
+
item: NodeProps | LinkProps,
|
|
1392
|
+
type: SankeyElementType,
|
|
1393
|
+
e: MouseEvent<SVGGraphicsElement>,
|
|
1394
|
+
) => {
|
|
1395
|
+
if (onMouseEnter) {
|
|
1396
|
+
onMouseEnter(item, type, e);
|
|
1397
|
+
}
|
|
1398
|
+
}, [onMouseEnter]);
|
|
1399
|
+
|
|
1400
|
+
const handleMouseLeave = useCallback((
|
|
1401
|
+
item: NodeProps | LinkProps,
|
|
1402
|
+
type: SankeyElementType,
|
|
1403
|
+
e: MouseEvent<SVGGraphicsElement>,
|
|
1404
|
+
) => {
|
|
1405
|
+
if (onMouseLeave) {
|
|
1406
|
+
onMouseLeave(item, type, e);
|
|
1407
|
+
}
|
|
1408
|
+
}, [onMouseLeave]);
|
|
1409
|
+
|
|
1410
|
+
const handleClick = useCallback((
|
|
1411
|
+
item: NodeProps | LinkProps,
|
|
1412
|
+
type: SankeyElementType,
|
|
1413
|
+
e: MouseEvent<SVGGraphicsElement>,
|
|
1414
|
+
) => {
|
|
1415
|
+
if (onClick) {
|
|
1416
|
+
onClick(item, type, e);
|
|
1417
|
+
}
|
|
1418
|
+
}, [onClick]);
|
|
1419
|
+
|
|
1420
|
+
if (
|
|
1421
|
+
!isPositiveNumber(width) || !isPositiveNumber(height) || !data || !data.links || !data.nodes
|
|
1422
|
+
) {
|
|
1423
|
+
return null;
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
return <>
|
|
1427
|
+
<SetComputedData computedData={{ links: modifiedLinks, nodes: modifiedNodes }} />
|
|
1428
|
+
<Surface {...attrs} width={width} height={height}>
|
|
1429
|
+
{children}
|
|
1430
|
+
<AllSankeyLinkElements
|
|
1431
|
+
graphicalItemId={id}
|
|
1432
|
+
links={links}
|
|
1433
|
+
modifiedLinks={modifiedLinks}
|
|
1434
|
+
linkContent={link}
|
|
1435
|
+
dataKey={dataKey}
|
|
1436
|
+
onMouseEnter={(linkProps: LinkProps, e: MouseEvent<SVGGraphicsElement>) => handleMouseEnter(
|
|
1437
|
+
linkProps,
|
|
1438
|
+
'link',
|
|
1439
|
+
e,
|
|
1440
|
+
)}
|
|
1441
|
+
onMouseLeave={(linkProps: LinkProps, e: MouseEvent<SVGGraphicsElement>) => handleMouseLeave(
|
|
1442
|
+
linkProps,
|
|
1443
|
+
'link',
|
|
1444
|
+
e,
|
|
1445
|
+
)}
|
|
1446
|
+
onClick={(linkProps: LinkProps, e: MouseEvent<SVGGraphicsElement>) => handleClick(
|
|
1447
|
+
linkProps,
|
|
1448
|
+
'link',
|
|
1449
|
+
e,
|
|
1450
|
+
)}
|
|
1451
|
+
/>
|
|
1452
|
+
<AllNodeElements
|
|
1453
|
+
graphicalItemId={id}
|
|
1454
|
+
modifiedNodes={modifiedNodes}
|
|
1455
|
+
nodeContent={node}
|
|
1456
|
+
dataKey={dataKey}
|
|
1457
|
+
onMouseEnter={(nodeProps: NodeProps, e: MouseEvent<SVGGraphicsElement>) => handleMouseEnter(
|
|
1458
|
+
nodeProps,
|
|
1459
|
+
'node',
|
|
1460
|
+
e,
|
|
1461
|
+
)}
|
|
1462
|
+
onMouseLeave={(nodeProps: NodeProps, e: MouseEvent<SVGGraphicsElement>) => handleMouseLeave(
|
|
1463
|
+
nodeProps,
|
|
1464
|
+
'node',
|
|
1465
|
+
e,
|
|
1466
|
+
)}
|
|
1467
|
+
onClick={(nodeProps: NodeProps, e: MouseEvent<SVGGraphicsElement>) => handleClick(
|
|
1468
|
+
nodeProps,
|
|
1469
|
+
'node',
|
|
1470
|
+
e,
|
|
1471
|
+
)}
|
|
1472
|
+
/>
|
|
1473
|
+
</Surface>
|
|
1474
|
+
</>;
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
/**
|
|
1478
|
+
* Flow diagram in which the width of the arrows is proportional to the flow rate.
|
|
1479
|
+
* It is typically used to visualize energy or material or cost transfers between processes.
|
|
1480
|
+
*
|
|
1481
|
+
* @consumes ResponsiveContainerContext
|
|
1482
|
+
* @provides TooltipEntrySettings
|
|
1483
|
+
*/
|
|
1484
|
+
export function Sankey(outsideProps: Props) {
|
|
1485
|
+
const props: PropsWithResolvedDefaults = resolveDefaultProps(outsideProps, sankeyDefaultProps);
|
|
1486
|
+
const { width, height, style, className, id: externalId, throttleDelay, throttledEvents } = props;
|
|
1487
|
+
const [tooltipPortal, setTooltipPortal] = useState<HTMLElement | null>(null);
|
|
1488
|
+
|
|
1489
|
+
return <RechartsStoreProvider preloadedState={{ options }} reduxStoreName={className ?? 'Sankey'}>
|
|
1490
|
+
<ReportChartSize width={width} height={height} />
|
|
1491
|
+
<ReportChartMargin margin={props.margin} />
|
|
1492
|
+
<ReportEventSettings throttleDelay={throttleDelay} throttledEvents={throttledEvents} />
|
|
1493
|
+
|
|
1494
|
+
<RechartsWrapper
|
|
1495
|
+
className={className}
|
|
1496
|
+
style={style}
|
|
1497
|
+
width={width}
|
|
1498
|
+
height={height}
|
|
1499
|
+
/*
|
|
1500
|
+
* Sankey, same as Treemap, suffers from overfilling the container
|
|
1501
|
+
* and causing infinite render loops where the chart keeps growing.
|
|
1502
|
+
*/
|
|
1503
|
+
responsive={false}
|
|
1504
|
+
ref={(node: HTMLDivElement | null) => {
|
|
1505
|
+
if (node && !tooltipPortal) {
|
|
1506
|
+
setTooltipPortal(node);
|
|
1507
|
+
}
|
|
1508
|
+
}}
|
|
1509
|
+
onMouseEnter={undefined}
|
|
1510
|
+
onMouseLeave={undefined}
|
|
1511
|
+
onClick={undefined}
|
|
1512
|
+
onMouseMove={undefined}
|
|
1513
|
+
onMouseDown={undefined}
|
|
1514
|
+
onMouseUp={undefined}
|
|
1515
|
+
onContextMenu={undefined}
|
|
1516
|
+
onDoubleClick={undefined}
|
|
1517
|
+
onTouchStart={undefined}
|
|
1518
|
+
onTouchMove={undefined}
|
|
1519
|
+
onTouchEnd={undefined}
|
|
1520
|
+
>
|
|
1521
|
+
<TooltipPortalContext.Provider value={tooltipPortal}>
|
|
1522
|
+
<RegisterGraphicalItemId id={externalId} type="sankey">
|
|
1523
|
+
{(id) => <>
|
|
1524
|
+
<SetSankeyTooltipEntrySettings
|
|
1525
|
+
dataKey={props.dataKey}
|
|
1526
|
+
nameKey={props.nameKey}
|
|
1527
|
+
stroke={props.stroke}
|
|
1528
|
+
strokeWidth={props.strokeWidth}
|
|
1529
|
+
fill={props.fill}
|
|
1530
|
+
name={props.name}
|
|
1531
|
+
data={props.data}
|
|
1532
|
+
id={id}
|
|
1533
|
+
/>
|
|
1534
|
+
<SankeyImpl {...props} id={id} />
|
|
1535
|
+
</>}
|
|
1536
|
+
</RegisterGraphicalItemId>
|
|
1537
|
+
</TooltipPortalContext.Provider>
|
|
1538
|
+
</RechartsWrapper>
|
|
1539
|
+
</RechartsStoreProvider>;
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
Sankey.displayName = 'Sankey';
|