@antv/g2 5.1.14 → 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.
Files changed (89) hide show
  1. package/dist/g2.lite.min.js +1 -1
  2. package/dist/g2.min.js +1 -1
  3. package/esm/api/utils.js +5 -3
  4. package/esm/api/utils.js.map +1 -1
  5. package/esm/index.d.ts +2 -0
  6. package/esm/interaction/index.d.ts +1 -0
  7. package/esm/interaction/index.js +1 -0
  8. package/esm/interaction/index.js.map +1 -1
  9. package/esm/interaction/legendFilter.d.ts +1 -0
  10. package/esm/interaction/legendFilter.js +6 -0
  11. package/esm/interaction/legendFilter.js.map +1 -1
  12. package/esm/interaction/scrollbarFilter.js +5 -1
  13. package/esm/interaction/scrollbarFilter.js.map +1 -1
  14. package/esm/interaction/sliderFilter.d.ts +1 -1
  15. package/esm/interaction/sliderFilter.js +19 -2
  16. package/esm/interaction/sliderFilter.js.map +1 -1
  17. package/esm/interaction/tooltip.js +1 -1
  18. package/esm/interaction/tooltip.js.map +1 -1
  19. package/esm/interaction/treemapDrillDown.d.ts +10 -0
  20. package/esm/interaction/treemapDrillDown.js +229 -0
  21. package/esm/interaction/treemapDrillDown.js.map +1 -0
  22. package/esm/lib/core.d.ts +2 -1
  23. package/esm/lib/core.js +2 -1
  24. package/esm/lib/core.js.map +1 -1
  25. package/esm/lib/std.d.ts +1 -0
  26. package/esm/mark/treemap.js +37 -57
  27. package/esm/mark/treemap.js.map +1 -1
  28. package/esm/mark/utils.d.ts +0 -5
  29. package/esm/mark/utils.js +0 -13
  30. package/esm/mark/utils.js.map +1 -1
  31. package/esm/runtime/plot.js +41 -28
  32. package/esm/runtime/plot.js.map +1 -1
  33. package/esm/runtime/types/common.d.ts +1 -1
  34. package/esm/shape/utils.js +2 -2
  35. package/esm/shape/utils.js.map +1 -1
  36. package/esm/utils/treeDataTransform.d.ts +21 -0
  37. package/esm/utils/treeDataTransform.js +90 -0
  38. package/esm/utils/treeDataTransform.js.map +1 -0
  39. package/lib/api/utils.js +5 -3
  40. package/lib/api/utils.js.map +1 -1
  41. package/lib/index.d.ts +2 -0
  42. package/lib/interaction/index.d.ts +1 -0
  43. package/lib/interaction/index.js +3 -1
  44. package/lib/interaction/index.js.map +1 -1
  45. package/lib/interaction/legendFilter.d.ts +1 -0
  46. package/lib/interaction/legendFilter.js +8 -1
  47. package/lib/interaction/legendFilter.js.map +1 -1
  48. package/lib/interaction/scrollbarFilter.js +5 -1
  49. package/lib/interaction/scrollbarFilter.js.map +1 -1
  50. package/lib/interaction/sliderFilter.d.ts +1 -1
  51. package/lib/interaction/sliderFilter.js +19 -2
  52. package/lib/interaction/sliderFilter.js.map +1 -1
  53. package/lib/interaction/tooltip.js +1 -1
  54. package/lib/interaction/tooltip.js.map +1 -1
  55. package/lib/interaction/treemapDrillDown.d.ts +10 -0
  56. package/lib/interaction/treemapDrillDown.js +233 -0
  57. package/lib/interaction/treemapDrillDown.js.map +1 -0
  58. package/lib/lib/core.d.ts +2 -1
  59. package/lib/lib/core.js +1 -0
  60. package/lib/lib/core.js.map +1 -1
  61. package/lib/lib/std.d.ts +1 -0
  62. package/lib/mark/treemap.js +36 -56
  63. package/lib/mark/treemap.js.map +1 -1
  64. package/lib/mark/utils.d.ts +0 -5
  65. package/lib/mark/utils.js +1 -15
  66. package/lib/mark/utils.js.map +1 -1
  67. package/lib/runtime/plot.js +40 -27
  68. package/lib/runtime/plot.js.map +1 -1
  69. package/lib/runtime/types/common.d.ts +1 -1
  70. package/lib/shape/utils.js +1 -1
  71. package/lib/shape/utils.js.map +1 -1
  72. package/lib/utils/treeDataTransform.d.ts +21 -0
  73. package/lib/utils/treeDataTransform.js +94 -0
  74. package/lib/utils/treeDataTransform.js.map +1 -0
  75. package/package.json +4 -3
  76. package/src/api/utils.ts +6 -3
  77. package/src/interaction/index.ts +1 -0
  78. package/src/interaction/legendFilter.ts +8 -0
  79. package/src/interaction/scrollbarFilter.ts +4 -0
  80. package/src/interaction/sliderFilter.ts +21 -1
  81. package/src/interaction/tooltip.ts +2 -2
  82. package/src/interaction/treemapDrillDown.ts +282 -0
  83. package/src/lib/core.ts +2 -0
  84. package/src/mark/treemap.ts +76 -123
  85. package/src/mark/utils.ts +0 -17
  86. package/src/runtime/plot.ts +68 -37
  87. package/src/runtime/types/common.ts +5 -1
  88. package/src/shape/utils.ts +3 -2
  89. package/src/utils/treeDataTransform.ts +153 -0
