@antv/g2 5.3.5 → 5.4.0

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 (154) hide show
  1. package/dist/g2.lite.min.js +24 -24
  2. package/dist/g2.min.js +24 -24
  3. package/esm/animation/morphing.js +11 -6
  4. package/esm/animation/morphing.js.map +1 -1
  5. package/esm/component/axis.d.ts +1 -0
  6. package/esm/component/axis.js +5 -5
  7. package/esm/component/axis.js.map +1 -1
  8. package/esm/composition/geoView.js +6 -1
  9. package/esm/composition/geoView.js.map +1 -1
  10. package/esm/interaction/legendFilter.d.ts +2 -0
  11. package/esm/interaction/legendFilter.js +56 -0
  12. package/esm/interaction/legendFilter.js.map +1 -1
  13. package/esm/interaction/scrollbarFilter.js +4 -1
  14. package/esm/interaction/scrollbarFilter.js.map +1 -1
  15. package/esm/interaction/sliderFilter.js +9 -19
  16. package/esm/interaction/sliderFilter.js.map +1 -1
  17. package/esm/interaction/tooltip.js +58 -11
  18. package/esm/interaction/tooltip.js.map +1 -1
  19. package/esm/label-transform/exceedAdjust.js +7 -1
  20. package/esm/label-transform/exceedAdjust.js.map +1 -1
  21. package/esm/lib/builtinlib.d.ts +4 -0
  22. package/esm/lib/builtinlib.js +2 -1
  23. package/esm/lib/builtinlib.js.map +1 -1
  24. package/esm/mark/interval.js +3 -1
  25. package/esm/mark/interval.js.map +1 -1
  26. package/esm/mark/sankey.js +29 -0
  27. package/esm/mark/sankey.js.map +1 -1
  28. package/esm/mark/utils.js +31 -1
  29. package/esm/mark/utils.js.map +1 -1
  30. package/esm/runtime/component.js +17 -6
  31. package/esm/runtime/component.js.map +1 -1
  32. package/esm/runtime/constant.d.ts +2 -0
  33. package/esm/runtime/constant.js +2 -0
  34. package/esm/runtime/constant.js.map +1 -1
  35. package/esm/runtime/mark.js +2 -1
  36. package/esm/runtime/mark.js.map +1 -1
  37. package/esm/runtime/option-preprocess/axis-breaks.d.ts +6 -0
  38. package/esm/runtime/option-preprocess/axis-breaks.js +25 -0
  39. package/esm/runtime/option-preprocess/axis-breaks.js.map +1 -0
  40. package/esm/runtime/option-preprocess/index.js +2 -1
  41. package/esm/runtime/option-preprocess/index.js.map +1 -1
  42. package/esm/runtime/plot.js +59 -8
  43. package/esm/runtime/plot.js.map +1 -1
  44. package/esm/runtime/render.js +1 -0
  45. package/esm/runtime/render.js.map +1 -1
  46. package/esm/runtime/transform.d.ts +1 -0
  47. package/esm/runtime/transform.js +9 -0
  48. package/esm/runtime/transform.js.map +1 -1
  49. package/esm/runtime/types/options.d.ts +1 -0
  50. package/esm/shape/axis-breaks/axis-breaks.d.ts +19 -0
  51. package/esm/shape/axis-breaks/axis-breaks.js +153 -0
  52. package/esm/shape/axis-breaks/axis-breaks.js.map +1 -0
  53. package/esm/shape/index.d.ts +2 -0
  54. package/esm/shape/index.js +1 -0
  55. package/esm/shape/index.js.map +1 -1
  56. package/esm/spec/labelTransform.d.ts +1 -1
  57. package/esm/theme/create.d.ts +1 -0
  58. package/esm/theme/create.js +2 -1
  59. package/esm/theme/create.js.map +1 -1
  60. package/esm/utils/scale.d.ts +1 -0
  61. package/esm/utils/scale.js +20 -0
  62. package/esm/utils/scale.js.map +1 -1
  63. package/esm/utils/selection.js +7 -2
  64. package/esm/utils/selection.js.map +1 -1
  65. package/lib/animation/morphing.js +10 -5
  66. package/lib/animation/morphing.js.map +1 -1
  67. package/lib/component/axis.d.ts +1 -0
  68. package/lib/component/axis.js +5 -5
  69. package/lib/component/axis.js.map +1 -1
  70. package/lib/composition/geoView.js +6 -1
  71. package/lib/composition/geoView.js.map +1 -1
  72. package/lib/interaction/legendFilter.d.ts +2 -0
  73. package/lib/interaction/legendFilter.js +58 -1
  74. package/lib/interaction/legendFilter.js.map +1 -1
  75. package/lib/interaction/scrollbarFilter.js +4 -1
  76. package/lib/interaction/scrollbarFilter.js.map +1 -1
  77. package/lib/interaction/sliderFilter.js +7 -17
  78. package/lib/interaction/sliderFilter.js.map +1 -1
  79. package/lib/interaction/tooltip.js +57 -10
  80. package/lib/interaction/tooltip.js.map +1 -1
  81. package/lib/label-transform/exceedAdjust.js +7 -1
  82. package/lib/label-transform/exceedAdjust.js.map +1 -1
  83. package/lib/lib/builtinlib.d.ts +4 -0
  84. package/lib/lib/builtinlib.js +1 -0
  85. package/lib/lib/builtinlib.js.map +1 -1
  86. package/lib/mark/interval.js +3 -1
  87. package/lib/mark/interval.js.map +1 -1
  88. package/lib/mark/sankey.js +29 -0
  89. package/lib/mark/sankey.js.map +1 -1
  90. package/lib/mark/utils.js +31 -1
  91. package/lib/mark/utils.js.map +1 -1
  92. package/lib/runtime/component.js +17 -6
  93. package/lib/runtime/component.js.map +1 -1
  94. package/lib/runtime/constant.d.ts +2 -0
  95. package/lib/runtime/constant.js +3 -1
  96. package/lib/runtime/constant.js.map +1 -1
  97. package/lib/runtime/mark.js +1 -0
  98. package/lib/runtime/mark.js.map +1 -1
  99. package/lib/runtime/option-preprocess/axis-breaks.d.ts +6 -0
  100. package/lib/runtime/option-preprocess/axis-breaks.js +30 -0
  101. package/lib/runtime/option-preprocess/axis-breaks.js.map +1 -0
  102. package/lib/runtime/option-preprocess/index.js +2 -1
  103. package/lib/runtime/option-preprocess/index.js.map +1 -1
  104. package/lib/runtime/plot.js +57 -6
  105. package/lib/runtime/plot.js.map +1 -1
  106. package/lib/runtime/render.js +1 -0
  107. package/lib/runtime/render.js.map +1 -1
  108. package/lib/runtime/transform.d.ts +1 -0
  109. package/lib/runtime/transform.js +11 -1
  110. package/lib/runtime/transform.js.map +1 -1
  111. package/lib/runtime/types/options.d.ts +1 -0
  112. package/lib/shape/axis-breaks/axis-breaks.d.ts +19 -0
  113. package/lib/shape/axis-breaks/axis-breaks.js +157 -0
  114. package/lib/shape/axis-breaks/axis-breaks.js.map +1 -0
  115. package/lib/shape/index.d.ts +2 -0
  116. package/lib/shape/index.js +3 -1
  117. package/lib/shape/index.js.map +1 -1
  118. package/lib/spec/labelTransform.d.ts +1 -1
  119. package/lib/theme/create.d.ts +1 -0
  120. package/lib/theme/create.js +2 -1
  121. package/lib/theme/create.js.map +1 -1
  122. package/lib/utils/scale.d.ts +1 -0
  123. package/lib/utils/scale.js +22 -1
  124. package/lib/utils/scale.js.map +1 -1
  125. package/lib/utils/selection.js +7 -2
  126. package/lib/utils/selection.js.map +1 -1
  127. package/package.json +5 -5
  128. package/src/animation/morphing.ts +33 -10
  129. package/src/component/axis.ts +5 -0
  130. package/src/composition/geoView.ts +6 -1
  131. package/src/interaction/legendFilter.ts +66 -0
  132. package/src/interaction/scrollbarFilter.ts +5 -1
  133. package/src/interaction/sliderFilter.ts +8 -24
  134. package/src/interaction/tooltip.ts +75 -20
  135. package/src/label-transform/exceedAdjust.ts +9 -2
  136. package/src/lib/builtinlib.ts +2 -1
  137. package/src/mark/interval.ts +3 -1
  138. package/src/mark/sankey.ts +30 -0
  139. package/src/mark/utils.ts +38 -1
  140. package/src/runtime/component.ts +20 -3
  141. package/src/runtime/constant.ts +2 -0
  142. package/src/runtime/mark.ts +2 -0
  143. package/src/runtime/option-preprocess/axis-breaks.ts +36 -0
  144. package/src/runtime/option-preprocess/index.ts +2 -1
  145. package/src/runtime/plot.ts +82 -7
  146. package/src/runtime/render.ts +1 -0
  147. package/src/runtime/transform.ts +17 -0
  148. package/src/runtime/types/options.ts +1 -0
  149. package/src/shape/axis-breaks/axis-breaks.ts +203 -0
  150. package/src/shape/index.ts +2 -0
  151. package/src/spec/labelTransform.ts +1 -1
  152. package/src/theme/create.ts +2 -1
  153. package/src/utils/scale.ts +24 -0
  154. package/src/utils/selection.ts +7 -2
