@antv/g2 5.0.7 → 5.0.9

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 (189) hide show
  1. package/dist/g2-lite.min.js +4 -4
  2. package/dist/g2.min.js +4 -4
  3. package/esm/api/chart.d.ts +11 -2
  4. package/esm/api/chart.js +98 -46
  5. package/esm/api/chart.js.map +1 -1
  6. package/esm/api/mark/index.d.ts +3 -1
  7. package/esm/api/mark/index.js +2 -1
  8. package/esm/api/mark/index.js.map +1 -1
  9. package/esm/api/mark/mark.d.ts +7 -1
  10. package/esm/api/mark/mark.js +9 -0
  11. package/esm/api/mark/mark.js.map +1 -1
  12. package/esm/api/node.d.ts +1 -0
  13. package/esm/api/node.js +11 -1
  14. package/esm/api/node.js.map +1 -1
  15. package/esm/api/utils.d.ts +8 -0
  16. package/esm/api/utils.js +146 -20
  17. package/esm/api/utils.js.map +1 -1
  18. package/esm/component/axis.js +23 -5
  19. package/esm/component/axis.js.map +1 -1
  20. package/esm/component/utils.d.ts +1 -1
  21. package/esm/component/utils.js +17 -3
  22. package/esm/component/utils.js.map +1 -1
  23. package/esm/composition/mark.js +2 -1
  24. package/esm/composition/mark.js.map +1 -1
  25. package/esm/data/index.d.ts +2 -0
  26. package/esm/data/index.js +1 -0
  27. package/esm/data/index.js.map +1 -1
  28. package/esm/data/kde.d.ts +10 -0
  29. package/esm/data/kde.js +30 -0
  30. package/esm/data/kde.js.map +1 -0
  31. package/esm/interaction/brushHighlight.d.ts +3 -3
  32. package/esm/interaction/brushHighlight.js +18 -13
  33. package/esm/interaction/brushHighlight.js.map +1 -1
  34. package/esm/interaction/brushXHighlight.d.ts +1 -1
  35. package/esm/interaction/brushYHighlight.d.ts +1 -1
  36. package/esm/interaction/tooltip.js +5 -2
  37. package/esm/interaction/tooltip.js.map +1 -1
  38. package/esm/mark/density.d.ts +4 -0
  39. package/esm/mark/density.js +61 -0
  40. package/esm/mark/density.js.map +1 -0
  41. package/esm/mark/index.d.ts +2 -0
  42. package/esm/mark/index.js +1 -0
  43. package/esm/mark/index.js.map +1 -1
  44. package/esm/runtime/component.d.ts +4 -2
  45. package/esm/runtime/component.js +164 -12
  46. package/esm/runtime/component.js.map +1 -1
  47. package/esm/runtime/layout.d.ts +3 -3
  48. package/esm/runtime/layout.js +40 -21
  49. package/esm/runtime/layout.js.map +1 -1
  50. package/esm/runtime/plot.js +22 -16
  51. package/esm/runtime/plot.js.map +1 -1
  52. package/esm/runtime/scale.js +1 -1
  53. package/esm/runtime/scale.js.map +1 -1
  54. package/esm/shape/box/violin.d.ts +3 -0
  55. package/esm/shape/box/violin.js +85 -0
  56. package/esm/shape/box/violin.js.map +1 -0
  57. package/esm/shape/density/density.d.ts +8 -0
  58. package/esm/shape/density/density.js +50 -0
  59. package/esm/shape/density/density.js.map +1 -0
  60. package/esm/shape/index.d.ts +4 -0
  61. package/esm/shape/index.js +2 -0
  62. package/esm/shape/index.js.map +1 -1
  63. package/esm/spec/composition.d.ts +22 -19
  64. package/esm/spec/dataTransform.d.ts +34 -2
  65. package/esm/spec/mark.d.ts +9 -8
  66. package/esm/spec/transform.d.ts +1 -1
  67. package/esm/spec/utils.d.ts +1 -0
  68. package/esm/stdlib/index.js +5 -5
  69. package/esm/stdlib/index.js.map +1 -1
  70. package/esm/theme/academy.js +2 -1
  71. package/esm/theme/academy.js.map +1 -1
  72. package/esm/theme/classic.js +2 -2
  73. package/esm/theme/classic.js.map +1 -1
  74. package/esm/theme/classicDark.js +2 -2
  75. package/esm/theme/classicDark.js.map +1 -1
  76. package/esm/utils/scale.js +5 -1
  77. package/esm/utils/scale.js.map +1 -1
  78. package/esm/utils/size.js +7 -4
  79. package/esm/utils/size.js.map +1 -1
  80. package/lib/api/chart.d.ts +11 -2
  81. package/lib/api/chart.js +96 -44
  82. package/lib/api/chart.js.map +1 -1
  83. package/lib/api/mark/index.d.ts +3 -1
  84. package/lib/api/mark/index.js +1 -0
  85. package/lib/api/mark/index.js.map +1 -1
  86. package/lib/api/mark/mark.d.ts +7 -1
  87. package/lib/api/mark/mark.js +10 -1
  88. package/lib/api/mark/mark.js.map +1 -1
  89. package/lib/api/node.d.ts +1 -0
  90. package/lib/api/node.js +11 -1
  91. package/lib/api/node.js.map +1 -1
  92. package/lib/api/utils.d.ts +8 -0
  93. package/lib/api/utils.js +149 -21
  94. package/lib/api/utils.js.map +1 -1
  95. package/lib/component/axis.js +23 -5
  96. package/lib/component/axis.js.map +1 -1
  97. package/lib/component/utils.d.ts +1 -1
  98. package/lib/component/utils.js +17 -3
  99. package/lib/component/utils.js.map +1 -1
  100. package/lib/composition/mark.js +2 -1
  101. package/lib/composition/mark.js.map +1 -1
  102. package/lib/data/index.d.ts +2 -0
  103. package/lib/data/index.js +3 -1
  104. package/lib/data/index.js.map +1 -1
  105. package/lib/data/kde.d.ts +10 -0
  106. package/lib/data/kde.js +38 -0
  107. package/lib/data/kde.js.map +1 -0
  108. package/lib/interaction/brushHighlight.d.ts +3 -3
  109. package/lib/interaction/brushHighlight.js +18 -13
  110. package/lib/interaction/brushHighlight.js.map +1 -1
  111. package/lib/interaction/brushXHighlight.d.ts +1 -1
  112. package/lib/interaction/brushYHighlight.d.ts +1 -1
  113. package/lib/interaction/tooltip.js +5 -2
  114. package/lib/interaction/tooltip.js.map +1 -1
  115. package/lib/mark/density.d.ts +4 -0
  116. package/lib/mark/density.js +65 -0
  117. package/lib/mark/density.js.map +1 -0
  118. package/lib/mark/index.d.ts +2 -0
  119. package/lib/mark/index.js +3 -1
  120. package/lib/mark/index.js.map +1 -1
  121. package/lib/runtime/component.d.ts +4 -2
  122. package/lib/runtime/component.js +163 -10
  123. package/lib/runtime/component.js.map +1 -1
  124. package/lib/runtime/layout.d.ts +3 -3
  125. package/lib/runtime/layout.js +40 -21
  126. package/lib/runtime/layout.js.map +1 -1
  127. package/lib/runtime/plot.js +22 -16
  128. package/lib/runtime/plot.js.map +1 -1
  129. package/lib/runtime/scale.js +1 -1
  130. package/lib/runtime/scale.js.map +1 -1
  131. package/lib/shape/box/violin.d.ts +3 -0
  132. package/lib/shape/box/violin.js +89 -0
  133. package/lib/shape/box/violin.js.map +1 -0
  134. package/lib/shape/density/density.d.ts +8 -0
  135. package/lib/shape/density/density.js +54 -0
  136. package/lib/shape/density/density.js.map +1 -0
  137. package/lib/shape/index.d.ts +4 -0
  138. package/lib/shape/index.js +6 -1
  139. package/lib/shape/index.js.map +1 -1
  140. package/lib/spec/composition.d.ts +22 -19
  141. package/lib/spec/dataTransform.d.ts +34 -2
  142. package/lib/spec/mark.d.ts +9 -8
  143. package/lib/spec/transform.d.ts +1 -1
  144. package/lib/spec/utils.d.ts +1 -0
  145. package/lib/stdlib/index.js +2 -2
  146. package/lib/stdlib/index.js.map +1 -1
  147. package/lib/theme/academy.js +2 -1
  148. package/lib/theme/academy.js.map +1 -1
  149. package/lib/theme/classic.js +2 -2
  150. package/lib/theme/classic.js.map +1 -1
  151. package/lib/theme/classicDark.js +2 -2
  152. package/lib/theme/classicDark.js.map +1 -1
  153. package/lib/utils/scale.js +5 -1
  154. package/lib/utils/scale.js.map +1 -1
  155. package/lib/utils/size.js +7 -4
  156. package/lib/utils/size.js.map +1 -1
  157. package/package.json +4 -3
  158. package/src/api/chart.ts +111 -55
  159. package/src/api/mark/index.ts +3 -0
  160. package/src/api/mark/mark.ts +12 -0
  161. package/src/api/node.ts +10 -1
  162. package/src/api/utils.ts +139 -20
  163. package/src/component/axis.ts +35 -12
  164. package/src/component/utils.ts +9 -4
  165. package/src/composition/mark.ts +2 -0
  166. package/src/data/index.ts +2 -0
  167. package/src/data/kde.ts +53 -0
  168. package/src/interaction/brushHighlight.ts +16 -13
  169. package/src/interaction/tooltip.ts +5 -2
  170. package/src/mark/density.ts +81 -0
  171. package/src/mark/index.ts +2 -0
  172. package/src/runtime/component.ts +210 -12
  173. package/src/runtime/layout.ts +74 -19
  174. package/src/runtime/plot.ts +44 -21
  175. package/src/runtime/scale.ts +1 -1
  176. package/src/shape/box/violin.ts +119 -0
  177. package/src/shape/density/density.ts +52 -0
  178. package/src/shape/index.ts +4 -0
  179. package/src/spec/composition.ts +22 -19
  180. package/src/spec/dataTransform.ts +35 -0
  181. package/src/spec/mark.ts +10 -6
  182. package/src/spec/transform.ts +1 -0
  183. package/src/spec/utils.ts +2 -0
  184. package/src/stdlib/index.ts +8 -0
  185. package/src/theme/academy.ts +2 -1
  186. package/src/theme/classic.ts +2 -2
  187. package/src/theme/classicDark.ts +2 -2
  188. package/src/utils/scale.ts +5 -1
  189. package/src/utils/size.ts +10 -4
