@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,258 @@
1
+ import type { DisplayObject } from '@antv/g';
2
+ import { get, deepMix, pick, keys } from '@antv/util';
3
+ import { DrillDownInteraction } from 'spec';
4
+ import { select } from '../utils/selection';
5
+ import { PLOT_CLASS_NAME } from '../runtime';
6
+ import {
7
+ CHILD_NODE_COUNT,
8
+ PARTITION_TYPE,
9
+ PARTITION_TYPE_FIELD,
10
+ } from '../mark/partition';
11
+
12
+ // Get partition element.
13
+ const getElementsPartition = (plot: DisplayObject) => {
14
+ return plot.querySelectorAll('.element').filter((item) => {
15
+ const itemStyle = (item as any).style || {};
16
+ return itemStyle[PARTITION_TYPE_FIELD] === PARTITION_TYPE;
17
+ });
18
+ };
19
+
20
+ function selectPlotArea(root: DisplayObject): DisplayObject {
21
+ return select(root).select(`.${PLOT_CLASS_NAME}`).node();
22
+ }
23
+
24
+ // Default breadCrumb config.
25
+ const DEFAULT_BREADCRUMB = {
26
+ rootText: 'root',
27
+ style: {
28
+ fill: 'rgba(0, 0, 0, 0.6)',
29
+ fontSize: 11,
30
+ },
31
+ y: 4,
32
+ active: {
33
+ fill: 'rgba(0, 0, 0, 0.4)',
34
+ },
35
+ };
36
+
37
+ /**
38
+ * DrillDown interaction for partition visualization.
39
+ * Based on the proven sunburst drilldown implementation.
40
+ */
41
+ export function DrillDown(drillDownOptions: DrillDownInteraction = {}) {
42
+ const { breadCrumb: textConfig = {} } = drillDownOptions;
43
+ const breadCrumb = deepMix({}, DEFAULT_BREADCRUMB, textConfig);
44
+
45
+ return (context: any) => {
46
+ const { update, setState, container, view, options: viewOptions } = context;
47
+
48
+ const document = container.ownerDocument;
49
+
50
+ const plotArea = selectPlotArea(container);
51
+
52
+ const partitionMark = viewOptions.marks.find(
53
+ ({ id }: any) => id === PARTITION_TYPE,
54
+ );
55
+ if (!partitionMark) return;
56
+ const { state } = partitionMark;
57
+
58
+ // Create breadCrumbTextsGroup to save textSeparator and drillTexts.
59
+ const textGroup = document.createElement('g');
60
+ textGroup.style.transform = `translate(${breadCrumb.x || 0}px, ${
61
+ breadCrumb.y || 0
62
+ }px)`;
63
+
64
+ textGroup.setAttribute('x', 'drilldown-breadcrumb');
65
+ plotArea.appendChild(textGroup);
66
+
67
+ // Modify data and scale according to the path and level of current click to achieve drilling down, drilling up and initialization effects.
68
+ const drillDownClick = async (path: string[]) => {
69
+ // Clear text.
70
+ textGroup.removeChildren();
71
+
72
+ // More path creation text.
73
+ if (path && path.length > 0) {
74
+ // Create root text.
75
+ const rootText = document.createElement('text', {
76
+ style: {
77
+ text: breadCrumb.rootText,
78
+ ...breadCrumb.style,
79
+ },
80
+ });
81
+
82
+ textGroup.appendChild(rootText);
83
+
84
+ let name = '';
85
+ const pathArray = path;
86
+ let y = breadCrumb.style.y;
87
+ let x = rootText.getBBox().width;
88
+
89
+ const maxWidth = plotArea.getBBox().width;
90
+
91
+ // Create path: 'type1 / type2 / type3' -> '/ type1 / type2 / type3'.
92
+ const drillTexts = pathArray.map((text, index) => {
93
+ const textSeparator = document.createElement('text', {
94
+ style: {
95
+ x,
96
+ text: ' / ',
97
+ ...breadCrumb.style,
98
+ y,
99
+ },
100
+ });
101
+
102
+ textGroup.appendChild(textSeparator);
103
+
104
+ x += textSeparator.getBBox().width;
105
+
106
+ name = `${name}${text} / `;
107
+
108
+ const drillText = document.createElement('text', {
109
+ name: name.replace(/\s\/\s$/, ''),
110
+ style: {
111
+ text,
112
+ x,
113
+ ...breadCrumb.style,
114
+ y,
115
+ },
116
+ });
117
+
118
+ textGroup.appendChild(drillText);
119
+
120
+ x += drillText.getBBox().width;
121
+
122
+ /**
123
+ * Page width exceeds maximum, line feed.
124
+ * | ----maxWidth---- |
125
+ * | / type1 / type2 / type3 |
126
+ * ->
127
+ * | ----maxWidth---- |
128
+ * | / type1 / type2 |
129
+ * | / type3 |
130
+ */
131
+ if (x > maxWidth) {
132
+ y = textGroup.getBBox().height;
133
+ x = 0;
134
+ textSeparator.attr({
135
+ x,
136
+ y,
137
+ });
138
+ x += textSeparator.getBBox().width;
139
+ drillText.attr({
140
+ x,
141
+ y,
142
+ });
143
+ x += drillText.getBBox().width;
144
+ }
145
+
146
+ return drillText;
147
+ });
148
+
149
+ // Add active state and drilldown interaction.
150
+ const textStack = [rootText, ...drillTexts];
151
+ textStack.forEach((item, index) => {
152
+ // Skip the last drillText
153
+ if (index === drillTexts.length) return;
154
+ const originalAttrs = { ...item.attributes };
155
+ item.attr('cursor', 'pointer');
156
+ item.addEventListener('mouseenter', () => {
157
+ item.attr(breadCrumb.active);
158
+ });
159
+ item.addEventListener('mouseleave', () => {
160
+ item.attr(originalAttrs);
161
+ });
162
+ item.addEventListener('click', () => {
163
+ drillDownClick(path.slice(0, index));
164
+ });
165
+ });
166
+ }
167
+
168
+ // Update marks.
169
+ setState('drillDown', (viewOptions: any) => {
170
+ const { marks } = viewOptions;
171
+ // Add filter transform for every mark,
172
+ // which will skip for mark without color channel.
173
+ const newMarks = marks.map((mark: any) => {
174
+ if (mark.id !== PARTITION_TYPE && mark.type !== 'rect') return mark;
175
+
176
+ // Inset after aggregate transform, such as group and bin.
177
+ const { data } = mark;
178
+
179
+ const newData = data.filter((item: any) => {
180
+ const itemPath = item.path ?? [];
181
+ if (path.length === 0) return true;
182
+ if (!Array.isArray(itemPath) || itemPath.length < path.length)
183
+ return false;
184
+ for (let i = 0; i < path.length; i++) {
185
+ if (itemPath[i] !== path[i]) return false;
186
+ }
187
+ return true;
188
+ });
189
+
190
+ // DrillDown by filtering the data and scale.
191
+ return deepMix({}, mark, {
192
+ data: newData,
193
+ });
194
+ });
195
+ return { ...viewOptions, marks: newMarks };
196
+ });
197
+
198
+ await update();
199
+ };
200
+
201
+ const createDrillClick = (e: Event) => {
202
+ const item = e.target as DisplayObject;
203
+
204
+ // Get properties directly from DOM element attributes instead of using get function.
205
+ const markType = (item as any).markType;
206
+ const itemStyle = (item as any).style || {};
207
+ const partitionType = itemStyle[PARTITION_TYPE_FIELD];
208
+ const childNodeCount = itemStyle[CHILD_NODE_COUNT];
209
+ const itemData = (item as any).__data__;
210
+
211
+ if (
212
+ markType !== 'rect' ||
213
+ partitionType !== PARTITION_TYPE ||
214
+ !childNodeCount
215
+ ) {
216
+ return;
217
+ }
218
+
219
+ const path = itemData?.data?.path ?? [];
220
+ drillDownClick(path);
221
+ };
222
+
223
+ // Add click drill interaction.
224
+ plotArea.addEventListener('click', createDrillClick);
225
+
226
+ // Change attribute keys.
227
+ const changeStyleKey = keys({ ...state.active, ...state.inactive });
228
+
229
+ const createActive = () => {
230
+ const elements = getElementsPartition(plotArea);
231
+ elements.forEach((element: DisplayObject) => {
232
+ const childNodeCount = get(element, ['style', CHILD_NODE_COUNT]);
233
+ const cursor = get(element, ['style', 'cursor']);
234
+ if (cursor !== 'pointer' && childNodeCount) {
235
+ (element as any).style.cursor = 'pointer';
236
+ const originalAttrs = pick(element.attributes, changeStyleKey);
237
+
238
+ element.addEventListener('mouseenter', () => {
239
+ element.attr(state.active);
240
+ });
241
+
242
+ element.addEventListener('mouseleave', () => {
243
+ element.attr(deepMix(originalAttrs, state.inactive));
244
+ });
245
+ }
246
+ });
247
+ };
248
+
249
+ // Animate elements update and add active state.
250
+ plotArea.addEventListener('mousemove', createActive);
251
+
252
+ return () => {
253
+ textGroup.remove();
254
+ plotArea.removeEventListener('click', createDrillClick);
255
+ plotArea.removeEventListener('mousemove', createActive);
256
+ };
257
+ };
258
+ }
@@ -23,4 +23,5 @@ export { ScrollbarFilter } from './scrollbarFilter';
23
23
  export { Poptip } from './poptip';