@@ -1,7 +1,7 @@
1
1
  import { Vector2 } from '@antv/coord';
2
2
  import { DisplayObject, IAnimation as GAnimation, Rect } from '@antv/g';
3
- import { deepMix, get, upperFirst } from '@antv/util';
4
- import { group, groups, sort } from 'd3-array';
3
+ import { deepMix, upperFirst, isArray } from '@antv/util';
4
+ import { group, groups } from 'd3-array';
5
5
  import { format } from 'd3-format';
6
6
  import { mapObject } from '../utils/array';
7
7
  import { ChartEvent } from '../utils/event';
@@ -90,6 +90,8 @@ import {
90
90
  G2ViewTree,
91
91
  } from './types/options';
92
92
 
93
+ type Store = Map<any, (options: G2ViewTree) => G2ViewTree>;
94
+
93
95
  export async function plot<T extends G2ViewTree>(
94
96
  options: T,
95
97
  selection: Selection,
@@ -242,12 +244,19 @@ export async function plot<T extends G2ViewTree>(
242
244
  // Apply interactions.
243
245
  const viewInstanceof = (
244
246
  viewContainer: Map<G2ViewDescriptor, DisplayObject>,
247
+ updateInteractions?: (
248
+ container: Map<G2ViewDescriptor, DisplayObject>,
249
+ updateTypes?: string[],
250
+ store?: Store,
251
+ ) => void,
252
+ oldStore?: Store,
245
253
  ) => {
246
254
  return Array.from(viewContainer.entries()).map(([view, container]) => {
247
255
  // Index state by component or interaction name,
248
256
  // such as legend, scrollbar, brushFilter.
249
257
  // Each state transform options to another options.
250
- const store = new Map<any, (options: G2ViewTree) => G2ViewTree>();
258
+ const store =
259
+ oldStore || new Map<any, (options: G2ViewTree) => G2ViewTree>();
251
260
  const setState = (key, reducer = (x) => x) => store.set(key, reducer);
252
261
  const options = viewNode.get(view);
253
262
  const update = createUpdateView(
@@ -261,18 +270,68 @@ export async function plot<T extends G2ViewTree>(
261
270
  container,
262
271
  options,
263
272
  setState,
264
- update: async (from) => {
273
+ update: async (from, updateTypes) => {
265
274
  // Apply all state functions to get new options.
266
275
  const reducer = compose(Array.from(store.values()));
267
276
  const newOptions = reducer(options);
268
- return await update(newOptions, from);
277
+ return await update(newOptions, from, () => {
278
+ if (isArray(updateTypes)) {
279
+ updateInteractions(viewContainer, updateTypes, store);
280
+ }
281
+ });
269
282
  },
270
283
  };
271
284
  });
272
285
  };
273
286
 
287
+ const updateInteractions = (
288
+ container = updateContainer,
289
+ updateType?: string[],
290
+ oldStore?: Map<any, (options: G2ViewTree) => G2ViewTree>,
291
+ ) => {
292
+ // Interactions for update views.
293
+ const updateViewInstances = viewInstanceof(
294
+ container,
295
+ updateInteractions,
296
+ oldStore,
297
+ );
298
+
299
+ for (const target of updateViewInstances) {
300
+ const { options, container } = target;
301
+ const nameInteraction = container['nameInteraction'];
302
+ let typeOptions = inferInteraction(options);
303
+
304
+ if (updateType) {
305
+ typeOptions = typeOptions.filter((v) => updateType.includes(v[0]));
306
+ }
307
+
308
+ for (const typeOption of typeOptions) {
309
+ const [type, option] = typeOption;
310
+ // Remove interaction for existed views.
311
+ const prevInteraction = nameInteraction.get(type);
312
+ if (prevInteraction) prevInteraction.destroy?.();
313
+
314
+ // Apply new interaction.
315
+ if (option) {
316
+ const interaction = useThemeInteraction(
317
+ target.view,
318
+ type,
319
+ option as Record<string, any>,
320
+ useInteraction,
321
+ );
322
+ const destroy = interaction(
323
+ target,
324
+ updateViewInstances,
325
+ context.emitter,
326
+ );
327
+ nameInteraction.set(type, { destroy });
328
+ }
329
+ }
330
+ }
331
+ };
332
+
274
333
  // Interactions for enter views.
275
- const enterViewInstances = viewInstanceof(enterContainer);
334
+ const enterViewInstances = viewInstanceof(enterContainer, updateInteractions);
276
335
  for (const target of enterViewInstances) {
277
336
  const { options } = target;
278
337
 
@@ -300,35 +359,7 @@ export async function plot<T extends G2ViewTree>(
300
359
  }
301
360
  }
302
361
 
303
- // Interactions for update views.
304
- const updateViewInstances = viewInstanceof(updateContainer);
305
- for (const target of updateViewInstances) {
306
- const { options, container } = target;
307
- const nameInteraction = container['nameInteraction'];
308
- for (const typeOption of inferInteraction(options)) {
309
- const [type, option] = typeOption;
310
-
311
- // Remove interaction for existed views.
312
- const prevInteraction = nameInteraction.get(type);
313
- if (prevInteraction) prevInteraction.destroy?.();
314
-
315
- // Apply new interaction.
316
- if (option) {
317
- const interaction = useThemeInteraction(
318
- target.view,
319
- type,
320
- option as Record<string, any>,
321
- useInteraction,
322
- );
323
- const destroy = interaction(
324
- target,
325
- updateViewInstances,
326
- context.emitter,
327
- );
328
- nameInteraction.set(type, { destroy });
329
- }
330
- }
331
- }
362
+ updateInteractions();
332
363
 
333
364
  // Author animations.
334
365
  const { width, height } = options;
@@ -403,7 +434,7 @@ function createUpdateView(
403
434
  .filter(filter)
404
435
  .map((d) => d[0]);
405
436
 
406
- return async (newOptions, source) => {
437
+ return async (newOptions, source, callback) => {
407
438
  const transitions = [];
408
439
  const [newView, newChildren] = await initializeView(newOptions, library);
409
440
  plotView(newView, selection, transitions, library, context);
@@ -416,7 +447,7 @@ function createUpdateView(
416
447
  for (const child of newChildren) {
417
448
  plot(child, selection, library, context);
418
449
  }
419
-
450
+ callback();
420
451
  return { options: newOptions, view: newView };
421
452
  };
422
453
  }
@@ -53,7 +53,11 @@ export type G2ViewInstance = {
53
53
  view: G2ViewDescriptor;
54
54
  container: DisplayObject;
55
55
  options: G2ViewTree;
56
- update: (options: G2ViewTree, source?: string) => Promise<any>;
56
+ update: (
57
+ options: G2ViewTree,
58
+ source?: string | string[],
59
+ callback?: any,
60
+ ) => Promise<any>;
57
61
  };
58
62
 
59
63
  export type ChannelGroups = {
@@ -1,6 +1,6 @@
1
1
  import { Coordinate } from '@antv/coord';
2
2
  import { Linear } from '@antv/scale';
3
- import { lowerFirst } from '@antv/util';
3
+ import { isNumber, lowerFirst } from '@antv/util';
4
4
  import { extent } from 'd3-array';
5
5
  import { Path as D3Path } from 'd3-path';
6
6
  import { Primitive, Vector2, Vector3 } from '../runtime';
@@ -148,7 +148,8 @@ export function computeGradient(
148
148
  range: [0, 100],
149
149
  });
150
150
 
151
- const percentage = (i) => p.map(P[i]);
151
+ const percentage = (i) =>
152
+ isNumber(P[i]) && !Number.isNaN(P[i]) ? p.map(P[i]) : 0;
152
153
 
153
154
  const gradientMode = {
154
155
  // Interpolate the colors for this segment.
@@ -0,0 +1,153 @@
1
+ import { isArray, get } from '@antv/util';
2
+ import {
3
+ stratify,
4
+ hierarchy,
5
+ treemap as treemapLayout,
6
+ treemapBinary,
7
+ treemapDice,
8
+ treemapSlice,
9
+ treemapSliceDice,
10
+ treemapSquarify,
11
+ treemapResquarify,
12
+ } from 'd3-hierarchy';
13
+ import type { Node } from 'd3-hierarchy';
14
+ import { field } from '../mark/utils';
15
+
16
+ type Layout = {
17
+ tile?:
18
+ | 'treemapBinary'
19
+ | 'treemapDice'
20
+ | 'treemapSlice'
21
+ | 'treemapSliceDice'
22
+ | 'treemapSquarify'
23
+ | 'treemapResquarify';
24
+ size?: [number, number];
25
+ round?: boolean;
26
+ // Ignore the value of the parent node when calculating the total value.
27
+ ignoreParentValue?: boolean;
28
+ ratio?: number;
29
+ padding?: number;
30
+ paddingInner?: number;
31
+ paddingOuter?: number;
32
+ paddingTop?: number;
33
+ paddingRight?: number;
34
+ paddingBottom?: number;
35
+ paddingLeft?: number;
36
+ sort?(a: any, b: any): number;
37
+ path?: (d: any) => any;
38
+ /** The granularity of Display layer. */
39
+ layer?: number | ((d: any) => any);
40
+ };
41
+
42
+ /**
43
+ * @description Path need when the data is a flat json structure,
44
+ * and the tree object structure do not need.
45
+ */
46
+ function generateHierarchyRoot(
47
+ data: any[] | Record<string, any>,
48
+ path: (d: any) => any,
49
+ ): Node {
50
+ if (Array.isArray(data)) {
51
+ return typeof path === 'function'
52
+ ? stratify().path(path)(data)
53
+ : stratify()(data);
54
+ }
55
+ return hierarchy(data);
56
+ }
57
+
58
+ function addObjectDataPath(root: Node, path = [root.data.name]) {
59
+ root.id = root.id || root.data.name;
60
+ root.path = path;
61
+
62
+ if (root.children) {
63
+ root.children.forEach((item) => {
64
+ item.id = `${root.id}/${item.data.name}`;
65
+ item.path = [...path, item.data.name];
66
+ addObjectDataPath(item, item.path);
67
+ });
68
+ }
69
+ }
70
+
71
+ function addArrayDataPath(root: Node) {
72
+ const name = get(root, ['data', 'name']);
73
+ if (name.replaceAll) {
74
+ root.path = name.replaceAll('.', '/').split('/');
75
+ }
76
+
77
+ if (root.children) {
78
+ root.children.forEach((item) => {
79
+ addArrayDataPath(item);
80
+ });
81
+ }
82
+ }
83
+
84
+ function getTileMethod(tile: string, ratio: number) {
85
+ const tiles = {
86
+ treemapBinary,
87
+ treemapDice,
88
+ treemapSlice,
89
+ treemapSliceDice,
90
+ treemapSquarify,
91
+ treemapResquarify,
92
+ };
93
+ const tileMethod =
94
+ tile === 'treemapSquarify' ? tiles[tile].ratio(ratio) : tiles[tile];
95
+ if (!tileMethod) {
96
+ throw new TypeError('Invalid tile method!');
97
+ }
98
+ return tileMethod;
99
+ }
100
+
101
+ export function treeDataTransform(
102
+ data,
103
+ layout: Layout,
104
+ encode,
105
+ ): [Node[], Node[]] {
106
+ const { value } = encode;
107
+ const tileMethod = getTileMethod(layout.tile, layout.ratio);
108
+
109
+ const root = generateHierarchyRoot(data, layout.path);
110
+
111
+ if (isArray(data)) {
112
+ addArrayDataPath(root);
113
+ } else {
114
+ addObjectDataPath(root);
115
+ }
116
+
117
+ // Calculate the value and sort.
118
+ value
119
+ ? root
120
+ .sum((d) =>
121
+ layout.ignoreParentValue && d.children ? 0 : field(value)(d),
122
+ )
123
+ .sort(layout.sort)
124
+ : root.count();
125
+
126
+ treemapLayout()
127
+ .tile(tileMethod)
128
+ // @ts-ignore
129
+ .size(layout.size)
130
+ .round(layout.round)
131
+ .paddingInner(layout.paddingInner)
132
+ .paddingOuter(layout.paddingOuter)
133
+ .paddingTop(layout.paddingTop)
134
+ .paddingRight(layout.paddingRight)
135
+ .paddingBottom(layout.paddingBottom)
136
+ .paddingLeft(layout.paddingLeft)(root);
137
+
138
+ const nodes = root.descendants().map((d) =>
139
+ Object.assign(d, {
140
+ id: d.id.replace(/^\//, ''),
141
+ x: [d.x0, d.x1],
142
+ y: [d.y0, d.y1],
143
+ }),
144
+ );
145
+
146
+ const filterData = nodes.filter(
147
+ typeof layout.layer === 'function'
148
+ ? layout.layer
149
+ : (d) => d.height === layout.layer,
150
+ );
151
+
152
+ return [filterData, nodes];
153
+ }