@antv/g2 5.1.2 → 5.1.4

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 (68) hide show
  1. package/README.md +6 -6
  2. package/README.zh-CN.md +6 -6
  3. package/dist/g2-lite.min.js +1 -1
  4. package/dist/g2.min.js +1 -1
  5. package/esm/api/runtime.js +3 -2
  6. package/esm/api/runtime.js.map +1 -1
  7. package/esm/api/utils.d.ts +1 -0
  8. package/esm/api/utils.js +6 -2
  9. package/esm/api/utils.js.map +1 -1
  10. package/esm/interaction/tooltip.d.ts +2 -2
  11. package/esm/interaction/tooltip.js +47 -22
  12. package/esm/interaction/tooltip.js.map +1 -1
  13. package/esm/interaction/utils.js +4 -2
  14. package/esm/interaction/utils.js.map +1 -1
  15. package/esm/lib/threed.d.ts +1 -0
  16. package/esm/lib/threed.js +2 -1
  17. package/esm/lib/threed.js.map +1 -1
  18. package/esm/mark/index.d.ts +1 -0
  19. package/esm/mark/index.js +1 -0
  20. package/esm/mark/index.js.map +1 -1
  21. package/esm/mark/interval3D.d.ts +8 -0
  22. package/esm/mark/interval3D.js +65 -0
  23. package/esm/mark/interval3D.js.map +1 -0
  24. package/esm/shape/index.d.ts +2 -1
  25. package/esm/shape/index.js +1 -0
  26. package/esm/shape/index.js.map +1 -1
  27. package/esm/shape/interval3D/cube.d.ts +6 -0
  28. package/esm/shape/interval3D/cube.js +71 -0
  29. package/esm/shape/interval3D/cube.js.map +1 -0
  30. package/esm/spec/interaction.d.ts +1 -0
  31. package/lib/api/runtime.js +2 -1
  32. package/lib/api/runtime.js.map +1 -1
  33. package/lib/api/utils.d.ts +1 -0
  34. package/lib/api/utils.js +7 -3
  35. package/lib/api/utils.js.map +1 -1
  36. package/lib/interaction/tooltip.d.ts +2 -2
  37. package/lib/interaction/tooltip.js +47 -22
  38. package/lib/interaction/tooltip.js.map +1 -1
  39. package/lib/interaction/utils.js +4 -2
  40. package/lib/interaction/utils.js.map +1 -1
  41. package/lib/lib/threed.d.ts +1 -0
  42. package/lib/lib/threed.js +1 -0
  43. package/lib/lib/threed.js.map +1 -1
  44. package/lib/mark/index.d.ts +1 -0
  45. package/lib/mark/index.js +3 -1
  46. package/lib/mark/index.js.map +1 -1
  47. package/lib/mark/interval3D.d.ts +8 -0
  48. package/lib/mark/interval3D.js +69 -0
  49. package/lib/mark/interval3D.js.map +1 -0
  50. package/lib/shape/index.d.ts +2 -1
  51. package/lib/shape/index.js +4 -2
  52. package/lib/shape/index.js.map +1 -1
  53. package/lib/shape/interval3D/cube.d.ts +6 -0
  54. package/lib/shape/interval3D/cube.js +75 -0
  55. package/lib/shape/interval3D/cube.js.map +1 -0
  56. package/lib/spec/interaction.d.ts +1 -0
  57. package/package.json +1 -1
  58. package/src/api/runtime.ts +2 -1
  59. package/src/api/utils.ts +7 -1
  60. package/src/interaction/tooltip.ts +49 -17
  61. package/src/interaction/utils.ts +5 -2
  62. package/src/lib/threed.ts +2 -1
  63. package/src/mark/index.ts +1 -0
  64. package/src/mark/interval3D.ts +100 -0
  65. package/src/shape/index.ts +2 -1
  66. package/src/shape/interval3D/cube.ts +69 -0
  67. package/src/spec/interaction.ts +1 -0
  68. package/CHANGELOG.md +0 -374
