@antv/g2 5.4.0 → 5.4.2

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 (141) hide show
  1. package/dist/g2.lite.min.js +10 -10
  2. package/dist/g2.min.js +10 -10
  3. package/esm/component/axis.d.ts +1 -0
  4. package/esm/component/axis.js.map +1 -1
  5. package/esm/component/constant.d.ts +1 -0
  6. package/esm/component/constant.js +8 -0
  7. package/esm/component/constant.js.map +1 -1
  8. package/esm/component/slider.js +3 -1
  9. package/esm/component/slider.js.map +1 -1
  10. package/esm/composition/view.js +4 -4
  11. package/esm/composition/view.js.map +1 -1
  12. package/esm/index.d.ts +4 -0
  13. package/esm/interaction/adaptiveFilter.d.ts +147 -0
  14. package/esm/interaction/adaptiveFilter.js +305 -0
  15. package/esm/interaction/adaptiveFilter.js.map +1 -0
  16. package/esm/interaction/brushHighlight.d.ts +5 -0
  17. package/esm/interaction/brushHighlight.js +4 -0
  18. package/esm/interaction/brushHighlight.js.map +1 -1
  19. package/esm/interaction/index.d.ts +1 -0
  20. package/esm/interaction/index.js +1 -0
  21. package/esm/interaction/index.js.map +1 -1
  22. package/esm/interaction/legendFilter.js +11 -1
  23. package/esm/interaction/legendFilter.js.map +1 -1
  24. package/esm/interaction/sliderFilter.d.ts +2 -1
  25. package/esm/interaction/sliderFilter.js +400 -42
  26. package/esm/interaction/sliderFilter.js.map +1 -1
  27. package/esm/interaction/sliderWheel.d.ts +16 -0
  28. package/esm/interaction/sliderWheel.js +163 -0
  29. package/esm/interaction/sliderWheel.js.map +1 -0
  30. package/esm/interaction/tooltip.d.ts +2 -2
  31. package/esm/interaction/tooltip.js +32 -8
  32. package/esm/interaction/tooltip.js.map +1 -1
  33. package/esm/interaction/utils.d.ts +79 -0
  34. package/esm/interaction/utils.js +221 -0
  35. package/esm/interaction/utils.js.map +1 -1
  36. package/esm/lib/builtinlib.d.ts +1 -1
  37. package/esm/lib/core.d.ts +3 -1
  38. package/esm/lib/core.js +4 -2
  39. package/esm/lib/core.js.map +1 -1
  40. package/esm/lib/std.d.ts +2 -0
  41. package/esm/mark/beeswarm.d.ts +7 -0
  42. package/esm/mark/beeswarm.js +98 -0
  43. package/esm/mark/beeswarm.js.map +1 -0
  44. package/esm/mark/index.d.ts +2 -0
  45. package/esm/mark/index.js +1 -0
  46. package/esm/mark/index.js.map +1 -1
  47. package/esm/mark/sankey.js +3 -1
  48. package/esm/mark/sankey.js.map +1 -1
  49. package/esm/runtime/layout.js +35 -4
  50. package/esm/runtime/layout.js.map +1 -1
  51. package/esm/runtime/mark.js +3 -2
  52. package/esm/runtime/mark.js.map +1 -1
  53. package/esm/runtime/render.js +1 -0
  54. package/esm/runtime/render.js.map +1 -1
  55. package/esm/shape/axis-breaks/axis-breaks.d.ts +3 -1
  56. package/esm/shape/axis-breaks/axis-breaks.js +52 -33
  57. package/esm/shape/axis-breaks/axis-breaks.js.map +1 -1
  58. package/esm/spec/interaction.d.ts +7 -0
  59. package/esm/utils/helper.js +17 -1
  60. package/esm/utils/helper.js.map +1 -1
  61. package/lib/component/axis.d.ts +1 -0
  62. package/lib/component/axis.js.map +1 -1
  63. package/lib/component/constant.d.ts +1 -0
  64. package/lib/component/constant.js +9 -1
  65. package/lib/component/constant.js.map +1 -1
  66. package/lib/component/slider.js +3 -1
  67. package/lib/component/slider.js.map +1 -1
  68. package/lib/composition/view.js +4 -4
  69. package/lib/composition/view.js.map +1 -1
  70. package/lib/index.d.ts +4 -0
  71. package/lib/interaction/adaptiveFilter.d.ts +147 -0
  72. package/lib/interaction/adaptiveFilter.js +315 -0
  73. package/lib/interaction/adaptiveFilter.js.map +1 -0
  74. package/lib/interaction/brushHighlight.d.ts +5 -0
  75. package/lib/interaction/brushHighlight.js +4 -0
  76. package/lib/interaction/brushHighlight.js.map +1 -1
  77. package/lib/interaction/index.d.ts +1 -0
  78. package/lib/interaction/index.js +3 -1
  79. package/lib/interaction/index.js.map +1 -1
  80. package/lib/interaction/legendFilter.js +11 -1
  81. package/lib/interaction/legendFilter.js.map +1 -1
  82. package/lib/interaction/sliderFilter.d.ts +2 -1
  83. package/lib/interaction/sliderFilter.js +399 -41
  84. package/lib/interaction/sliderFilter.js.map +1 -1
  85. package/lib/interaction/sliderWheel.d.ts +16 -0
  86. package/lib/interaction/sliderWheel.js +167 -0
  87. package/lib/interaction/sliderWheel.js.map +1 -0
  88. package/lib/interaction/tooltip.d.ts +2 -2
  89. package/lib/interaction/tooltip.js +32 -8
  90. package/lib/interaction/tooltip.js.map +1 -1
  91. package/lib/interaction/utils.d.ts +79 -0
  92. package/lib/interaction/utils.js +228 -1
  93. package/lib/interaction/utils.js.map +1 -1
  94. package/lib/lib/builtinlib.d.ts +1 -1
  95. package/lib/lib/core.d.ts +3 -1
  96. package/lib/lib/core.js +2 -0
  97. package/lib/lib/core.js.map +1 -1
  98. package/lib/lib/std.d.ts +2 -0
  99. package/lib/mark/beeswarm.d.ts +7 -0
  100. package/lib/mark/beeswarm.js +102 -0
  101. package/lib/mark/beeswarm.js.map +1 -0
  102. package/lib/mark/index.d.ts +2 -0
  103. package/lib/mark/index.js +3 -1
  104. package/lib/mark/index.js.map +1 -1
  105. package/lib/mark/sankey.js +3 -1
  106. package/lib/mark/sankey.js.map +1 -1
  107. package/lib/runtime/layout.js +41 -10
  108. package/lib/runtime/layout.js.map +1 -1
  109. package/lib/runtime/mark.js +3 -2
  110. package/lib/runtime/mark.js.map +1 -1
  111. package/lib/runtime/render.js +1 -0
  112. package/lib/runtime/render.js.map +1 -1
  113. package/lib/shape/axis-breaks/axis-breaks.d.ts +3 -1
  114. package/lib/shape/axis-breaks/axis-breaks.js +51 -32
  115. package/lib/shape/axis-breaks/axis-breaks.js.map +1 -1
  116. package/lib/spec/interaction.d.ts +7 -0
  117. package/lib/utils/helper.js +17 -1
  118. package/lib/utils/helper.js.map +1 -1
  119. package/package.json +30 -9
  120. package/src/component/axis.ts +5 -0
  121. package/src/component/constant.ts +9 -0
  122. package/src/component/slider.ts +6 -1
  123. package/src/composition/view.ts +4 -1
  124. package/src/interaction/adaptiveFilter.ts +517 -0
  125. package/src/interaction/brushHighlight.ts +5 -0
  126. package/src/interaction/index.ts +1 -0
  127. package/src/interaction/legendFilter.ts +12 -0
  128. package/src/interaction/sliderFilter.ts +762 -72
  129. package/src/interaction/sliderWheel.ts +224 -0
  130. package/src/interaction/tooltip.ts +33 -8
  131. package/src/interaction/utils.ts +284 -0
  132. package/src/lib/core.ts +4 -0
  133. package/src/mark/beeswarm.ts +150 -0
  134. package/src/mark/index.ts +2 -0
  135. package/src/mark/sankey.ts +3 -0
  136. package/src/runtime/layout.ts +46 -3
  137. package/src/runtime/mark.ts +2 -1
  138. package/src/runtime/render.ts +1 -0
  139. package/src/shape/axis-breaks/axis-breaks.ts +72 -40
  140. package/src/spec/interaction.ts +7 -0
  141. package/src/utils/helper.ts +19 -1
