@antv/g2 5.4.1 → 5.4.3

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 (110) 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/legendCategory.js +7 -1
  7. package/esm/component/legendCategory.js.map +1 -1
  8. package/esm/component/legendContinuous.js +4 -0
  9. package/esm/component/legendContinuous.js.map +1 -1
  10. package/esm/component/slider.js +3 -1
  11. package/esm/component/slider.js.map +1 -1
  12. package/esm/composition/view.js +4 -4
  13. package/esm/composition/view.js.map +1 -1
  14. package/esm/index.d.ts +2 -0
  15. package/esm/interaction/adaptiveFilter.d.ts +147 -0
  16. package/esm/interaction/adaptiveFilter.js +305 -0
  17. package/esm/interaction/adaptiveFilter.js.map +1 -0
  18. package/esm/interaction/legendFilter.d.ts +2 -0
  19. package/esm/interaction/legendFilter.js +203 -6
  20. package/esm/interaction/legendFilter.js.map +1 -1
  21. package/esm/interaction/sliderFilter.d.ts +2 -1
  22. package/esm/interaction/sliderFilter.js +400 -42
  23. package/esm/interaction/sliderFilter.js.map +1 -1
  24. package/esm/interaction/sliderWheel.d.ts +0 -3
  25. package/esm/interaction/sliderWheel.js +91 -10
  26. package/esm/interaction/sliderWheel.js.map +1 -1
  27. package/esm/interaction/tooltip.d.ts +2 -2
  28. package/esm/interaction/tooltip.js +40 -10
  29. package/esm/interaction/tooltip.js.map +1 -1
  30. package/esm/interaction/utils.d.ts +69 -0
  31. package/esm/interaction/utils.js +201 -0
  32. package/esm/interaction/utils.js.map +1 -1
  33. package/esm/lib/core.d.ts +1 -0
  34. package/esm/lib/core.js +2 -1
  35. package/esm/lib/core.js.map +1 -1
  36. package/esm/lib/std.d.ts +1 -0
  37. package/esm/mark/beeswarm.d.ts +7 -0
  38. package/esm/mark/beeswarm.js +98 -0
  39. package/esm/mark/beeswarm.js.map +1 -0
  40. package/esm/mark/index.d.ts +2 -0
  41. package/esm/mark/index.js +1 -0
  42. package/esm/mark/index.js.map +1 -1
  43. package/esm/runtime/layout.js +35 -4
  44. package/esm/runtime/layout.js.map +1 -1
  45. package/esm/runtime/render.js +1 -0
  46. package/esm/runtime/render.js.map +1 -1
  47. package/esm/spec/interaction.d.ts +8 -0
  48. package/lib/component/constant.d.ts +1 -0
  49. package/lib/component/constant.js +9 -1
  50. package/lib/component/constant.js.map +1 -1
  51. package/lib/component/legendCategory.js +7 -1
  52. package/lib/component/legendCategory.js.map +1 -1
  53. package/lib/component/legendContinuous.js +4 -0
  54. package/lib/component/legendContinuous.js.map +1 -1
  55. package/lib/component/slider.js +3 -1
  56. package/lib/component/slider.js.map +1 -1
  57. package/lib/composition/view.js +4 -4
  58. package/lib/composition/view.js.map +1 -1
  59. package/lib/index.d.ts +2 -0
  60. package/lib/interaction/adaptiveFilter.d.ts +147 -0
  61. package/lib/interaction/adaptiveFilter.js +315 -0
  62. package/lib/interaction/adaptiveFilter.js.map +1 -0
  63. package/lib/interaction/legendFilter.d.ts +2 -0
  64. package/lib/interaction/legendFilter.js +205 -7
  65. package/lib/interaction/legendFilter.js.map +1 -1
  66. package/lib/interaction/sliderFilter.d.ts +2 -1
  67. package/lib/interaction/sliderFilter.js +399 -41
  68. package/lib/interaction/sliderFilter.js.map +1 -1
  69. package/lib/interaction/sliderWheel.d.ts +0 -3
  70. package/lib/interaction/sliderWheel.js +91 -10
  71. package/lib/interaction/sliderWheel.js.map +1 -1
  72. package/lib/interaction/tooltip.d.ts +2 -2
  73. package/lib/interaction/tooltip.js +40 -10
  74. package/lib/interaction/tooltip.js.map +1 -1
  75. package/lib/interaction/utils.d.ts +69 -0
  76. package/lib/interaction/utils.js +207 -1
  77. package/lib/interaction/utils.js.map +1 -1
  78. package/lib/lib/core.d.ts +1 -0
  79. package/lib/lib/core.js +1 -0
  80. package/lib/lib/core.js.map +1 -1
  81. package/lib/lib/std.d.ts +1 -0
  82. package/lib/mark/beeswarm.d.ts +7 -0
  83. package/lib/mark/beeswarm.js +102 -0
  84. package/lib/mark/beeswarm.js.map +1 -0
  85. package/lib/mark/index.d.ts +2 -0
  86. package/lib/mark/index.js +3 -1
  87. package/lib/mark/index.js.map +1 -1
  88. package/lib/runtime/layout.js +41 -10
  89. package/lib/runtime/layout.js.map +1 -1
  90. package/lib/runtime/render.js +1 -0
  91. package/lib/runtime/render.js.map +1 -1
  92. package/lib/spec/interaction.d.ts +8 -0
  93. package/package.json +3 -3
  94. package/src/component/constant.ts +9 -0
  95. package/src/component/legendCategory.ts +9 -0
  96. package/src/component/legendContinuous.ts +4 -0
  97. package/src/component/slider.ts +6 -1
  98. package/src/composition/view.ts +4 -1
  99. package/src/interaction/adaptiveFilter.ts +517 -0
  100. package/src/interaction/legendFilter.ts +222 -7
  101. package/src/interaction/sliderFilter.ts +762 -72
  102. package/src/interaction/sliderWheel.ts +109 -10
  103. package/src/interaction/tooltip.ts +43 -10
  104. package/src/interaction/utils.ts +261 -0
  105. package/src/lib/core.ts +2 -0
  106. package/src/mark/beeswarm.ts +150 -0
  107. package/src/mark/index.ts +2 -0
  108. package/src/runtime/layout.ts +46 -3
  109. package/src/runtime/render.ts +1 -0
  110. package/src/spec/interaction.ts +8 -0