@@ -63,7 +63,16 @@ function createTooltip(
63
63
  enterable,
64
64
  bounding,
65
65
  containerOffset,
66
+ css = {},
66
67
  ) {
68
+ const defaults = {
69
+ '.g2-tooltip': {},
70
+ '.g2-tooltip-title': {
71
+ overflow: 'hidden',
72
+ 'white-space': 'nowrap',
73
+ 'text-overflow': 'ellipsis',
74
+ },
75
+ };
67
76
  const tooltipElement = new TooltipComponent({
68
77
  className: 'tooltip',
69
78
  style: {
@@ -79,14 +88,7 @@ function createTooltip(
79
88
  template: {
80
89
  prefixCls: 'g2-',
81
90
  },
82
- style: {
83
- '.g2-tooltip': {},
84
- '.g2-tooltip-title': {
85
- overflow: 'hidden',
86
- 'white-space': 'nowrap',
87
- 'text-overflow': 'ellipsis',
88
- },
89
- },
91
+ style: deepMix(defaults, css),
90
92
  },
91
93
  });
92
94
  container.appendChild(tooltipElement.HTMLTooltipElement);
@@ -103,6 +105,7 @@ function showTooltip({
103
105
  single,
104
106
  position = 'right-bottom',
105
107
  enterable = false,
108
+ css,
106
109
  mount,
107
110
  bounding,
108
111
  }) {
@@ -123,6 +126,7 @@ function showTooltip({
123
126
  enterable,
124
127
  b,
125
128
  containerOffset,
129
+ css,
126
130
  ),
127
131
  } = parent as any;
128
132
  const { items, title = '' } = data;
@@ -431,6 +435,7 @@ export function seriesTooltip(
431
435
  disableNative = false,
432
436
  marker = true,
433
437
  style: _style = {},
438
+ css = {},
434
439
  ...rest
435
440
  }: Record<string, any>,
436
441
  ) {
@@ -452,11 +457,14 @@ export function seriesTooltip(
452
457
  const itemElements = [];
453
458
  for (const element of elements) {
454
459
  const { __data__: data } = element;
455
- const { seriesX } = data;
460
+ const { seriesX, title, items } = data;
456
461
  if (seriesX) seriesElements.push(element);
457
- else itemElements.push(element);
462
+ else if (title || items) itemElements.push(element);
458
463
  }
459
464
 
465
+ const isBandScale = !!(transposed ? scale.y : scale.x).getBandWidth;
466
+ const closest = isBandScale && itemElements.length > 0;
467
+
460
468
  // Sorted elements from top to bottom visually,
461
469
  // or from right to left in transpose coordinate.
462
470
  seriesElements.sort((a, b) => {
@@ -465,6 +473,20 @@ export function seriesTooltip(
465
473
  return transposed ? minY(b) - minY(a) : minY(a) - minY(b);
466
474
  });
467
475
 
476
+ const extent = (d) => {
477
+ const index = transposed ? 1 : 0;
478
+ const { min, max } = d.getLocalBounds();
479
+ return sort([min[index], max[index]]);
480
+ };
481
+ // Sort itemElements by x or y.
482
+ itemElements.sort((a, b) => {
483
+ const [minA, maxA] = extent(a);
484
+ const [minB, maxB] = extent(b);
485
+ const midA = (minA + maxA) / 2;
486
+ const midB = (minB + maxB) / 2;
487
+ return transposed ? midB - midA : midA - midB;
488
+ });
489
+
468
490
  // Get sortedIndex and X for each series elements
469
491
  const elementSortedX = new Map(
470
492
  seriesElements.map((element) => {
@@ -489,8 +511,9 @@ export function seriesTooltip(
489
511
  const indexByFocus = (focus, I, X) => {
490
512
  const finalX = abstractX(focus);
491
513
  const [minX, maxX] = sort([X[0], X[X.length - 1]]);
492
- // Skip x out of range.
493
- if (finalX < minX || finalX > maxX) return null;
514
+ // If closest is true, always find at least one element.
515
+ // Otherwise, skip element out of plot area.
516
+ if (!closest && (finalX < minX || finalX > maxX)) return null;
494
517
  const search = bisector((i) => X[+i]).center;
495
518
  const i = search(I, finalX);
496
519
  return I[i];
@@ -499,14 +522,20 @@ export function seriesTooltip(
499
522
  const elementsByFocus = (focus, elements) => {
500
523
  const index = transposed ? 1 : 0;
501
524
  const x = focus[index];
502
- const extent = (d) => {
503
- const { min, max } = d.getLocalBounds();
504
- return sort([min[index], max[index]]);
505
- };
506
- return elements.filter((element) => {
525
+ const filtered = elements.filter((element) => {
507
526
  const [min, max] = extent(element);
508
527
  return x >= min && x <= max;
509
528
  });
529
+ // If closet is true, always find at least one element.
530
+ if (!closest || filtered.length > 0) return filtered;
531
+
532
+ // Search the closet element to the focus.
533
+ const search = bisector((element) => {
534
+ const [min, max] = extent(element);
535
+ return (min + max) / 2;
536
+ }).center;
537
+ const i = search(elements, x);
538
+ return [elements[i]].filter(defined);
510
539
  };
511
540
 
512
541
  const seriesData = (element, index) => {
@@ -597,6 +626,7 @@ export function seriesTooltip(
597
626
  enterable,
598
627
  mount,
599
628
  bounding,
629
+ css,
600
630
  });
601
631
  }
602
632
 
@@ -733,6 +763,7 @@ export function tooltip(
733
763
  shared = false,
734
764
  body = true,
735
765
  disableNative = false,
766
+ css = {},
736
767
  }: Record<string, any>,
737
768
  ) {
738
769
  const elements = elementsof(root);
@@ -780,6 +811,7 @@ export function tooltip(
780
811
  enterable,
781
812
  mount,
782
813
  bounding,
814
+ css,
783
815
  });
784
816
  }
785
817
 
@@ -451,11 +451,14 @@ export function setCursor(root, cursor) {
451
451
  // @ts-ignore
452
452
  const canvas = root.getRootNode().defaultView;
453
453
  const dom = canvas.getContextService().getDomElement();
454
- if (dom?.style) dom.style.cursor = cursor;
454
+ if (dom?.style) {
455
+ root.cursor = dom.style.cursor;
456
+ dom.style.cursor = cursor;
457
+ }
455
458
  }
456
459
 
457
460
  export function restoreCursor(root) {
458
- setCursor(root, 'default');
461
+ setCursor(root, root.cursor);
459
462
  }
460
463
 
461
464
  export function selectElementByData(elements, data, datum) {
package/src/lib/threed.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Cartesian3D } from '../coordinate';
2
2
  import { AxisZ } from '../component';
3
- import { Point3D, Line3D } from '../mark';
3
+ import { Point3D, Line3D, Interval3D } from '../mark';
4
4
 
5
5
  export function threedlib() {
6
6
  return {
@@ -8,5 +8,6 @@ export function threedlib() {
8
8
  'component.axisZ': AxisZ,
9
9
  'mark.point3D': Point3D,
10
10
  'mark.line3D': Line3D,
11
+ 'mark.interval3D': Interval3D,
11
12
  } as const;
12
13
  }
package/src/mark/index.ts CHANGED
@@ -4,6 +4,7 @@ export { Line } from './line';
4
4
  export { Line3D } from './line3D';
5
5
  export { Point } from './point';
6
6
  export { Point3D } from './point3D';
7
+ export { Interval3D } from './interval3D';
7
8
  export { Text } from './text';
8
9
  export { Cell } from './cell';
9
10
  export { Area } from './area';
@@ -0,0 +1,100 @@
1
+ import { Coordinate3D } from '@antv/coord';
2
+ import { Band } from '@antv/scale';
3
+ import { MarkComponent as MC, Vector3 } from '../runtime';
4
+ import { PointMark } from '../spec';
5
+ import {
6
+ MaybeZeroX,
7
+ MaybeZeroY,
8
+ MaybeZeroZ,
9
+ MaybeSize,
10
+ MaybeZeroY1,
11
+ } from '../transform';
12
+ import { Sphere, IntervalCube } from '../shape';
13
+ import {
14
+ baseGeometryChannels,
15
+ basePostInference,
16
+ basePreInference,
17
+ tooltip3d,
18
+ } from './utils';
19
+
20
+ export type PointOptions = Omit<PointMark, 'type'>;
21
+
22
+ /**
23
+ * Convert value for each channel to rect shapes.
24
+ * return two 3D points, which represents the bounding box of a rect.
25
+ */
26
+ export const Interval3D: MC<PointOptions> = (options) => {
27
+ return (index, scale, value, coordinate) => {
28
+ const {
29
+ x: X,
30
+ y: Y,
31
+ y1: Y1,
32
+ z: Z,
33
+ size: SZ,
34
+ dx: DX,
35
+ dy: DY,
36
+ dz: DZ,
37
+ } = value;
38
+
39
+ // The scales for x and series channels must be band scale.
40
+ const x = scale.x as Band;
41
+ const z = scale.x as Band;
42
+ const [width, height, depth] = (
43
+ coordinate as unknown as Coordinate3D
44
+ ).getSize();
45
+ const x1x2 = (x: number, w: number, i: number) => [x, x + w];
46
+
47
+ // Calc the points of bounding box for the interval.
48
+ // They are start from left-top corner in clock wise order.
49
+ // TODO: series support
50
+ const P = Array.from(index, (i) => {
51
+ const groupWidthX = bandWidth(x, X[i]);
52
+ const groupWidthZ = bandWidth(z, Z[i]);
53
+ const x0 = +X[i];
54
+ const z0 = +Z[i];
55
+ const [x1, x2] = x1x2(x0, groupWidthX, i);
56
+ const [z1, z2] = x1x2(z0, groupWidthZ, i);
57
+ const y1 = +Y[i];
58
+ const y2 = +Y1[i];
59
+
60
+ const p1 = [x1, y1, z1];
61
+ const p2 = [x2, y2, z2];
62
+
63
+ return [
64
+ (coordinate as unknown as Coordinate3D).map([...p1]),
65
+ (coordinate as unknown as Coordinate3D).map([...p2]),
66
+ ];
67
+ });
68
+ return [index, P];
69
+ };
70
+ };
71
+
72
+ function bandWidth(scale: Band, xz: any): number {
73
+ return scale.getBandWidth(scale.invert(xz));
74
+ }
75
+
76
+ const shape = {
77
+ cube: IntervalCube,
78
+ };
79
+
80
+ Interval3D.props = {
81
+ defaultShape: 'cube',
82
+ defaultLabelShape: 'label',
83
+ composite: false,
84
+ shape,
85
+ channels: [
86
+ ...baseGeometryChannels({ shapes: Object.keys(shape) }),
87
+ { name: 'x', scale: 'band', required: true },
88
+ { name: 'z', scale: 'band', required: true },
89
+ { name: 'y', required: true },
90
+ { name: 'series', scale: 'band' },
91
+ { name: 'size' },
92
+ ],
93
+ preInference: [
94
+ ...basePreInference(),
95
+ { type: MaybeZeroX },
96
+ { type: MaybeZeroY1 },
97
+ { type: MaybeZeroZ },
98
+ ],
99
+ postInference: [...basePostInference(), { type: MaybeSize }, ...tooltip3d()],
100
+ };
@@ -32,6 +32,7 @@ export { Triangle as PointTriangle } from './point/triangle';
32
32
  export { TriangleDown as PointTriangleDown } from './point/triangleDown';
33
33
  export { Sphere } from './point3D/sphere';
34
34
  export { Cube } from './point3D/cube';
35
+ export { Cube as IntervalCube } from './interval3D/cube';
35
36
  export { Vector as VectorShape } from './vector/vector';
36
37
  export { Text as TextShape } from './text/text';
37
38
  export { Badge as TextBadge } from './text/badge';
@@ -65,7 +66,7 @@ export { Liquid as LiquidShape } from './liquid/liquid';
65
66
  export type { RectOptions as IntervalShapeOptions } from './interval/rect';
66
67
  export type { HollowOptions as IntervalHollowOptions } from './interval/hollow';
67
68
  export type { FunnelOptions as IntervalFunnelOptions } from './interval/funnel';
68
- export type { PyramidOptions as IntervalPyramidOptions } from './interval/pyramid';
69
+ export type { PyramidOptions as IntervalPyline3DramidOptions } from './interval/pyramid';
69
70
  export type { LineOptions as LineShapeOptions } from './line/line';
70
71
  export type { SmoothOptions as LineSmoothOptions } from './line/smooth';
71
72
  export type { HVOptions as LineHVOptions } from './line/hv';
@@ -0,0 +1,69 @@
1
+ import { MeshLambertMaterial, CubeGeometry, Mesh } from '@antv/g-plugin-3d';
2
+ import { applyStyle, getOrigin, toOpacityKey } from '../utils';
3
+ import { ShapeComponent as SC, Vector3 } from '../../runtime';
4
+ import { select } from '../../utils/selection';
5
+
6
+ export type CubeOptions = Record<string, any>;
7
+
8
+ /**
9
+ * @see https://g.antv.antgroup.com/api/3d/geometry#cubegeometry
10
+ */
11
+ export const Cube: SC<CubeOptions> = (options, context) => {
12
+ // Render border only when colorAttribute is stroke.
13
+ const { ...style } = options;
14
+
15
+ // @ts-ignore
16
+ if (!context.cubeGeometry) {
17
+ const renderer = context.canvas.getConfig().renderer;
18
+ const plugin = renderer.getPlugin('device-renderer');
19
+ const device = plugin.getDevice();
20
+ // create a cube geometry
21
+ // @ts-ignore
22
+ context.cubeGeometry = new CubeGeometry(device, {
23
+ width: 1,
24
+ height: 1,
25
+ depth: 1,
26
+ });
27
+ // create a material with Lambert lighting model
28
+ // @ts-ignore
29
+ context.cubeMaterial = new MeshLambertMaterial(device);
30
+ }
31
+
32
+ return (_points, value, defaults) => {
33
+ const points = _points as unknown as Vector3[];
34
+ const { color: defaultColor } = defaults;
35
+ const { color = defaultColor, transform, opacity } = value;
36
+ const [cx, cy, cz] = getOrigin(points);
37
+
38
+ const width = Math.abs(points[1][0] - points[0][0]);
39
+ const height = Math.abs(points[1][1] - points[0][1]);
40
+ const depth = Math.abs(points[1][2] - points[0][2]);
41
+
42
+ const cube = new Mesh({
43
+ style: {
44
+ x: cx,
45
+ y: cy,
46
+ z: cz,
47
+ // @ts-ignore
48
+ geometry: context.cubeGeometry,
49
+ // @ts-ignore
50
+ material: context.cubeMaterial,
51
+ },
52
+ });
53
+ cube.setOrigin(0, 0, 0);
54
+ cube.scale([width, height, depth]);
55
+
56
+ const selection = select(cube)
57
+ .call(applyStyle, defaults)
58
+ .style('fill', color)
59
+ .style('transform', transform)
60
+ .style(toOpacityKey(options), opacity)
61
+ .call(applyStyle, style)
62
+ .node();
63
+ return selection;
64
+ };
65
+ };
66
+
67
+ Cube.props = {
68
+ defaultMarker: 'cube',
69
+ };
@@ -180,6 +180,7 @@ export type TooltipInteraction = {
180
180
  position?: TooltipStyleProps['position'];
181
181
  bounding?: BBox;
182
182
  mount?: string | HTMLElement;
183
+ css?: Record<string, any>; // @todo
183
184
  // enterable?: boolean;
184
185
  sort?: (d: TooltipItemValue) => any;
185
186
  filter?: (d: TooltipItemValue) => any;