@antv/g2 5.4.1 → 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 (98) hide show
  1. package/dist/g2.lite.min.js +10 -10
  2. package/dist/g2.min.js +10 -10
  3. package/esm/component/constant.d.ts +1 -0
  4. package/esm/component/constant.js +8 -0
  5. package/esm/component/constant.js.map +1 -1
  6. package/esm/component/slider.js +3 -1
  7. package/esm/component/slider.js.map +1 -1
  8. package/esm/composition/view.js +4 -4
  9. package/esm/composition/view.js.map +1 -1
  10. package/esm/index.d.ts +2 -0
  11. package/esm/interaction/adaptiveFilter.d.ts +147 -0
  12. package/esm/interaction/adaptiveFilter.js +305 -0
  13. package/esm/interaction/adaptiveFilter.js.map +1 -0
  14. package/esm/interaction/legendFilter.js +4 -0
  15. package/esm/interaction/legendFilter.js.map +1 -1
  16. package/esm/interaction/sliderFilter.d.ts +2 -1
  17. package/esm/interaction/sliderFilter.js +400 -42
  18. package/esm/interaction/sliderFilter.js.map +1 -1
  19. package/esm/interaction/sliderWheel.d.ts +0 -3
  20. package/esm/interaction/sliderWheel.js +91 -10
  21. package/esm/interaction/sliderWheel.js.map +1 -1
  22. package/esm/interaction/tooltip.d.ts +2 -2
  23. package/esm/interaction/tooltip.js +32 -8
  24. package/esm/interaction/tooltip.js.map +1 -1
  25. package/esm/interaction/utils.d.ts +69 -0
  26. package/esm/interaction/utils.js +201 -0
  27. package/esm/interaction/utils.js.map +1 -1
  28. package/esm/lib/core.d.ts +1 -0
  29. package/esm/lib/core.js +2 -1
  30. package/esm/lib/core.js.map +1 -1
  31. package/esm/lib/std.d.ts +1 -0
  32. package/esm/mark/beeswarm.d.ts +7 -0
  33. package/esm/mark/beeswarm.js +98 -0
  34. package/esm/mark/beeswarm.js.map +1 -0
  35. package/esm/mark/index.d.ts +2 -0
  36. package/esm/mark/index.js +1 -0
  37. package/esm/mark/index.js.map +1 -1
  38. package/esm/runtime/layout.js +35 -4
  39. package/esm/runtime/layout.js.map +1 -1
  40. package/esm/runtime/render.js +1 -0
  41. package/esm/runtime/render.js.map +1 -1
  42. package/esm/spec/interaction.d.ts +7 -0
  43. package/lib/component/constant.d.ts +1 -0
  44. package/lib/component/constant.js +9 -1
  45. package/lib/component/constant.js.map +1 -1
  46. package/lib/component/slider.js +3 -1
  47. package/lib/component/slider.js.map +1 -1
  48. package/lib/composition/view.js +4 -4
  49. package/lib/composition/view.js.map +1 -1
  50. package/lib/index.d.ts +2 -0
  51. package/lib/interaction/adaptiveFilter.d.ts +147 -0
  52. package/lib/interaction/adaptiveFilter.js +315 -0
  53. package/lib/interaction/adaptiveFilter.js.map +1 -0
  54. package/lib/interaction/legendFilter.js +4 -0
  55. package/lib/interaction/legendFilter.js.map +1 -1
  56. package/lib/interaction/sliderFilter.d.ts +2 -1
  57. package/lib/interaction/sliderFilter.js +399 -41
  58. package/lib/interaction/sliderFilter.js.map +1 -1
  59. package/lib/interaction/sliderWheel.d.ts +0 -3
  60. package/lib/interaction/sliderWheel.js +91 -10
  61. package/lib/interaction/sliderWheel.js.map +1 -1
  62. package/lib/interaction/tooltip.d.ts +2 -2
  63. package/lib/interaction/tooltip.js +32 -8
  64. package/lib/interaction/tooltip.js.map +1 -1
  65. package/lib/interaction/utils.d.ts +69 -0
  66. package/lib/interaction/utils.js +207 -1
  67. package/lib/interaction/utils.js.map +1 -1
  68. package/lib/lib/core.d.ts +1 -0
  69. package/lib/lib/core.js +1 -0
  70. package/lib/lib/core.js.map +1 -1
  71. package/lib/lib/std.d.ts +1 -0
  72. package/lib/mark/beeswarm.d.ts +7 -0
  73. package/lib/mark/beeswarm.js +102 -0
  74. package/lib/mark/beeswarm.js.map +1 -0
  75. package/lib/mark/index.d.ts +2 -0
  76. package/lib/mark/index.js +3 -1
  77. package/lib/mark/index.js.map +1 -1
  78. package/lib/runtime/layout.js +41 -10
  79. package/lib/runtime/layout.js.map +1 -1
  80. package/lib/runtime/render.js +1 -0
  81. package/lib/runtime/render.js.map +1 -1
  82. package/lib/spec/interaction.d.ts +7 -0
  83. package/package.json +3 -3
  84. package/src/component/constant.ts +9 -0
  85. package/src/component/slider.ts +6 -1
  86. package/src/composition/view.ts +4 -1
  87. package/src/interaction/adaptiveFilter.ts +517 -0
  88. package/src/interaction/legendFilter.ts +4 -0
  89. package/src/interaction/sliderFilter.ts +762 -72
  90. package/src/interaction/sliderWheel.ts +109 -10
  91. package/src/interaction/tooltip.ts +33 -8
  92. package/src/interaction/utils.ts +261 -0
  93. package/src/lib/core.ts +2 -0
  94. package/src/mark/beeswarm.ts +150 -0
  95. package/src/mark/index.ts +2 -0
  96. package/src/runtime/layout.ts +46 -3
  97. package/src/runtime/render.ts +1 -0
  98. package/src/spec/interaction.ts +7 -0
