@antv/g2 5.1.13 → 5.1.15
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/dist/g2.lite.min.js +1 -1
- package/dist/g2.min.js +1 -1
- package/esm/api/utils.js +27 -15
- package/esm/api/utils.js.map +1 -1
- package/esm/component/axis.js +13 -7
- package/esm/component/axis.js.map +1 -1
- package/esm/coordinate/polar.d.ts +1 -1
- package/esm/coordinate/polar.js +3 -1
- package/esm/coordinate/polar.js.map +1 -1
- package/esm/index.d.ts +2 -0
- package/esm/interaction/event.js +15 -3
- package/esm/interaction/event.js.map +1 -1
- package/esm/interaction/index.d.ts +1 -0
- package/esm/interaction/index.js +1 -0
- package/esm/interaction/index.js.map +1 -1
- package/esm/interaction/legendFilter.d.ts +1 -0
- package/esm/interaction/legendFilter.js +6 -0
- package/esm/interaction/legendFilter.js.map +1 -1
- package/esm/interaction/scrollbarFilter.js +5 -1
- package/esm/interaction/scrollbarFilter.js.map +1 -1
- package/esm/interaction/sliderFilter.d.ts +1 -1
- package/esm/interaction/sliderFilter.js +19 -2
- package/esm/interaction/sliderFilter.js.map +1 -1
- package/esm/interaction/tooltip.js +1 -1
- package/esm/interaction/tooltip.js.map +1 -1
- package/esm/interaction/treemapDrillDown.d.ts +10 -0
- package/esm/interaction/treemapDrillDown.js +229 -0
- package/esm/interaction/treemapDrillDown.js.map +1 -0
- package/esm/lib/core.d.ts +2 -1
- package/esm/lib/core.js +2 -1
- package/esm/lib/core.js.map +1 -1
- package/esm/lib/std.d.ts +1 -0
- package/esm/mark/treemap.js +37 -57
- package/esm/mark/treemap.js.map +1 -1
- package/esm/mark/utils.d.ts +0 -5
- package/esm/mark/utils.js +0 -13
- package/esm/mark/utils.js.map +1 -1
- package/esm/runtime/plot.js +46 -28
- package/esm/runtime/plot.js.map +1 -1
- package/esm/runtime/types/common.d.ts +1 -1
- package/esm/shape/utils.js +2 -2
- package/esm/shape/utils.js.map +1 -1
- package/esm/utils/angle.d.ts +4 -0
- package/esm/utils/angle.js +18 -0
- package/esm/utils/angle.js.map +1 -1
- package/esm/utils/treeDataTransform.d.ts +21 -0
- package/esm/utils/treeDataTransform.js +90 -0
- package/esm/utils/treeDataTransform.js.map +1 -0
- package/lib/api/utils.js +27 -15
- package/lib/api/utils.js.map +1 -1
- package/lib/component/axis.js +13 -7
- package/lib/component/axis.js.map +1 -1
- package/lib/coordinate/polar.d.ts +1 -1
- package/lib/coordinate/polar.js +3 -1
- package/lib/coordinate/polar.js.map +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/interaction/event.js +15 -3
- package/lib/interaction/event.js.map +1 -1
- package/lib/interaction/index.d.ts +1 -0
- package/lib/interaction/index.js +3 -1
- package/lib/interaction/index.js.map +1 -1
- package/lib/interaction/legendFilter.d.ts +1 -0
- package/lib/interaction/legendFilter.js +8 -1
- package/lib/interaction/legendFilter.js.map +1 -1
- package/lib/interaction/scrollbarFilter.js +5 -1
- package/lib/interaction/scrollbarFilter.js.map +1 -1
- package/lib/interaction/sliderFilter.d.ts +1 -1
- package/lib/interaction/sliderFilter.js +19 -2
- package/lib/interaction/sliderFilter.js.map +1 -1
- package/lib/interaction/tooltip.js +1 -1
- package/lib/interaction/tooltip.js.map +1 -1
- package/lib/interaction/treemapDrillDown.d.ts +10 -0
- package/lib/interaction/treemapDrillDown.js +233 -0
- package/lib/interaction/treemapDrillDown.js.map +1 -0
- package/lib/lib/core.d.ts +2 -1
- package/lib/lib/core.js +1 -0
- package/lib/lib/core.js.map +1 -1
- package/lib/lib/std.d.ts +1 -0
- package/lib/mark/treemap.js +36 -56
- package/lib/mark/treemap.js.map +1 -1
- package/lib/mark/utils.d.ts +0 -5
- package/lib/mark/utils.js +1 -15
- package/lib/mark/utils.js.map +1 -1
- package/lib/runtime/plot.js +45 -27
- package/lib/runtime/plot.js.map +1 -1
- package/lib/runtime/types/common.d.ts +1 -1
- package/lib/shape/utils.js +1 -1
- package/lib/shape/utils.js.map +1 -1
- package/lib/utils/angle.d.ts +4 -0
- package/lib/utils/angle.js +20 -1
- package/lib/utils/angle.js.map +1 -1
- package/lib/utils/treeDataTransform.d.ts +21 -0
- package/lib/utils/treeDataTransform.js +94 -0
- package/lib/utils/treeDataTransform.js.map +1 -0
- package/package.json +3 -2
- package/src/api/utils.ts +41 -13
- package/src/component/axis.ts +17 -10
- package/src/coordinate/polar.ts +6 -1
- package/src/interaction/event.ts +20 -7
- package/src/interaction/index.ts +1 -0
- package/src/interaction/legendFilter.ts +8 -0
- package/src/interaction/scrollbarFilter.ts +4 -0
- package/src/interaction/sliderFilter.ts +21 -1
- package/src/interaction/tooltip.ts +2 -2
- package/src/interaction/treemapDrillDown.ts +282 -0
- package/src/lib/core.ts +2 -0
- package/src/mark/treemap.ts +76 -123
- package/src/mark/utils.ts +0 -17
- package/src/runtime/plot.ts +79 -38
- package/src/runtime/types/common.ts +5 -1
- package/src/shape/utils.ts +3 -2
- package/src/utils/angle.ts +22 -0
- package/src/utils/treeDataTransform.ts +153 -0
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { Text, Group } from '@antv/g';
|
|
2
|
+
import { get, deepMix, pick, keys, find, size, last } from '@antv/util';
|
|
3
|
+
import type { Node } from 'd3-hierarchy';
|
|
4
|
+
import type { DisplayObject } from '@antv/g';
|
|
5
|
+
import { subObject } from '../utils/helper';
|
|
6
|
+
import { PLOT_CLASS_NAME } from '../runtime';
|
|
7
|
+
import { select } from '../utils/selection';
|
|
8
|
+
import { treeDataTransform } from '../utils/treeDataTransform';
|
|
9
|
+
import { legendClearSetState } from './legendFilter';
|
|
10
|
+
|
|
11
|
+
// Get element.
|
|
12
|
+
const getElements = (plot) => {
|
|
13
|
+
return plot.querySelectorAll('.element');
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function selectPlotArea(root: DisplayObject): DisplayObject {
|
|
17
|
+
return select(root).select(`.${PLOT_CLASS_NAME}`).node();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type DrillDownOptions = {
|
|
21
|
+
originData?: Node[];
|
|
22
|
+
layout?: any;
|
|
23
|
+
[key: string]: any;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Default breadCrumb config.
|
|
27
|
+
const DEFAULT_BREADCRUMB_STYLE = {
|
|
28
|
+
breadCrumbFill: 'rgba(0, 0, 0, 0.85)',
|
|
29
|
+
breadCrumbFontSize: 12,
|
|
30
|
+
breadCrumbY: 12,
|
|
31
|
+
activeFill: 'rgba(0, 0, 0, 0.5)',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* TreemapDrillDown interaction.
|
|
36
|
+
*/
|
|
37
|
+
export function TreemapDrillDown(drillDownOptions: DrillDownOptions = {}) {
|
|
38
|
+
const { originData = [], layout, ...style } = drillDownOptions;
|
|
39
|
+
|
|
40
|
+
const breadCrumb = deepMix({}, DEFAULT_BREADCRUMB_STYLE, style);
|
|
41
|
+
|
|
42
|
+
const breadCrumbStyle = subObject(breadCrumb, 'breadCrumb');
|
|
43
|
+
const breadCrumbActiveStyle = subObject(breadCrumb, 'active');
|
|
44
|
+
|
|
45
|
+
return (context) => {
|
|
46
|
+
const { update, setState, container, options } = context;
|
|
47
|
+
const plotArea = selectPlotArea(container);
|
|
48
|
+
const mark = options.marks[0];
|
|
49
|
+
|
|
50
|
+
const { state } = mark;
|
|
51
|
+
|
|
52
|
+
// Create breadCrumbTextsGroup,save textSeparator、drillTexts.
|
|
53
|
+
const textGroup = new Group();
|
|
54
|
+
plotArea.appendChild(textGroup);
|
|
55
|
+
|
|
56
|
+
// Modify the data and scale according to the path and the level of the current click, so as to achieve the effect of drilling down and drilling up and initialization.
|
|
57
|
+
const drillDownClick = async (path: string[], depth: number) => {
|
|
58
|
+
// Clear text.
|
|
59
|
+
textGroup.removeChildren();
|
|
60
|
+
|
|
61
|
+
// More path creation text.
|
|
62
|
+
if (depth) {
|
|
63
|
+
let name = '';
|
|
64
|
+
let y = breadCrumbStyle.y;
|
|
65
|
+
let x = 0;
|
|
66
|
+
const textPath = [];
|
|
67
|
+
|
|
68
|
+
const maxWidth = plotArea.getBBox().width;
|
|
69
|
+
|
|
70
|
+
// Create path: 'type1 / type2 / type3' -> '/ type1 / type2 / type3'.
|
|
71
|
+
const drillTexts = path.map((text, index) => {
|
|
72
|
+
name = `${name}${text}/`;
|
|
73
|
+
textPath.push(text);
|
|
74
|
+
const drillText = new Text({
|
|
75
|
+
name: name.replace(/\/$/, ''),
|
|
76
|
+
style: {
|
|
77
|
+
text,
|
|
78
|
+
x,
|
|
79
|
+
// @ts-ignore
|
|
80
|
+
path: [...textPath],
|
|
81
|
+
depth: index,
|
|
82
|
+
...breadCrumbStyle,
|
|
83
|
+
y,
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
textGroup.appendChild(drillText);
|
|
88
|
+
|
|
89
|
+
x += drillText.getBBox().width;
|
|
90
|
+
|
|
91
|
+
const textSeparator = new Text({
|
|
92
|
+
style: {
|
|
93
|
+
x,
|
|
94
|
+
text: ' / ',
|
|
95
|
+
...breadCrumbStyle,
|
|
96
|
+
y,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
textGroup.appendChild(textSeparator);
|
|
101
|
+
|
|
102
|
+
x += textSeparator.getBBox().width;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Page width exceeds maximum, line feed.
|
|
106
|
+
* | ----maxWidth---- |
|
|
107
|
+
* | / tyep1 / tyep2 / type3 |
|
|
108
|
+
* ->
|
|
109
|
+
* | ----maxWidth---- |
|
|
110
|
+
* | / tyep1 / tyep2 |
|
|
111
|
+
* | / type3 |
|
|
112
|
+
*/
|
|
113
|
+
if (x > maxWidth) {
|
|
114
|
+
y = textGroup.getBBox().height + breadCrumbStyle.y;
|
|
115
|
+
x = 0;
|
|
116
|
+
drillText.attr({
|
|
117
|
+
x,
|
|
118
|
+
y,
|
|
119
|
+
});
|
|
120
|
+
x += drillText.getBBox().width;
|
|
121
|
+
textSeparator.attr({
|
|
122
|
+
x,
|
|
123
|
+
y,
|
|
124
|
+
});
|
|
125
|
+
x += textSeparator.getBBox().width;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (index === size(path) - 1) {
|
|
129
|
+
textSeparator.remove();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return drillText;
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Add Active, Add TreemapDrillDown
|
|
136
|
+
drillTexts.forEach((item, index) => {
|
|
137
|
+
// Last drillText
|
|
138
|
+
if (index === size(drillTexts) - 1) return;
|
|
139
|
+
const originalAttrs = { ...item.attributes };
|
|
140
|
+
item.attr('cursor', 'pointer');
|
|
141
|
+
item.addEventListener('mouseenter', () => {
|
|
142
|
+
item.attr(breadCrumbActiveStyle);
|
|
143
|
+
});
|
|
144
|
+
item.addEventListener('mouseleave', () => {
|
|
145
|
+
item.attr(originalAttrs);
|
|
146
|
+
});
|
|
147
|
+
item.addEventListener('click', () => {
|
|
148
|
+
drillDownClick(
|
|
149
|
+
get(item, ['style', 'path']),
|
|
150
|
+
get(item, ['style', 'depth']),
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// LegendFilter interaction and treemapDrillDown clash.
|
|
157
|
+
legendClearSetState(container, setState);
|
|
158
|
+
|
|
159
|
+
// Update marks.
|
|
160
|
+
setState('treemapDrillDown', (viewOptions) => {
|
|
161
|
+
const { marks } = viewOptions;
|
|
162
|
+
// Add filter transform for every marks,
|
|
163
|
+
// which will skip for mark without color channel.
|
|
164
|
+
|
|
165
|
+
const strPath = path.join('/');
|
|
166
|
+
|
|
167
|
+
const newMarks = marks.map((mark) => {
|
|
168
|
+
if (mark.type !== 'rect') return mark;
|
|
169
|
+
let newData = originData;
|
|
170
|
+
|
|
171
|
+
if (depth) {
|
|
172
|
+
const filterData = originData
|
|
173
|
+
.filter((item) => {
|
|
174
|
+
const id = get(item, ['id']);
|
|
175
|
+
return id && (id.match(`${strPath}/`) || strPath.match(id));
|
|
176
|
+
})
|
|
177
|
+
.map((item) => ({
|
|
178
|
+
value: item.height === 0 ? get(item, ['value']) : undefined,
|
|
179
|
+
name: get(item, ['id']),
|
|
180
|
+
}));
|
|
181
|
+
|
|
182
|
+
const { paddingLeft, paddingBottom, paddingRight } = layout;
|
|
183
|
+
|
|
184
|
+
// New drill layout for calculation x y and filtration data.
|
|
185
|
+
const newLayout = {
|
|
186
|
+
...layout,
|
|
187
|
+
paddingTop:
|
|
188
|
+
(layout.paddingTop || textGroup.getBBox().height + 10) /
|
|
189
|
+
(depth + 1),
|
|
190
|
+
paddingLeft: paddingLeft / (depth + 1),
|
|
191
|
+
paddingBottom: paddingBottom / (depth + 1),
|
|
192
|
+
paddingRight: paddingRight / (depth + 1),
|
|
193
|
+
path: (d) => d.name,
|
|
194
|
+
layer: (d) => d.depth === depth + 1,
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// Transform the new matrix tree data.
|
|
198
|
+
newData = treeDataTransform(filterData, newLayout, {
|
|
199
|
+
value: 'value',
|
|
200
|
+
})[0];
|
|
201
|
+
} else {
|
|
202
|
+
newData = originData.filter((item) => {
|
|
203
|
+
return item.depth === 1;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const colorDomain = [];
|
|
208
|
+
newData.forEach(({ path }) => {
|
|
209
|
+
colorDomain.push(last(path));
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// TreemapDrillDown by filtering the data and scale.
|
|
213
|
+
return deepMix({}, mark, {
|
|
214
|
+
data: newData,
|
|
215
|
+
scale: {
|
|
216
|
+
color: { domain: colorDomain },
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
return { ...viewOptions, marks: newMarks };
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// The second argument is to allow the legendFilter event to be re-added; the update method itself causes legend to lose the interaction event.
|
|
225
|
+
await update(undefined, ['legendFilter']);
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// Elements and BreadCrumb click.
|
|
229
|
+
const createDrillClick = (e) => {
|
|
230
|
+
const item = e.target;
|
|
231
|
+
if (get(item, ['markType']) !== 'rect') return;
|
|
232
|
+
|
|
233
|
+
const key = get(item, ['__data__', 'key']);
|
|
234
|
+
const node = find(originData, (d) => d.id === key);
|
|
235
|
+
|
|
236
|
+
// Node height = 0 no children
|
|
237
|
+
if (get(node, 'height')) {
|
|
238
|
+
drillDownClick(get(node, 'path'), get(node, 'depth'));
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
// Add click drill interaction.
|
|
243
|
+
plotArea.addEventListener('click', createDrillClick);
|
|
244
|
+
|
|
245
|
+
// Change attributes keys.
|
|
246
|
+
const changeStyleKey = keys({ ...state.active, ...state.inactive });
|
|
247
|
+
|
|
248
|
+
const createActive = () => {
|
|
249
|
+
const elements = getElements(plotArea);
|
|
250
|
+
elements.forEach((element) => {
|
|
251
|
+
const cursor = get(element, ['style', 'cursor']);
|
|
252
|
+
const node = find(
|
|
253
|
+
originData,
|
|
254
|
+
(d) => d.id === get(element, ['__data__', 'key']),
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
if (cursor !== 'pointer' && node?.height) {
|
|
258
|
+
element.style.cursor = 'pointer';
|
|
259
|
+
const originalAttrs = pick(element.attributes, changeStyleKey);
|
|
260
|
+
|
|
261
|
+
element.addEventListener('mouseenter', () => {
|
|
262
|
+
element.attr(state.active);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
element.addEventListener('mouseleave', () => {
|
|
266
|
+
element.attr(deepMix(originalAttrs, state.inactive));
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
createActive();
|
|
273
|
+
// Animate elements update, Add active.
|
|
274
|
+
plotArea.addEventListener('mousemove', createActive);
|
|
275
|
+
|
|
276
|
+
return () => {
|
|
277
|
+
textGroup.remove();
|
|
278
|
+
plotArea.removeEventListener('click', createDrillClick);
|
|
279
|
+
plotArea.removeEventListener('mousemove', createActive);
|
|
280
|
+
};
|
|
281
|
+
};
|
|
282
|
+
}
|
package/src/lib/core.ts
CHANGED
|
@@ -100,6 +100,7 @@ import {
|
|
|
100
100
|
SliderFilter,
|
|
101
101
|
Poptip,
|
|
102
102
|
ScrollbarFilter,
|
|
103
|
+
TreemapDrillDown,
|
|
103
104
|
} from '../interaction';
|
|
104
105
|
import {
|
|
105
106
|
SpaceLayer,
|
|
@@ -299,6 +300,7 @@ export function corelib() {
|
|
|
299
300
|
'interaction.sliderFilter': SliderFilter,
|
|
300
301
|
'interaction.scrollbarFilter': ScrollbarFilter,
|
|
301
302
|
'interaction.poptip': Poptip,
|
|
303
|
+
'interaction.treemapDrillDown': TreemapDrillDown,
|
|
302
304
|
'composition.spaceLayer': SpaceLayer,
|
|
303
305
|
'composition.spaceFlex': SpaceFlex,
|
|
304
306
|
'composition.facetRect': FacetRect,
|
package/src/mark/treemap.ts
CHANGED
|
@@ -1,111 +1,12 @@
|
|
|
1
|
-
import { deepMix } from '@antv/util';
|
|
2
|
-
import {
|
|
3
|
-
treemap as treemapLayout,
|
|
4
|
-
treemapBinary,
|
|
5
|
-
treemapDice,
|
|
6
|
-
treemapSlice,
|
|
7
|
-
treemapSliceDice,
|
|
8
|
-
treemapSquarify,
|
|
9
|
-
treemapResquarify,
|
|
10
|
-
} from 'd3-hierarchy';
|
|
1
|
+
import { deepMix, get, last } from '@antv/util';
|
|
11
2
|
import { subObject } from '../utils/helper';
|
|
12
3
|
import { CompositionComponent as CC } from '../runtime';
|
|
13
4
|
import { TreemapMark } from '../spec';
|
|
14
5
|
import { maybeTooltip } from '../utils/mark';
|
|
15
|
-
import {
|
|
6
|
+
import { treeDataTransform } from '../utils/treeDataTransform';
|
|
16
7
|
|
|
17
8
|
export type TreemapOptions = Omit<TreemapMark, 'type'>;
|
|
18
9
|
|
|
19
|
-
type Layout = {
|
|
20
|
-
tile?:
|
|
21
|
-
| 'treemapBinary'
|
|
22
|
-
| 'treemapDice'
|
|
23
|
-
| 'treemapSlice'
|
|
24
|
-
| 'treemapSliceDice'
|
|
25
|
-
| 'treemapSquarify'
|
|
26
|
-
| 'treemapResquarify';
|
|
27
|
-
size?: [number, number];
|
|
28
|
-
round?: boolean;
|
|
29
|
-
// Ignore the value of the parent node when calculating the total value.
|
|
30
|
-
ignoreParentValue?: boolean;
|
|
31
|
-
ratio?: number;
|
|
32
|
-
padding?: number;
|
|
33
|
-
paddingInner?: number;
|
|
34
|
-
paddingOuter?: number;
|
|
35
|
-
paddingTop?: number;
|
|
36
|
-
paddingRight?: number;
|
|
37
|
-
paddingBottom?: number;
|
|
38
|
-
paddingLeft?: number;
|
|
39
|
-
sort?(a: any, b: any): number;
|
|
40
|
-
path?: (d: any) => any;
|
|
41
|
-
/** The granularity of Display layer. */
|
|
42
|
-
layer?: number | ((d: any) => any);
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
type TreemapData = {
|
|
46
|
-
name: string;
|
|
47
|
-
children: TreemapData[];
|
|
48
|
-
[key: string]: any;
|
|
49
|
-
}[];
|
|
50
|
-
|
|
51
|
-
function getTileMethod(tile: string, ratio: number) {
|
|
52
|
-
const tiles = {
|
|
53
|
-
treemapBinary,
|
|
54
|
-
treemapDice,
|
|
55
|
-
treemapSlice,
|
|
56
|
-
treemapSliceDice,
|
|
57
|
-
treemapSquarify,
|
|
58
|
-
treemapResquarify,
|
|
59
|
-
};
|
|
60
|
-
const tileMethod =
|
|
61
|
-
tile === 'treemapSquarify' ? tiles[tile].ratio(ratio) : tiles[tile];
|
|
62
|
-
if (!tileMethod) {
|
|
63
|
-
throw new TypeError('Invalid tile method!');
|
|
64
|
-
}
|
|
65
|
-
return tileMethod;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function dataTransform(data, layout: Layout, encode): TreemapData {
|
|
69
|
-
const { value } = encode;
|
|
70
|
-
const tileMethod = getTileMethod(layout.tile, layout.ratio);
|
|
71
|
-
const root = generateHierarchyRoot(data, layout.path);
|
|
72
|
-
|
|
73
|
-
// Calculate the value and sort.
|
|
74
|
-
value
|
|
75
|
-
? root
|
|
76
|
-
.sum((d) =>
|
|
77
|
-
layout.ignoreParentValue && d.children ? 0 : field(value)(d),
|
|
78
|
-
)
|
|
79
|
-
.sort(layout.sort)
|
|
80
|
-
: root.count();
|
|
81
|
-
|
|
82
|
-
treemapLayout()
|
|
83
|
-
.tile(tileMethod)
|
|
84
|
-
// @ts-ignore
|
|
85
|
-
.size(layout.size)
|
|
86
|
-
.round(layout.round)
|
|
87
|
-
.paddingInner(layout.paddingInner)
|
|
88
|
-
.paddingOuter(layout.paddingOuter)
|
|
89
|
-
.paddingTop(layout.paddingTop)
|
|
90
|
-
.paddingRight(layout.paddingRight)
|
|
91
|
-
.paddingBottom(layout.paddingBottom)
|
|
92
|
-
.paddingLeft(layout.paddingLeft)(root);
|
|
93
|
-
|
|
94
|
-
return root
|
|
95
|
-
.descendants()
|
|
96
|
-
.map((d) =>
|
|
97
|
-
Object.assign(d, {
|
|
98
|
-
x: [d.x0, d.x1],
|
|
99
|
-
y: [d.y0, d.y1],
|
|
100
|
-
}),
|
|
101
|
-
)
|
|
102
|
-
.filter(
|
|
103
|
-
typeof layout.layer === 'function'
|
|
104
|
-
? layout.layer
|
|
105
|
-
: (d) => d.height === layout.layer,
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
10
|
// Defaults
|
|
110
11
|
const GET_DEFAULT_LAYOUT_OPTIONS = (width, height) => ({
|
|
111
12
|
tile: 'treemapSquarify',
|
|
@@ -130,7 +31,8 @@ const GET_DEFAULT_OPTIONS = (width, height) => ({
|
|
|
130
31
|
encode: {
|
|
131
32
|
x: 'x',
|
|
132
33
|
y: 'y',
|
|
133
|
-
|
|
34
|
+
key: 'id',
|
|
35
|
+
color: (d) => d.path[1],
|
|
134
36
|
},
|
|
135
37
|
scale: {
|
|
136
38
|
x: { domain: [0, width], range: [0, 1] },
|
|
@@ -139,11 +41,15 @@ const GET_DEFAULT_OPTIONS = (width, height) => ({
|
|
|
139
41
|
style: {
|
|
140
42
|
stroke: '#fff',
|
|
141
43
|
},
|
|
44
|
+
state: {
|
|
45
|
+
active: { opacity: 0.6 },
|
|
46
|
+
inactive: { opacity: 1 },
|
|
47
|
+
},
|
|
142
48
|
});
|
|
143
49
|
|
|
144
50
|
const DEFAULT_LABEL_OPTIONS = {
|
|
145
51
|
fontSize: 10,
|
|
146
|
-
text: (d) => d.
|
|
52
|
+
text: (d) => last(d.path),
|
|
147
53
|
position: 'inside',
|
|
148
54
|
fill: '#000',
|
|
149
55
|
textOverflow: 'clip',
|
|
@@ -153,12 +59,17 @@ const DEFAULT_LABEL_OPTIONS = {
|
|
|
153
59
|
};
|
|
154
60
|
|
|
155
61
|
const DEFAULT_TOOLTIP_OPTIONS = {
|
|
156
|
-
title: (d) => d.
|
|
62
|
+
title: (d) => d.path?.join?.('.'),
|
|
63
|
+
items: [{ field: 'value' }],
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const DEFAULT_TOOLTIP_OPTIONS_DRILL = {
|
|
67
|
+
title: (d) => last(d.path),
|
|
157
68
|
items: [{ field: 'value' }],
|
|
158
69
|
};
|
|
159
70
|
|
|
160
71
|
export const Treemap: CC<TreemapOptions> = (options, context) => {
|
|
161
|
-
const { width, height } = context;
|
|
72
|
+
const { width, height, options: markOptions } = context;
|
|
162
73
|
|
|
163
74
|
const {
|
|
164
75
|
data,
|
|
@@ -171,32 +82,74 @@ export const Treemap: CC<TreemapOptions> = (options, context) => {
|
|
|
171
82
|
...resOptions
|
|
172
83
|
} = options;
|
|
173
84
|
|
|
85
|
+
const treemapDrillDown = get(markOptions, [
|
|
86
|
+
'interaction',
|
|
87
|
+
'treemapDrillDown',
|
|
88
|
+
]);
|
|
89
|
+
|
|
90
|
+
// Layout
|
|
91
|
+
const layoutOptions = deepMix(
|
|
92
|
+
{},
|
|
93
|
+
GET_DEFAULT_LAYOUT_OPTIONS(width, height),
|
|
94
|
+
layout,
|
|
95
|
+
{
|
|
96
|
+
layer: treemapDrillDown
|
|
97
|
+
? (d) => {
|
|
98
|
+
return d.depth === 1;
|
|
99
|
+
}
|
|
100
|
+
: layout.layer,
|
|
101
|
+
},
|
|
102
|
+
);
|
|
103
|
+
|
|
174
104
|
// Data
|
|
175
|
-
const transformedData =
|
|
105
|
+
const [transformedData, transformedDataAll] = treeDataTransform(
|
|
176
106
|
data,
|
|
177
|
-
|
|
107
|
+
layoutOptions,
|
|
178
108
|
encode,
|
|
179
109
|
);
|
|
180
110
|
|
|
181
111
|
// Label
|
|
182
112
|
const labelStyle = subObject(style, 'label');
|
|
183
113
|
|
|
184
|
-
return deepMix(
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
114
|
+
return deepMix(
|
|
115
|
+
{},
|
|
116
|
+
GET_DEFAULT_OPTIONS(width, height),
|
|
117
|
+
{
|
|
118
|
+
data: transformedData,
|
|
119
|
+
scale,
|
|
120
|
+
style,
|
|
121
|
+
labels: [
|
|
122
|
+
{
|
|
123
|
+
...DEFAULT_LABEL_OPTIONS,
|
|
124
|
+
...labelStyle,
|
|
125
|
+
},
|
|
126
|
+
...labels,
|
|
127
|
+
],
|
|
128
|
+
...resOptions,
|
|
129
|
+
encode,
|
|
130
|
+
tooltip: maybeTooltip(tooltip, DEFAULT_TOOLTIP_OPTIONS),
|
|
131
|
+
axis: false,
|
|
132
|
+
},
|
|
133
|
+
treemapDrillDown
|
|
134
|
+
? {
|
|
135
|
+
interaction: {
|
|
136
|
+
...resOptions.interaction,
|
|
137
|
+
treemapDrillDown: treemapDrillDown
|
|
138
|
+
? {
|
|
139
|
+
...treemapDrillDown,
|
|
140
|
+
originData: transformedDataAll,
|
|
141
|
+
layout: layoutOptions,
|
|
142
|
+
}
|
|
143
|
+
: undefined,
|
|
144
|
+
},
|
|
145
|
+
encode: {
|
|
146
|
+
color: (d) => last(d.path),
|
|
147
|
+
...encode,
|
|
148
|
+
},
|
|
149
|
+
tooltip: maybeTooltip(tooltip, DEFAULT_TOOLTIP_OPTIONS_DRILL),
|
|
150
|
+
}
|
|
151
|
+
: {},
|
|
152
|
+
);
|
|
200
153
|
};
|
|
201
154
|
|
|
202
155
|
Treemap.props = {};
|
package/src/mark/utils.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Band } from '@antv/scale';
|
|
2
|
-
import { stratify, hierarchy } from 'd3-hierarchy';
|
|
3
2
|
import { Primitive } from 'd3-array';
|
|
4
3
|
import { Vector2 } from '@antv/coord';
|
|
5
4
|
import { Scale } from '../runtime/types/component';
|
|
@@ -156,19 +155,3 @@ export function initializeData(
|
|
|
156
155
|
nodes: nodes || Array.from(new Set([...LS, ...LT]), (key) => ({ key })),
|
|
157
156
|
};
|
|
158
157
|
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* @description Path need when the data is a flat json structure,
|
|
162
|
-
* and the tree object structure do not need.
|
|
163
|
-
*/
|
|
164
|
-
export function generateHierarchyRoot(
|
|
165
|
-
data: any[] | Record<string, any>,
|
|
166
|
-
path: (d: any) => any,
|
|
167
|
-
) {
|
|
168
|
-
if (Array.isArray(data)) {
|
|
169
|
-
return typeof path === 'function'
|
|
170
|
-
? stratify().path(path)(data)
|
|
171
|
-
: stratify()(data);
|
|
172
|
-
}
|
|
173
|
-
return hierarchy(data);
|
|
174
|
-
}
|