@@ -1,6 +1,6 @@
1
1
  import { Circle, DisplayObject, IElement, Line } from '@antv/g';
2
2
  import { sort, group, mean, bisector, minIndex } from '@antv/vendor/d3-array';
3
- import { deepMix, lowerFirst, set, throttle, last } from '@antv/util';
3
+ import { deepMix, lowerFirst, set, throttle, last, isNumber } from '@antv/util';
4
4
  import { Tooltip as TooltipComponent } from '@antv/component';
5
5
  import {
6
6
  defined,
@@ -198,9 +198,29 @@ function showUndefined(item) {
198
198
  }
199
199
 
200
200
  function heatmapItem(element) {
201
- const datum = element.__data__;
202
- const normalizedX = datum?.normalized?.x ?? 0;
201
+ const { __data__: datum } = element;
202
+ const { title, items = [] } = datum;
203
203
 
204
+ // Check if there are user-defined items using the existing CALLBACK_ITEM_SYMBOL
205
+ const hasCustomItems = items.some((item) => CALLBACK_ITEM_SYMBOL in item);
206
+
207
+ if (hasCustomItems) {
208
+ // User has defined custom items, pass them through as is
209
+ const newItems = items
210
+ .filter(defined)
211
+ .map(({ color = itemColorOf(element), ...item }) => ({
212
+ ...item,
213
+ color,
214
+ }))
215
+ .map(showUndefined);
216
+ return {
217
+ ...(title && { title }),
218
+ items: newItems,
219
+ };
220
+ }
221
+
222
+ // No user-defined items, use special logic for heatmap
223
+ const normalizedX = datum?.normalized?.x ?? 0;
204
224
  const originalDatum = element.parentNode?.__data__;
205
225
  const encode = originalDatum?.encode ?? {};
206
226
  const { x = {}, y = {}, color = {} } = encode;
@@ -571,6 +591,35 @@ function normalizedPosition(coordinate, position) {
571
591
  };
572
592
  }
573
593
 
594
+ /**
595
+ * Determine whether the band widths occupied by different categories are the same.
596
+ */
597
+ function equalBandWidth(scale) {
598
+ const { x } = scale;
599
+ if (!x || !x.valueBandWidth) return true;
600
+ const { valueBandWidth } = x;
601
+ if (isNumber(valueBandWidth)) return true;
602
+ return new Set(valueBandWidth.values()).size === 1;
603
+ }
604
+
605
+ /**
606
+ * Get the index of the element closest to the abstractX
607
+ */
608
+ function findNearestElementIndex(scale, abstractX): number {
609
+ const { adjustedRange, valueBandWidth, valueStep } = scale;
610
+ const values: number[] = Array.from(valueBandWidth.values());
611
+ const steps: number[] = Array.from(valueStep.values());
612
+ const ranges = adjustedRange.map((v, i) => {
613
+ const halfStep = (steps[i] - values[i]) / 2;
614
+ return [v - halfStep, v + values[i] + halfStep];
615
+ });
616
+ const index = ranges.findIndex(
617
+ ([start, end]) => start <= abstractX && abstractX <= end,
618
+ );
619
+ if (index !== -1) return index;
620
+ return abstractX > 0.5 ? adjustedRange.length - 1 : 0;
621
+ }
622
+
574
623
  /**
575
624
  * Finds a single element based on the mouse event in a non-series context (e.g., single item tooltip).
576
625
  * @param root - The root display object of the chart.
@@ -596,18 +645,20 @@ export function findSingleElement({
596
645
  const inInterval = (d) => d.markType === 'interval';
597
646
  const isBar = elements.every(inInterval) && !isPolar(coordinate);
598
647
  const scaleX = scale.x;
648
+ const isEqualWidth = equalBandWidth(scale);
599
649
  const scaleSeries = scale.series;
600
650
  const bandWidth = scaleX?.getBandWidth?.() ?? 0;
601
- const xof = scaleSeries
602
- ? (d) => {
603
- const seriesCount = Math.round(1 / scaleSeries.valueBandWidth);
604
- return (
605
- d.__data__.x +
606
- d.__data__.series * bandWidth +
607
- bandWidth / (seriesCount * 2)
608
- );
609
- }
610
- : (d) => d.__data__.x + bandWidth / 2;
651
+ const xof =
652
+ scaleSeries && scaleSeries.valueBandWidth
653
+ ? (d) => {
654
+ const seriesCount = Math.round(1 / scaleSeries.valueBandWidth);
655
+ return (
656
+ d.__data__.x +
657
+ d.__data__.series * bandWidth +
658
+ bandWidth / (seriesCount * 2)
659
+ );
660
+ }
661
+ : (d) => d.__data__.x + bandWidth / 2;
611
662
 
612
663
  // Sort for bisector search.
613
664
  if (isBar) elements.sort((a, b) => xof(a) - xof(b));
@@ -635,7 +686,9 @@ export function findSingleElement({
635
686
  if (!mouse) return;
636
687
  const [abstractX] = coordinate.invert(mouse);
637
688
  const search = bisector(xof).center;
638
- const i = search(elements, abstractX);
689
+ const i = isEqualWidth
690
+ ? search(elements, abstractX)
691
+ : findNearestElementIndex(scaleX, abstractX);
639
692
  const target = elements[i];
640
693
 
641
694
  if (!shared) {
@@ -643,7 +696,7 @@ export function findSingleElement({
643
696
  const isGrouped = elements.find(
644
697
  (d) => d !== target && xof(d) === xof(target),
645
698
  );
646
- if (isGrouped) return findElementByTarget(event);
699
+ if (isGrouped) return findElementByTarget(event) || isGrouped;
647
700
  }
648
701
  return target;
649
702
  }
@@ -1068,6 +1121,11 @@ export function seriesTooltip(
1068
1121
  destroy();
1069
1122
  };
1070
1123
 
1124
+ const pointerleave = (e) => {
1125
+ if (mousePosition(root, e)) return;
1126
+ hide(e);
1127
+ };
1128
+
1071
1129
  const onTooltipEnable = () => {
1072
1130
  addEventListeners();
1073
1131
  };
@@ -1078,10 +1136,7 @@ export function seriesTooltip(
1078
1136
  root.addEventListener('pointerenter', update);
1079
1137
  root.addEventListener('pointermove', update);
1080
1138
  // Only emit pointerleave event when the pointer is not in the root area.
1081
- root.addEventListener('pointerleave', (e) => {
1082
- if (mousePosition(root, e)) return;
1083
- hide(e);
1084
- });
1139
+ root.addEventListener('pointerleave', pointerleave);
1085
1140
  root.addEventListener('pointerup', hide);
1086
1141
  }
1087
1142
  };
@@ -1091,7 +1146,7 @@ export function seriesTooltip(
1091
1146
  root.removeEventListener('pointerdown', update);
1092
1147
  root.removeEventListener('pointerenter', update);
1093
1148
  root.removeEventListener('pointermove', update);
1094
- root.removeEventListener('pointerleave', hide);
1149
+ root.removeEventListener('pointerleave', pointerleave);
1095
1150
  root.removeEventListener('pointerup', hide);
1096
1151
  }
1097
1152
  };
@@ -1,4 +1,4 @@
1
- import { DisplayObject } from '@antv/g';
1
+ import { AABB, DisplayObject } from '@antv/g';
2
2
  import { LabelTransformComponent as LLC } from '../runtime';
3
3
  import { ExceedAdjustLabel } from '../spec';
4
4
  import { Bounds } from '../utils/bounds';
@@ -28,6 +28,13 @@ const adjustPosition = (target: Bounds, edge: Bounds) => {
28
28
  return [changeX, changeY];
29
29
  };
30
30
 
31
+ const union = (a: AABB, b: AABB) => {
32
+ return {
33
+ min: [Math.min(a.min[0], b.min[0]), Math.min(a.min[1], b.min[1])],
34
+ max: [Math.max(a.max[0], b.max[0]), Math.max(a.max[1], b.max[1])],
35
+ };
36
+ };
37
+
31
38
  export type ExceedAdjustOptions = Omit<ExceedAdjustLabel, 'type'> & {
32
39
  /** X-axis offset default is 0 */
33
40
  offsetX?: number;
@@ -85,7 +92,7 @@ export const ExceedAdjust: LLC<ExceedAdjustOptions> = (options = {}) => {
85
92
 
86
93
  labels.forEach((l) => {
87
94
  show(l);
88
- const { max, min } = l.getRenderBounds();
95
+ const { max, min } = union(l.getRenderBounds(), l.getBounds());
89
96
  const [xMax, yMax] = max,
90
97
  [xMin, yMin] = min;
91
98
  const changeValue = adjustPosition(
@@ -7,7 +7,7 @@ import {
7
7
  LegendContinuousSize,
8
8
  } from '../component';
9
9
  import { Mark, View } from '../composition';
10
- import { LabelShape } from '../shape';
10
+ import { LabelShape, BreakShape } from '../shape';
11
11
  import { Event } from '../interaction/event';
12
12
 
13
13
  // Some private visual components, they are not public to
@@ -24,5 +24,6 @@ export function builtinlib() {
24
24
  'composition.mark': Mark,
25
25
  'composition.view': View,
26
26
  'shape.label.label': LabelShape,
27
+ 'shape.break': BreakShape,
27
28
  } as const;
28
29
  }
@@ -38,7 +38,9 @@ export type IntervalOptions = Omit<IntervalMark, 'type'>;
38
38
  */
39
39
  export const Interval: MC<IntervalOptions> = () => {
40
40
  return (index, scale, value, coordinate) => {
41
- const { x: X, y: Y, y1: Y1, series: S, size: SZ } = value;
41
+ const { x: X, y1: Y1, series: S, size: SZ } = value;
42
+ let { y: Y } = value;
43
+ Y = Y.map((d) => (d !== undefined ? d : 1));
42
44
 
43
45
  // Calc width for each interval.
44
46
  // The scales for x and series channels must be band scale.
@@ -75,6 +75,36 @@ export const Sankey: CC<SankeyOptions> = (options) => {
75
75
  // Initialize data, generating nodes by link if is not specified.
76
76
  const { links, nodes } = initializeData(data, encode);
77
77
 
78
+ // Early check for empty data - return empty marks instead of processing
79
+ if (!links || links.length === 0) {
80
+ return [
81
+ // Empty node mark
82
+ deepMix({}, DEFAULT_NODE_OPTIONS, {
83
+ data: [],
84
+ encode: {},
85
+ scale,
86
+ style: subObject(style, 'node'),
87
+ labels: [],
88
+ tooltip: false,
89
+ animate: false,
90
+ axis: false,
91
+ interaction,
92
+ state: {},
93
+ }),
94
+ // Empty link mark
95
+ deepMix({}, DEFAULT_LINK_OPTIONS, {
96
+ data: [],
97
+ encode: {},
98
+ labels: [],
99
+ style: subObject(style, 'link'),
100
+ tooltip: false,
101
+ animate: false,
102
+ interaction,
103
+ state: {},
104
+ }),
105
+ ];
106
+ }
107
+
78
108
  // Extract encode for node and link.
79
109
  const nodeEncode = subObject(encode, 'node');
80
110
  const linkEncode = subObject(encode, 'link');
package/src/mark/utils.ts CHANGED
@@ -130,6 +130,31 @@ export function valueof(data: Record<string, any>[], encode: Encode): any[] {
130
130
  return Array.from(data, field(encode));
131
131
  }
132
132
 
133
+ /**
134
+ * Normalizes data input for graph charts (sankey, chord, forceGraph) to ensure consistent format
135
+ * Supports both array input (like other charts) and object input (original format)
136
+ *
137
+ * @param data - Input data, can be array or object with links/nodes properties
138
+ * @returns Normalized data object with links and optional nodes properties
139
+ */
140
+ function normalizeGraphData(data: any): { links: any[]; nodes?: any[] } {
141
+ // Handle array input - convert to { links: data } format
142
+ if (Array.isArray(data)) {
143
+ return { links: data };
144
+ }
145
+
146
+ // Handle object input - return as is (original format)
147
+ if (data && typeof data === 'object') {
148
+ return {
149
+ links: data.links || [],
150
+ nodes: data.nodes,
151
+ };
152
+ }
153
+
154
+ // Handle null/undefined and unexpected input types
155
+ return { links: [] };
156
+ }
157
+
133
158
  export function initializeData(
134
159
  data: { nodes?: any[]; links: any[] },
135
160
  encode: Record<string, Encode>,
@@ -137,15 +162,27 @@ export function initializeData(
137
162
  links: { target: string; source: string; value: any }[];
138
163
  nodes: { key: string }[];
139
164
  } {
165
+ const normalizedData = normalizeGraphData(data);
140
166
  const {
141
167
  source = (d) => d.source,
142
168
  target = (d) => d.target,
143
169
  value = (d) => d.value,
144
170
  } = encode;
145
- const { links, nodes } = data;
171
+
172
+ const { links, nodes } = normalizedData;
173
+
174
+ // Early return for empty links
175
+ if (!links.length) {
176
+ return {
177
+ links: [],
178
+ nodes: nodes || [],
179
+ };
180
+ }
181
+
146
182
  const LS = valueof(links, source);
147
183
  const LT = valueof(links, target);
148
184
  const LV = valueof(links, value);
185
+
149
186
  return {
150
187
  links: links.map((_, i) => ({
151
188
  target: LT[i],
@@ -724,7 +724,18 @@ function computeAxisSize(
724
724
  // Compute Labels.
725
725
  const scale = createScale(component, library);
726
726
  const labelBBoxes = computeLabelsBBox(rest, scale);
727
- const paddingTick = tickLength + labelSpacing;
727
+
728
+ // Compute dynamic tickLength if it's a function
729
+ let maxTickLength = tickLength;
730
+ if (typeof component.tickLength === 'function') {
731
+ const ticks = scale.getTicks?.() || scale.getOptions().domain;
732
+ const tickLengths = ticks.map((d, i, array) =>
733
+ component.tickLength(d, i, array),
734
+ );
735
+ maxTickLength = Math.max(...tickLengths, 0);
736
+ }
737
+
738
+ const paddingTick = maxTickLength + labelSpacing;
728
739
  if (labelBBoxes && labelBBoxes.length) {
729
740
  const maxLabelWidth = max(labelBBoxes, (d) => d.width);
730
741
  const maxLabelHeight = max(labelBBoxes, (d) => d.height);
@@ -748,7 +759,7 @@ function computeAxisSize(
748
759
  }
749
760
  }
750
761
  } else {
751
- component.size = tickLength;
762
+ component.size = maxTickLength;
752
763
  }
753
764
 
754
765
  // Compute title.
@@ -835,7 +846,9 @@ function computeCategoryLegendSize(
835
846
  };
836
847
 
837
848
  const {
849
+ focus,
838
850
  itemSpacing,
851
+ focusMarkerSize,
839
852
  itemMarkerSize,
840
853
  titleSpacing,
841
854
  rowPadding,
@@ -866,7 +879,11 @@ function computeCategoryLegendSize(
866
879
 
867
880
  const height = Math.max(labelBBoxes[0].height, itemMarkerSize) + rowPadding;
868
881
  const widthOf = (w, padding = 0) =>
869
- itemMarkerSize + w + itemSpacing[0] + padding;
882
+ itemMarkerSize +
883
+ w +
884
+ itemSpacing[0] +
885
+ padding +
886
+ (focus ? focusMarkerSize + itemSpacing[2] : 0);
870
887
 
871
888
  // Only support grid layout for vertical area.
872
889
  const computeVerticalSize = () => {
@@ -7,3 +7,5 @@ export const COMPONENT_CLASS_NAME = 'component';
7
7
  export const LABEL_CLASS_NAME = 'label';
8
8
  export const AREA_CLASS_NAME = 'area';
9
9
  export const MASK_CLASS_NAME = 'mask';
10
+ export const BREAK_CLASS_NAME = 'axis-breaks';
11
+ export const BREAK_GROUP_CLASS_NAME = 'axis-breaks-group';
@@ -24,6 +24,7 @@ import {
24
24
  maybeNonAnimate,
25
25
  normalizeTooltip,
26
26
  extractTooltip,
27
+ appendMarkScaleKey,
27
28
  } from './transform';
28
29
 
29
30
  export async function initializeMark(
@@ -159,6 +160,7 @@ async function applyMarkTransform(
159
160
  maybeNonAnimate,
160
161
  addGuideToScale,
161
162
  normalizeTooltip,
163
+ appendMarkScaleKey,
162
164
  ...preInference.map(useTransform),
163
165
  ...transform.map(useTransform),
164
166
  ...postInference.map(useTransform),
@@ -0,0 +1,36 @@
1
+ import { get, set } from '@antv/util';
2
+ import { G2ViewTree } from '../types/options';
3
+
4
+ /**
5
+ * Convert a percentage string to a ratio.
6
+ */
7
+ export const percentToRatio = (gap: string) => {
8
+ if (!gap || typeof gap !== 'string') {
9
+ return gap;
10
+ }
11
+ const value = gap.endsWith('%')
12
+ ? parseFloat(gap.slice(0, -1)) / 100
13
+ : parseFloat(gap);
14
+ if (isNaN(value) || value < 0 || value > 1) {
15
+ throw new Error(`Invalid gap value: ${gap}. It should be between 0 and 1.`);
16
+ }
17
+ return value;
18
+ };
19
+
20
+ export function axisBreaks<T extends G2ViewTree = G2ViewTree>(options: T): T {
21
+ const { axis } = options;
22
+ const breaks = get(axis, 'y.breaks');
23
+
24
+ if (breaks) {
25
+ set(
26
+ options,
27
+ 'scale.y.breaks',
28
+ breaks.map((item) => ({
29
+ key: `break-${item.start}-${item.end}`,
30
+ ...item,
31
+ gap: percentToRatio(item.gap),
32
+ })),
33
+ );
34
+ }
35
+ return options;
36
+ }
@@ -1,6 +1,7 @@
1
1
  import { G2ViewTree } from '../types/options';
2
2
  import { flow } from '../../utils/flow';
3
3
  import { columnWidthRatio } from './style';
4
+ import { axisBreaks } from './axis-breaks';
4
5
 
5
6
  export function preprocessOption<T extends G2ViewTree = G2ViewTree>(
6
7
  options: T,
@@ -19,5 +20,5 @@ export function preprocessOption<T extends G2ViewTree = G2ViewTree>(
19
20
  // Entry point for all syntactic sugar functions.
20
21
  function adapter<T extends G2ViewTree = G2ViewTree>(options: T): T {
21
22
  //@todo define a type for params of flow
22
- return flow(columnWidthRatio)(options);
23
+ return flow(columnWidthRatio, axisBreaks)(options);
23
24
  }
@@ -1,6 +1,6 @@
1
1
  import { Vector2 } from '@antv/coord';
2
2
  import { DisplayObject, IAnimation as GAnimation, Rect } from '@antv/g';
3
- import { deepMix, upperFirst, isArray } from '@antv/util';
3
+ import { deepMix, upperFirst, isArray, get } from '@antv/util';
4
4
  import { group, groups } from '@antv/vendor/d3-array';
5
5
  import { format } from '@antv/vendor/d3-format';
6
6
  import { mapObject } from '../utils/array';
@@ -32,6 +32,8 @@ import {
32
32
  MAIN_LAYER_CLASS_NAME,
33
33
  PLOT_CLASS_NAME,
34
34
  VIEW_CLASS_NAME,
35
+ BREAK_CLASS_NAME,
36
+ BREAK_GROUP_CLASS_NAME,
35
37
  } from './constant';
36
38
  import { coordinate2Transform, createCoordinate } from './coordinate';
37
39
  import {
@@ -263,7 +265,7 @@ export async function plot<T extends G2ViewTree>(
263
265
  const setState = (key, reducer = (x) => x) => store.set(key, reducer);
264
266
  const options = viewNode.get(view);
265
267
  const update = createUpdateView(select(container), options, context);
266
- return {
268
+ const target = {
267
269
  view,
268
270
  container,
269
271
  options,
@@ -279,6 +281,9 @@ export async function plot<T extends G2ViewTree>(
279
281
  });
280
282
  },
281
283
  };
284
+ context.externals.update = target.update;
285
+ context.externals.setState = setState;
286
+ return target;
282
287
  });
283
288
  };
284
289
 
@@ -872,6 +877,7 @@ function initializeState(
872
877
  style: framedStyle,
873
878
  components,
874
879
  data: dataMap,
880
+ options: options,
875
881
  labelTransform: compose(labelTransform.map(useLabelTransform)),
876
882
  };
877
883
 
@@ -1027,11 +1033,12 @@ async function plotView(
1027
1033
  (update) =>
1028
1034
  update
1029
1035
  .call(updateLayers, Array.from(markState.keys()))
1030
- .call((selection) => {
1031
- return animationExtent
1032
- ? animateBBox(selection, animationExtent)
1033
- : updateBBox(selection);
1034
- })
1036
+ .call(updateBBox)
1037
+ // .call((selection) => {
1038
+ // return animationExtent
1039
+ // ? animateBBox(selection, animationExtent)
1040
+ // : updateBBox(selection);
1041
+ // })
1035
1042
  .call(applyClip, clip),
1036
1043
  )
1037
1044
  .transitions();
@@ -1099,6 +1106,11 @@ async function plotView(
1099
1106
  }
1100
1107
  return animation;
1101
1108
  })
1109
+ .each(function (d, i, element) {
1110
+ if (element.__removed__) {
1111
+ element.__removed__ = false;
1112
+ }
1113
+ })
1102
1114
  .attr('markType', type)
1103
1115
  .attr('className', ELEMENT_CLASS_NAME);
1104
1116
  }),
@@ -1155,6 +1167,7 @@ async function plotView(
1155
1167
 
1156
1168
  // Plot label for this view.
1157
1169
  plotLabel(view, selection, transitions, library, context);
1170
+ plotBreak(view, selection, library, context);
1158
1171
  }
1159
1172
 
1160
1173
  /**
@@ -1286,6 +1299,68 @@ function getLabels(
1286
1299
  return selector ? selector(F) : F;
1287
1300
  }
1288
1301
 
1302
+ /**
1303
+ * Plot break shapes.
1304
+ */
1305
+ function plotBreak(
1306
+ view: G2ViewDescriptor,
1307
+ selection: Selection,
1308
+ library: G2Library,
1309
+ context: G2Context,
1310
+ ) {
1311
+ const scale = view.scale;
1312
+ const breaks = get(scale, 'y.options.breaks', []);
1313
+ const { document } = context.canvas;
1314
+ [BREAK_CLASS_NAME, BREAK_GROUP_CLASS_NAME].forEach((d) => {
1315
+ document.getElementsByClassName(d).forEach((e) => {
1316
+ e.remove();
1317
+ });
1318
+ });
1319
+ if (!breaks.length) {
1320
+ return;
1321
+ }
1322
+ const breakLayer = selection.select(className(PLOT_CLASS_NAME)).node();
1323
+ const [useShape] = useLibrary<G2ShapeOptions, ShapeComponent, Shape>(
1324
+ 'shape',
1325
+ library,
1326
+ );
1327
+ const breaksShapeFunction = new Map();
1328
+ breaks.forEach((breakConfig, index) => {
1329
+ breaksShapeFunction.set(
1330
+ breakConfig,
1331
+ useShape(
1332
+ {
1333
+ type: 'break',
1334
+ },
1335
+ {
1336
+ view,
1337
+ selection,
1338
+ context,
1339
+ },
1340
+ ),
1341
+ );
1342
+ });
1343
+
1344
+ // Render all breaks.
1345
+ select(breakLayer)
1346
+ .selectAll(className(BREAK_CLASS_NAME))
1347
+ .data(breaks, (d) => d.key)
1348
+ .join(
1349
+ (enter) =>
1350
+ enter
1351
+ .append((d, index) => breaksShapeFunction.get(d)(d, index))
1352
+ .attr('className', BREAK_CLASS_NAME),
1353
+ (update) =>
1354
+ update.each(function (d, i, element) {
1355
+ const shapeFunction = breaksShapeFunction.get(d);
1356
+ const node = shapeFunction(d, i);
1357
+ copyAttributes(element, node);
1358
+ }),
1359
+ (exit) => exit.remove(),
1360
+ )
1361
+ .nodes();
1362
+ }
1363
+
1289
1364
  function filterValid([I, P, S]: [number[], Vector2[][], number[][]?]): [
1290
1365
  number[],
1291
1366
  Vector2[][],
@@ -84,6 +84,7 @@ export function render<T extends G2ViewTree = G2ViewTree>(
84
84
 
85
85
  context.canvas = canvas;
86
86
  context.emitter = emitter;
87
+ context.externals = {};
87
88
 
88
89
  const { width: prevWidth, height: prevHeight } = canvas.getConfig();
89
90
  if (prevWidth !== width || prevHeight !== height) {
@@ -314,6 +314,23 @@ export function maybeNonAnimate(
314
314
  return [I, mark];
315
315
  }
316
316
 
317
+ export function appendMarkScaleKey(
318
+ I: number[],
319
+ mark: G2Mark,
320
+ context: G2Context,
321
+ ): [number[], G2Mark] {
322
+ deepMix(mark, {
323
+ scale: {
324
+ series: {
325
+ key: `DEFAULT_${mark.type}_SERIES_KEY`,
326
+ ...(mark?.scale?.series ?? {}),
327
+ },
328
+ },
329
+ });
330
+
331
+ return [I, mark];
332
+ }
333
+
317
334
  function isTypedChannel(channel): boolean {
318
335
  if (
319
336
  typeof channel !== 'object' ||
@@ -59,6 +59,7 @@ export type G2Context = {
59
59
  group?: DisplayObject;
60
60
  animations?: GAnimation[];
61
61
  views?: G2ViewDescriptor[];
62
+ externals?: Record<string, any>;
62
63
  /**
63
64
  * Tell G2 how to create a canvas-like element, some marks will use it later such as wordcloud & heatmap.
64
65
  * Use `document.createElement('canvas')` instead if not provided.