@@ -0,0 +1,224 @@
1
+ import { CustomEvent, DisplayObject } 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
+ /**
11
+ * Get the real DOM canvas element from G2 container.
12
+ * This helper function provides better type safety than using 'as any' directly.
13
+ */
14
+ function getCanvasDOM(container: any): HTMLElement | null {
15
+ try {
16
+ const canvas = container.ownerDocument?.defaultView;
17
+ if (!canvas || typeof canvas.getContextService !== 'function') {
18
+ return null;
19
+ }
20
+ const dom = canvas.getContextService().getDomElement();
21
+ return dom instanceof HTMLElement ? dom : null;
22
+ } catch {
23
+ return null;
24
+ }
25
+ }
26
+
27
+ type SliderDirection = true | false | 'shift' | 'ctrl' | 'alt';
28
+
29
+ interface SliderWheelOptions {
30
+ className?: string;
31
+ setValue?: (component: any, values: [number, number]) => void;
32
+ minRange?: number; // Minimum range (0.000001-1), auto-clamped
33
+ wheelSensitivity?: number;
34
+ x?: SliderDirection;
35
+ y?: SliderDirection;
36
+ }
37
+
38
+ export function SliderWheel({
39
+ className = SLIDER_CLASS_NAME,
40
+ setValue = (component, values) => component.setValues(values),
41
+ minRange = 0.01,
42
+ wheelSensitivity = 0.05,
43
+ x = true,
44
+ y = true,
45
+ }: SliderWheelOptions = {}) {
46
+ return (context) => {
47
+ const { container, view } = context;
48
+ const { coordinate } = view;
49
+ const transposed = isTranspose(coordinate);
50
+
51
+ // Get the real DOM canvas element to attach wheel listener
52
+ const canvasDOM = getCanvasDOM(container);
53
+ const safeMinRange = Math.max(0.000001, Math.min(1, minRange));
54
+
55
+ const sliders = container.getElementsByClassName(className);
56
+ if (!sliders.length) return () => {};
57
+
58
+ const isModifierKeyActive = (
59
+ config: SliderDirection,
60
+ event: WheelEvent,
61
+ ): boolean => {
62
+ if (config === true) return true;
63
+ if (config === false) return false;
64
+ if (config === 'shift')
65
+ return event.shiftKey && !event.ctrlKey && !event.altKey;
66
+ if (config === 'ctrl')
67
+ return event.ctrlKey && !event.shiftKey && !event.altKey;
68
+ if (config === 'alt')
69
+ return event.altKey && !event.shiftKey && !event.ctrlKey;
70
+ return false;
71
+ };
72
+
73
+ const triggerSliderValueChange = (
74
+ slider: any,
75
+ values: [number, number],
76
+ ) => {
77
+ setValue(slider, values);
78
+ slider.dispatchEvent(
79
+ new CustomEvent('valuechange', {
80
+ detail: { value: values },
81
+ nativeEvent: true,
82
+ }),
83
+ );
84
+ };
85
+
86
+ /**
87
+ * Calculate zoom center based on mouse position relative to slider track.
88
+ * @param mousePos - Mouse position (X or Y) relative to canvas
89
+ * @param sliderPos - Slider position (X or Y)
90
+ * @param trackLength - Length of slider track
91
+ * @param v0 - Current selection start value [0-1]
92
+ * @param v1 - Current selection end value [0-1]
93
+ * @returns Normalized center position [0-1] clamped to current selection
94
+ */
95
+ const calculateZoomCenter = (
96
+ mousePos: number,
97
+ sliderPos: number,
98
+ trackLength: number,
99
+ v0: number,
100
+ v1: number,
101
+ ): number => {
102
+ const relativePos = mousePos - sliderPos;
103
+ const normalizedPosition = relativePos / trackLength;
104
+ // Clamp to [0, 1] range first, then to current selection range
105
+ const clamped = Math.max(0, Math.min(1, normalizedPosition));
106
+ return Math.max(v0, Math.min(v1, clamped));
107
+ };
108
+
109
+ const handleWheel = (event: WheelEvent) => {
110
+ // Early return if canvas DOM is not available
111
+ if (!canvasDOM) return;
112
+
113
+ // Check if the event target is within our canvas container
114
+ const target = event.target as HTMLElement;
115
+ if (!canvasDOM.contains(target)) {
116
+ return;
117
+ }
118
+
119
+ // Get mouse position relative to canvas
120
+ const canvasRect = canvasDOM.getBoundingClientRect();
121
+ const mouseX = event.clientX - canvasRect.left;
122
+ const mouseY = event.clientY - canvasRect.top;
123
+
124
+ // Find all sliders that should respond to this event
125
+ const activeSliders: DisplayObject[] = [];
126
+
127
+ for (const slider of sliders) {
128
+ const { values, orientation } = (slider as any).attributes;
129
+ if (!values) continue;
130
+
131
+ const isHorizontal = orientation === 'horizontal';
132
+ const actualAxis = transposed
133
+ ? isHorizontal
134
+ ? 'y'
135
+ : 'x'
136
+ : isHorizontal
137
+ ? 'x'
138
+ : 'y';
139
+ const axisConfig = actualAxis === 'x' ? x : y;
140
+
141
+ if (isModifierKeyActive(axisConfig, event)) {
142
+ activeSliders.push(slider);
143
+ }
144
+ }
145
+
146
+ // If no slider should handle this event, let it propagate
147
+ if (activeSliders.length === 0) {
148
+ return;
149
+ }
150
+
151
+ // Prevent page scroll since we have active sliders
152
+ event.preventDefault();
153
+ event.stopPropagation();
154
+
155
+ // Process all active sliders
156
+ for (const slider of activeSliders) {
157
+ const {
158
+ values,
159
+ orientation,
160
+ x: sliderX,
161
+ y: sliderY,
162
+ trackLength,
163
+ } = (slider as any).attributes;
164
+ const [v0, v1] = values;
165
+ const range = v1 - v0;
166
+ const isHorizontal = orientation === 'horizontal';
167
+
168
+ // Calculate zoom center based on mouse position relative to slider
169
+ const center = isHorizontal
170
+ ? calculateZoomCenter(mouseX, sliderX, trackLength, v0, v1)
171
+ : calculateZoomCenter(mouseY, sliderY, trackLength, v0, v1);
172
+
173
+ const adaptiveSensitivity =
174
+ wheelSensitivity * calculateSensitivityMultiplier(range);
175
+
176
+ const delta = event.deltaY * adaptiveSensitivity;
177
+ const zoomFactor = 1 + delta;
178
+ const newRange = Math.max(
179
+ safeMinRange,
180
+ Math.min(1, range * zoomFactor),
181
+ );
182
+
183
+ // Calculate new range boundaries based on mouse position
184
+ // The zoom should maintain the ratio of distances from center to edges
185
+ const leftRatio = (center - v0) / range;
186
+ const rightRatio = (v1 - center) / range;
187
+
188
+ let newV0 = center - newRange * leftRatio;
189
+ let newV1 = center + newRange * rightRatio;
190
+
191
+ // Handle boundary conditions while trying to maintain mouse position as center
192
+ if (newV0 < 0) {
193
+ newV0 = 0;
194
+ newV1 = Math.min(1, newRange);
195
+ } else if (newV1 > 1) {
196
+ newV1 = 1;
197
+ newV0 = Math.max(0, 1 - newRange);
198
+ }
199
+
200
+ triggerSliderValueChange(slider, [newV0, newV1]);
201
+ }
202
+ };
203
+
204
+ // Listen on the real DOM canvas element with passive: false AND capture: true
205
+ // Capture phase ensures we intercept BEFORE @antv/g-canvas's passive listener
206
+ // This is the only way to preventDefault() before the passive listener receives it
207
+ if (canvasDOM) {
208
+ canvasDOM.addEventListener('wheel', handleWheel, {
209
+ passive: false,
210
+ capture: true,
211
+ });
212
+ }
213
+
214
+ return () => {
215
+ if (canvasDOM) {
216
+ canvasDOM.removeEventListener('wheel', handleWheel, { capture: true });
217
+ }
218
+ };
219
+ };
220
+ }
221
+
222
+ SliderWheel.props = {
223
+ reapplyWhenUpdate: true,
224
+ };
@@ -26,6 +26,8 @@ import {
26
26
  maybeRoot,
27
27
  } from './utils';