@@ -0,0 +1,150 @@
1
+ import {
2
+ forceSimulation,
3
+ forceX,
4
+ forceY,
5
+ forceCollide,
6
+ } from '@antv/vendor/d3-force';
7
+ import { MarkComponent as MC, Vector2 } from '../runtime';
8
+ import { PointMark } from '../spec';
9
+ import {
10
+ PointBowtie,
11
+ PointCross,
12
+ PointDiamond,
13
+ PointHexagon,
14
+ PointHollowBowtie,
15
+ PointHollowDiamond,
16
+ PointHollowHexagon,
17
+ PointHollow,
18
+ PointHollowSquare,
19
+ PointHollowTriangle,
20
+ PointHollowTriangleDown,
21
+ PointHyphen,
22
+ PointLine,
23
+ PointTriangleDown,
24
+ PointPlus,
25
+ PointSquare,
26
+ PointShape,
27
+ PointTick,
28
+ PointTriangle,
29
+ PointCircle,
30
+ PointHollowCircle,
31
+ } from '../shape';
32
+ import { MaybeZeroX, MaybeZeroY, MaybeSize } from '../transform';
33
+ import {
34
+ baseGeometryChannels,
35
+ basePostInference,
36
+ basePreInference,
37
+ createBandOffset,
38
+ tooltip2d,
39
+ } from './utils';
40
+
41
+ const shape = {
42
+ hollow: PointHollow,
43
+ hollowDiamond: PointHollowDiamond,
44
+ hollowHexagon: PointHollowHexagon,
45
+ hollowSquare: PointHollowSquare,
46
+ hollowTriangleDown: PointHollowTriangleDown,
47
+ hollowTriangle: PointHollowTriangle,
48
+ hollowBowtie: PointHollowBowtie,
49
+ hollowCircle: PointHollowCircle,
50
+ point: PointShape,
51
+ plus: PointPlus,
52
+ diamond: PointDiamond,
53
+ square: PointSquare,
54
+ triangle: PointTriangle,
55
+ hexagon: PointHexagon,
56
+ cross: PointCross,
57
+ bowtie: PointBowtie,
58
+ hyphen: PointHyphen,
59
+ line: PointLine,
60
+ tick: PointTick,
61
+ triangleDown: PointTriangleDown,
62
+ circle: PointCircle,
63
+ };
64
+
65
+ export type BeeswarmOptions = Omit<PointMark, 'type'>;
66
+
67
+ const DEFAULT_RADIUS = 4;
68
+ const ITERATIONS = 200;
69
+
70
+ /**
71
+ * 蜂群图 (Beeswarm)
72
+ */
73
+ export const Beeswarm: MC<BeeswarmOptions> = (options) => {
74
+ return (index, scale, value, coordinate) => {
75
+ const { x: X, y: Y, size: S } = value;
76
+
77
+ if (!X.length || !Y.length) return [index, Y.map(() => [[]])];
78
+ const [width, height] = coordinate.getSize();
79
+ const offset = createBandOffset(scale, value, options);
80
+
81
+ const nodes = Array.from(index, (i) => {
82
+ const x0 = +X[i] * width;
83
+ const y0 = +Y[i] * height;
84
+ const r = +S[i] || DEFAULT_RADIUS;
85
+ return { i, x: x0, y: y0, r };
86
+ });
87
+
88
+ // 2. forceSimulation
89
+ const sim = forceSimulation(nodes)
90
+ .stop()
91
+ .force(
92
+ 'collide',
93
+ forceCollide<any>()
94
+ .radius((d) => d.r + 1)
95
+ .strength(1),
96
+ );
97
+
98
+ // 主轴吸附
99
+ sim.force('x', forceX<any>((d) => d.x).strength(0.5));
100
+ sim.force('y', forceY<any>((d) => d.y).strength(5));
101
+
102
+ for (let i = 0; i < ITERATIONS; i++) sim.tick();
103
+ sim.stop();
104
+
105
+ const xy = (i: number): Vector2 => {
106
+ const n = nodes.find((d) => d.i === i)!;
107
+ return [n.x / width, n.y / height];
108
+ };
109
+
110
+ const P = S
111
+ ? Array.from(index, (i) => {
112
+ const [cx, cy] = xy(i);
113
+ const r = +S[i];
114
+ const a = r / width;
115
+ const b = r / height;
116
+ const p1: Vector2 = [cx - a, cy - b];
117
+ const p2: Vector2 = [cx + a, cy + b];
118
+ return [
119
+ coordinate.map(offset(p1, i)),
120
+ coordinate.map(offset(p2, i)),
121
+ ] as Vector2[];
122
+ })
123
+ : Array.from(
124
+ index,
125
+ (i) => [coordinate.map(offset(xy(i), i))] as Vector2[],
126
+ );
127
+
128
+ return [index, P];
129
+ };
130
+ };
131
+
132
+ Beeswarm.props = {
133
+ defaultShape: 'point',
134
+ defaultLabelShape: 'label',
135
+ composite: false,
136
+ shape,
137
+ channels: [
138
+ ...baseGeometryChannels({ shapes: Object.keys(shape) }),
139
+ { name: 'x', required: true },
140
+ { name: 'y', required: true },
141
+ { name: 'series', scale: 'band' },
142
+ { name: 'size', quantitative: 'sqrt' },
143
+ ],
144
+ preInference: [
145
+ ...basePreInference(),
146
+ { type: MaybeZeroX },
147
+ { type: MaybeZeroY },
148
+ ],
149
+ postInference: [...basePostInference(), { type: MaybeSize }, ...tooltip2d()],
150
+ };
package/src/mark/index.ts CHANGED
@@ -30,6 +30,7 @@ export { Gauge } from './gauge';
30
30
  export { Density } from './density';