24
24
  export { Event } from './event';
25
25
  export { TreemapDrillDown } from './treemapDrillDown';
26
+ export { DrillDown } from './drillDown';
26
27
  export { ElementPointMove } from './elementPointMove';
@@ -1112,7 +1112,7 @@ export function seriesTooltip(
1112
1112
  const scaleX = scale.x;
1113
1113
  const x1 = scaleX.map(x);
1114
1114
  const [x2, y2] = coordinate.map([x1, 0.5]);
1115
- const rootBounds = root.getRenderBounds();
1115
+ const rootBounds = bboxOf(root);
1116
1116
  const minX = rootBounds.min[0];
1117
1117
  const minY = rootBounds.min[1];
1118
1118
  update({
@@ -830,17 +830,47 @@ export function extractChannelValues(view: G2ViewDescriptor): {
830
830
  };
831
831
  }
832
832
 
833
+ /**
834
+ * Check if there are multiple independent axis for a given channel.
835
+ * Multi-axis can be defined by:
836
+ * 1. Explicit `independent: true` in scale configuration
837
+ * 2. Different `scale.key` values for the same channel name
838
+ *
839
+ * @param channel1 - Channel name (x or y)
840
+ * @param marks - Array of marks to check
841
+ * @returns true if multiple independent axis exist
842
+ */
833
843
  export function hasIndependentXYScale(
834
844
  channel1: string,
835
845
  marks: readonly unknown[],
836
846
  ): boolean {
837
- return marks.some((mark) => {
847
+ const scaleKeys = new Set<string>();
848
+
849
+ for (const mark of marks) {
838
850
  const { scale: markScale } = mark as Record<string, unknown>;
839
851
  const channelScale = (markScale as Record<string, unknown>)?.[channel1] as
840
852
  | Record<string, unknown>
841
853
  | undefined;
842
- return !!channelScale?.independent;
843
- });
854
+
855
+ if (!channelScale) continue;
856
+
857
+ // Early return if explicit independent flag is found
858
+ if (channelScale.independent) {
859
+ return true;
860
+ }
861
+
862
+ // Collect scale keys, treating undefined/missing key as 'default'
863
+ const key = (channelScale.key as string | undefined) || 'default';
864
+ scaleKeys.add(key);
865
+
866
+ // Early return if multiple different keys are detected
867
+ if (scaleKeys.size > 1) {
868
+ return true;
869
+ }
870
+ }
871
+
872
+ // No multiple axes detected
873
+ return false;
844
874
  }
845
875
 
846
876
  /**
@@ -928,6 +958,10 @@ export interface IndependentScaleInfo {
928
958
  * This function performs a single traversal to compute all independent scale related information,
929
959
  * avoiding repeated calculations throughout the codebase.
930
960
  *
961
+ * Multi-axis detection logic:
962
+ * 1. Explicit independent: scale.y.independent = true
963
+ * 2. Different scale keys: scale.y.key = 'left' vs scale.y.key = 'right'
964
+ *
931
965
  * @param view The view object containing markState
932
966
  * @returns Complete independent scale information
933
967
  */
@@ -946,26 +980,49 @@ export function calculateAllIndependentScaleInfo(
946
980
  const markToXScaleMap = new Map<string, string>();
947
981
  const markToYScaleMap = new Map<string, string>();
948
982
 
949
- // Single traversal to compute all classifications and mappings
983
+ // Track scale key assignments
984
+ const xScaleKeyMap = new Map<string, number>(); // key -> index
985
+ const yScaleKeyMap = new Map<string, number>(); // key -> index
950
986
  let xIndex = 1;
951
987
  let yIndex = 1;
952
988
 
989
+ // Single traversal to compute all classifications and mappings
953
990
  for (const [mark] of view.markState.entries()) {
954
991
  const markKey = mark.key;
955
992
 
956
993
  // X axis processing
957
- if (mark?.scale?.x?.independent) {
994
+ const xScaleKey = (mark?.scale?.x?.key as string) || 'x';
995
+ const hasExplicitIndependentX = !!mark?.scale?.x?.independent;
996
+
997
+ if (hasExplicitIndependentX || (hasIndependentX && xScaleKey !== 'x')) {
958
998
  marksWithIndependentX.push(markKey);
959
- markToXScaleMap.set(markKey, `x${xIndex++}`);
999
+
1000
+ // Assign scale name based on key
1001
+ // Always use indexed names (x1, x2, ...) to avoid collision with shared 'x'
1002
+ if (!xScaleKeyMap.has(xScaleKey)) {
1003
+ xScaleKeyMap.set(xScaleKey, xIndex++);
1004
+ }
1005
+ const scaleIndex = xScaleKeyMap.get(xScaleKey);
1006
+ markToXScaleMap.set(markKey, `x${scaleIndex}`);
960
1007
  } else {
961
1008
  marksWithSharedX.push(markKey);
962
1009
  markToXScaleMap.set(markKey, 'x');
963
1010
  }
964
1011
 
965
1012
  // Y axis processing
966
- if (mark?.scale?.y?.independent) {
1013
+ const yScaleKey = (mark?.scale?.y?.key as string) || 'y';
1014
+ const hasExplicitIndependentY = !!mark?.scale?.y?.independent;
1015
+
1016
+ if (hasExplicitIndependentY || (hasIndependentY && yScaleKey !== 'y')) {
967
1017
  marksWithIndependentY.push(markKey);
968
- markToYScaleMap.set(markKey, `y${yIndex++}`);
1018
+
1019
+ // Assign scale name based on key
1020
+ // Always use indexed names (y1, y2, ...) to avoid collision with shared 'y'
1021
+ if (!yScaleKeyMap.has(yScaleKey)) {
1022
+ yScaleKeyMap.set(yScaleKey, yIndex++);
1023
+ }
1024
+ const scaleIndex = yScaleKeyMap.get(yScaleKey);
1025
+ markToYScaleMap.set(markKey, `y${scaleIndex}`);
969
1026
  } else {
970
1027
  marksWithSharedY.push(markKey);
971
1028
  markToYScaleMap.set(markKey, 'y');
@@ -29,6 +29,17 @@ const adjustPosition = (target: Bounds, edge: Bounds) => {
29
29
  };
30
30
 
31
31
  const union = (a: AABB, b: AABB) => {
32
+ // Handle null bounds by returning the non-null one, or a default AABB
33
+ if (!a || !a.min || !a.max) {
34
+ if (!b || !b.min || !b.max) {
35
+ return { min: [0, 0], max: [0, 0] };
36
+ }
37
+ return b;
38
+ }
39
+ if (!b || !b.min || !b.max) {
40
+ return a;
41
+ }
42
+
32
43
  return {
33
44
  min: [Math.min(a.min[0], b.min[0]), Math.min(a.min[1], b.min[1])],
34
45
  max: [Math.max(a.max[0], b.max[0]), Math.max(a.max[1], b.max[1])],
@@ -93,6 +104,17 @@ export const ExceedAdjust: LLC<ExceedAdjustOptions> = (options = {}) => {
93
104
  labels.forEach((l) => {
94
105
  show(l);
95
106
  const { max, min } = union(l.getRenderBounds(), l.getBounds());
107
+
108
+ // Skip labels with invalid bounds (filtered out data points)
109
+ // When bounds are all zeros or invalid, it means the label shouldn't be displayed
110
+ if (
111
+ !max ||
112
+ !min ||
113
+ (max[0] === 0 && max[1] === 0 && min[0] === 0 && min[1] === 0)
114
+ ) {
115
+ return;
116
+ }
117
+
96
118
  const [xMax, yMax] = max,
97
119
  [xMin, yMin] = min;
98
120
  const changeValue = adjustPosition(
package/src/lib/core.ts CHANGED
@@ -106,6 +106,7 @@ import {
106
106
  Poptip,
107
107
  ScrollbarFilter,
108
108
  TreemapDrillDown,
109
+ DrillDown,
109
110
  ElementPointMove,
110
111
  } from '../interaction';
111
112
  import {
@@ -318,6 +319,7 @@ export function corelib() {
318
319
  'interaction.scrollbarFilter': ScrollbarFilter,
319
320
  'interaction.poptip': Poptip,
320
321
  'interaction.treemapDrillDown': TreemapDrillDown,
322
+ 'interaction.drillDown': DrillDown,
321
323
  'interaction.elementPointMove': ElementPointMove,
322
324
  'composition.spaceLayer': SpaceLayer,
323
325
  'composition.spaceFlex': SpaceFlex,
package/src/lib/graph.ts CHANGED
@@ -1,4 +1,12 @@
1
- import { Sankey, Treemap, Pack, ForceGraph, Tree, Chord } from '../mark';
1
+ import {
2
+ Sankey,
3
+ Treemap,
4
+ Pack,
5
+ ForceGraph,
6
+ Tree,
7
+ Chord,
8
+ Partition,
9
+ } from '../mark';
2
10
  import { Arc, Cluster } from '../data';
3
11
 
4
12
  export function graphlib() {
@@ -11,5 +19,6 @@ export function graphlib() {
11
19
  'mark.sankey': Sankey,
12
20
  'mark.chord': Chord,
13
21
  'mark.treemap': Treemap,
22
+ 'mark.partition': Partition,
14
23
  } as const;
15
24
  }
package/src/mark/index.ts CHANGED
@@ -31,6 +31,7 @@ export { Density } from './density';
31
31
  export { Heatmap } from './heatmap';
32
32
  export { Liquid } from './liquid';
33
33
  export { Beeswarm } from './beeswarm';
34
+ export { Partition } from './partition';
34
35
 
35
36
  export type { IntervalOptions } from './interval';
36
37
  export type { RectOptions } from './rect';