@antv/g2 5.4.0 → 5.4.1

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 (76) hide show
  1. package/dist/g2.lite.min.js +10 -10
  2. package/dist/g2.min.js +8 -8
  3. package/esm/component/axis.d.ts +1 -0
  4. package/esm/component/axis.js.map +1 -1
  5. package/esm/index.d.ts +2 -0
  6. package/esm/interaction/brushHighlight.d.ts +5 -0
  7. package/esm/interaction/brushHighlight.js +4 -0
  8. package/esm/interaction/brushHighlight.js.map +1 -1
  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/legendFilter.js +7 -1
  13. package/esm/interaction/legendFilter.js.map +1 -1
  14. package/esm/interaction/sliderWheel.d.ts +19 -0
  15. package/esm/interaction/sliderWheel.js +82 -0
  16. package/esm/interaction/sliderWheel.js.map +1 -0
  17. package/esm/interaction/utils.d.ts +10 -0
  18. package/esm/interaction/utils.js +20 -0
  19. package/esm/interaction/utils.js.map +1 -1
  20. package/esm/lib/builtinlib.d.ts +1 -1
  21. package/esm/lib/core.d.ts +2 -1
  22. package/esm/lib/core.js +2 -1
  23. package/esm/lib/core.js.map +1 -1
  24. package/esm/lib/std.d.ts +1 -0
  25. package/esm/mark/sankey.js +3 -1
  26. package/esm/mark/sankey.js.map +1 -1
  27. package/esm/runtime/mark.js +3 -2
  28. package/esm/runtime/mark.js.map +1 -1
  29. package/esm/shape/axis-breaks/axis-breaks.d.ts +3 -1
  30. package/esm/shape/axis-breaks/axis-breaks.js +52 -33
  31. package/esm/shape/axis-breaks/axis-breaks.js.map +1 -1
  32. package/esm/utils/helper.js +17 -1
  33. package/esm/utils/helper.js.map +1 -1
  34. package/lib/component/axis.d.ts +1 -0
  35. package/lib/component/axis.js.map +1 -1
  36. package/lib/index.d.ts +2 -0
  37. package/lib/interaction/brushHighlight.d.ts +5 -0
  38. package/lib/interaction/brushHighlight.js +4 -0
  39. package/lib/interaction/brushHighlight.js.map +1 -1
  40. package/lib/interaction/index.d.ts +1 -0
  41. package/lib/interaction/index.js +3 -1
  42. package/lib/interaction/index.js.map +1 -1
  43. package/lib/interaction/legendFilter.js +7 -1
  44. package/lib/interaction/legendFilter.js.map +1 -1
  45. package/lib/interaction/sliderWheel.d.ts +19 -0
  46. package/lib/interaction/sliderWheel.js +86 -0
  47. package/lib/interaction/sliderWheel.js.map +1 -0
  48. package/lib/interaction/utils.d.ts +10 -0
  49. package/lib/interaction/utils.js +22 -1
  50. package/lib/interaction/utils.js.map +1 -1
  51. package/lib/lib/builtinlib.d.ts +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/std.d.ts +1 -0
  56. package/lib/mark/sankey.js +3 -1
  57. package/lib/mark/sankey.js.map +1 -1
  58. package/lib/runtime/mark.js +3 -2
  59. package/lib/runtime/mark.js.map +1 -1
  60. package/lib/shape/axis-breaks/axis-breaks.d.ts +3 -1
  61. package/lib/shape/axis-breaks/axis-breaks.js +51 -32
  62. package/lib/shape/axis-breaks/axis-breaks.js.map +1 -1
  63. package/lib/utils/helper.js +17 -1
  64. package/lib/utils/helper.js.map +1 -1
  65. package/package.json +28 -7
  66. package/src/component/axis.ts +5 -0
  67. package/src/interaction/brushHighlight.ts +5 -0
  68. package/src/interaction/index.ts +1 -0
  69. package/src/interaction/legendFilter.ts +8 -0
  70. package/src/interaction/sliderWheel.ts +125 -0
  71. package/src/interaction/utils.ts +23 -0
  72. package/src/lib/core.ts +2 -0
  73. package/src/mark/sankey.ts +3 -0
  74. package/src/runtime/mark.ts +2 -1
  75. package/src/shape/axis-breaks/axis-breaks.ts +72 -40
  76. package/src/utils/helper.ts +19 -1
