@antv/g2 5.4.4 → 5.4.5

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 (82) hide show
  1. package/dist/g2.lite.min.js +10 -10
  2. package/dist/g2.min.js +10 -10
  3. package/esm/api/runtime.js +4 -0
  4. package/esm/api/runtime.js.map +1 -1
  5. package/esm/index.d.ts +4 -0
  6. package/esm/interaction/drillDown.d.ts +6 -0
  7. package/esm/interaction/drillDown.js +210 -0
  8. package/esm/interaction/drillDown.js.map +1 -0
  9. package/esm/interaction/index.d.ts +1 -0
  10. package/esm/interaction/index.js +1 -0
  11. package/esm/interaction/index.js.map +1 -1
  12. package/esm/interaction/tooltip.js +1 -1
  13. package/esm/interaction/tooltip.js.map +1 -1
  14. package/esm/interaction/utils.d.ts +14 -0
  15. package/esm/interaction/utils.js +57 -9
  16. package/esm/interaction/utils.js.map +1 -1
  17. package/esm/label-transform/exceedAdjust.js +17 -0
  18. package/esm/label-transform/exceedAdjust.js.map +1 -1
  19. package/esm/lib/core.d.ts +2 -1
  20. package/esm/lib/core.js +2 -1
  21. package/esm/lib/core.js.map +1 -1
  22. package/esm/lib/graph.d.ts +1 -0
  23. package/esm/lib/graph.js +2 -1
  24. package/esm/lib/graph.js.map +1 -1
  25. package/esm/lib/std.d.ts +2 -0
  26. package/esm/mark/index.d.ts +1 -0
  27. package/esm/mark/index.js +1 -0
  28. package/esm/mark/index.js.map +1 -1
  29. package/esm/mark/partition.d.ts +42 -0
  30. package/esm/mark/partition.js +217 -0
  31. package/esm/mark/partition.js.map +1 -0
  32. package/esm/runtime/plot.js +11 -1
  33. package/esm/runtime/plot.js.map +1 -1
  34. package/esm/spec/interaction.d.ts +11 -2
  35. package/esm/spec/mark.d.ts +13 -2
  36. package/lib/api/runtime.js +4 -0
  37. package/lib/api/runtime.js.map +1 -1
  38. package/lib/index.d.ts +4 -0
  39. package/lib/interaction/drillDown.d.ts +6 -0
  40. package/lib/interaction/drillDown.js +213 -0
  41. package/lib/interaction/drillDown.js.map +1 -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/tooltip.js +1 -1
  46. package/lib/interaction/tooltip.js.map +1 -1
  47. package/lib/interaction/utils.d.ts +14 -0
  48. package/lib/interaction/utils.js +57 -9
  49. package/lib/interaction/utils.js.map +1 -1
  50. package/lib/label-transform/exceedAdjust.js +17 -0
  51. package/lib/label-transform/exceedAdjust.js.map +1 -1
  52. package/lib/lib/core.d.ts +2 -1
  53. package/lib/lib/core.js +1 -0
  54. package/lib/lib/core.js.map +1 -1
  55. package/lib/lib/graph.d.ts +1 -0
  56. package/lib/lib/graph.js +1 -0
  57. package/lib/lib/graph.js.map +1 -1
  58. package/lib/lib/std.d.ts +2 -0
  59. package/lib/mark/index.d.ts +1 -0
  60. package/lib/mark/index.js +3 -1
  61. package/lib/mark/index.js.map +1 -1
  62. package/lib/mark/partition.d.ts +42 -0
  63. package/lib/mark/partition.js +223 -0
  64. package/lib/mark/partition.js.map +1 -0
  65. package/lib/runtime/plot.js +11 -1
  66. package/lib/runtime/plot.js.map +1 -1
  67. package/lib/spec/interaction.d.ts +11 -2
  68. package/lib/spec/mark.d.ts +13 -2
  69. package/package.json +2 -2
  70. package/src/api/runtime.ts +5 -0
  71. package/src/interaction/drillDown.ts +258 -0
  72. package/src/interaction/index.ts +1 -0
  73. package/src/interaction/tooltip.ts +1 -1
  74. package/src/interaction/utils.ts +65 -8
  75. package/src/label-transform/exceedAdjust.ts +22 -0
  76. package/src/lib/core.ts +2 -0
  77. package/src/lib/graph.ts +10 -1
  78. package/src/mark/index.ts +1 -0
  79. package/src/mark/partition.ts +312 -0
  80. package/src/runtime/plot.ts +10 -1
  81. package/src/spec/interaction.ts +13 -1
  82. package/src/spec/mark.ts +18 -0