@@ -1,4 +1,4 @@
1
- import { CustomEvent } from '@antv/g';
1
+ import { CustomEvent, DisplayObject } from '@antv/g';
2
2
  import { isTranspose } from '../utils/coordinate';
3
3
  import { SLIDER_CLASS_NAME } from './sliderFilter';
4
4
  import { calculateSensitivityMultiplier } from './utils';
@@ -7,6 +7,23 @@ import { calculateSensitivityMultiplier } from './utils';
7
7
  * SliderWheel interaction for mouse wheel/touchpad gestures on charts.
8
8
  */
9
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
+
10
27
  type SliderDirection = true | false | 'shift' | 'ctrl' | 'alt';
11
28
 
12
29
  interface SliderWheelOptions {
@@ -30,6 +47,9 @@ export function SliderWheel({
30
47
  const { container, view } = context;
31
48
  const { coordinate } = view;
32
49
  const transposed = isTranspose(coordinate);
50
+
51
+ // Get the real DOM canvas element to attach wheel listener
52
+ const canvasDOM = getCanvasDOM(container);
33
53
  const safeMinRange = Math.max(0.000001, Math.min(1, minRange));
34
54
 
35
55
  const sliders = container.getElementsByClassName(className);
@@ -63,9 +83,46 @@ export function SliderWheel({
63
83
  );
64
84
  };
65
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
+
66
109
  const handleWheel = (event: WheelEvent) => {
67
- event.preventDefault();
68
- event.stopPropagation();
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[] = [];
69
126
 
70
127
  for (const slider of sliders) {
71
128
  const { values, orientation } = (slider as any).attributes;
@@ -81,11 +138,38 @@ export function SliderWheel({
81
138
  : 'y';
82
139
  const axisConfig = actualAxis === 'x' ? x : y;
83
140
 
84
- if (!isModifierKeyActive(axisConfig, event)) continue;
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();
85
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;
86
164
  const [v0, v1] = values;
87
165
  const range = v1 - v0;
88
- const center = (v0 + v1) / 2;
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
+
89
173
  const adaptiveSensitivity =
90
174
  wheelSensitivity * calculateSensitivityMultiplier(range);
91
175
 
@@ -96,10 +180,15 @@ export function SliderWheel({
96
180
  Math.min(1, range * zoomFactor),
97
181
  );
98
182
 
99
- const halfRange = newRange / 2;
100
- let newV0 = center - halfRange;
101
- let newV1 = center + halfRange;
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;
102
190
 
191
+ // Handle boundary conditions while trying to maintain mouse position as center
103
192
  if (newV0 < 0) {
104
193
  newV0 = 0;
105
194
  newV1 = Math.min(1, newRange);
@@ -112,10 +201,20 @@ export function SliderWheel({
112
201
  }
113
202
  };
114
203
 
115
- container.addEventListener('wheel', handleWheel, { passive: false });
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
+ }
116
213
 
117
214
  return () => {
118
- container.removeEventListener('wheel', handleWheel);
215
+ if (canvasDOM) {
216
+ canvasDOM.removeEventListener('wheel', handleWheel, { capture: true });
217
+ }
119
218
  };
120
219
  };
121
220
  }
@@ -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,
@@ -722,3 +722,264 @@ export function calculateSensitivityMultiplier(range: number): number {
722
722
  // Clamp to reasonable range: 0.1x to 100x
723
723
  return Math.max(MIN_MULTIPLIER, Math.min(MAX_MULTIPLIER, multiplier));
724
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 {
@@ -247,6 +248,7 @@ export function corelib() {
247
248
  'mark.density': Density,
248
249
  'mark.heatmap': Heatmap,
249
250
  'mark.wordCloud': WordCloudGeometry,
251
+ 'mark.beeswarm': Beeswarm,
250
252
  'palette.category10': Category10,
251
253
  'palette.category20': Category20,
252
254
  'scale.linear': ScaleLinear,