@antv/g2 5.0.6 → 5.0.8

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 (297) hide show
  1. package/dist/g2-lite.min.js +4 -4
  2. package/dist/g2.min.js +4 -4
  3. package/esm/animation/morphing.js +4 -0
  4. package/esm/animation/morphing.js.map +1 -1
  5. package/esm/api/chart.d.ts +4 -7
  6. package/esm/api/chart.js +22 -90
  7. package/esm/api/chart.js.map +1 -1
  8. package/esm/api/mark/index.d.ts +3 -1
  9. package/esm/api/mark/index.js +2 -1
  10. package/esm/api/mark/index.js.map +1 -1
  11. package/esm/api/mark/mark.d.ts +7 -1
  12. package/esm/api/mark/mark.js +9 -0
  13. package/esm/api/mark/mark.js.map +1 -1
  14. package/esm/api/utils.d.ts +10 -0
  15. package/esm/api/utils.js +71 -0
  16. package/esm/api/utils.js.map +1 -0
  17. package/esm/component/axis.js +17 -19
  18. package/esm/component/axis.js.map +1 -1
  19. package/esm/component/axisX.js +3 -1
  20. package/esm/component/axisX.js.map +1 -1
  21. package/esm/component/axisY.js +1 -1
  22. package/esm/component/axisY.js.map +1 -1
  23. package/esm/component/constant.js +6 -0
  24. package/esm/component/constant.js.map +1 -1
  25. package/esm/component/legendCategory.js +8 -5
  26. package/esm/component/legendCategory.js.map +1 -1
  27. package/esm/component/legendContinuous.js +91 -33
  28. package/esm/component/legendContinuous.js.map +1 -1
  29. package/esm/component/utils.d.ts +9 -5
  30. package/esm/component/utils.js +14 -14
  31. package/esm/component/utils.js.map +1 -1
  32. package/esm/composition/mark.js +2 -1
  33. package/esm/composition/mark.js.map +1 -1
  34. package/esm/data/index.d.ts +2 -0
  35. package/esm/data/index.js +1 -0
  36. package/esm/data/index.js.map +1 -1
  37. package/esm/data/kde.d.ts +10 -0
  38. package/esm/data/kde.js +30 -0
  39. package/esm/data/kde.js.map +1 -0
  40. package/esm/interaction/brushFilter.js +2 -6
  41. package/esm/interaction/brushFilter.js.map +1 -1
  42. package/esm/interaction/brushHighlight.d.ts +3 -2
  43. package/esm/interaction/brushHighlight.js +202 -28
  44. package/esm/interaction/brushHighlight.js.map +1 -1
  45. package/esm/interaction/brushXHighlight.js +1 -1
  46. package/esm/interaction/brushXHighlight.js.map +1 -1
  47. package/esm/interaction/brushYHighlight.js +1 -1
  48. package/esm/interaction/brushYHighlight.js.map +1 -1
  49. package/esm/interaction/event.d.ts +1 -0
  50. package/esm/interaction/event.js +14 -12
  51. package/esm/interaction/event.js.map +1 -1
  52. package/esm/interaction/tooltip.d.ts +4 -4
  53. package/esm/interaction/tooltip.js +103 -31
  54. package/esm/interaction/tooltip.js.map +1 -1
  55. package/esm/mark/density.d.ts +4 -0
  56. package/esm/mark/density.js +61 -0
  57. package/esm/mark/density.js.map +1 -0
  58. package/esm/mark/gauge.d.ts +7 -0
  59. package/esm/mark/gauge.js +4 -6
  60. package/esm/mark/gauge.js.map +1 -1
  61. package/esm/mark/index.d.ts +2 -0
  62. package/esm/mark/index.js +1 -0
  63. package/esm/mark/index.js.map +1 -1
  64. package/esm/runtime/component.d.ts +4 -2
  65. package/esm/runtime/component.js +164 -12
  66. package/esm/runtime/component.js.map +1 -1
  67. package/esm/runtime/layout.d.ts +3 -3
  68. package/esm/runtime/layout.js +40 -21
  69. package/esm/runtime/layout.js.map +1 -1
  70. package/esm/runtime/plot.js +10 -7
  71. package/esm/runtime/plot.js.map +1 -1
  72. package/esm/runtime/scale.js +2 -2
  73. package/esm/runtime/scale.js.map +1 -1
  74. package/esm/runtime/transform.js +4 -1
  75. package/esm/runtime/transform.js.map +1 -1
  76. package/esm/runtime/types/component.d.ts +1 -0
  77. package/esm/shape/area/area.js +1 -1
  78. package/esm/shape/area/area.js.map +1 -1
  79. package/esm/shape/area/curve.js +1 -1
  80. package/esm/shape/area/curve.js.map +1 -1
  81. package/esm/shape/box/violin.d.ts +3 -0
  82. package/esm/shape/box/violin.js +85 -0
  83. package/esm/shape/box/violin.js.map +1 -0
  84. package/esm/shape/connector/connector.js +1 -1
  85. package/esm/shape/density/density.d.ts +8 -0
  86. package/esm/shape/density/density.js +50 -0
  87. package/esm/shape/density/density.js.map +1 -0
  88. package/esm/shape/index.d.ts +4 -0
  89. package/esm/shape/index.js +2 -0
  90. package/esm/shape/index.js.map +1 -1
  91. package/esm/shape/line/curve.js +1 -1
  92. package/esm/shape/line/curve.js.map +1 -1
  93. package/esm/shape/text/advance.d.ts +1 -1
  94. package/esm/shape/text/advance.js +1 -1
  95. package/esm/shape/text/advance.js.map +1 -1
  96. package/esm/shape/text/badge.js +1 -1
  97. package/esm/shape/utils.js +1 -1
  98. package/esm/shape/utils.js.map +1 -1
  99. package/esm/spec/composition.d.ts +22 -19
  100. package/esm/spec/dataTransform.d.ts +34 -2
  101. package/esm/spec/interaction.d.ts +3 -1
  102. package/esm/spec/mark.d.ts +9 -8
  103. package/esm/spec/transform.d.ts +1 -1
  104. package/esm/spec/utils.d.ts +1 -0
  105. package/esm/stdlib/index.js +5 -5
  106. package/esm/stdlib/index.js.map +1 -1
  107. package/esm/theme/classic.js +3 -0
  108. package/esm/theme/classic.js.map +1 -1
  109. package/esm/theme/classicDark.js +1 -0
  110. package/esm/theme/classicDark.js.map +1 -1
  111. package/esm/transform/filter.js +6 -2
  112. package/esm/transform/filter.js.map +1 -1
  113. package/esm/{shape → utils}/createElement.d.ts +1 -1
  114. package/esm/{shape → utils}/createElement.js.map +1 -1
  115. package/esm/utils/helper.d.ts +3 -0
  116. package/esm/utils/helper.js +3 -0
  117. package/esm/utils/helper.js.map +1 -1
  118. package/esm/utils/scale.js +5 -1
  119. package/esm/utils/scale.js.map +1 -1
  120. package/esm/utils/size.js +7 -4
  121. package/esm/utils/size.js.map +1 -1
  122. package/lib/animation/morphing.js +4 -0
  123. package/lib/animation/morphing.js.map +1 -1
  124. package/lib/api/chart.d.ts +4 -7
  125. package/lib/api/chart.js +27 -97
  126. package/lib/api/chart.js.map +1 -1
  127. package/lib/api/mark/index.d.ts +3 -1
  128. package/lib/api/mark/index.js +1 -0
  129. package/lib/api/mark/index.js.map +1 -1
  130. package/lib/api/mark/mark.d.ts +7 -1
  131. package/lib/api/mark/mark.js +10 -1
  132. package/lib/api/mark/mark.js.map +1 -1
  133. package/lib/api/utils.d.ts +10 -0
  134. package/lib/api/utils.js +80 -0
  135. package/lib/api/utils.js.map +1 -0
  136. package/lib/component/axis.js +17 -19
  137. package/lib/component/axis.js.map +1 -1
  138. package/lib/component/axisX.js +3 -1
  139. package/lib/component/axisX.js.map +1 -1
  140. package/lib/component/axisY.js +1 -1
  141. package/lib/component/axisY.js.map +1 -1
  142. package/lib/component/constant.js +6 -0
  143. package/lib/component/constant.js.map +1 -1
  144. package/lib/component/legendCategory.js +7 -4
  145. package/lib/component/legendCategory.js.map +1 -1
  146. package/lib/component/legendContinuous.js +90 -32
  147. package/lib/component/legendContinuous.js.map +1 -1
  148. package/lib/component/utils.d.ts +9 -5
  149. package/lib/component/utils.js +16 -15
  150. package/lib/component/utils.js.map +1 -1
  151. package/lib/composition/mark.js +2 -1
  152. package/lib/composition/mark.js.map +1 -1
  153. package/lib/data/index.d.ts +2 -0
  154. package/lib/data/index.js +3 -1
  155. package/lib/data/index.js.map +1 -1
  156. package/lib/data/kde.d.ts +10 -0
  157. package/lib/data/kde.js +38 -0
  158. package/lib/data/kde.js.map +1 -0
  159. package/lib/interaction/brushFilter.js +2 -6
  160. package/lib/interaction/brushFilter.js.map +1 -1
  161. package/lib/interaction/brushHighlight.d.ts +3 -2
  162. package/lib/interaction/brushHighlight.js +201 -27
  163. package/lib/interaction/brushHighlight.js.map +1 -1
  164. package/lib/interaction/brushXHighlight.js +1 -1
  165. package/lib/interaction/brushXHighlight.js.map +1 -1
  166. package/lib/interaction/brushYHighlight.js +1 -1
  167. package/lib/interaction/brushYHighlight.js.map +1 -1
  168. package/lib/interaction/event.d.ts +1 -0
  169. package/lib/interaction/event.js +15 -12
  170. package/lib/interaction/event.js.map +1 -1
  171. package/lib/interaction/tooltip.d.ts +4 -4
  172. package/lib/interaction/tooltip.js +102 -30
  173. package/lib/interaction/tooltip.js.map +1 -1
  174. package/lib/mark/density.d.ts +4 -0
  175. package/lib/mark/density.js +65 -0
  176. package/lib/mark/density.js.map +1 -0
  177. package/lib/mark/gauge.d.ts +7 -0
  178. package/lib/mark/gauge.js +4 -6
  179. package/lib/mark/gauge.js.map +1 -1
  180. package/lib/mark/index.d.ts +2 -0
  181. package/lib/mark/index.js +3 -1
  182. package/lib/mark/index.js.map +1 -1
  183. package/lib/runtime/component.d.ts +4 -2
  184. package/lib/runtime/component.js +163 -10
  185. package/lib/runtime/component.js.map +1 -1
  186. package/lib/runtime/layout.d.ts +3 -3
  187. package/lib/runtime/layout.js +40 -21
  188. package/lib/runtime/layout.js.map +1 -1
  189. package/lib/runtime/plot.js +10 -7
  190. package/lib/runtime/plot.js.map +1 -1
  191. package/lib/runtime/scale.js +2 -2
  192. package/lib/runtime/scale.js.map +1 -1
  193. package/lib/runtime/transform.js +4 -1
  194. package/lib/runtime/transform.js.map +1 -1
  195. package/lib/runtime/types/component.d.ts +1 -0
  196. package/lib/shape/area/area.js +1 -1
  197. package/lib/shape/area/area.js.map +1 -1
  198. package/lib/shape/area/curve.js +1 -1
  199. package/lib/shape/area/curve.js.map +1 -1
  200. package/lib/shape/box/violin.d.ts +3 -0
  201. package/lib/shape/box/violin.js +89 -0
  202. package/lib/shape/box/violin.js.map +1 -0
  203. package/lib/shape/connector/connector.js +1 -1
  204. package/lib/shape/density/density.d.ts +8 -0
  205. package/lib/shape/density/density.js +54 -0
  206. package/lib/shape/density/density.js.map +1 -0
  207. package/lib/shape/index.d.ts +4 -0
  208. package/lib/shape/index.js +6 -1
  209. package/lib/shape/index.js.map +1 -1
  210. package/lib/shape/line/curve.js +1 -1
  211. package/lib/shape/line/curve.js.map +1 -1
  212. package/lib/shape/text/advance.d.ts +1 -1
  213. package/lib/shape/text/advance.js +1 -1
  214. package/lib/shape/text/advance.js.map +1 -1
  215. package/lib/shape/text/badge.js +1 -1
  216. package/lib/shape/utils.js +1 -1
  217. package/lib/shape/utils.js.map +1 -1
  218. package/lib/spec/composition.d.ts +22 -19
  219. package/lib/spec/dataTransform.d.ts +34 -2
  220. package/lib/spec/interaction.d.ts +3 -1
  221. package/lib/spec/mark.d.ts +9 -8
  222. package/lib/spec/transform.d.ts +1 -1
  223. package/lib/spec/utils.d.ts +1 -0
  224. package/lib/stdlib/index.js +2 -2
  225. package/lib/stdlib/index.js.map +1 -1
  226. package/lib/theme/classic.js +3 -0
  227. package/lib/theme/classic.js.map +1 -1
  228. package/lib/theme/classicDark.js +1 -0
  229. package/lib/theme/classicDark.js.map +1 -1
  230. package/lib/transform/filter.js +6 -2
  231. package/lib/transform/filter.js.map +1 -1
  232. package/lib/{shape → utils}/createElement.d.ts +1 -1
  233. package/lib/{shape → utils}/createElement.js.map +1 -1
  234. package/lib/utils/helper.d.ts +3 -0
  235. package/lib/utils/helper.js +5 -1
  236. package/lib/utils/helper.js.map +1 -1
  237. package/lib/utils/scale.js +5 -1
  238. package/lib/utils/scale.js.map +1 -1
  239. package/lib/utils/size.js +7 -4
  240. package/lib/utils/size.js.map +1 -1
  241. package/package.json +5 -3
  242. package/src/animation/morphing.ts +5 -0
  243. package/src/api/chart.ts +30 -113
  244. package/src/api/mark/index.ts +3 -0
  245. package/src/api/mark/mark.ts +12 -0
  246. package/src/api/utils.ts +79 -0
  247. package/src/component/axis.ts +22 -18
  248. package/src/component/axisX.ts +3 -1
  249. package/src/component/axisY.ts +2 -1
  250. package/src/component/constant.ts +6 -0
  251. package/src/component/legendCategory.ts +19 -6
  252. package/src/component/legendContinuous.ts +185 -74
  253. package/src/component/utils.ts +14 -3
  254. package/src/composition/mark.ts +2 -0
  255. package/src/data/index.ts +2 -0
  256. package/src/data/kde.ts +53 -0
  257. package/src/interaction/brushFilter.ts +8 -6
  258. package/src/interaction/brushHighlight.ts +258 -20
  259. package/src/interaction/brushXHighlight.ts +1 -0
  260. package/src/interaction/brushYHighlight.ts +1 -0
  261. package/src/interaction/event.ts +19 -13
  262. package/src/interaction/tooltip.ts +154 -25
  263. package/src/mark/density.ts +81 -0
  264. package/src/mark/gauge.ts +13 -7
  265. package/src/mark/index.ts +2 -0
  266. package/src/runtime/component.ts +210 -12
  267. package/src/runtime/layout.ts +74 -19
  268. package/src/runtime/plot.ts +8 -6
  269. package/src/runtime/scale.ts +2 -2
  270. package/src/runtime/transform.ts +2 -1
  271. package/src/runtime/types/component.ts +1 -0
  272. package/src/shape/area/area.ts +1 -1
  273. package/src/shape/area/curve.ts +1 -1
  274. package/src/shape/box/violin.ts +119 -0
  275. package/src/shape/connector/connector.ts +1 -1
  276. package/src/shape/density/density.ts +52 -0
  277. package/src/shape/index.ts +4 -0
  278. package/src/shape/line/curve.ts +1 -1
  279. package/src/shape/text/advance.ts +1 -1
  280. package/src/shape/text/badge.ts +1 -1
  281. package/src/shape/utils.ts +1 -1
  282. package/src/spec/composition.ts +22 -19
  283. package/src/spec/dataTransform.ts +35 -0
  284. package/src/spec/interaction.ts +3 -2
  285. package/src/spec/mark.ts +10 -6
  286. package/src/spec/transform.ts +1 -0
  287. package/src/spec/utils.ts +2 -0
  288. package/src/stdlib/index.ts +8 -0
  289. package/src/theme/classic.ts +3 -0
  290. package/src/theme/classicDark.ts +1 -0
  291. package/src/transform/filter.ts +6 -2
  292. package/src/{shape → utils}/createElement.ts +1 -1
  293. package/src/utils/helper.ts +12 -1
  294. package/src/utils/scale.ts +5 -1
  295. package/src/utils/size.ts +10 -4
  296. /package/esm/{shape → utils}/createElement.js +0 -0
  297. /package/lib/{shape → utils}/createElement.js +0 -0