@@ -0,0 +1,312 @@
1
+ import { deepMix, pick } from '@antv/util';
2
+ import { CompositeMarkComponent } from '../runtime';
3
+ import { BaseMark, ChannelTypes, PartitionNode } from '../spec';
4
+ export type PartitionMark = BaseMark<'rect', 'value' | ChannelTypes>;
5
+
6
+ export interface PartitionDataNode {
7
+ data: PartitionNode;
8
+ depth: number;
9
+ parent: PartitionDataNode | null;
10
+ children: PartitionDataNode[];
11
+ x0: number;
12
+ x1: number;
13
+ value: number;
14
+ }
15
+
16
+ export interface LayoutOptions {
17
+ valueField?: string;
18
+ sort?: (a: PartitionNode, b: PartitionNode) => number;
19
+ fillParent?: boolean; // Whether child nodes fill parent width.
20
+ nameField?: string;
21
+ }
22
+
23
+ /**
24
+ * Partition layout algorithm.
25
+ * Child nodes start layout from the parent's starting position to show parent-child relationships.
26
+ *
27
+ * @param data Hierarchical data
28
+ * @param options Configuration options
29
+ */
30
+ export function partitionLayout(
31
+ data: PartitionNode[],
32
+ options: LayoutOptions = {},
33
+ ) {
34
+ const {
35
+ valueField = 'value',
36
+ sort,
37
+ fillParent = true,
38
+ nameField = 'name',
39
+ } = options;
40
+
41
+ if (!data || data.length === 0) return [];
42
+
43
+ // Build hierarchical structure
44
+ const buildPartition = (
45
+ node: PartitionNode,
46
+ parent: PartitionDataNode | null = null,
47
+ depth = 0,
48
+ ): PartitionDataNode => {
49
+ const partitionNode: PartitionDataNode = {
50
+ data: node,
51
+ depth,
52
+ parent,
53
+ children: [],
54
+ x0: 0,
55
+ x1: 0,
56
+ value: node[valueField] || 0,
57
+ };
58
+
59
+ if (node.children && node.children.length > 0) {
60
+ partitionNode.children = node.children.map((child: PartitionNode) =>
61
+ buildPartition(child, partitionNode, depth + 1),
62
+ );
63
+ }
64
+
65
+ return partitionNode;
66
+ };
67
+
68
+ // Process each root node
69
+ const result: Array<Record<string, any>> = [];
70
+ let currentRootStartX = 0; // Track the starting position for the next root node
71
+
72
+ data.forEach((rootData: PartitionNode) => {
73
+ const root = buildPartition(rootData);
74
+
75
+ // Calculate position for each node - key point: child nodes start layout from parent's starting position
76
+ const calculateLayout = (
77
+ node: PartitionDataNode,
78
+ parentStartX = 0,
79
+ isRootNode = false,
80
+ parentWidth = 0, // Parent node actual width.
81
+ ): void => {
82
+ if (isRootNode || node.depth === 0) {
83
+ // Root node: start from current root position
84
+ node.x0 = isRootNode ? parentStartX : 0;
85
+ node.x1 = node.x0 + node.value;
86
+ } else {
87
+ // Child node: start layout from parent's starting position
88
+ node.x0 = parentStartX;
89
+ if (fillParent && parentWidth > 0) {
90
+ // If fillParent is true, calculate width based on parent width and value ratio.
91
+ const siblingsTotalValue = node.parent
92
+ ? node.parent.children.reduce((acc, child) => acc + child.value, 0)
93
+ : node.value;
94
+ const siblingsCount = node.parent ? node.parent.children.length : 1;
95
+ const ratio =
96
+ siblingsTotalValue > 0
97
+ ? node.value / siblingsTotalValue
98
+ : 1 / siblingsCount;
99
+ node.x1 = parentStartX + parentWidth * ratio;
100
+ } else {
101
+ // If fillParent is false, use node own value as width.
102
+ node.x1 = parentStartX + node.value;
103
+ }
104
+ }
105
+
106
+ // Calculate position for child nodes - start from current node's starting position
107
+ let childStartX = node.x0;
108
+ const nodeWidth = node.x1 - node.x0;
109
+
110
+ const sortedChildren = sort
111
+ ? [...node.children].sort(
112
+ (a: PartitionDataNode, b: PartitionDataNode) =>
113
+ sort(a.data, b.data),
114
+ )
115
+ : node.children;
116
+
117
+ if (fillParent && sortedChildren.length > 0) {
118
+ // fillParent mode: child nodes fill parent width proportionally.
119
+ const childrenTotalValue = node.children.reduce(
120
+ (sum, c) => sum + c.value,
121
+ 0,
122
+ );
123
+ sortedChildren.forEach((child: PartitionDataNode) => {
124
+ calculateLayout(child, childStartX, false, nodeWidth);
125
+ const ratio =
126
+ childrenTotalValue > 0
127
+ ? child.value / childrenTotalValue
128
+ : 1 / sortedChildren.length;
129
+ childStartX += nodeWidth * ratio;
130
+ });
131
+ } else {
132
+ // Non-fillParent mode: child nodes layout independently based on own value.
133
+ sortedChildren.forEach((child: PartitionDataNode) => {
134
+ calculateLayout(child, childStartX, false, 0);
135
+ // Next child node starts from current child node's end position.
136
+ childStartX += child.x1 - child.x0;
137
+ });
138
+ }
139
+ };
140
+
141
+ // Start layout calculation from root node, using current root start position.
142
+ calculateLayout(root, currentRootStartX, true);
143
+
144
+ // Update the starting position for the next root node.
145
+ currentRootStartX += root.value;
146
+
147
+ // Convert to final format.
148
+ const processNode = (node: PartitionDataNode): Record<string, any> => {
149
+ const getName = (d: PartitionNode) => d[nameField] ?? d.name;
150
+ const path = [getName(node.data)];
151
+ let ancestorNode = node;
152
+ while (ancestorNode.parent) {
153
+ path.unshift(getName(ancestorNode.parent.data));
154
+ ancestorNode = ancestorNode.parent;
155
+ }
156
+
157
+ return {
158
+ ...pick(node.data, [valueField]),
159
+ [PARTITION_PATH_FIELD]: path,
160
+ [PARTITION_ANCESTOR_FIELD]:
161
+ ancestorNode.parent?.data?.[nameField] ?? node.data[nameField],
162
+ name: node.data[nameField],
163
+ depth: node.depth,
164
+ value: node.value,
165
+ x: [node.x0, node.x1],
166
+ y: [node.depth, node.depth + 1],
167
+ // Add child node count attribute for drill-down interaction judgment.
168
+ [CHILD_NODE_COUNT]: node.children.length,
169
+ };
170
+ };
171
+
172
+ // Collect all nodes.
173
+ const collectResultNodes = (node: PartitionDataNode): void => {
174
+ result.push(processNode(node));
175
+ node.children.forEach(collectResultNodes);
176
+ };
177
+
178
+ collectResultNodes(root);
179
+ });
180
+
181
+ return result;
182
+ }
183
+
184
+ export type PartitionData = PartitionNode[];
185
+
186
+ export type PartitionOptions = Omit<PartitionMark, 'type'> & {
187
+ fillParent?: boolean; // Whether child nodes fill parent width.
188
+ };
189
+
190
+ export const PARTITION_TYPE = 'partition';
191
+ export const PARTITION_TYPE_FIELD = 'markType';
192
+ export const PARTITION_PATH_FIELD = 'path';
193
+ export const PARTITION_ANCESTOR_FIELD = 'ancestor-node';
194
+ export const CHILD_NODE_COUNT = 'childNodeCount';
195
+
196
+ export function transformData(
197
+ options: Pick<PartitionOptions, 'data' | 'encode'> & {
198
+ fillParent?: boolean;
199
+ sort?: (a: PartitionNode, b: PartitionNode) => number;
200
+ },
201
+ ) {
202
+ const { data, encode, fillParent, sort } = options;
203
+ const { color, value, name } = encode as any;
204
+
205
+ const nodes = partitionLayout(data, {
206
+ valueField: value,
207
+ fillParent,
208
+ nameField: name,
209
+ sort,
210
+ });
211
+
212
+ return nodes.map((node: Record<string, any>) => {
213
+ // Handle color mapping.
214
+ const nodeInfo = { ...node };
215
+ if (color && color !== PARTITION_ANCESTOR_FIELD) {
216
+ nodeInfo[color] = node[color];
217
+ }
218
+ return nodeInfo;
219
+ });
220
+ }
221
+
222
+ const DEFAULT_OPTIONS = {
223
+ id: PARTITION_TYPE,
224
+ encode: {
225
+ x: 'x',
226
+ y: 'y',
227
+ key: PARTITION_PATH_FIELD,
228
+ color: PARTITION_ANCESTOR_FIELD,
229
+ value: 'value',
230
+ name: 'name',
231
+ },
232
+ labels: [
233
+ {
234
+ style: {
235
+ pointerEvents: 'none',
236
+ },
237
+ text: 'value',
238
+ position: 'inside',
239
+ transform: [
240
+ {
241
+ type: 'overflowHide',
242
+ },
243
+ ],
244
+ },
245
+ ],
246
+ axis: {
247
+ x: { title: 'Time/Order', label: true },
248
+ y: false,
249
+ },
250
+ style: {
251
+ [PARTITION_TYPE_FIELD]: PARTITION_TYPE,
252
+ [CHILD_NODE_COUNT]: 'childNodeCount', // Add child node count attribute for drill-down interaction.
253
+ },
254
+ state: {
255
+ active: { zIndex: 2 },
256
+ inactive: { zIndex: 1 },
257
+ },
258
+ legend: false,
259
+ coordinate: {
260
+ type: 'cartesian',
261
+ grid: false, // Remove grid lines.
262
+ },
263
+ interaction: {
264
+ drillDown: true,
265
+ },
266
+ };
267
+
268
+ export const Partition: CompositeMarkComponent<PartitionOptions> = (
269
+ options,
270
+ ) => {
271
+ const {
272
+ encode: encodeOption,
273
+ data = [],
274
+ layout = {},
275
+ ...resOptions
276
+ } = options;
277
+
278
+ const { fillParent = true, sort } = layout as {
279
+ fillParent?: boolean;
280
+ sort?: (a: PartitionNode, b: PartitionNode) => number;
281
+ };
282
+
283
+ const encode = { ...DEFAULT_OPTIONS.encode, ...encodeOption };
284
+ const { value } = encode;
285
+ const rectData = transformData({ encode, data, fillParent, sort });
286
+
287
+ return [
288
+ deepMix({}, DEFAULT_OPTIONS, {
289
+ type: 'rect',
290
+ data: rectData,
291
+ encode,
292
+ tooltip: {
293
+ title: 'path',
294
+ items: [
295
+ (d: Record<string, any>) => {
296
+ return {
297
+ name: value as string,
298
+ value: d[value],
299
+ };
300
+ },
301
+ ],
302
+ },
303
+ // Add basic interaction.
304
+ interaction: {
305
+ elementHighlight: true,
306
+ },
307
+ ...resOptions,
308
+ }),
309
+ ];
310
+ };
311
+
312
+ Partition.props = {};
@@ -1209,7 +1209,16 @@ function plotLabel(
1209
1209
  .selectAll(className(ELEMENT_CLASS_NAME))
1210
1210
  .nodes()
1211
1211
  // Only select the valid element.
1212
- .filter((n) => !n.__removed__);
1212
+ .filter((n) => {
1213
+ if (n.__removed__) return false;
1214
+ // Filter out elements that are hidden (e.g., by slider filtering)
1215
+ // Check if element or its children have visibility: hidden
1216
+ const isHidden =
1217
+ n.style?.visibility === 'hidden' ||
1218
+ (n.children &&
1219
+ n.children.some((child) => child.style?.visibility === 'hidden'));
1220
+ return !isHidden;
1221
+ });
1213
1222
  return labelOptions.flatMap((labelOption, i) => {
1214
1223
  const { transform = [], ...options } = labelOption;
1215
1224
  return elements.flatMap((e) => {
@@ -1,4 +1,5 @@
1
1
  import type { TooltipStyleProps } from '@antv/component';
2
+ import { TextStyleProps } from '@antv/g';
2
3
  import { BBox, InteractionComponent } from '../runtime';
3
4
  import { FisheyeCoordinate } from './coordinateTransform';
4
5
  import { TooltipItemValue } from './component';
@@ -25,7 +26,8 @@ export type Interaction =
25
26
  | BrushYFilterInteraction
26
27
  | BrushXFilterInteraction
27
28
  | SliderFilterInteraction
28
- | PoptipInteraction;
29
+ | PoptipInteraction
30
+ | DrillDownInteraction;
29
31
 
30
32
  export type InteractionTypes =
31
33
  | 'elementHighlight'
@@ -49,6 +51,7 @@ export type InteractionTypes =
49
51
  | 'brushYFilter'
50
52
  | 'sliderFilter'
51
53
  | 'poptip'
54
+ | 'drillDown'
52
55
  | InteractionComponent;
53
56
 
54
57
  export type BrushHighlightInteraction = {
@@ -231,3 +234,12 @@ export type CustomInteraction = {
231
234
  type?: InteractionComponent;
232
235
  [key: string]: any;
233
236
  };
237
+
238
+ export type DrillDownInteraction = {
239
+ type?: 'drillDown';
240
+ breadCrumb?: {
241
+ rootText?: string;
242
+ style?: TextStyleProps;
243
+ active?: TextStyleProps;
244
+ };
245
+ } & Record<string, any>;
package/src/spec/mark.ts CHANGED
@@ -48,6 +48,7 @@ export type Mark =
48
48
  | TreeMark
49
49
  | WordCloudMark
50
50
  | DensityMark
51
+ | PartitionMark
51
52
  | CustomMark
52
53
  | CompositeMark;
53
54
 
@@ -87,6 +88,7 @@ export type MarkTypes =
87
88
  | 'density'
88
89
  | 'heatmap'
89
90
  | 'liquid'
91
+ | 'partition'
90
92
  | MarkComponent
91
93
  | CompositeMarkType;
92
94
 
@@ -422,4 +424,20 @@ export type DensityMark = BaseMark<'density', ChannelTypes | 'series'>;
422
424
  export type HeatmapMark = BaseMark<'heatmap'>;
423
425
  export type LiquidMark = BaseMark<'liquid'>;
424
426
 
427
+ export type PartitionMark = BaseMark<
428
+ 'partition',
429
+ 'value' | 'name' | ChannelTypes
430
+ > & {
431
+ layout?: {
432
+ fillParent?: boolean; // If true, child nodes will fill the parent width proportionally.
433
+ sort?: (a: PartitionNode, b: PartitionNode) => number;
434
+ };
435
+ };
436
+
437
+ export interface PartitionNode {
438
+ name: string;
439
+ value: number;
440
+ children?: PartitionNode[];
441
+ }
442
+
425
443
  export type CustomMark = BaseMark<MarkComponent, ChannelTypes>;