@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
@@ -0,0 +1,203 @@
1
+ import { Path } from '@antv/g';
2
+ import { get, set, deepMix } from '@antv/util';
3
+ import type { PathStyleProps } from '@antv/g';
4
+ import {
5
+ BREAK_GROUP_CLASS_NAME,
6
+ PLOT_CLASS_NAME,
7
+ } from '../../runtime/constant';
8
+
9
+ export const BREAKS_GAP = 0.03; // Default gap ratio for axis breaks
10
+
11
+ export type BreakOptions = {
12
+ /** Start position of the break. */
13
+ start: number;
14
+ /** End position of the break. */
15
+ end: number;
16
+ /** Gap ratio of the break, default is 0.1. */
17
+ gap?: number;
18
+ /** Number of wave vertices, default is 50. */
19
+ vertices?: number;
20
+ /** Offset of each vertex, default is 3. */
21
+ verticeOffset?: number;
22
+ /** Custom styles of the break. */
23
+ [key: string]: any;
24
+ };
25
+
26
+ const DEFAULT_STYLE = {
27
+ fill: '#fff',
28
+ stroke: '#aaa',
29
+ lineDash: '4 3',
30
+ lineWidth: 0.5,
31
+ fillOpacity: 1,
32
+ strokeOpacity: 1,
33
+ };
34
+
35
+ const PADDING = 0;
36
+
37
+ /**
38
+ * Create path points and corresponding clip paths.
39
+ * @param y baseline Y coordinate
40
+ * @param width total width of the path
41
+ * @param offset vertical offset of wave
42
+ * @param vertices number of generated points
43
+ * @param isLowerBoundary whether it is the lower boundary
44
+ * @param lineWidth line width of path
45
+ * @returns tuple of [pathPoints, clipPoints]
46
+ */
47
+ const createPathPoints = (
48
+ y: number,
49
+ width: number,
50
+ offset: number,
51
+ vertices: number,
52
+ isLowerBoundary: boolean,
53
+ lineWidth: number,
54
+ ) => {
55
+ const pathPoints: string[] = [];
56
+ const clipPoints: string[] = [];
57
+ const segments = vertices - 1;
58
+
59
+ for (let i = 1; i < segments; i++) {
60
+ const x = (i / segments) * width;
61
+ const offsetY = y + (i % 2 === 0 ? offset : -offset);
62
+
63
+ pathPoints.push(`${x},${offsetY}`);
64
+ clipPoints.push(
65
+ `${x},${isLowerBoundary ? offsetY - lineWidth : offsetY + lineWidth}`,
66
+ );
67
+ }
68
+
69
+ // Ensure last point reaches width
70
+ pathPoints.push(`${width},${y}`);
71
+ clipPoints.push(`${width + lineWidth},${y}`);
72
+
73
+ return [pathPoints, clipPoints] as const;
74
+ };
75
+
76
+ const updateScale = (view, breakValues) => {
77
+ const scale = get(view, 'scale.y');
78
+ const scaleOptions = get(scale, 'options', {});
79
+ const { breaks } = scaleOptions;
80
+ const filterBreaks = breaks.filter(
81
+ (b) => b.start !== breakValues[0] && b.end !== breakValues[1],
82
+ );
83
+ scale.update({
84
+ ...scaleOptions,
85
+ breaks: filterBreaks,
86
+ });
87
+ };
88
+
89
+ export const AxisBreaks = (options, params) => {
90
+ const { context, selection, view } = params;
91
+ const layer = selection.select(`.${PLOT_CLASS_NAME}`).node();
92
+ const { document } = context.canvas;
93
+ const { externals } = context;
94
+ const { scale } = view;
95
+ return (option: BreakOptions) => {
96
+ const {
97
+ key,
98
+ start,
99
+ end,
100
+ gap = BREAKS_GAP,
101
+ vertices = 50,
102
+ lineWidth = 0.5,
103
+ verticeOffset = 3,
104
+ ...style
105
+ } = option;
106
+
107
+ const g = document.createElement('g', {
108
+ id: `break-group-${key}`,
109
+ className: BREAK_GROUP_CLASS_NAME,
110
+ });
111
+
112
+ const xDomain = get(scale, 'x.sortedDomain', []);
113
+ const yScale = scale['y'].getOptions();
114
+ const { range, domain } = yScale;
115
+ const startIndex = domain.indexOf(start);
116
+ const endIndex = domain.indexOf(end);
117
+ const { width: plotWidth, height: plotHeight } = layer.getBBox();
118
+ if (startIndex === -1 || endIndex === -1 || !xDomain.length) return g;
119
+ const reverse = range[0] > range[1];
120
+ const lowerY = range[startIndex] * plotHeight;
121
+ const upperY = range[endIndex] * plotHeight;
122
+
123
+ let linePath = '';
124
+ let clipPath = '';
125
+
126
+ for (const [boundaryIndex, { y, isLower }] of [
127
+ { y: upperY, isLower: false },
128
+ { y: lowerY, isLower: true },
129
+ ].entries()) {
130
+ const clipOffset = reverse ? lineWidth : -lineWidth;
131
+ const [pathPoints, clipPoints] = createPathPoints(
132
+ y,
133
+ plotWidth - PADDING,
134
+ verticeOffset,
135
+ vertices,
136
+ isLower,
137
+ clipOffset,
138
+ );
139
+ if (boundaryIndex === 0) {
140
+ // start point + Top boundary path
141
+ linePath = `M ${PADDING},${y} L ${pathPoints.join(' L ')} `;
142
+ clipPath = `M ${PADDING - lineWidth},${
143
+ y + clipOffset
144
+ } L ${clipPoints.join(' L ')} `;
145
+ } else {
146
+ // Bottom boundary path + close point
147
+ linePath += `L ${plotWidth - PADDING},${y} L ${[...pathPoints]
148
+ .reverse()
149
+ .join(' L ')} L ${PADDING},${y} Z`;
150
+ clipPath += `L ${plotWidth - PADDING + lineWidth + 2},${
151
+ y - clipOffset
152
+ } L ${[...clipPoints].reverse().join(' L ')} L ${PADDING - lineWidth},${
153
+ y - clipOffset
154
+ } Z`;
155
+ }
156
+ }
157
+
158
+ const pathAttrs = { ...DEFAULT_STYLE, ...style } as PathStyleProps;
159
+
160
+ try {
161
+ const path1 = new Path({ style: { ...pathAttrs, d: linePath } });
162
+ const path2 = new Path({
163
+ style: { ...pathAttrs, d: clipPath, lineWidth: 0 },
164
+ });
165
+ path2.addEventListener('click', async (e) => {
166
+ // double click to remove break
167
+ if (e.detail === 2) {
168
+ updateScale(view, [start, end]);
169
+ const { update, setState } = context.externals;
170
+ setState('options', (prev) => {
171
+ console.log(prev);
172
+ const { marks } = prev;
173
+ if (!marks || !marks.length) return prev;
174
+ const newMarks = marks.map((mark) => {
175
+ const breaks = get(mark, 'scale.y.breaks', []);
176
+ const newBreaks = breaks.filter(
177
+ (b) => b.start !== start && b.end !== end && !b.used,
178
+ );
179
+ // add used: true flag to the corresponding breaks
180
+ breaks.forEach((b) => {
181
+ if (b.start === start && b.end === end) {
182
+ b.used = true;
183
+ }
184
+ });
185
+ return deepMix({}, mark, { scale: { y: { breaks: newBreaks } } });
186
+ });
187
+ return { ...prev, marks: newMarks };
188
+ });
189
+ await update();
190
+ }
191
+ });
192
+ g.appendChild(path1);
193
+ g.appendChild(path2);
194
+ layer.appendChild(g);
195
+ } catch (e) {
196
+ console.error('Failed to create break path:', e);
197
+ }
198
+
199
+ return g;
200
+ };
201
+ };
202
+
203
+ AxisBreaks.props = {};
@@ -62,6 +62,7 @@ export { Hollow as RectHollow } from './interval/hollow';
62
62
  export { Shape as ShapeShape } from './shape/shape';