@@ -6,38 +6,71 @@ import { Constant, Identity } from '@antv/scale';
6
6
  import { defined, subObject } from '../utils/helper';
7
7
  import { isTranspose, isPolar } from '../utils/coordinate';
8
8
  import { angle, sub } from '../utils/vector';
9
+ import { invert } from '../utils/scale';
10
+ import { BBox } from '../runtime';
9
11
  import {
10
12
  selectG2Elements,
11
13
  createXKey,
12
14
  selectPlotArea,
13
15
  mousePosition,
14
16
  selectFacetG2Elements,
17
+ createDatumof,
15
18
  } from './utils';
16
-
17
- function getContainer(group: IElement) {
19
+ import { dataOf } from './event';
20
+
21
+ function getContainer(
22
+ group: IElement,
23
+ mount: string | HTMLElement,
24
+ ): HTMLElement {
25
+ if (mount)
26
+ return typeof mount === 'string' ? document.querySelector(mount) : mount;
18
27
  // @ts-ignore
19
28
  return group.getRootNode().defaultView.getConfig().container;
20
29
  }
21
30
 
22
- function createTooltip(root, x0, y0, position, enterable) {
31
+ function getBounding(root): BBox {
23
32
  const bbox = root.getBounds();
24
33
  const {
25
- min: [x, y],
26
- max: [x1, y1],
34
+ min: [x1, y1],
35
+ max: [x2, y2],
27
36
  } = bbox;
37
+ return {
38
+ x: x1,
39
+ y: y1,
40
+ width: x2 - x1,
41
+ height: y2 - y1,
42
+ };
43
+ }
44
+
45
+ function getContainerOffset(
46
+ container1: HTMLElement,
47
+ container2: HTMLElement,
48
+ ): { x: number; y: number } {
49
+ const r1 = container1.getBoundingClientRect();
50
+ const r2 = container2.getBoundingClientRect();
51
+ return {
52
+ x: r1.x - r2.x,
53
+ y: r1.y - r2.y,
54
+ };
55
+ }
56
+
57
+ function createTooltip(
58
+ container: HTMLElement,
59
+ x0,
60
+ y0,
61
+ position,
62
+ enterable,
63
+ bounding,
64
+ containerOffset,
65
+ ) {
28
66
  const tooltipElement = new TooltipComponent({
29
67
  className: 'tooltip',
30
68
  style: {
31
69
  x: x0,
32
70
  y: y0,
33
- container: { x: 0, y: 0 },
71
+ container: containerOffset,
34
72
  data: [],
35
- bounding: {
36
- x,
37
- y,
38
- width: x1 - x,
39
- height: y1 - y,
40
- },
73
+ bounding,
41
74
  position,
42
75
  enterable,
43
76
  title: '',
@@ -52,8 +85,6 @@ function createTooltip(root, x0, y0, position, enterable) {
52
85
  },
53
86
  },
54
87
  });
55
- // @ts-ignore
56
- const container = getContainer(root);
57
88
  container.appendChild(tooltipElement.HTMLTooltipElement);
58
89
  return tooltipElement;
59
90
  }
@@ -68,11 +99,25 @@ function showTooltip({
68
99
  single,
69
100
  position = 'right-bottom',
70
101
  enterable = false,
102
+ mount,
103
+ bounding,
71
104
  }) {
72
105
  // All the views share the same tooltip.
73
- const container = single ? getContainer(root) : root;
74
- const { tooltipElement = createTooltip(root, x, y, position, enterable) } =
75
- container;
106
+ const canvasContainer = root.getRootNode().defaultView.getConfig().container;
107
+ const container = single ? getContainer(root, mount) : root;
108
+ const b = bounding || getBounding(root);
109
+ const containerOffset = getContainerOffset(canvasContainer, container);
110
+ const {
111
+ tooltipElement = createTooltip(
112
+ container,
113
+ x,
114
+ y,
115
+ position,
116
+ enterable,
117
+ b,
118
+ containerOffset,
119
+ ),
120
+ } = container;
76
121
  const { items, title = '' } = data;
77
122
  tooltipElement.update({
78
123
  x,
@@ -88,10 +133,15 @@ function showTooltip({
88
133
  container.tooltipElement = tooltipElement;
89
134
  }
90
135
 
91
- function hideTooltip(root, single) {
92
- const container = single ? getContainer(root) : root;
136
+ function hideTooltip({ root, single, emitter, nativeEvent = true, mount }) {
137
+ const container = single ? getContainer(root, mount) : root;
93
138
  const { tooltipElement } = container;
94
- if (tooltipElement) tooltipElement.hide();
139
+ if (tooltipElement) {
140
+ tooltipElement.hide();
141
+ if (nativeEvent) {
142
+ emitter.emit('tooltip:hide', { nativeEvent });
143
+ }
144
+ }
95
145
  }
96
146
 
97
147
  function destroyTooltip(root) {
@@ -300,6 +350,7 @@ export function seriesTooltip(
300
350
  crosshairs,
301
351
  render,
302
352
  groupName,
353
+ emitter,
303
354
  wait = 50,
304
355
  leading = true,
305
356
  trailing = false,
@@ -309,6 +360,8 @@ export function seriesTooltip(
309
360
  single = true,
310
361
  position,
311
362
  enterable,
363
+ mount,
364
+ bounding,
312
365
  style: _style = {},
313
366
  ...rest
314
367
  }: Record<string, any>,
@@ -468,6 +521,8 @@ export function seriesTooltip(
468
521
  single,
469
522
  position,
470
523
  enterable,
524
+ mount,
525
+ bounding,
471
526
  });
472
527
  }
473
528
 
@@ -483,16 +538,41 @@ export function seriesTooltip(
483
538
  polar,
484
539
  });
485
540
  }
541
+
542
+ emitter.emit('tooltip:show', {
543
+ ...event,
544
+ nativeEvent: true,
545
+ data: { data: { x: invert(scale.x, abstractX(focus), true) } },
546
+ });
486
547
  },
487
548
  wait,
488
549
  { leading, trailing },
489
550
  ) as (...args: any[]) => void;
490
551
 
491
552
  const hide = () => {
492
- hideTooltip(root, single);
553
+ hideTooltip({ root, single, emitter, mount });
493
554
  if (crosshairs) hideRuleY(root);
494
555
  };
495
556
 
557
+ const onTooltipShow = ({ nativeEvent, data }) => {
558
+ if (nativeEvent) return;
559
+ const { x } = data.data;
560
+ const { x: scaleX } = scale;
561
+ const x1 = scaleX.map(x);
562
+ const [x2, y2] = coordinate.map([x1, 0.5]);
563
+ const {
564
+ min: [minX, minY],
565
+ } = root.getRenderBounds();
566
+ update({ offsetX: x2 + minX, offsetY: y2 + minY });
567
+ };
568
+
569
+ const onTooltipHide = () => {
570
+ hideTooltip({ root, single, emitter, nativeEvent: false, mount });
571
+ };
572
+
573
+ emitter.on('tooltip:show', onTooltipShow);
574
+ emitter.on('tooltip:hide', onTooltipHide);
575
+
496
576
  root.addEventListener('pointerenter', update);
497
577
  root.addEventListener('pointermove', update);
498
578
  root.addEventListener('pointerleave', hide);
@@ -501,6 +581,8 @@ export function seriesTooltip(
501
581
  root.removeEventListener('pointerenter', update);
502
582
  root.removeEventListener('pointermove', update);
503
583
  root.removeEventListener('pointerleave', hide);
584
+ emitter.off('tooltip:show', onTooltipShow);
585
+ emitter.off('tooltip:hide', onTooltipHide);
504
586
  destroyTooltip(root);
505
587
  if (crosshairs) hideRuleY(root);
506
588
  };
@@ -518,6 +600,7 @@ export function tooltip(
518
600
  groupName,
519
601
  sort: sortFunction,
520
602
  filter: filterFunction,
603
+ emitter,
521
604
  wait = 50,
522
605
  leading = true,
523
606
  trailing = false,
@@ -525,6 +608,10 @@ export function tooltip(
525
608
  single = true,
526
609
  position,
527
610
  enterable,
611
+ datum,
612
+ view,
613
+ mount,
614
+ bounding,
528
615
  }: Record<string, any>,
529
616
  ) {
530
617
  const elements = elementsof(root);
@@ -535,7 +622,7 @@ export function tooltip(
535
622
  (event) => {
536
623
  const { target: element } = event;
537
624
  if (!elementSet.has(element)) {
538
- hideTooltip(root, single);
625
+ hideTooltip({ root, single, emitter, mount });
539
626
  return;
540
627
  }
541
628
  const k = groupKey(element);
@@ -554,7 +641,7 @@ export function tooltip(
554
641
  }
555
642
 
556
643
  if (isEmptyTooltipData(data)) {
557
- hideTooltip(root, single);
644
+ hideTooltip({ root, single, emitter, mount });
558
645
  return;
559
646
  }
560
647
 
@@ -569,6 +656,16 @@ export function tooltip(
569
656
  single,
570
657
  position,
571
658
  enterable,
659
+ mount,
660
+ bounding,
661
+ });
662
+
663
+ emitter.emit('tooltip:show', {
664
+ ...event,
665
+ nativeEvent: true,
666
+ data: {
667
+ data: dataOf(element, view),
668
+ },
572
669
  });
573
670
  },
574
671
  wait,
@@ -578,9 +675,34 @@ export function tooltip(
578
675
  const pointerout = (event) => {
579
676
  const { target: element } = event;
580
677
  if (!elementSet.has(element)) return;
581
- hideTooltip(root, single);
678
+ hideTooltip({ root, single, emitter, mount });
582
679
  };
583
680
 
681
+ const onTooltipShow = ({ nativeEvent, data }) => {
682
+ if (nativeEvent) return;
683
+ const element = elements.find((d) =>
684
+ Object.entries(data.data).every(
685
+ ([key, value]) => datum(d)[key] === value,
686
+ ),
687
+ );
688
+ if (!element) return;
689
+ const bbox = element.getBBox();
690
+ const { x, y, width, height } = bbox;
691
+ pointerover({
692
+ target: element,
693
+ offsetX: x + width / 2,
694
+ offsetY: y + height / 2,
695
+ });
696
+ };
697
+
698
+ const onTooltipHide = ({ nativeEvent }: any = {}) => {
699
+ if (nativeEvent) return;
700
+ hideTooltip({ root, single, emitter, nativeEvent: false, mount });
701
+ };
702
+
703
+ emitter.on('tooltip:show', onTooltipShow);
704
+ emitter.on('tooltip:hide', onTooltipHide);
705
+
584
706
  root.addEventListener('pointerover', pointerover);
585
707
  root.addEventListener('pointermove', pointerover);
586
708
  root.addEventListener('pointerout', pointerout);
@@ -589,6 +711,8 @@ export function tooltip(
589
711
  root.removeEventListener('pointerover', pointerover);
590
712
  root.removeEventListener('pointermove', pointerover);
591
713
  root.removeEventListener('pointerout', pointerout);
714
+ emitter.off('tooltip:show', onTooltipShow);
715
+ emitter.off('tooltip:hide', onTooltipHide);
592
716
  destroyTooltip(root);
593
717
  };
594
718
  }
@@ -603,7 +727,7 @@ export function Tooltip(options) {
603
727
  facet = false,
604
728
  ...rest
605
729
  } = options;
606
- return (target, viewInstances) => {
730
+ return (target, viewInstances, emitter) => {
607
731
  const { container, view } = target;
608
732
  const { scale, markState, coordinate } = view;
609
733
  // Get default value from mark states.
@@ -621,6 +745,7 @@ export function Tooltip(options) {
621
745
  coordinate,
622
746
  crosshairs: maybeValue(crosshairs, defaultShowCrosshairs),
623
747
  item,
748
+ emitter,
624
749
  });
625
750
  }
626
751
 
@@ -649,16 +774,20 @@ export function Tooltip(options) {
649
774
  item,
650
775
  startX,
651
776
  startY,
777
+ emitter,
652
778
  });
653
779
  }
654
780
 
655
781
  return tooltip(plotArea, {
656
782
  ...rest,
783
+ datum: createDatumof(view),
657
784
  elements: selectG2Elements,
658
785
  scale,
659
786
  coordinate,
660
787
  groupKey: shared ? createXKey(view) : undefined,
661
788
  item,
789
+ emitter,
790
+ view,
662
791
  });
663
792
  };
664
793
  }