31
31
  export { Heatmap } from './heatmap';
32
32
  export { Liquid } from './liquid';
33
+ export { Beeswarm } from './beeswarm';
33
34
 
34
35
  export type { IntervalOptions } from './interval';
35
36
  export type { RectOptions } from './rect';
@@ -61,3 +62,4 @@ export type { GaugeOptions } from './gauge';
61
62
  export type { DensityOptions } from './density';
62
63
  export type { HeatmapOptions } from './heatmap';
63
64
  export type { LiquidOptions } from './liquid';
65
+ export type { BeeswarmOptions } from './beeswarm';
@@ -7,7 +7,8 @@ import {
7
7
  min,
8
8
  sum,
9
9
  } from '@antv/vendor/d3-array';
10
- import { deepMix } from '@antv/util';
10
+ import { measureTextWidth } from '@antv/component';
11
+ import { deepMix, head, last, get } from '@antv/util';
11
12
  import { isParallel, isPolar, isRadar, radiusOf } from '../utils/coordinate';
12
13
  import { capitalizeFirst, defined } from '../utils/helper';
13
14
  import { divide } from '../utils/array';
@@ -35,6 +36,8 @@ import {
35
36
  isRadial as isRadarOptions,
36
37
  } from './coordinate';
37
38
 
39
+ const DEFAULT_MARGIN = 16;
40
+
38
41
  export function processAxisZ(components: G2GuideComponentOptions[]) {
39
42
  const axisX = components.find(({ type }) => type === 'axisX');
40
43
  const axisY = components.find(({ type }) => type === 'axisY');
@@ -104,6 +107,16 @@ export function computeLayout(
104
107
  paddingTop = padding,
105
108
  } = computeInset(components, options, theme, library);
106
109
 
110
+ const isDefaultLayoutLeft =
111
+ marginLeft === DEFAULT_MARGIN && paddingLeft === 'auto';
112
+ const isDefaultLayoutRight =
113
+ marginRight === DEFAULT_MARGIN && paddingRight === 'auto';
114
+ const isTranspose = get(options, 'coordinates', []).some(
115
+ (t) => t.type === 'transpose',
116
+ );
117
+ const axisX = components.find(({ type }) => type === 'axisX');
118
+ const { size, labelTransform } = axisX || {};
119
+
107
120
  const MIN_CONTENT_RATIO = 1 / 4;
108
121
 
109
122
  const maybeClamp = (viewWidth, paddingLeft, paddingRight, pl0, pr0) => {
@@ -140,8 +153,8 @@ export function computeLayout(
140
153
  );
141
154
  const { paddingLeft: pl0, paddingRight: pr0 } = horizontalPadding;
142
155
  const viewWidth = width - marginLeft - marginRight;
143
- const [pl, pr] = maybeClamp(viewWidth, paddingLeft, paddingRight, pl0, pr0);
144
- const iw = viewWidth - pl - pr;
156
+ let [pl, pr] = maybeClamp(viewWidth, paddingLeft, paddingRight, pl0, pr0);
157
+ let iw = viewWidth - pl - pr;
145
158
 
146
159
  // Compute paddingBottom and paddingTop based on innerWidth.
147
160
  const verticalPadding = computePadding(
@@ -158,6 +171,36 @@ export function computeLayout(
158
171
  const [pb, pt] = maybeClamp(viewHeight, paddingBottom, paddingTop, pb0, pt0);
159
172
  const ih = viewHeight - pb - pt;
160
173
 
174
+ // Adjust paddingLeft and paddingRight for axisX when they are 'auto' and not specified by user.
175
+ if (size && !isTranspose && !labelTransform) {
176
+ const { fontSize = 12, fontFamily = 'sans-serif', scales = [] } = axisX;
177
+ const domain = scales?.[0]?.domain ?? [];
178
+ if (!domain.length) return;
179
+
180
+ const adjustSide = (
181
+ side: 'left' | 'right',
182
+ labelText: string,
183
+ margin: number,
184
+ padding: number,
185
+ ) => {
186
+ const labelWidth = measureTextWidth(labelText, { fontSize, fontFamily });
187
+ const diff = labelWidth / 2 - margin - padding;
188
+ if (diff > 0) {
189
+ iw -= diff;
190
+ if (side === 'left') pl += labelWidth / 2 - margin;
191
+ else pr += labelWidth / 2 - margin;
192
+ }
193
+ };
194
+
195
+ if (isDefaultLayoutLeft) {
196
+ adjustSide('left', head(domain), marginLeft, pl);
197
+ }
198
+
199
+ if (isDefaultLayoutRight) {
200
+ adjustSide('right', last(domain), marginRight, pr);
201
+ }
202
+ }
203
+
161
204
  return {
162
205
  width,
163
206
  height,
@@ -147,6 +147,7 @@ export function renderToMountedElement<T extends G2ViewTree = G2ViewTree>(
147
147
  const selection = select(group);
148
148
  context.group = group;
149
149
  context.emitter = emitter;
150
+ context.externals = {};
150
151
  context.canvas =
151
152
  context.canvas || (group?.ownerDocument?.defaultView as GCanvas);
152
153
 
@@ -167,9 +167,16 @@ export type ChartIndexInteraction = {
167
167
 
168
168
  export type SliderFilterInteraction = {
169
169
  type?: 'sliderFilter';
170
+ initDomain?: Record<string, any>;
171
+ className?: string;
172
+ prefix?: string;
173
+ setValue?: (component: any, values: any) => void;
174
+ hasState?: boolean;
170
175
  wait?: number;
171
176
  leading?: boolean;
172
177
  trailing?: boolean;
178
+ adaptiveMode?: 'filter' | false | null;
179
+ getInitValues?: (slider: any) => any;
173
180
  };
174
181
 
175
182
  export type TooltipInteraction = {
@@ -182,6 +189,7 @@ export type TooltipInteraction = {
182
189
  marker?: boolean;
183
190
  groupName?: boolean;
184
191
  disableNative?: boolean;
192
+ disableAutoHide?: boolean;
185
193
  offset?: [number, number];
186
194
  position?: TooltipStyleProps['position'];
187
195
  bounding?: BBox;