@@ -302,7 +302,7 @@ export function brush(
302
302
  };
303
303
 
304
304
  // Remove mask and reset states.
305
- const removeMask = () => {
305
+ const removeMask = (emit = true) => {
306
306
  if (mask) mask.remove();
307
307
  if (background) background.remove();
308
308
  start = null;
@@ -311,11 +311,11 @@ export function brush(
311
311
  creating = false;
312
312
  mask = null;
313
313
  background = null;
314
- brushended();
314
+ if (emit) brushended();
315
315
  };
316
316
 
317
317
  // Update mask and invoke brushended callback.
318
- const updateMask = (start, end) => {
318
+ const updateMask = (start, end, emit = true) => {
319
319
  const [x, y, x1, y1] = normalizeBounds(
320
320
  start[0],
321
321
  start[1],
@@ -326,7 +326,7 @@ export function brush(
326
326
  const [fx, fy, fx1, fy1] = brushRegion(x, y, x1, y1, extent);
327
327
  if (reverse) updateReverseMask(fx, fy, fx1, fy1);
328
328
  else updateNormalMask(fx, fy, fx1, fy1);
329
- brushed(fx, fy, fx1, fy1);
329
+ brushed(fx, fy, fx1, fy1, emit);
330
330
  return [fx, fy, fx1, fy1];
331
331
  };
332
332
 
@@ -470,17 +470,18 @@ export function brush(
470
470
 
471
471
  return {
472
472
  mask,
473
- move(x, y, x1, y1) {
473
+ move(x, y, x1, y1, emit = true) {
474
474
  if (!mask) initMask(x, y);
475
475
  start = [x, y];
476
476
  end = [x1, y1];
477
- updateMask([x, y], [x1, y1]);
477
+ updateMask([x, y], [x1, y1], emit);
478
478
  },
479
479
  remove() {
480
480
  if (mask) removeMask();
481
481
  },
482
482
  destroy() {
483
- removeMask();
483
+ // Do not emit brush:end event.
484
+ if (mask) removeMask(false);
484
485
  setCursor(root, 'default');
485
486
  root.removeEventListener('dragstart', dragstart);
486
487
  root.removeEventListener('drag', drag);
@@ -631,12 +632,14 @@ export function brushHighlight(
631
632
  emitter.emit('brush:end', { nativeEvent: true });
632
633
  handler();
633
634
  },
634
- brushed: (x, y, x1, y1) => {
635
+ brushed: (x, y, x1, y1, emit) => {
635
636
  const selection = selectionOf(x, y, x1, y1, scale, coordinate);
636
- emitter.emit('brush:highlight', {
637
- nativeEvent: true,
638
- data: { selection },
639
- });
637
+ if (emit) {
638
+ emitter.emit('brush:highlight', {
639
+ nativeEvent: true,
640
+ data: { selection },
641
+ });
642
+ }
640
643
  const handler = series ? seriesBrushed : brushed;
641
644
  handler(x, y, x1, y1);
642
645
  },
@@ -647,7 +650,7 @@ export function brushHighlight(
647
650
  if (nativeEvent) return;
648
651
  const { selection } = data;
649
652
  const [x, y, x1, y1] = pixelsOf(selection, scale, coordinate);
650
- brushHandler.move(x, y, x1, y1);
653
+ brushHandler.move(x, y, x1, y1, false);
651
654
  };
652
655
  emitter.on('brush:highlight', onHighlight);
653
656
 
@@ -75,9 +75,12 @@ function createTooltip(
75
75
  enterable,
76
76
  title: '',
77
77
  offset: [10, 10],
78
+ template: {
79
+ prefixCls: 'g2-',
80
+ },
78
81
  style: {
79
- '.tooltip': {},
80
- '.tooltip-title': {
82
+ '.g2-tooltip': {},
83
+ '.g2-tooltip-title': {
81
84
  overflow: 'hidden',
82
85
  'white-space': 'nowrap',
83
86
  'text-overflow': 'ellipsis',
@@ -0,0 +1,81 @@
1
+ import { Band } from '@antv/scale';
2
+ import { MarkComponent as MC, Vector2 } from '../runtime';
3
+ import { DensityMark } from '../spec';
4
+ import {
5
+ baseGeometryChannels,
6
+ basePostInference,
7
+ basePreInference,
8
+ tooltip1d,
9
+ } from './utils';
10
+
11
+ export type DensityOptions = Omit<DensityMark, 'type'>;
12
+
13
+ export const Density: MC<DensityOptions> = () => {
14
+ return (index, scale, value, coordinate) => {
15
+ const { x: X, series: S } = value;
16
+
17
+ const Yn = Object.entries(value)
18
+ .filter(([key]) => key.startsWith('y'))
19
+ .map(([, value]) => value);
20
+
21
+ const SZn = Object.entries(value)
22
+ .filter(([key]) => key.startsWith('size'))
23
+ .map(([, value]) => value);
24
+
25
+ // Because x and y channel is not strictly required in Line.props,
26
+ // it should throw error with empty x or y channels.
27
+ if (X === undefined || Yn === undefined || SZn === undefined) {
28
+ throw new Error('Missing encode for x or y or size channel.');
29
+ }
30
+
31
+ // Calc width for each box.
32
+ // The scales for x and series channels must be band scale.
33
+ const xScale = scale.x as Band;
34
+ const series = scale.series as Band;
35
+
36
+ const P = Array.from(index, (i) => {
37
+ const groupWidth = xScale.getBandWidth(xScale.invert(+X[i]));
38
+ const ratio = series ? series.getBandWidth(series.invert(+S?.[i])) : 1;
39
+ const width = groupWidth * ratio;
40
+ const offset = (+S?.[i] || 0) * groupWidth;
41
+
42
+ const x = +X[i] + offset + width / 2;
43
+
44
+ const PN = [
45
+ ...Yn.map((_, idx) => [x + +SZn[idx][i] / index.length, +Yn[idx][i]]), // right
46
+ ...Yn.map((_, idx) => [
47
+ x - +SZn[idx][i] / index.length,
48
+ +Yn[idx][i],
49
+ ]).reverse(), // left
50
+ ];
51
+
52
+ return PN.map((p) => coordinate.map(p)) as Vector2[];
53
+ });
54
+ return [index, P];
55
+ };
56
+ };
57
+
58
+ const shapes = ['density'];
59
+
60
+ Density.props = {
61
+ defaultShape: 'density',
62
+ defaultLabelShape: 'label',
63
+ composite: false,
64
+ channels: [
65
+ ...baseGeometryChannels({ shapes }),
66
+ { name: 'x', scale: 'band', required: true },
67
+ { name: 'y', required: true },
68
+ { name: 'size', required: true },
69
+ { name: 'series', scale: 'band' },
70
+ { name: 'size', required: true, scale: 'identity' },
71
+ ],
72
+ preInference: [
73
+ ...basePreInference(),
74
+ { type: 'maybeZeroY1' },
75
+ { type: 'maybeZeroX' },
76
+ ],
77
+ postInference: [...basePostInference(), ...tooltip1d()],
78
+ interaction: {
79
+ shareTooltip: true,
80
+ },
81
+ };
package/src/mark/index.ts CHANGED
@@ -26,6 +26,7 @@ export { ForceGraph } from './forceGraph';
26
26
  export { Tree } from './tree';
27
27
  export { WordCloud } from './wordCloud';
28
28
  export { Gauge } from './gauge';
29
+ export { Density } from './density';
29
30
 
30
31
  export type { IntervalOptions } from './interval';
31
32
  export type { RectOptions } from './rect';
@@ -53,3 +54,4 @@ export type { ForceGraphOptions } from './forceGraph';
53
54
  export type { TreeOptions } from './tree';
54
55
  export type { WordCloudOptions } from './wordCloud';
55
56
  export type { GaugeOptions } from './gauge';
57
+ export type { DensityOptions } from './density';
@@ -2,8 +2,10 @@
2
2
  * @see https://github.com/antvis/G2/discussions/4557
3
3
  */
4
4
  import { Coordinate } from '@antv/coord';
5
- import { isEqual } from '@antv/util';
6
- import { group } from 'd3-array';
5
+ import { deepMix, isEqual } from '@antv/util';
6
+ import { group, max, sum } from 'd3-array';
7
+ import { format } from 'd3-format';
8
+ import { DisplayObject, Text } from '@antv/g';
7
9
  import {
8
10
  getPolarOptions,
9
11
  getRadialOptions,
@@ -11,7 +13,7 @@ import {
11
13
  type RadialOptions,
12
14
  } from '../coordinate';
13
15
  import { combine } from '../utils/array';
14
- import { defined } from '../utils/helper';
16
+ import { capitalizeFirst, defined, subObject } from '../utils/helper';
15
17
  import { LEGEND_INFER_STRATEGIES } from '../component/constant';
16
18
  import {
17
19
  coordOf,
@@ -36,6 +38,8 @@ import {
36
38
  import {
37
39
  GuideComponent,
38
40
  GuideComponentComponent as GCC,
41
+ ScaleComponent,
42
+ Scale,
39
43
  } from './types/component';
40
44
  import {
41
45
  G2CoordinateOptions,
@@ -90,13 +94,13 @@ export function inferComponent(
90
94
  });
91
95
  }
92
96
 
93
- const inferedComponents = inferComponentsType(displayedScales, coordinates);
97
+ const inferredComponents = inferComponentsType(displayedScales, coordinates);
94
98
 
95
- inferedComponents.forEach(([type, relativeScales]) => {
99
+ inferredComponents.forEach(([type, relativeScales]) => {
96
100
  const { props } = createGuideComponent(type);
97
101
  const { defaultPosition, defaultOrientation, defaultSize, defaultOrder } =
98
102
  props;
99
- // @todo to be comfirm if the scale can be merged.
103
+ // @todo to be confirm if the scale can be merged.
100
104
  const scale: G2ScaleOptions = Object.assign({}, ...relativeScales);
101
105
  const { guide: guideOptions, field } = scale;
102
106
  // A scale may have multiple guides.
@@ -263,10 +267,10 @@ function inferComponentsType(
263
267
  scales: G2ScaleOptions[],
264
268
  coordinates: G2CoordinateOptions[],
265
269
  ): [string | GCC, G2ScaleOptions[]][] {
266
- const avaliableScales = scales.filter((scale) => isValidScale(scale));
270
+ const availableScales = scales.filter((scale) => isValidScale(scale));
267
271
  return [
268
- ...inferLegendComponentType(avaliableScales, coordinates),
269
- ...inferAxisComponentType(avaliableScales, coordinates),
272
+ ...inferLegendComponentType(availableScales, coordinates),
273
+ ...inferAxisComponentType(availableScales, coordinates),
270
274
  ];
271
275
  }
272
276
 
@@ -307,7 +311,7 @@ function inferAxisPositionAndOrientation(
307
311
  // todo, in current resolution, the radar chart is implement by parallel + polar coordinate.
308
312
  // implementation plan to be confirmed.
309
313
  // in current implementation, it must to add the first position encode to it's last.
310
- // so we won't render the last axis repeatly.
314
+ // so we won't render the last axis repeatably.
311
315
  if (type === 'axisRadar') {
312
316
  const positions = scales.filter((scale) =>
313
317
  scale.name.startsWith('position'),
@@ -396,7 +400,7 @@ function inferComponentPositionAndOrientation(
396
400
  return ordinalPositionAndOrientation;
397
401
  }
398
402
 
399
- function inferSrollableType(name: string, type: string, coordinates = []) {
403
+ function inferScrollableType(name: string, type: string, coordinates = []) {
400
404
  if (name === 'x') return isTranspose(coordinates) ? `${type}Y` : `${type}X`;
401
405
  if (name === 'y') return isTranspose(coordinates) ? `${type}X` : `${type}Y`;
402
406
  return null;
@@ -424,7 +428,7 @@ function inferScrollableComponents(
424
428
  scale: G2ScaleOptions,
425
429
  options: Record<string, any>,
426
430
  ) {
427
- const componentType = inferSrollableType(channelName, type, coordinates);
431
+ const componentType = inferScrollableType(channelName, type, coordinates);
428
432
  if (!options || !componentType) return;
429
433
 
430
434
  const { props } = createGuideComponent(componentType);
@@ -450,3 +454,197 @@ function inferScrollableComponents(
450
454
  })
451
455
  .filter((d) => !!d);
452
456
  }
457
+
458
+ // !!! Note Mutate component.size and component.style.
459
+ export function computeComponentSize(
460
+ component: G2GuideComponentOptions,
461
+ crossSize: number,
462
+ crossPadding: [number, number],
463
+ position: GCP,
464
+ theme: G2Theme,
465
+ library: G2Library,
466
+ ): G2GuideComponentOptions {
467
+ const [useScale] = useLibrary<G2ScaleOptions, ScaleComponent, Scale>(
468
+ 'scale',
469
+ library,
470
+ );
471
+
472
+ // Only compute and update size of axis component in padding area.
473
+ // @todo Legend, slider.
474
+ const { type } = component;
475
+ const paddingAreas = ['left', 'right', 'bottom', 'top'];
476
+ if (typeof type !== 'string' || !type.startsWith('axis')) return;
477
+ if (!paddingAreas.includes(position)) return;
478
+
479
+ // If padding is auto, use hide as the labelTransform by default
480
+ // to avoid overlap between labels.
481
+ component.transform = component.transform || [{ type: 'hide' }];
482
+
483
+ const { labelFormatter, scales, title, tickCount, tickMethod, tickFilter } =
484
+ component;
485
+ const isVertical = position === 'left' || position === 'right';
486
+
487
+ // Get styles to be applied.
488
+ const style = styleOf(component, position, theme);
489
+ const { tickLength = 0, labelSpacing = 0, titleSpacing = 0, ...rest } = style;
490
+
491
+ // Init scale, the tickCount of axis has higher priority than scale.
492
+ const [scaleOptions] = scales;
493
+ if (tickCount !== undefined) scaleOptions.tickCount = tickCount;
494
+ if (tickMethod !== undefined) scaleOptions.tickMethod = tickMethod;
495
+ const scale = useScale(scaleOptions);
496
+
497
+ // Get labels to be rendered.
498
+ const labels = labelsOf(scale, labelFormatter, tickFilter);
499
+ const labeStyle = subObject(rest, 'label');
500
+ const labelBBoxes = labels.map((d, i) => {
501
+ const normalizeStyle = Object.fromEntries(
502
+ Object.entries(labeStyle).map(([key, value]) => [
503
+ key,
504
+ typeof value === 'function' ? value(d, i) : value,
505
+ ]),
506
+ );
507
+ // Auto padding should ignore transform for horizontal axis.
508
+ if (!isVertical) normalizeStyle.transform = 'none';
509
+ return computeLabelSize(d, normalizeStyle);
510
+ });
511
+
512
+ const maxLabelWidth = max(labelBBoxes, (d) => d.width);
513
+ const paddingTick = tickLength + labelSpacing;
514
+
515
+ if (isVertical) {
516
+ component.size = maxLabelWidth + paddingTick;
517
+ } else {
518
+ // If the labels can't be placed horizontally,
519
+ // rotate 90 deg to display them.
520
+ if (overflowX(scale, labelBBoxes, crossSize, crossPadding, tickFilter)) {
521
+ component.size = maxLabelWidth + paddingTick;
522
+ component.style = {
523
+ ...component.style,
524
+ labelTransform: 'rotate(90)',
525
+ };
526
+ } else {
527
+ const maxLabelHeight = max(labelBBoxes, (d) => d.height);
528
+ component.size = maxLabelHeight + paddingTick;
529
+ }
530
+ }
531
+
532
+ // Cache boxes to avoid computed twice.
533
+ const I = labels.map((_, i) => i);
534
+ component.indexBBox = new Map(I.map((i) => [i, [labels[i], labelBBoxes[i]]]));
535
+
536
+ if (title === false || title === null || title === undefined) return;
537
+
538
+ // Get title to be rendered.
539
+ const titleStyle = subObject(rest, 'title');
540
+ const titleText = Array.isArray(title) ? title.join(',') : title;
541
+ const titleBBox = computeLabelSize(titleText, titleStyle);
542
+
543
+ if (isVertical) {
544
+ component.size += titleSpacing + titleBBox.width;
545
+ } else {
546
+ component.size += titleSpacing + titleBBox.height;
547
+ }
548
+ }
549
+
550
+ function styleOf(
551
+ axis: G2GuideComponentOptions,
552
+ position: GCP,
553
+ theme: G2Theme,
554
+ ): Record<string, any> {
555
+ const {
556
+ axis: baseStyle,
557
+ // @ts-ignore
558
+ [`axis${capitalizeFirst(position)}`]: positionStyle,
559
+ } = theme;
560
+ return deepMix({}, baseStyle, positionStyle, axis.style);
561
+ }
562
+
563
+ function ticksOf(scale: Scale, tickFilter: (d: any) => boolean): any[] {
564
+ const ticks = scale.getTicks ? scale.getTicks() : scale.getOptions().domain;
565
+ if (!tickFilter) return ticks;
566
+ return ticks.filter(tickFilter);
567
+ }
568
+
569
+ function labelsOf(
570
+ scale: Scale,
571
+ labelFormatter: (d: any) => string | DisplayObject,
572
+ tickFilter,
573
+ ): (string | DisplayObject)[] {
574
+ const T = ticksOf(scale, tickFilter);
575
+ const ticks = T.map((d) => (typeof d === 'number' ? prettyNumber(d) : d));
576
+ const formatter = labelFormatter
577
+ ? typeof labelFormatter === 'string'
578
+ ? format(labelFormatter)
579
+ : labelFormatter
580
+ : scale.getFormatter
581
+ ? scale.getFormatter()
582
+ : (d) => `${d}`;
583
+ return ticks.map(formatter);
584
+ }
585
+
586
+ function offsetOf(scale: Scale, d: any): number {
587
+ if (!scale.getBandWidth) return 0;
588
+ const offset = scale.getBandWidth(d) / 2;
589
+ return offset;
590
+ }
591
+
592
+ function overflowX(
593
+ scale: Scale,
594
+ labelBBoxes: DOMRect[],
595
+ crossSize: number,
596
+ crossPadding: [number, number],
597
+ tickFilter: (d: any) => boolean,
598
+ ): boolean {
599
+ // If actual size bigger than container size, overflow.
600
+ const totalSize = sum(labelBBoxes, (d) => d.width);
601
+ if (totalSize > crossSize) return true;
602
+
603
+ // Clone scale to get visual position for labels.
604
+ const scaleX = scale.clone();
605
+ scaleX.update({ range: [0, crossSize] });
606
+ const ticks = ticksOf(scale, tickFilter);
607
+ const X = ticks.map((d) => scaleX.map(d) + offsetOf(scaleX, d));
608
+
609
+ const I = ticks.map((_, i) => i);
610
+ const startX = -crossPadding[0];
611
+ const endX = crossSize + crossPadding[1];
612
+ const extent = (x, bbox) => {
613
+ const { width } = bbox;
614
+ return [x - width / 2, x + width / 2];
615
+ };
616
+
617
+ // Collision detection.
618
+ for (let i = 0; i < I.length; i++) {
619
+ const x = X[i];
620
+ const [x0, x1] = extent(x, labelBBoxes[i]);
621
+ // If a label is out of plot area, overflow.
622
+ if (x0 < startX || x1 > endX) return true;
623
+ const y = X[i + 1];
624
+ if (y) {
625
+ // If two labels intersect, overflow.
626
+ const [y0] = extent(y, labelBBoxes[i + 1]);
627
+ if (x1 > y0) return true;
628
+ }
629
+ }
630
+ return false;
631
+ }
632
+
633
+ function computeLabelSize(
634
+ d: string | DisplayObject,
635
+ style: Record<string, any>,
636
+ ): DOMRect {
637
+ const shape = normalizeLabel(d);
638
+ shape.attr({ ...style, visibility: 'none' });
639
+ const bbox = shape.getBBox();
640
+ return bbox;
641
+ }
642
+
643
+ function normalizeLabel(d: string | DisplayObject): DisplayObject {
644
+ if (d instanceof DisplayObject) return d;
645
+ return new Text({ style: { text: `${d}` } });
646
+ }
647
+
648
+ function prettyNumber(n: number) {
649
+ return Math.abs(n) < 1e-15 ? n : parseFloat(n.toFixed(15));
650
+ }
@@ -3,21 +3,24 @@ import { ascending, group } from 'd3-array';
3
3
  import { isParallel, isPolar, isRadar, radiusOf } from '../utils/coordinate';
4
4
  import { capitalizeFirst } from '../utils/helper';
5
5
  import { divide } from '../utils/array';
6
+ import { camelCase } from '../utils/string';
6
7
  import {
7
8
  GuideComponentPosition as GCP,
8
9
  GuideComponentOrientation as GCO,
9
10
  Layout,
10
11
  Section,
11
12
  SectionArea,
13
+ G2Theme,
12
14
  } from './types/common';
13
- import { G2GuideComponentOptions, G2View } from './types/options';
15
+ import { computeComponentSize } from './component';
16
+ import { G2GuideComponentOptions, G2Library, G2View } from './types/options';
14
17
 
15
18
  export function computeLayout(
16
19
  components: G2GuideComponentOptions[],
17
20
  options: G2View,
21
+ theme: G2Theme,
22
+ library: G2Library,
18
23
  ): Layout {
19
- const padding = computePadding(components, options);
20
- const { paddingLeft, paddingRight, paddingTop, paddingBottom } = padding;
21
24
  const {
22
25
  width,
23
26
  height,
@@ -33,18 +36,62 @@ export function computeLayout(
33
36
  marginBottom = margin,
34
37
  marginTop = margin,
35
38
  marginRight = margin,
39
+ padding,
40
+ paddingBottom = padding,
41
+ paddingLeft = padding,
42
+ paddingRight = padding,
43
+ paddingTop = padding,
36
44
  } = options;
37
45
 
46
+ const MAX_PADDING_RATIO = 1 / 3;
47
+ const clamp = (origin, computed, max) =>
48
+ origin === 'auto' ? Math.min(max, computed) : computed;
49
+
50
+ // Compute paddingLeft and paddingRight first to get innerWidth.
51
+ const horizontalPadding = computePadding(
52
+ components,
53
+ height,
54
+ [0, 0],
55
+ ['left', 'right'],
56
+ options,
57
+ theme,
58
+ library,
59
+ );
60
+ const { paddingLeft: pl0, paddingRight: pr0 } = horizontalPadding;
61
+ const viewWidth = width - marginLeft - marginRight;
62
+ const pl = clamp(paddingLeft, pl0, viewWidth * MAX_PADDING_RATIO);
63
+ const pr = clamp(paddingRight, pr0, viewWidth * MAX_PADDING_RATIO);
64
+ const iw = viewWidth - pl - pr;
65
+
66
+ // Compute paddingBottom and paddingTop based on innerWidth.
67
+ const verticalPadding = computePadding(
68
+ components,
69
+ iw,
70
+ [pl, pr],
71
+ ['bottom', 'top'],
72
+ options,
73
+ theme,
74
+ library,
75
+ );
76
+ const { paddingTop: pt0, paddingBottom: pb0 } = verticalPadding;
77
+ const viewHeight = height - marginBottom - marginTop;
78
+ const pb = clamp(paddingBottom, pb0, viewHeight * MAX_PADDING_RATIO);
79
+ const pt = clamp(paddingTop, pt0, viewHeight * MAX_PADDING_RATIO);
80
+ const ih = viewHeight - pb - pt;
81
+
38
82
  return {
39
- ...padding,
40
83
  width,
41
84
  height,
42
85
  insetLeft,
43
86
  insetTop,
44
87
  insetBottom,
45
88
  insetRight,
46
- innerWidth: width - paddingLeft - paddingRight - marginLeft - marginRight,
47
- innerHeight: height - paddingTop - paddingBottom - marginTop - marginBottom,
89
+ innerWidth: iw,
90
+ innerHeight: ih,
91
+ paddingLeft: pl,
92
+ paddingRight: pr,
93
+ paddingTop: pt,
94
+ paddingBottom: pb,
48
95
  marginLeft,
49
96
  marginBottom,
50
97
  marginTop,
@@ -59,18 +106,13 @@ export function computeLayout(
59
106
  */
60
107
  function computePadding(
61
108
  components: G2GuideComponentOptions[],
109
+ crossSize: number,
110
+ crossPadding: [number, number],
111
+ positions: GCP[],
62
112
  options: G2View,
113
+ theme: G2Theme,
114
+ library: G2Library,
63
115
  ) {
64
- const positions: GCP[] = [
65
- 'left',
66
- 'right',
67
- 'top',
68
- 'bottom',
69
- 'top-left',
70
- 'top-right',
71
- 'bottom-left',
72
- 'bottom-right',
73
- ];
74
116
  const positionComponents = group(components, (d) => d.position);
75
117
  const {
76
118
  padding,
@@ -86,12 +128,25 @@ function computePadding(
86
128
  paddingRight,
87
129
  };
88
130
  for (const position of positions) {
89
- const key = `padding${capitalizeFirst(position)}`;
90
- if (layout[key] === undefined) {
131
+ const key = `padding${capitalizeFirst(camelCase(position))}`;
132
+ const value = layout[key];
133
+ if (value === undefined || value === 'auto') {
91
134
  if (!positionComponents.has(position)) {
92
135
  layout[key] = 30;
93
136
  } else {
94
137
  const components = positionComponents.get(position);
138
+ if (value === 'auto') {
139
+ components.forEach((component) =>
140
+ computeComponentSize(
141
+ component,
142
+ crossSize,
143
+ crossPadding,
144
+ position,
145
+ theme,
146
+ library,
147
+ ),
148
+ );
149
+ }
95
150
  const totalSize = components.reduce((sum, { size }) => sum + size, 0);
96
151
  layout[key] = totalSize;
97
152
  }
@@ -161,7 +216,7 @@ export function placeComponents(
161
216
  /**
162
217
  * @description non-entity components: axis in the center, inner, outer, component in the center
163
218
  * @description entity components: other components
164
- * @description no volumn components take up no extra space
219
+ * @description no volume components take up no extra space
165
220
  */
166
221
 
167
222
  const [nonEntityComponents, entityComponents] = divide(