@@ -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/gauge.ts CHANGED
@@ -10,20 +10,27 @@ import { Radial } from '../coordinate';
10
10
  import { applyStyle, getOrigin } from '../shape/utils';
11
11
  import { select } from '../utils/selection';
12
12
 
13
+ export type GaugeData = {
14
+ target?: number;
15
+ total?: number;
16
+ percent?: number;
17
+ name?: string;
18
+ thresholds?: number[];
19
+ };
20
+
13
21
  export type GaugeOptions = Omit<GaugeMark, 'type'>;
14
22
 
15
- function dataTransform(data, scale) {
16
- const { name = 'score', target, total, percent } = data;
23
+ function dataTransform(data: GaugeData, scale) {
24
+ const { name = 'score', target, total, percent, thresholds = [] } = data;
17
25
  const _target = percent || target;
18
26
  const _total = percent ? 1 : total;
19
- const { color } = scale;
20
27
  const newScale = {
21
28
  y: {
22
29
  domain: [0, _total],
23
30
  },
24
31
  ...scale,
25
32
  };
26
- if (!color) {
33
+ if (!thresholds.length) {
27
34
  return {
28
35
  targetData: [{ x: name, y: _target, color: 'target' }],
29
36
  totalData: [
@@ -35,12 +42,11 @@ function dataTransform(data, scale) {
35
42
  scale: newScale,
36
43
  };
37
44
  }
38
- const { domain = [] } = color;
39
45
  return {
40
46
  targetData: [{ x: name, y: _target, color: 'target' }],
41
- totalData: domain.map((d, i) => ({
47
+ totalData: thresholds.map((d, i) => ({
42
48
  x: name,
43
- y: i >= 1 ? d - domain[i - 1] : d,
49
+ y: i >= 1 ? d - thresholds[i - 1] : d,
44
50
  color: i,
45
51
  })),
46
52
  target: _target,
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';