@@ -72,6 +72,7 @@ function legendFilterOrdinal(
72
72
  label: labelOf, // given the legend returns the label
73
73
  datum, // given the legend returns the value
74
74
  filter, // invoke when dispatch filter event,
75
+ defaultSelect,
75
76
  emitter,
76
77
  channel,
77
78
  state = {} as Record<string, any>, // state options
@@ -237,6 +238,12 @@ function legendFilterOrdinal(
237
238
  emitter.on('legend:focus', onFocus);
238
239
  emitter.on('legend:reset', onEnd);
239
240
 
241
+ if (defaultSelect) {
242
+ emitter.emit('legend:filter', {
243
+ data: { channel, values: defaultSelect },
244
+ });
245
+ }
246
+
240
247
  return () => {
241
248
  for (const item of items) {
242
249
  item.removeEventListener('click', itemClick.get(item));
@@ -375,6 +382,7 @@ export function LegendFilter() {
375
382
  else filter(context, options);
376
383
  },
377
384
  state: legend.attributes.state,
385
+ defaultSelect: legend.attributes.defaultSelect,
378
386
  channel,
379
387
  emitter,
380
388
  });
@@ -0,0 +1,125 @@
1
+ import { CustomEvent } from '@antv/g';
2
+ import { isTranspose } from '../utils/coordinate';
3
+ import { SLIDER_CLASS_NAME } from './sliderFilter';
4
+ import { calculateSensitivityMultiplier } from './utils';
5
+
6
+ /**
7
+ * SliderWheel interaction for mouse wheel/touchpad gestures on charts.
8
+ */
9
+
10
+ type SliderDirection = true | false | 'shift' | 'ctrl' | 'alt';
11
+
12
+ interface SliderWheelOptions {
13
+ className?: string;
14
+ setValue?: (component: any, values: [number, number]) => void;
15
+ minRange?: number; // Minimum range (0.000001-1), auto-clamped
16
+ wheelSensitivity?: number;
17
+ x?: SliderDirection;
18
+ y?: SliderDirection;
19
+ }
20
+
21
+ export function SliderWheel({
22
+ className = SLIDER_CLASS_NAME,
23
+ setValue = (component, values) => component.setValues(values),
24
+ minRange = 0.01,
25
+ wheelSensitivity = 0.05,
26
+ x = true,
27
+ y = true,
28
+ }: SliderWheelOptions = {}) {
29
+ return (context) => {
30
+ const { container, view } = context;
31
+ const { coordinate } = view;
32
+ const transposed = isTranspose(coordinate);
33
+ const safeMinRange = Math.max(0.000001, Math.min(1, minRange));
34
+
35
+ const sliders = container.getElementsByClassName(className);
36
+ if (!sliders.length) return () => {};
37
+
38
+ const isModifierKeyActive = (
39
+ config: SliderDirection,
40
+ event: WheelEvent,
41
+ ): boolean => {
42
+ if (config === true) return true;
43
+ if (config === false) return false;
44
+ if (config === 'shift')
45
+ return event.shiftKey && !event.ctrlKey && !event.altKey;
46
+ if (config === 'ctrl')
47
+ return event.ctrlKey && !event.shiftKey && !event.altKey;
48
+ if (config === 'alt')
49
+ return event.altKey && !event.shiftKey && !event.ctrlKey;
50
+ return false;
51
+ };
52
+
53
+ const triggerSliderValueChange = (
54
+ slider: any,
55
+ values: [number, number],
56
+ ) => {
57
+ setValue(slider, values);
58
+ slider.dispatchEvent(
59
+ new CustomEvent('valuechange', {
60
+ detail: { value: values },
61
+ nativeEvent: true,
62
+ }),
63
+ );
64
+ };
65
+
66
+ const handleWheel = (event: WheelEvent) => {
67
+ event.preventDefault();
68
+ event.stopPropagation();
69
+
70
+ for (const slider of sliders) {
71
+ const { values, orientation } = (slider as any).attributes;
72
+ if (!values) continue;
73
+
74
+ const isHorizontal = orientation === 'horizontal';
75
+ const actualAxis = transposed
76
+ ? isHorizontal
77
+ ? 'y'
78
+ : 'x'
79
+ : isHorizontal
80
+ ? 'x'
81
+ : 'y';
82
+ const axisConfig = actualAxis === 'x' ? x : y;
83
+
84
+ if (!isModifierKeyActive(axisConfig, event)) continue;
85
+
86
+ const [v0, v1] = values;
87
+ const range = v1 - v0;
88
+ const center = (v0 + v1) / 2;
89
+ const adaptiveSensitivity =
90
+ wheelSensitivity * calculateSensitivityMultiplier(range);
91
+
92
+ const delta = event.deltaY * adaptiveSensitivity;
93
+ const zoomFactor = 1 + delta;
94
+ const newRange = Math.max(
95
+ safeMinRange,
96
+ Math.min(1, range * zoomFactor),
97
+ );
98
+
99
+ const halfRange = newRange / 2;
100
+ let newV0 = center - halfRange;
101
+ let newV1 = center + halfRange;
102
+
103
+ if (newV0 < 0) {
104
+ newV0 = 0;
105
+ newV1 = Math.min(1, newRange);
106
+ } else if (newV1 > 1) {
107
+ newV1 = 1;
108
+ newV0 = Math.max(0, 1 - newRange);
109
+ }
110
+
111
+ triggerSliderValueChange(slider, [newV0, newV1]);
112
+ }
113
+ };
114
+
115
+ container.addEventListener('wheel', handleWheel, { passive: false });
116
+
117
+ return () => {
118
+ container.removeEventListener('wheel', handleWheel);
119
+ };
120
+ };
121
+ }
122
+
123
+ SliderWheel.props = {
124
+ reapplyWhenUpdate: true,
125
+ };
@@ -699,3 +699,26 @@ export function createFindElementByEvent({
699
699
  });
700
700
  };
701
701
  }