63
63
  export { Liquid as LiquidShape } from './liquid/liquid';
64
64
  export { Round as GaugeRound } from './gauge/round';
65
+ export { AxisBreaks as BreakShape } from './axis-breaks/axis-breaks';
65
66
 
66
67
  export type { RectOptions as IntervalShapeOptions } from './interval/rect';
67
68
  export type { HollowOptions as IntervalHollowOptions } from './interval/hollow';
@@ -113,3 +114,4 @@ export type { LabelOptions as LabelShapeOptions } from './label/label';
113
114
  export type { DensityOptions as DensityShapeOptions } from './density/density';
114
115
  export type { ShapeOptions as ShapeShapeOptions } from './shape/shape';
115
116
  export type { LiquidOptions } from './liquid/liquid';
117
+ export type { BreakOptions as PathShapeOptions } from './axis-breaks/axis-breaks';
@@ -36,7 +36,7 @@ export type ContrastReverseLabelTransform = {
36
36
  };
37
37
 
38
38
  export type OverflowStrokeTransform = {
39
- type?: 'overlapStroke';
39
+ type?: 'overflowStroke';
40
40
  /**
41
41
  * Stroke when label overflow more than threshold.
42
42
  * Default is `2`.
@@ -305,7 +305,8 @@ export function create({
305
305
  itemLabelFontWeight: 'normal',
306
306
  itemMarkerFillOpacity: 1,
307
307
  itemMarkerSize: 8,
308
- itemSpacing: [padding1, padding1],
308
+ focusMarkerSize: 12,
309
+ itemSpacing: [padding1, padding1, padding1 / 2],
309
310
  itemValueFill: colorBlack,
310
311
  itemValueFillOpacity: 0.65,
311
312
  itemValueFontSize: 12,
@@ -11,6 +11,9 @@ export function isOrdinalScale(scale) {
11
11
  export function invert(scale, x, start) {
12
12
  if (!isOrdinalScale(scale)) return scale.invert(x);
13
13
  const { adjustedRange } = scale;
14
+ if (adjustedRange.includes(x)) {
15
+ return scale.invert(x);
16
+ }
14
17
  const { domain } = scale.getOptions();
15
18
  const offset = start ? -1 : 0;
16
19
  const step = scale.getStep();
@@ -64,6 +67,27 @@ export function abstractOf(domain, scale) {
64
67
  return [scale.map(d0), scale.map(d1) + maybeStep(scale)];
65
68
  }
66
69
 
70
+ // For slider display, use direct index-based mapping instead of scale.map()
71
+ // to avoid inaccuracies caused by padding, band width, etc.
72
+ export const sliderAbstractOf = (domain, scale) => {
73
+ const [d0, d1] = domain;
74
+ const scaleDomain = scale.getOptions?.()?.domain || [];
75
+
76
+ const index0 = scaleDomain.indexOf(d0);
77
+ const index1 = scaleDomain.indexOf(d1);
78
+
79
+ if (index0 === -1 || index1 === -1) {
80
+ return [scale.map(d0), scale.map(d1)];
81
+ }
82
+
83
+ const count = scaleDomain.length;
84
+ if (count <= 1) {
85
+ return [0, 1];
86
+ }
87
+
88
+ return [index0 / (count - 1), index1 / (count - 1)];
89
+ };
90
+
67
91
  export function pixelsOf(selection, scale, coordinate) {
68
92
  const { x: scaleX, y: scaleY } = scale;
69
93
  const [X, Y] = selection;
@@ -344,15 +344,20 @@ export class Selection<T = any> {
344
344
  // otherwise wait until transition finished.
345
345
  for (let i = 0; i < this._elements.length; i++) {
346
346
  const transition = this._transitions[i];
347
+ this._elements[i].__removed__ = true;
347
348
  if (transition) {
348
349
  const T = Array.isArray(transition) ? transition : [transition];
349
350
  Promise.all(T.map((d) => d.finished)).then(() => {
350
351
  const element = this._elements[i];
351
- element.remove();
352
+ if (element.__removed__) {
353
+ element.remove();
354
+ }
352
355
  });
353
356
  } else {
354
357
  const element = this._elements[i];
355
- element.remove();
358
+ if (element.__removed__) {
359
+ element.remove();
360
+ }
356
361
  }
357
362
  }
358
363
  return new Selection<T>(