28
28
 
29
+ const LOCKED_SYMBOL = 'tooltipLocked';
30
+
29
31
  function getContainer(
30
32
  group: IElement,
31
33
  mount?: string | HTMLElement,
@@ -144,7 +146,10 @@ function showTooltip({
144
146
  tooltipElement.update({
145
147
  x,
146
148
  y,
147
- data: items,
149
+ data: items.map((item) => ({
150
+ ...item,
151
+ value: !item.value && item.value !== 0 ? '' : item.value,
152
+ })),
148
153
  title,
149
154
  position,
150
155
  enterable,
@@ -942,6 +947,7 @@ export function seriesTooltip(
942
947
  preserve = false,
943
948
  style: _style = {},
944
949
  css = {},
950
+ clickLock = false,
945
951
  ...rest
946
952
  }: Record<string, any>,
947
953
  ) {
@@ -959,6 +965,7 @@ export function seriesTooltip(
959
965
  } = coordinate.getOptions();
960
966
  const update = throttle(
961
967
  (event) => {
968
+ if (clickLock && root.getAttribute(LOCKED_SYMBOL)) return;
962
969
  const mouse = mousePosition(root, event);
963
970
  if (!mouse) return;
964
971
  const bbox = bboxOf(root);
@@ -1088,6 +1095,7 @@ export function seriesTooltip(
1088
1095
  ) as (...args: any[]) => void;
1089
1096
 
1090
1097
  const hide = (event: MouseEvent) => {
1098
+ if (clickLock && root.getAttribute(LOCKED_SYMBOL)) return;
1091
1099
  hideTooltip({ root, single, emitter, event });
1092
1100
  };
1093
1101
 
@@ -1130,9 +1138,16 @@ export function seriesTooltip(
1130
1138
  addEventListeners();
1131
1139
  };
1132
1140
 
1141
+ const pointerdown = (e) => {
1142
+ if (clickLock) {
1143
+ root.setAttribute(LOCKED_SYMBOL, !root.getAttribute(LOCKED_SYMBOL));
1144
+ }
1145
+ update(e);
1146
+ };
1147
+
1133
1148
  const addEventListeners = () => {
1134
1149
  if (!disableNative) {
1135
- root.addEventListener('pointerdown', update);
1150
+ root.addEventListener('pointerdown', pointerdown);
1136
1151
  root.addEventListener('pointerenter', update);
1137
1152
  root.addEventListener('pointermove', update);
1138
1153
  // Only emit pointerleave event when the pointer is not in the root area.
@@ -1143,7 +1158,7 @@ export function seriesTooltip(
1143
1158
 
1144
1159
  const removeEventListeners = () => {
1145
1160
  if (!disableNative) {
1146
- root.removeEventListener('pointerdown', update);
1161
+ root.removeEventListener('pointerdown', pointerdown);
1147
1162
  root.removeEventListener('pointerenter', update);
1148
1163
  root.removeEventListener('pointermove', update);
1149
1164
  root.removeEventListener('pointerleave', pointerleave);
@@ -1204,12 +1219,14 @@ export function tooltip(
1204
1219
  disableNative = false,
1205
1220
  preserve = false,
1206
1221
  css = {},
1222
+ clickLock = false,
1207
1223
  }: Record<string, any>,
1208
1224
  ) {
1209
1225
  const elements = elementsof(root);
1210
1226
  const keyGroup = group(elements, groupKey);
1211
1227
  const pointermove = throttle(
1212
1228
  (event) => {
1229
+ if (clickLock && root.getAttribute(LOCKED_SYMBOL)) return;
1213
1230
  const element = findSingleElement({
1214
1231
  root,
1215
1232
  event,
@@ -1281,9 +1298,16 @@ export function tooltip(
1281
1298
  hideTooltip({ root, single, emitter, event });
1282
1299
  };
1283
1300
 
1301
+ const pointerdown = (e) => {
1302
+ if (clickLock) {
1303
+ root.setAttribute(LOCKED_SYMBOL, !root.getAttribute(LOCKED_SYMBOL));
1304
+ }
1305
+ pointermove(e);
1306
+ };
1307
+
1284
1308
  const addEventListeners = () => {
1285
1309
  if (!disableNative) {
1286
- root.addEventListener('pointerdown', pointermove);
1310
+ root.addEventListener('pointerdown', pointerdown);
1287
1311
  root.addEventListener('pointermove', pointermove);
1288
1312
  // Only emit pointerleave event when the pointer is not in the root area.
1289
1313
  // !!!DO NOT USE pointerout event, it will emit when the pointer is in the child area.
@@ -1294,7 +1318,7 @@ export function tooltip(
1294
1318
 
1295
1319
  const removeEventListeners = () => {
1296
1320
  if (!disableNative) {
1297
- root.removeEventListener('pointerdown', pointermove);
1321
+ root.removeEventListener('pointerdown', pointerdown);
1298
1322
  root.removeEventListener('pointermove', pointermove);
1299
1323
  root.removeEventListener('pointerleave', pointerleave);
1300
1324
  root.removeEventListener('pointerup', pointerleave);
@@ -1373,7 +1397,7 @@ export function Tooltip(options) {
1373
1397
  const plotArea = selectPlotArea(container);
1374
1398
  const isSeries = maybeValue(series, defaultSeries);
1375
1399
  const crosshairsSetting = maybeValue(crosshairs, defaultShowCrosshairs);
1376
-
1400
+ if (rest.clickLock && !facet) plotArea.setAttribute(LOCKED_SYMBOL, false);
1377
1401
  // For non-facet and series tooltip.
1378
1402
  if (isSeries && hasSeries(markState) && !facet) {
1379
1403
  return seriesTooltip(plotArea, {
@@ -1406,11 +1430,12 @@ export function Tooltip(options) {
1406
1430
  const startX = bbox.min[0];
1407
1431
  const startY = bbox.min[1];
1408
1432
  Object.assign(scale, { facet: true });
1409
-
1433
+ const root = plotArea.parentNode.parentNode as DisplayObject;
1434
+ if (rest.clickLock) root.setAttribute(LOCKED_SYMBOL, false);
1410
1435
  // @todo Nested structure rather than flat structure for facet?
1411
1436
  // Add listener to the root area.
1412
1437
  // @ts-ignore
1413
- return seriesTooltip(plotArea.parentNode.parentNode, {
1438
+ return seriesTooltip(root, {
1414
1439
  ...rest,
1415
1440
  theme,
1416
1441
  elements: () => elements,
@@ -699,3 +699,287 @@ 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
+ }
725
+
726
+ /**
727
+ * Check if a value is considered "falsy" for configuration purposes.
728
+ * Returns true for false, null, or undefined values.
729
+ * Uses type predicate for better type safety.
730
+ *
731
+ * @param value The value to check
732
+ * @returns true if the value is falsy (false, null, undefined)
733
+ */
734
+ export function isFalsyValue(
735
+ value: unknown,
736
+ ): value is false | null | undefined {
737
+ return value === false || value === null || value === undefined;
738
+ }
739
+
740
+ /**
741
+ * Extract channel data with preserved X-Y relationships from all marks in a view.
742
+ * Supports multi-mark scenarios, bin transforms, and array-encoded Y values.
743
+ *
744
+ * @param view The view object containing markState
745
+ * @returns Object containing flattened values for backward compatibility and structured mark data
746
+ */
747
+ export function extractChannelValues(view: G2ViewDescriptor): {
748
+ xChannelValues: unknown[];
749
+ yChannelValues: unknown[];
750
+ markDataPairs: Array<{
751
+ markKey: string;
752
+ channelData: { [key: string]: unknown[] };
753
+ }>;
754
+ } {
755
+ const allXChannelValues: unknown[][] = [];
756
+ const allYChannelValues: unknown[][] = [];
757
+ const markDataPairs: Array<{
758
+ markKey: string;
759
+ channelData: { [key: string]: unknown[] };
760
+ }> = [];
761
+ const marks = view.markState;
762
+
763
+ if (marks) {
764
+ for (const [mark, state] of marks.entries()) {
765
+ if (state?.channels) {
766
+ const channelData: { [key: string]: any[] } = {};
767
+
768
+ // Process each channel for the current mark
769
+ for (const channel of state.channels) {
770
+ if (channel?.name === 'x' && channel.values?.length > 0) {
771
+ // Collect X values (supports bin transforms with multiple values)
772
+ let xValues: unknown[] = [];
773
+ for (const valueItem of channel.values) {
774
+ if (valueItem?.value) {
775
+ xValues = xValues.concat(valueItem.value);
776
+ allXChannelValues.push(valueItem.value);
777
+ }
778
+ }
779
+ channelData['x'] = xValues;
780
+ } else if (
781
+ channel &&
782
+ (channel.name === 'y' || channel.name.startsWith('y')) && // Support y, y1, y2, y3, etc.
783
+ channel.values?.length > 0
784
+ ) {
785
+ const channelName = channel.name;
786
+ const channelValues: unknown[] = [];
787
+
788
+ // Handle Y and Y1+ channels for multi-Y marks (e.g., area charts, candlestick charts)
789
+ for (const valueItem of channel.values) {
790
+ if (valueItem?.value) {
791
+ const values = valueItem.value;
792
+ // Preserve G2's internal structure for array-encoded data
793
+ // Area charts: [[low1,low2,low3], [high1,high2,high3]]
794
+ // Line charts: [[value1,value2,value3]]
795
+ channelValues.push(values);
796
+ if (channelName === 'y' || channelName === 'y1') {
797
+ // For global domain calculation, flatten only for allYChannelValues
798
+ if (Array.isArray(values)) {
799
+ allYChannelValues.push(values.flat());
800
+ } else {
801
+ allYChannelValues.push([values]);
802
+ }
803
+ }
804
+ }
805
+ }
806
+
807
+ // Store all Y-related channels
808
+ channelData[channelName] = channelValues;
809
+ }
810
+ }
811
+
812
+ // Store mark data with preserved X-Y relationships
813
+ const xValues = channelData['x'] || [];
814
+ const yValues = channelData['y'] || [];
815
+
816
+ if (xValues.length > 0 && yValues.length > 0) {
817
+ markDataPairs.push({
818
+ markKey: mark.key || `mark_${markDataPairs.length}`,
819
+ channelData,
820
+ });
821
+ }
822
+ }
823
+ }
824
+ }
825
+
826
+ return {
827
+ xChannelValues: allXChannelValues.flat(),
828
+ yChannelValues: allYChannelValues.flat(),
829
+ markDataPairs,
830
+ };
831
+ }
832
+
833
+ export function hasIndependentXYScale(
834
+ channel1: string,
835
+ marks: readonly unknown[],
836
+ ): boolean {
837
+ return marks.some((mark) => {
838
+ const { scale: markScale } = mark as Record<string, unknown>;
839
+ const channelScale = (markScale as Record<string, unknown>)?.[channel1] as
840
+ | Record<string, unknown>
841
+ | undefined;
842
+ return !!channelScale?.independent;
843
+ });
844
+ }
845
+
846
+ /**
847
+ * Calculate multi-axis channel domains for slider filtering.
848
+ * When independent scales are detected, generates separate domains for each axis (x1, y1, x2, y2, etc.)
849
+ *
850
+ * @param view The view object containing markState
851
+ * @param initDomain Initial domain configuration
852
+ * @param scaleX X scale instance
853
+ * @param scaleY Y scale instance
854
+ * @param independentInfo Pre-computed independent scale information
855
+ * @returns Extended channelDomain object with multi-axis support
856
+ */
857
+ export function calculateMultiAxisChannelDomains(
858
+ view: G2ViewDescriptor,
859
+ initDomain: Record<string, unknown>,
860
+ scaleX: { getOptions(): { domain: unknown } },
861
+ scaleY: { getOptions(): { domain: unknown } },
862
+ independentInfo?: IndependentScaleInfo,
863
+ ): Record<string, unknown[]> {
864
+ const channelDomain: Record<string, unknown[]> = {
865
+ x:
866
+ (initDomain.x as unknown[]) ||
867
+ (scaleX.getOptions().domain as unknown[]) ||
868
+ [],
869
+ y:
870
+ (initDomain.y as unknown[]) ||
871
+ (scaleY.getOptions().domain as unknown[]) ||
872
+ [],
873
+ };
874
+
875
+ // Use pre-computed info if available, otherwise calculate
876
+ const info = independentInfo || calculateAllIndependentScaleInfo(view);
877
+ const { hasIndependentX, hasIndependentY } = info;
878
+
879
+ if (hasIndependentX || hasIndependentY) {
880
+ let xIndex = 1;
881
+ let yIndex = 1;
882
+
883
+ // Iterate through marks to collect independent scale domains
884
+ for (const [mark, state] of view.markState.entries()) {
885
+ if (state?.channels) {
886
+ // Process independent X scales
887
+ if (hasIndependentX) {
888
+ const xChannel = state.channels.find((ch) => ch.name === 'x');
889
+ if (mark?.scale?.x?.independent) {
890
+ const xKey = `x${xIndex}`;
891
+ channelDomain[xKey] = xChannel.scale.domain;
892
+ xIndex++;
893
+ }
894
+ }
895
+
896
+ // Process independent Y scales
897
+ if (hasIndependentY) {
898
+ const yChannel = state.channels.find((ch) => ch.name === 'y');
899
+ if (mark?.scale?.y?.independent) {
900
+ const yKey = `y${yIndex}`;
901
+ channelDomain[yKey] = yChannel.scale.domain;
902
+ yIndex++;
903
+ }
904
+ }
905
+ }
906
+ }
907
+ }
908
+
909
+ return channelDomain;
910
+ }
911
+
912
+ /**
913
+ * Independent scale information cache interface
914
+ */
915
+ export interface IndependentScaleInfo {
916
+ hasIndependentX: boolean;
917
+ hasIndependentY: boolean;
918
+ marksWithSharedX: string[];
919
+ marksWithIndependentX: string[];
920
+ marksWithSharedY: string[];
921
+ marksWithIndependentY: string[];
922
+ markToXScaleMap: Map<string, string>;
923
+ markToYScaleMap: Map<string, string>;
924
+ }
925
+
926
+ /**
927
+ * Calculate all independent scale information in one pass
928
+ * This function performs a single traversal to compute all independent scale related information,
929
+ * avoiding repeated calculations throughout the codebase.
930
+ *
931
+ * @param view The view object containing markState
932
+ * @returns Complete independent scale information
933
+ */
934
+ export function calculateAllIndependentScaleInfo(
935
+ view: G2ViewDescriptor,
936
+ ): IndependentScaleInfo {
937
+ const marks = Array.from(view.markState.keys());
938
+ const hasIndependentX = hasIndependentXYScale('x', marks);
939
+ const hasIndependentY = hasIndependentXYScale('y', marks);
940
+
941
+ const marksWithSharedX: string[] = [];
942
+ const marksWithIndependentX: string[] = [];
943
+ const marksWithSharedY: string[] = [];
944
+ const marksWithIndependentY: string[] = [];
945
+
946
+ const markToXScaleMap = new Map<string, string>();
947
+ const markToYScaleMap = new Map<string, string>();
948
+
949
+ // Single traversal to compute all classifications and mappings
950
+ let xIndex = 1;
951
+ let yIndex = 1;
952
+
953
+ for (const [mark] of view.markState.entries()) {
954
+ const markKey = mark.key;
955
+
956
+ // X axis processing
957
+ if (mark?.scale?.x?.independent) {
958
+ marksWithIndependentX.push(markKey);
959
+ markToXScaleMap.set(markKey, `x${xIndex++}`);
960
+ } else {
961
+ marksWithSharedX.push(markKey);
962
+ markToXScaleMap.set(markKey, 'x');
963
+ }
964
+
965
+ // Y axis processing
966
+ if (mark?.scale?.y?.independent) {
967
+ marksWithIndependentY.push(markKey);
968
+ markToYScaleMap.set(markKey, `y${yIndex++}`);
969
+ } else {
970
+ marksWithSharedY.push(markKey);
971
+ markToYScaleMap.set(markKey, 'y');
972
+ }
973
+ }
974
+
975
+ return {
976
+ hasIndependentX,
977
+ hasIndependentY,
978
+ marksWithSharedX,
979
+ marksWithIndependentX,
980
+ marksWithSharedY,
981
+ marksWithIndependentY,
982
+ markToXScaleMap,
983
+ markToYScaleMap,
984
+ };
985
+ }
package/src/lib/core.ts CHANGED
@@ -34,6 +34,7 @@ import {
34
34
  Density,
35
35
  Heatmap,
36
36
  WordCloud as WordCloudGeometry,
37
+ Beeswarm,
37
38
  } from '../mark';
38
39
  import { Category10, Category20 } from '../palette';
39
40
  import {
@@ -100,6 +101,7 @@ import {
100
101
  BrushXFilter,
101
102
  BrushYFilter,
102
103
  SliderFilter,
104
+ SliderWheel,
103
105
  Poptip,
104
106
  ScrollbarFilter,
105
107
  TreemapDrillDown,
@@ -246,6 +248,7 @@ export function corelib() {
246
248
  'mark.density': Density,
247
249
  'mark.heatmap': Heatmap,
248
250
  'mark.wordCloud': WordCloudGeometry,
251
+ 'mark.beeswarm': Beeswarm,
249
252
  'palette.category10': Category10,
250
253
  'palette.category20': Category20,
251
254
  'scale.linear': ScaleLinear,
@@ -309,6 +312,7 @@ export function corelib() {
309
312
  'interaction.brushXFilter': BrushXFilter,
310
313
  'interaction.brushYFilter': BrushYFilter,
311
314
  'interaction.sliderFilter': SliderFilter,
315
+ 'interaction.sliderWheel': SliderWheel,
312
316
  'interaction.scrollbarFilter': ScrollbarFilter,
313
317
  'interaction.poptip': Poptip,
314
318
  'interaction.treemapDrillDown': TreemapDrillDown,