702
+
703
+ /**
704
+ * Calculate adaptive sensitivity multiplier (inversely proportional to range).
705
+ *
706
+ * - Smaller range → higher sensitivity
707
+ * - Larger range → lower sensitivity
708
+ *
709
+ * @param range Current range (0-1)
710
+ * @returns Sensitivity multiplier (0.1x ~ 100x)
711
+ */
712
+ export function calculateSensitivityMultiplier(range: number): number {
713
+ // Base sensitivity factor (adjust this to tune overall responsiveness)
714
+ const BASE_FACTOR = 0.01;
715
+ const MIN_RANGE_FOR_SENSITIVITY = 0.0001;
716
+ const MIN_MULTIPLIER = 0.1;
717
+ const MAX_MULTIPLIER = 100;
718
+
719
+ // Simple inverse relationship with reasonable bounds
720
+ const multiplier = BASE_FACTOR / Math.max(range, MIN_RANGE_FOR_SENSITIVITY);
721
+
722
+ // Clamp to reasonable range: 0.1x to 100x
723
+ return Math.max(MIN_MULTIPLIER, Math.min(MAX_MULTIPLIER, multiplier));
724
+ }
package/src/lib/core.ts CHANGED
@@ -100,6 +100,7 @@ import {
100
100
  BrushXFilter,
101
101
  BrushYFilter,
102
102
  SliderFilter,
103
+ SliderWheel,
103
104
  Poptip,
104
105
  ScrollbarFilter,
105
106
  TreemapDrillDown,
@@ -309,6 +310,7 @@ export function corelib() {
309
310
  'interaction.brushXFilter': BrushXFilter,
310
311
  'interaction.brushYFilter': BrushYFilter,
311
312
  'interaction.sliderFilter': SliderFilter,
313
+ 'interaction.sliderWheel': SliderWheel,
312
314
  'interaction.scrollbarFilter': ScrollbarFilter,
313
315
  'interaction.poptip': Poptip,
314
316
  'interaction.treemapDrillDown': TreemapDrillDown,
@@ -70,6 +70,7 @@ export const Sankey: CC<SankeyOptions> = (options) => {
70
70
  tooltip = {},
71
71
  interaction,
72
72
  state = {},
73
+ viewStyle = {},
73
74
  } = options;
74
75
 
75
76
  // Initialize data, generating nodes by link if is not specified.
@@ -84,6 +85,7 @@ export const Sankey: CC<SankeyOptions> = (options) => {
84
85
  encode: {},
85
86
  scale,
86
87
  style: subObject(style, 'node'),
88
+ viewStyle,
87
89
  labels: [],
88
90
  tooltip: false,
89
91
  animate: false,
@@ -160,6 +162,7 @@ export const Sankey: CC<SankeyOptions> = (options) => {
160
162
  encode: { ...nodeEncode, color },
161
163
  scale,
162
164
  style: subObject(style, 'node'),
165
+ viewStyle,
163
166
  labels: [
164
167
  {
165
168
  ...DEFAULT_LABEL_OPTIONS,
@@ -39,7 +39,7 @@ export async function initializeMark(
39
39
  context,
40
40
  );
41
41
 
42
- const { encode, scale, data, tooltip } = transformedMark;
42
+ const { encode, scale, data, tooltip, key: markerKey } = transformedMark;
43
43
 
44
44
  // Skip mark with non-tabular data. Do not skip empty
45
45
  // data, they are useful for facet to display axes.
@@ -107,6 +107,7 @@ export async function initializeMark(
107
107
  scaleKey: independent || isConstant ? Symbol('independent') : key,
108
108
  scale: {
109
109
  type,
110
+ markerKey,
110
111
  range: finalRange,
111
112
  ...scaleOptions,
112
113
  quantitative,
@@ -1,5 +1,5 @@
1
1
  import { Path } from '@antv/g';
2
- import { get, set, deepMix } from '@antv/util';
2
+ import { get, deepMix, set } from '@antv/util';
3
3
  import type { PathStyleProps } from '@antv/g';
4
4
  import {
5
5
  BREAK_GROUP_CLASS_NAME,
@@ -19,6 +19,8 @@ export type BreakOptions = {
19
19
  vertices?: number;
20
20
  /** Offset of each vertex, default is 3. */
21
21
  verticeOffset?: number;
22
+ /** Compression type of the break, default is 'middle'. */
23
+ compress?: 'start' | 'end' | 'middle';
22
24
  /** Custom styles of the break. */
23
25
  [key: string]: any;
24
26
  };
@@ -73,25 +75,67 @@ const createPathPoints = (
73
75
  return [pathPoints, clipPoints] as const;
74
76
  };
75
77
 
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) => {
78
+ export const AxisBreaks = (_, params) => {
90
79
  const { context, selection, view } = params;
91
80
  const layer = selection.select(`.${PLOT_CLASS_NAME}`).node();
92
81
  const { document } = context.canvas;
93
- const { externals } = context;
94
82
  const { scale } = view;
83
+
84
+ const collapsed = new Map<string, BreakOptions>();
85
+
86
+ const handleCollapseToggle = async (
87
+ key: string,
88
+ start: number,
89
+ end: number,
90
+ ) => {
91
+ const { update, setState } = context.externals;
92
+ setState('options', (prev) => {
93
+ const { marks } = prev;
94
+ if (!marks || !marks.length) return prev;
95
+ const newMarks = marks.map((mark) => {
96
+ const breaks = get(mark, 'scale.y.breaks', []);
97
+
98
+ const newBreaks = breaks.filter(
99
+ (b) => b.start !== start && b.end !== end && !b.collapsed,
100
+ );
101
+ // add collapsed: true flag to the corresponding breaks
102
+ breaks.forEach((b) => {
103
+ if (b.start === start && b.end === end) {
104
+ b.collapsed = true;
105
+ }
106
+ });
107
+ console.log('breaks group:', breaks, newBreaks);
108
+ return deepMix({}, mark, { scale: { y: { breaks: newBreaks } } });
109
+ });
110
+ collapsed.set(key, { start, end });
111
+ return { ...prev, marks: newMarks };
112
+ });
113
+ await update();
114
+ };
115
+
116
+ const resetCollapsed = async () => {
117
+ if (!collapsed.size) return;
118
+ const { update, setState } = context.externals;
119
+ setState('options', (prev) => {
120
+ const { marks } = prev;
121
+ const newMarks = marks.map((mark) => {
122
+ const breaks = get(mark, 'scale.y.breaks', []);
123
+ set(
124
+ mark,
125
+ 'scale.y.breaks',
126
+ breaks.map((b) => ({
127
+ ...b,
128
+ collapsed: false,
129
+ })),
130
+ );
131
+ return mark;
132
+ });
133
+ collapsed.clear();
134
+ return { ...prev, marks: newMarks };
135
+ });
136
+ await update();
137
+ };
138
+
95
139
  return (option: BreakOptions) => {
96
140
  const {
97
141
  key,
@@ -160,37 +204,25 @@ export const AxisBreaks = (options, params) => {
160
204
  try {
161
205
  const path1 = new Path({ style: { ...pathAttrs, d: linePath } });
162
206
  const path2 = new Path({
163
- style: { ...pathAttrs, d: clipPath, lineWidth: 0 },
207
+ style: { ...pathAttrs, d: clipPath, lineWidth: 0, cursor: 'pointer' },
164
208
  });
209
+ // double click to remove break
165
210
  path2.addEventListener('click', async (e) => {
166
- // double click to remove break
211
+ e.stopPropagation();
167
212
  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();
213
+ await handleCollapseToggle(key, start, end);
190
214
  }
191
215
  });
192
216
  g.appendChild(path1);
193
217
  g.appendChild(path2);
218
+
219
+ // reset collapsed breaks on double click the plot background
220
+ layer.addEventListener('click', async (e) => {
221
+ if (e.detail === 2) {
222
+ await resetCollapsed();
223
+ }
224
+ });
225
+
194
226
  layer.appendChild(g);
195
227
  } catch (e) {
196
228
  console.error('Failed to create break path:', e);
@@ -61,12 +61,30 @@ export function seriesOf(elemenet: G2Element): string {
61
61
  return groupNameOf(scale, elemenet.__data__);
62
62
  }
63
63
 
64
+ /**
65
+ * Get series scale by markerKey
66
+ */
67
+ function getSeriesByMarkerKey(scale: Record<string, Base<any>>, datum) {
68
+ // For path mark, markerKey is in datum.element?.__data__?.markKey.
69
+ const markerKey = datum.markKey ?? datum.element?.__data__?.markKey;
70
+
71
+ const seriesKey = Object.keys(scale).find((channel) => {
72
+ if (channel.startsWith('series')) {
73
+ const options = scale[channel].getOptions();
74
+ return options.name === 'series' && options.markerKey === markerKey;
75
+ }
76
+ });
77
+ return scale[seriesKey] ?? scale.series;
78
+ }
79
+
64
80
  /**
65
81
  * Get group name with view's scale and element's datum.
66
82
  */
67
83
  export function groupNameOf(scale: Record<string, Base<any>>, datum) {
68
- const { color: scaleColor, series: scaleSeries, facet = false } = scale;
84
+ const { color: scaleColor, facet = false } = scale;
69
85
  const { color, series } = datum;
86
+ const scaleSeries = getSeriesByMarkerKey(scale, datum);
87
+
70
88
  const invertAble = (scale) => {
71
89
  return (
72
90
  scale &&