@antv/g2 5.0.0-beta.11 → 5.0.0-beta.12

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 (251) hide show
  1. package/dist/g2-lite.min.js +3 -3
  2. package/dist/g2.min.js +3 -3
  3. package/esm/api/chart.d.ts +2 -1
  4. package/esm/api/chart.js +2 -2
  5. package/esm/api/chart.js.map +1 -1
  6. package/esm/api/composition/base.d.ts +1 -1
  7. package/esm/api/composition/base.js +6 -17
  8. package/esm/api/composition/base.js.map +1 -1
  9. package/esm/api/composition/facetCircle.d.ts +2 -2
  10. package/esm/api/composition/facetCircle.js +2 -2
  11. package/esm/api/composition/facetCircle.js.map +1 -1
  12. package/esm/api/composition/facetRect.d.ts +2 -2
  13. package/esm/api/composition/facetRect.js +2 -2
  14. package/esm/api/composition/facetRect.js.map +1 -1
  15. package/esm/api/composition/geoPath.d.ts +2 -2
  16. package/esm/api/composition/geoPath.js +2 -2
  17. package/esm/api/composition/geoPath.js.map +1 -1
  18. package/esm/api/composition/geoView.d.ts +2 -2
  19. package/esm/api/composition/geoView.js +2 -2
  20. package/esm/api/composition/geoView.js.map +1 -1
  21. package/esm/api/composition/index.d.ts +2 -2
  22. package/esm/api/composition/index.js +2 -2
  23. package/esm/api/composition/index.js.map +1 -1
  24. package/esm/api/composition/repeatMatrix.d.ts +2 -2
  25. package/esm/api/composition/repeatMatrix.js +2 -2
  26. package/esm/api/composition/repeatMatrix.js.map +1 -1
  27. package/esm/api/composition/spaceFlex.d.ts +2 -2
  28. package/esm/api/composition/spaceFlex.js +2 -2
  29. package/esm/api/composition/spaceFlex.js.map +1 -1
  30. package/esm/api/composition/spaceLayer.d.ts +2 -2
  31. package/esm/api/composition/spaceLayer.js +2 -2
  32. package/esm/api/composition/spaceLayer.js.map +1 -1
  33. package/esm/api/composition/timingKeyframe.d.ts +2 -2
  34. package/esm/api/composition/timingKeyframe.js +2 -2
  35. package/esm/api/composition/timingKeyframe.js.map +1 -1
  36. package/esm/api/composition/view.d.ts +2 -2
  37. package/esm/api/composition/view.js +2 -2
  38. package/esm/api/composition/view.js.map +1 -1
  39. package/esm/api/index.d.ts +2 -0
  40. package/esm/api/index.js +2 -0
  41. package/esm/api/index.js.map +1 -1
  42. package/esm/api/mark/base.d.ts +1 -1
  43. package/esm/api/mark/base.js +7 -7
  44. package/esm/api/mark/base.js.map +1 -1
  45. package/esm/api/mark/index.d.ts +1 -0
  46. package/esm/api/mark/index.js +1 -0
  47. package/esm/api/mark/index.js.map +1 -1
  48. package/esm/api/mark/mark.d.ts +27 -27
  49. package/esm/api/mark/mark.js +28 -27
  50. package/esm/api/mark/mark.js.map +1 -1
  51. package/esm/api/mark/types.d.ts +1 -0
  52. package/esm/api/node.d.ts +1 -1
  53. package/esm/api/node.js +1 -1
  54. package/esm/api/node.js.map +1 -1
  55. package/esm/api/props.d.ts +1 -1
  56. package/esm/api/props.js +20 -0
  57. package/esm/api/props.js.map +1 -1
  58. package/esm/composition/timingKeyframe.js +13 -7
  59. package/esm/composition/timingKeyframe.js.map +1 -1
  60. package/esm/index.d.ts +1 -1
  61. package/esm/index.js +1 -1
  62. package/esm/index.js.map +1 -1
  63. package/esm/interaction/builtin/tooltip.d.ts +10 -2
  64. package/esm/interaction/index.d.ts +1 -0
  65. package/esm/interaction/index.js +1 -0
  66. package/esm/interaction/index.js.map +1 -1
  67. package/esm/interaction/native/event.d.ts +1 -0
  68. package/esm/interaction/native/event.js +42 -0
  69. package/esm/interaction/native/event.js.map +1 -0
  70. package/esm/interaction/native/tooltip.d.ts +2 -3
  71. package/esm/interaction/native/tooltip.js +68 -73
  72. package/esm/interaction/native/tooltip.js.map +1 -1
  73. package/esm/mark/utils.js +1 -5
  74. package/esm/mark/utils.js.map +1 -1
  75. package/esm/runtime/mark.js +5 -3
  76. package/esm/runtime/mark.js.map +1 -1
  77. package/esm/runtime/plot.js +33 -16
  78. package/esm/runtime/plot.js.map +1 -1
  79. package/esm/runtime/transform.d.ts +5 -0
  80. package/esm/runtime/transform.js +62 -4
  81. package/esm/runtime/transform.js.map +1 -1
  82. package/esm/runtime/types/common.d.ts +1 -0
  83. package/esm/runtime/types/component.d.ts +2 -1
  84. package/esm/runtime/types/options.d.ts +6 -1
  85. package/esm/spec/geometry.d.ts +33 -17
  86. package/esm/spec/interaction.d.ts +6 -1
  87. package/esm/stdlib/index.js +2 -2
  88. package/esm/stdlib/index.js.map +1 -1
  89. package/esm/transform/groupN.js +2 -2
  90. package/esm/transform/groupN.js.map +1 -1
  91. package/esm/transform/maybeTitle.js +13 -3
  92. package/esm/transform/maybeTitle.js.map +1 -1
  93. package/esm/transform/maybeTooltip.js +20 -20
  94. package/esm/transform/maybeTooltip.js.map +1 -1
  95. package/esm/transform/maybeZeroX.js +2 -2
  96. package/esm/transform/maybeZeroX.js.map +1 -1
  97. package/esm/transform/maybeZeroY.js +2 -2
  98. package/esm/transform/maybeZeroY.js.map +1 -1
  99. package/esm/transform/maybeZeroY1.js +2 -2
  100. package/esm/transform/maybeZeroY1.js.map +1 -1
  101. package/esm/transform/utils/helper.d.ts +12 -0
  102. package/esm/transform/utils/helper.js +8 -0
  103. package/esm/transform/utils/helper.js.map +1 -1
  104. package/esm/utils/helper.js +4 -1
  105. package/esm/utils/helper.js.map +1 -1
  106. package/lib/api/chart.d.ts +2 -1
  107. package/lib/api/chart.js +2 -2
  108. package/lib/api/chart.js.map +1 -1
  109. package/lib/api/composition/base.d.ts +1 -1
  110. package/lib/api/composition/base.js +8 -18
  111. package/lib/api/composition/base.js.map +1 -1
  112. package/lib/api/composition/facetCircle.d.ts +2 -2
  113. package/lib/api/composition/facetCircle.js +1 -1
  114. package/lib/api/composition/facetCircle.js.map +1 -1
  115. package/lib/api/composition/facetRect.d.ts +2 -2
  116. package/lib/api/composition/facetRect.js +1 -1
  117. package/lib/api/composition/facetRect.js.map +1 -1
  118. package/lib/api/composition/geoPath.d.ts +2 -2
  119. package/lib/api/composition/geoPath.js +1 -1
  120. package/lib/api/composition/geoPath.js.map +1 -1
  121. package/lib/api/composition/geoView.d.ts +2 -2
  122. package/lib/api/composition/geoView.js +1 -1
  123. package/lib/api/composition/geoView.js.map +1 -1
  124. package/lib/api/composition/index.d.ts +2 -2
  125. package/lib/api/composition/index.js +2 -2
  126. package/lib/api/composition/index.js.map +1 -1
  127. package/lib/api/composition/repeatMatrix.d.ts +2 -2
  128. package/lib/api/composition/repeatMatrix.js +1 -1
  129. package/lib/api/composition/repeatMatrix.js.map +1 -1
  130. package/lib/api/composition/spaceFlex.d.ts +2 -2
  131. package/lib/api/composition/spaceFlex.js +1 -1
  132. package/lib/api/composition/spaceFlex.js.map +1 -1
  133. package/lib/api/composition/spaceLayer.d.ts +2 -2
  134. package/lib/api/composition/spaceLayer.js +1 -1
  135. package/lib/api/composition/spaceLayer.js.map +1 -1
  136. package/lib/api/composition/timingKeyframe.d.ts +2 -2
  137. package/lib/api/composition/timingKeyframe.js +1 -1
  138. package/lib/api/composition/timingKeyframe.js.map +1 -1
  139. package/lib/api/composition/view.d.ts +2 -2
  140. package/lib/api/composition/view.js +1 -1
  141. package/lib/api/composition/view.js.map +1 -1
  142. package/lib/api/index.d.ts +2 -0
  143. package/lib/api/index.js +5 -1
  144. package/lib/api/index.js.map +1 -1
  145. package/lib/api/mark/base.d.ts +1 -1
  146. package/lib/api/mark/base.js +9 -9
  147. package/lib/api/mark/base.js.map +1 -1
  148. package/lib/api/mark/index.d.ts +1 -0
  149. package/lib/api/mark/index.js +3 -1
  150. package/lib/api/mark/index.js.map +1 -1
  151. package/lib/api/mark/mark.d.ts +27 -27
  152. package/lib/api/mark/mark.js +27 -26
  153. package/lib/api/mark/mark.js.map +1 -1
  154. package/lib/api/mark/types.d.ts +1 -0
  155. package/lib/api/node.d.ts +1 -1
  156. package/lib/api/node.js +1 -1
  157. package/lib/api/node.js.map +1 -1
  158. package/lib/api/props.d.ts +1 -1
  159. package/lib/api/props.js +20 -0
  160. package/lib/api/props.js.map +1 -1
  161. package/lib/composition/timingKeyframe.js +13 -7
  162. package/lib/composition/timingKeyframe.js.map +1 -1
  163. package/lib/index.d.ts +1 -1
  164. package/lib/index.js +3 -1
  165. package/lib/index.js.map +1 -1
  166. package/lib/interaction/builtin/tooltip.d.ts +10 -2
  167. package/lib/interaction/index.d.ts +1 -0
  168. package/lib/interaction/index.js +3 -1
  169. package/lib/interaction/index.js.map +1 -1
  170. package/lib/interaction/native/event.d.ts +1 -0
  171. package/lib/interaction/native/event.js +46 -0
  172. package/lib/interaction/native/event.js.map +1 -0
  173. package/lib/interaction/native/tooltip.d.ts +2 -3
  174. package/lib/interaction/native/tooltip.js +67 -72
  175. package/lib/interaction/native/tooltip.js.map +1 -1
  176. package/lib/mark/utils.js +1 -5
  177. package/lib/mark/utils.js.map +1 -1
  178. package/lib/runtime/mark.js +4 -2
  179. package/lib/runtime/mark.js.map +1 -1
  180. package/lib/runtime/plot.js +33 -16
  181. package/lib/runtime/plot.js.map +1 -1
  182. package/lib/runtime/transform.d.ts +5 -0
  183. package/lib/runtime/transform.js +64 -4
  184. package/lib/runtime/transform.js.map +1 -1
  185. package/lib/runtime/types/common.d.ts +1 -0
  186. package/lib/runtime/types/component.d.ts +2 -1
  187. package/lib/runtime/types/options.d.ts +6 -1
  188. package/lib/spec/geometry.d.ts +33 -17
  189. package/lib/spec/interaction.d.ts +6 -1
  190. package/lib/stdlib/index.js +1 -1
  191. package/lib/stdlib/index.js.map +1 -1
  192. package/lib/transform/groupN.js +1 -1
  193. package/lib/transform/groupN.js.map +1 -1
  194. package/lib/transform/maybeTitle.js +12 -2
  195. package/lib/transform/maybeTitle.js.map +1 -1
  196. package/lib/transform/maybeTooltip.js +20 -20
  197. package/lib/transform/maybeTooltip.js.map +1 -1
  198. package/lib/transform/maybeZeroX.js +1 -1
  199. package/lib/transform/maybeZeroX.js.map +1 -1
  200. package/lib/transform/maybeZeroY.js +1 -1
  201. package/lib/transform/maybeZeroY.js.map +1 -1
  202. package/lib/transform/maybeZeroY1.js +1 -1
  203. package/lib/transform/maybeZeroY1.js.map +1 -1
  204. package/lib/transform/utils/helper.d.ts +12 -0
  205. package/lib/transform/utils/helper.js +11 -1
  206. package/lib/transform/utils/helper.js.map +1 -1
  207. package/lib/utils/helper.js +4 -1
  208. package/lib/utils/helper.js.map +1 -1
  209. package/package.json +1 -1
  210. package/src/api/chart.ts +2 -2
  211. package/src/api/composition/base.ts +5 -7
  212. package/src/api/composition/facetCircle.ts +2 -2
  213. package/src/api/composition/facetRect.ts +2 -2
  214. package/src/api/composition/geoPath.ts +2 -2
  215. package/src/api/composition/geoView.ts +2 -2
  216. package/src/api/composition/index.ts +2 -2
  217. package/src/api/composition/repeatMatrix.ts +2 -2
  218. package/src/api/composition/spaceFlex.ts +2 -2
  219. package/src/api/composition/spaceLayer.ts +2 -2
  220. package/src/api/composition/timingKeyframe.ts +2 -2
  221. package/src/api/composition/view.ts +2 -2
  222. package/src/api/index.ts +2 -0
  223. package/src/api/mark/base.ts +7 -7
  224. package/src/api/mark/index.ts +2 -0
  225. package/src/api/mark/mark.ts +28 -27
  226. package/src/api/mark/types.ts +1 -0
  227. package/src/api/node.ts +1 -1
  228. package/src/api/props.ts +22 -1
  229. package/src/composition/timingKeyframe.ts +13 -7
  230. package/src/index.ts +1 -1
  231. package/src/interaction/index.ts +1 -0
  232. package/src/interaction/native/event.ts +44 -0
  233. package/src/interaction/native/tooltip.ts +66 -92
  234. package/src/mark/utils.ts +1 -5
  235. package/src/runtime/mark.ts +6 -2
  236. package/src/runtime/plot.ts +44 -18
  237. package/src/runtime/transform.ts +71 -4
  238. package/src/runtime/types/common.ts +1 -0
  239. package/src/runtime/types/component.ts +2 -0
  240. package/src/runtime/types/options.ts +6 -1
  241. package/src/spec/geometry.ts +38 -30
  242. package/src/spec/interaction.ts +5 -1
  243. package/src/stdlib/index.ts +2 -0
  244. package/src/transform/groupN.ts +2 -2
  245. package/src/transform/maybeTitle.ts +11 -3
  246. package/src/transform/maybeTooltip.ts +17 -21
  247. package/src/transform/maybeZeroX.ts +2 -2
  248. package/src/transform/maybeZeroY.ts +2 -2
  249. package/src/transform/maybeZeroY1.ts +2 -2
  250. package/src/transform/utils/helper.ts +10 -0
  251. package/src/utils/helper.ts +6 -1
@@ -55,7 +55,7 @@ function createTooltip(root, x0, y0) {
55
55
  return tooltipElement;
56
56
  }
57
57
 
58
- function showTooltip(root, data, x, y) {
58
+ function showTooltip(root, data, x, y, render, event) {
59
59
  const { tooltipElement = createTooltip(root, x, y) } = root;
60
60
  const { items, title } = data;
61
61
  tooltipElement.show();
@@ -63,6 +63,9 @@ function showTooltip(root, data, x, y) {
63
63
  tooltipElement.update({
64
64
  items,
65
65
  title,
66
+ ...(render !== undefined && {
67
+ customContent: render(event, { items, title }),
68
+ }),
66
69
  });
67
70
  root.tooltipElement = tooltipElement;
68
71
  }
@@ -80,39 +83,16 @@ function destroyTooltip(root) {
80
83
  }
81
84
  }
82
85
 
83
- function filterDefined(obj) {
84
- return Object.fromEntries(
85
- Object.entries(obj).filter(([, value]) => defined(value)),
86
- );
87
- }
88
-
89
- function singleItem(element, item, scale) {
86
+ function singleItem(element) {
90
87
  const { __data__: datum } = element;
91
- const { title, ...rest } = datum;
92
- const defaultColor = itemColorOf(element);
93
- const items = Object.entries(rest)
94
- .filter(([key]) => key.startsWith('tooltip'))
95
- .map(([key, d]: any) => {
96
- const { field: f, title = f } = scale[key].getOptions();
97
- const {
98
- field = undefined,
99
- color = defaultColor,
100
- name = field || title,
101
- value,
102
- ...rest
103
- } = normalizeTooltip(d);
104
- return {
105
- ...rest,
106
- color,
107
- name,
108
- value,
109
- ...filterDefined(item({ channel: key, value })),
110
- };
111
- })
112
- .filter(({ value }) => value !== undefined);
88
+ const { title, items = [] } = datum;
89
+ const newItems = items.map(({ color = itemColorOf(element), ...item }) => ({
90
+ ...item,
91
+ color,
92
+ }));
113
93
  return {
114
94
  ...(title && { title }),
115
- items,
95
+ items: newItems,
116
96
  };
117
97
  }
118
98
 
@@ -157,39 +137,22 @@ function uniqueTitles(titles) {
157
137
 
158
138
  function groupItems(
159
139
  elements,
160
- item,
161
140
  scale,
162
141
  data = elements.map((d) => d['__data__']),
163
142
  ) {
164
143
  const T = uniqueTitles(data.map((d) => d.title)).filter(defined);
165
- const items = data.flatMap((datum, i) => {
144
+ const newItems = data.flatMap((datum, i) => {
166
145
  const element = elements[i];
167
- const { title, ...rest } = datum;
168
- const defaultColor = itemColorOf(element);
169
- return Object.entries(rest)
170
- .filter(([key]) => key.startsWith('tooltip'))
171
- .map(([key, d]: any) => {
172
- const { field: f, title = f } = scale[key].getOptions();
173
- const {
174
- field = undefined,
175
- color = defaultColor,
176
- name = groupNameOf(scale, datum) || field || title,
177
- value,
178
- ...rest
179
- } = normalizeTooltip(d);
180
- return {
181
- ...rest,
182
- name,
183
- color,
184
- value,
185
- ...filterDefined(item({ channel: key, value })),
186
- };
187
- })
188
- .filter(({ value }) => defined(value));
146
+ const { items = [], title } = datum;
147
+ return items.map(({ color = itemColorOf(element), name, ...item }) => ({
148
+ ...item,
149
+ color,
150
+ name: groupNameOf(scale, datum) || name || title,
151
+ }));
189
152
  });
190
153
  return {
191
154
  ...(T.length > 0 && { title: T.join(',') }),
192
- items,
155
+ items: newItems,
193
156
  };
194
157
  }
195
158
 
@@ -265,6 +228,7 @@ export function seriesTooltip(
265
228
  coordinate,
266
229
  crosshairs,
267
230
  item,
231
+ render,
268
232
  startX = 0,
269
233
  startY = 0,
270
234
  ...rest
@@ -283,6 +247,12 @@ export function seriesTooltip(
283
247
  else itemElements.push(element);
284
248
  }
285
249
 
250
+ // Sorted elements from top to bottom visually.
251
+ seriesElements.sort((a, b) => {
252
+ const minY = (d) => d.getBounds().min[1];
253
+ return minY(a) - minY(b);
254
+ });
255
+
286
256
  // Get sortedIndex and X for each series elements
287
257
  const elementSortedX = new Map(
288
258
  seriesElements.map((element) => {
@@ -310,32 +280,32 @@ export function seriesTooltip(
310
280
 
311
281
  const indexByFocus = (focus, I, X) => {
312
282
  const [normalizedX] = coordinate.invert(focus);
283
+ const finalX = normalizedX - offsetX;
284
+ const [minX, maxX] = sort([X[0], X[X.length - 1]]);
285
+ // Skip x out of range.
286
+ if (finalX < minX || finalX > maxX) return null;
313
287
  const search = bisector((i) => X[+i]).center;
314
- const i = search(I, normalizedX - offsetX);
288
+ const i = search(I, finalX);
315
289
  return I[i];
316
290
  };
317
291
 
318
- const indicesByFocus = (focus, I, X) => {
319
- const len = I.length;
320
- if (len === 0) return [];
321
- const [normalizedX] = coordinate.invert(focus);
322
- const x = normalizedX - offsetX;
323
- const search = bisector((i) => X[+i]).center;
324
- const center = search(I, x);
325
-
326
- // Find the least index and greatest index of X[center]
327
- let left = center;
328
- let right = center;
329
- while (left - 1 > 0 && X[I[left - 1]] == X[I[center]]) left--;
330
- while (right + 1 < len && X[I[right + 1]] === X[I[center]]) right++;
331
- return range(left, right + 1).map((i) => I[i]);
292
+ const elementsByFocus = (focus, elements) => {
293
+ const x = focus[0];
294
+ const extent = (d) => {
295
+ const { min, max } = d.getLocalBounds();
296
+ return sort([min[0], max[0]]);
297
+ };
298
+ return elements.filter((element) => {
299
+ const [min, max] = extent(element);
300
+ return x >= min && x <= max;
301
+ });
332
302
  };
333
303
 
334
304
  const seriesData = (element, index) => {
335
305
  const { __data__: data } = element;
336
306
  return Object.fromEntries(
337
307
  Object.entries(data)
338
- .filter(([key]) => key.startsWith('series'))
308
+ .filter(([key]) => key.startsWith('series') && key !== 'series')
339
309
  .map(([key, V]) => {
340
310
  const d = V[index];
341
311
  return [lowerFirst(key.replace('series', '')), d];
@@ -353,32 +323,38 @@ export function seriesTooltip(
353
323
  const focus = [mouse[0] - startX, mouse[1] - startY];
354
324
  if (!focus) return;
355
325
  // Get selected item element.
356
- const selectedItemIndices = indicesByFocus(focus, itemSortedIndex, itemX);
357
- const selectedItems = selectedItemIndices.map((i) => itemElements[i]);
326
+ const selectedItems = elementsByFocus(focus, itemElements);
358
327
 
359
328
  // Get selected data item from both series element and item element.
360
- const selectedSeriesData = seriesElements.map((element) => {
329
+ const selectedSeriesElements = [];
330
+ const selectedSeriesData = [];
331
+ for (const element of seriesElements) {
361
332
  const [sortedIndex, X] = elementSortedX.get(element);
362
333
  const index = indexByFocus(focus, sortedIndex, X);
363
- const d = seriesData(element, index);
364
- const { x, y } = d;
365
- return [d, coordinate.map([x + offsetX, y])] as const;
366
- });
334
+ if (index !== null) {
335
+ selectedSeriesElements.push(element);
336
+ const d = seriesData(element, index);
337
+ const { x, y } = d;
338
+ const p = coordinate.map([x + offsetX, y]);
339
+ selectedSeriesData.push([d, p] as const);
340
+ }
341
+ }
367
342
  const selectedData = [
368
343
  ...selectedSeriesData.map((d) => d[0]),
369
344
  ...selectedItems.map((d) => d.__data__),
370
345
  ];
371
346
 
372
347
  // Get the displayed tooltip data.
373
- const selectedElements = [...seriesElements, ...selectedItems];
374
- const tooltipData = groupItems(
375
- selectedElements,
376
- item,
377
- scale,
378
- selectedData,
379
- );
348
+ const selectedElements = [...selectedSeriesElements, ...selectedItems];
349
+ const tooltipData = groupItems(selectedElements, scale, selectedData);
380
350
 
381
- showTooltip(root, tooltipData, mouse[0] + x, mouse[1] + y);
351
+ // Hide tooltip with no selected tooltip.
352
+ if (selectedElements.length === 0) {
353
+ hide();
354
+ return;
355
+ }
356
+
357
+ showTooltip(root, tooltipData, mouse[0] + x, mouse[1] + y, render, event);
382
358
  if (crosshairs) {
383
359
  const points = selectedSeriesData.map((d) => d[1]);
384
360
  updateRuleY(root, points, { ...ruleStyle, height, startX, startY });
@@ -414,11 +390,11 @@ export function tooltip(
414
390
  {
415
391
  elements: elementsof,
416
392
  scale,
393
+ render,
417
394
  wait = 50,
418
395
  leading = true,
419
396
  trailing = false,
420
397
  groupKey = (d) => d, // group elements by specified key
421
- item,
422
398
  }: Record<string, any>,
423
399
  ) {
424
400
  const elements = elementsof(root);
@@ -432,11 +408,9 @@ export function tooltip(
432
408
  const k = groupKey(element);
433
409
  const group = keyGroup.get(k);
434
410
  const data =
435
- group.length === 1
436
- ? singleItem(group[0], item, scale)
437
- : groupItems(group, item, scale);
411
+ group.length === 1 ? singleItem(group[0]) : groupItems(group, scale);
438
412
  const { offsetX, offsetY } = event;
439
- showTooltip(root, data, offsetX, offsetY);
413
+ showTooltip(root, data, offsetX, offsetY, render, event);
440
414
  },
441
415
  wait,
442
416
  { leading, trailing },
package/src/mark/utils.ts CHANGED
@@ -26,11 +26,7 @@ export function baseChannels(options: ChannelOptions = {}): Channel[] {
26
26
  }
27
27
 
28
28
  export function baseGeometryChannels(options: ChannelOptions = {}): Channel[] {
29
- return [
30
- ...baseChannels(options),
31
- { name: 'title', scale: 'identity' },
32
- { name: 'tooltip', scale: 'identity', independent: true },
33
- ];
29
+ return [...baseChannels(options), { name: 'title', scale: 'identity' }];
34
30
  }
35
31
 
36
32
  export function tooltip2d() {
@@ -26,6 +26,8 @@ import {
26
26
  maybeVisualChannel,
27
27
  addGuideToScale,
28
28
  maybeNonAnimate,
29
+ normalizeTooltip,
30
+ extractTooltip,
29
31
  } from './transform';
30
32
 
31
33
  export async function initializeMark(
@@ -41,7 +43,7 @@ export async function initializeMark(
41
43
  context,
42
44
  );
43
45
 
44
- const { encode, scale, data } = transformedMark;
46
+ const { encode, scale, data, tooltip } = transformedMark;
45
47
 
46
48
  // Skip mark with non-tabular data. Do not skip empty
47
49
  // data, they are useful for facet to display axes.
@@ -109,7 +111,7 @@ export async function initializeMark(
109
111
  });
110
112
  });
111
113
 
112
- return [transformedMark, { ...partialProps, index: I, channels }];
114
+ return [transformedMark, { ...partialProps, index: I, channels, tooltip }];
113
115
  }
114
116
 
115
117
  export function createColumnOf(library: G2Library): ColumnOf {
@@ -152,9 +154,11 @@ async function applyMarkTransform(
152
154
  maybeArrayField,
153
155
  maybeNonAnimate,
154
156
  addGuideToScale,
157
+ normalizeTooltip,
155
158
  ...preInference.map(useTransform),
156
159
  ...transform.map(useTransform),
157
160
  ...postInference.map(useTransform),
161
+ extractTooltip,
158
162
  ];
159
163
  let index = [];
160
164
  let transformedMark = mark;
@@ -231,7 +231,7 @@ export async function plot<T extends G2ViewTree>(
231
231
  // Apply interactions.
232
232
  for (const option of inferInteraction(options)) {
233
233
  const interaction = useInteraction(option);
234
- const destroy = interaction(target, enterViewInstances);
234
+ const destroy = interaction(target, enterViewInstances, context.emitter);
235
235
  nameInteraction.set(option.type, { destroy });
236
236
  }
237
237
  }
@@ -248,7 +248,7 @@ export async function plot<T extends G2ViewTree>(
248
248
 
249
249
  // Apply new interaction.
250
250
  const interaction = useInteraction(option);
251
- const destroy = interaction(target, updateViewInstances);
251
+ const destroy = interaction(target, updateViewInstances, context.emitter);
252
252
  nameInteraction.set(options.type, { destroy });
253
253
  }
254
254
  }
@@ -317,7 +317,14 @@ async function initializeView(
317
317
  library: G2Library,
318
318
  ): Promise<[G2ViewDescriptor, G2ViewTree[]]> {
319
319
  const flattenOptions = await transformMarks(options, library);
320
+
320
321
  const mergedOptions = bubbleOptions(flattenOptions);
322
+ // @todo Remove this.
323
+ // !!! NOTE: Mute original view options.
324
+ // Update interaction and coordinate for this view.
325
+ options.interaction = mergedOptions.interaction;
326
+ options.coordinate = mergedOptions.coordinate;
327
+
321
328
  const transformedOptions = coordinate2Transform(mergedOptions, library);
322
329
  const state = await initializeMarks(transformedOptions, library);
323
330
  return initializeState(state, transformedOptions, library);
@@ -376,7 +383,7 @@ async function transformMarks(
376
383
  useMark as (options: G2MarkOptions) => CompositeMark
377
384
  )(mark)(options);
378
385
  const M = Array.isArray(marks) ? marks : [marks];
379
- discovered.unshift(...M.map((d, i) => ({ key: `${key}-${i}`, ...d })));
386
+ discovered.unshift(...M.map((d, i) => ({ ...d, key: `${key}-${i}` })));
380
387
  }
381
388
  }
382
389
 
@@ -520,7 +527,7 @@ function initializeState(
520
527
  modifier,
521
528
  key: markKey,
522
529
  } = mark;
523
- const { index, channels } = state;
530
+ const { index, channels, tooltip } = state;
524
531
  const scale = Object.fromEntries(
525
532
  channels.map(({ name, scale }) => [name, scale]),
526
533
  );
@@ -541,6 +548,8 @@ function initializeState(
541
548
  );
542
549
  const count = dataDomain || I.length;
543
550
  const T = modifier ? modifier(P, count, layout) : [];
551
+ const titleOf = (i) => tooltip.title?.[i]?.value;
552
+ const itemsOf = (i) => tooltip.items.map((V) => V[i]);
544
553
  const visualData: Record<string, any>[] = I.map((d, i) => {
545
554
  const datum = {
546
555
  points: P[i],
@@ -548,12 +557,20 @@ function initializeState(
548
557
  index: d,
549
558
  markKey,
550
559
  viewKey: key,
560
+ ...(tooltip && {
561
+ title: titleOf(d),
562
+ items: itemsOf(d),
563
+ }),
551
564
  };
552
565
  for (const [k, V] of Object.entries(value)) {
553
566
  datum[k] = V[d];
554
567
  if (S) datum[`series${upperFirst(k)}`] = S[i].map((i) => V[i]);
555
568
  }
556
569
  if (S) datum['seriesIndex'] = S[i];
570
+ if (S && tooltip) {
571
+ datum['seriesItems'] = S[i].map((si) => itemsOf(si));
572
+ datum['seriesTitle'] = S[i].map((si) => titleOf(si));
573
+ }
557
574
  return datum;
558
575
  });
559
576
  state.data = visualData;
@@ -726,12 +743,14 @@ async function plotView(
726
743
  // Render marks with corresponding data.
727
744
  for (const [mark, state] of markState.entries()) {
728
745
  const { data } = state;
729
- const { key, class: cls } = mark;
746
+ const { key, class: cls, type } = mark;
730
747
  const shapeFunction = createMarkShapeFunction(mark, state, view, library);
731
748
  const enterFunction = createEnterFunction(mark, state, view, library);
732
749
  const updateFunction = createUpdateFunction(mark, state, view, library);
733
750
  const exitFunction = createExitFunction(mark, state, view, library);
734
751
  const facetElements = selectFacetElements(selection, cls, 'element');
752
+ const classNames = [ELEMENT_CLASS_NAME];
753
+ if (typeof type === 'string') classNames.push(type);
735
754
  const T = selection
736
755
  .select(`#${key}`)
737
756
  .selectAll(className(ELEMENT_CLASS_NAME))
@@ -745,7 +764,7 @@ async function plotView(
745
764
  (enter) =>
746
765
  enter
747
766
  .append(shapeFunction)
748
- .attr('className', ELEMENT_CLASS_NAME)
767
+ .attr('className', classNames.join(' '))
749
768
  .transition(function (data) {
750
769
  return enterFunction(data, [this]);
751
770
  }),
@@ -1041,17 +1060,22 @@ function computeAnimationExtent(markState): [number, number] {
1041
1060
  for (const [mark, state] of markState) {
1042
1061
  const { animate = {} } = mark;
1043
1062
  const { data } = state;
1063
+ const { enter = {}, update = {}, exit = {} } = animate;
1064
+ const {
1065
+ type: defaultUpdateType,
1066
+ duration: defaultUpdateDuration = 300,
1067
+ delay: defaultUpdateDelay = 0,
1068
+ } = update;
1044
1069
  const {
1045
- updateType: defaultUpdateType,
1046
- updateDuration: defaultUpdateDuration = 300,
1047
- updateDelay: defaultUpdateDelay = 0,
1048
- enterType: defaultEnterType,
1049
- enterDuration: defaultEnterDuration = 300,
1050
- enterDelay: defaultEnterDelay = 0,
1051
- exitType: defaultExitType,
1052
- exitDuration: defaultExitDuration = 300,
1053
- exitDelay: defaultExitDelay = 0,
1054
- } = animate;
1070
+ type: defaultEnterType,
1071
+ duration: defaultEnterDuration = 300,
1072
+ delay: defaultEnterDelay = 0,
1073
+ } = enter;
1074
+ const {
1075
+ type: defaultExitType,
1076
+ duration: defaultExitDuration = 300,
1077
+ delay: defaultExitDelay = 0,
1078
+ } = exit;
1055
1079
  for (const d of data) {
1056
1080
  const {
1057
1081
  updateType = defaultUpdateType,
@@ -1192,7 +1216,7 @@ function createAnimationFunction(
1192
1216
  shapeName(mark, defaultShape),
1193
1217
  ).props;
1194
1218
  const { [type]: defaultEffectTiming = {} } = theme;
1195
- const animate = subObject(mark.animate || {}, type);
1219
+ const animate = mark.animate?.[type] || {};
1196
1220
  return (data, from, to) => {
1197
1221
  const {
1198
1222
  [`${type}Type`]: animation,
@@ -1272,7 +1296,9 @@ function inferTheme(theme: G2ThemeOptions = { type: 'light' }): G2ThemeOptions {
1272
1296
  * @todo Infer builtin tooltips.
1273
1297
  */
1274
1298
  function inferInteraction(view: G2View): G2InteractionOptions[] {
1275
- const defaults = {};
1299
+ const defaults = {
1300
+ event: true,
1301
+ };
1276
1302
  const { interaction = {} } = view;
1277
1303
  return Object.entries(deepMix(defaults, interaction))
1278
1304
  .filter((d) => !!d[1])
@@ -1,7 +1,7 @@
1
1
  import { Primitive } from 'd3-array';
2
2
  import { deepMix } from '@antv/util';
3
3
  import { indexOf, mapObject } from '../utils/array';
4
- import { composeAsync, defined } from '../utils/helper';
4
+ import { composeAsync, defined, isStrictObject } from '../utils/helper';
5
5
  import { useLibrary } from './library';
6
6
  import { createColumnOf } from './mark';
7
7
  import { Data, DataComponent } from './types/data';
@@ -105,6 +105,73 @@ export function extractColumns(
105
105
  return [I, { ...mark, encode: valuedEncode }];
106
106
  }
107
107
 
108
+ /**
109
+ * Normalize mark.tooltip to {title, items}.
110
+ */
111
+ export function normalizeTooltip(
112
+ I: number[],
113
+ mark: G2Mark,
114
+ context: TransformContext,
115
+ ): [number[], G2Mark] {
116
+ const { tooltip = {} } = mark;
117
+ const isFullTooltip = (tooltip) => {
118
+ if (Object.keys(tooltip).length === 0) return true;
119
+ const { title, items } = tooltip;
120
+ return title !== undefined || items !== undefined;
121
+ };
122
+ if (tooltip === null) return [I, mark];
123
+ if (Array.isArray(tooltip)) {
124
+ return [I, { ...mark, tooltip: { items: tooltip } }];
125
+ }
126
+ if (isStrictObject(tooltip) && isFullTooltip(tooltip)) {
127
+ return [I, { ...mark, tooltip }];
128
+ }
129
+ return [I, { ...mark, tooltip: { items: [tooltip] } }];
130
+ }
131
+
132
+ export function extractTooltip(
133
+ I: number[],
134
+ mark: G2Mark,
135
+ context: TransformContext,
136
+ ): [number[], G2Mark] {
137
+ const { data, encode, tooltip = {} } = mark;
138
+ if (tooltip === null) return [I, mark];
139
+ const valueOf = (item) => {
140
+ if (!item) return item;
141
+ if (typeof item === 'string') {
142
+ return I.map((i) => ({ name: item, value: data[i][item] }));
143
+ }
144
+ if (isStrictObject(item)) {
145
+ const { field, channel, color, name = field } = item;
146
+ const channelField = channel && encode[channel].field;
147
+ return I.map((i) => ({
148
+ name: name || channelField || channel,
149
+ color,
150
+ value: field
151
+ ? data[i][field]
152
+ : channel
153
+ ? encode[channel].value[i]
154
+ : null,
155
+ }));
156
+ }
157
+ if (typeof item === 'function') {
158
+ return I.map((i) => {
159
+ const v = item(data[i], i, data, encode);
160
+ if (isStrictObject(v)) return v;
161
+ return { value: v };
162
+ });
163
+ }
164
+ return item;
165
+ };
166
+ const { title, items = [], ...rest } = tooltip;
167
+ const newTooltip = {
168
+ title: valueOf(title),
169
+ items: items.map(valueOf),
170
+ ...rest,
171
+ };
172
+ return [I, { ...mark, tooltip: newTooltip }];
173
+ }
174
+
108
175
  export function maybeArrayField(
109
176
  I: number[],
110
177
  mark: G2Mark,
@@ -190,9 +257,9 @@ export function maybeNonAnimate(
190
257
  if (animate || animate === undefined) return [I, mark];
191
258
  deepMix(mark, {
192
259
  animate: {
193
- enterType: null,
194
- exitType: null,
195
- updateType: null,
260
+ enter: { type: null },
261
+ exit: { type: null },
262
+ update: { type: null },
196
263
  },
197
264
  });
198
265
  return [I, mark];
@@ -68,6 +68,7 @@ export type G2MarkState = {
68
68
  index?: number[];
69
69
  data?: Record<string, any>[];
70
70
  channels?: ChannelGroups[];
71
+ tooltip?: any; // @todo
71
72
  } & Omit<MarkProps, 'channels'>;
72
73
 
73
74
  export type MaybeArray<T> = T | T[];
@@ -1,5 +1,6 @@
1
1
  import { Coordinate, Transformation } from '@antv/coord';
2
2
  import { DisplayObject, IAnimation as GAnimation } from '@antv/g';
3
+ import EventEmitter from '@antv/event-emitter';
3
4
  import { G2ViewTree } from './options';
4
5
  import {
5
6
  G2Theme,
@@ -191,6 +192,7 @@ export type AnimationComponent<O = Record<string, unknown>> = G2BaseComponent<
191
192
  export type Interaction = (
192
193
  target: G2ViewInstance,
193
194
  viewInstances: G2ViewInstance[],
195
+ emitter?: EventEmitter,
194
196
  ) => void;
195
197
  export type InteractionComponent<O = Record<string, unknown>> = G2BaseComponent<
196
198
  Interaction,
@@ -106,12 +106,17 @@ export type G2Mark = {
106
106
  scale?: Record<string, G2ScaleOptions>;
107
107
  encode?: Record<string, any | G2EncodeOptions>;
108
108
  type?: string | MarkComponent;
109
- animate?: Record<string, Primitive>;
109
+ animate?: {
110
+ enter?: Record<string, Primitive>;
111
+ update?: Record<string, Primitive>;
112
+ exit?: Record<string, Primitive>;
113
+ };
110
114
  facet?: boolean;
111
115
  axis?: boolean | Record<string, any>;
112
116
  legend?: boolean | Record<string, any>;
113
117
  slider?: Record<string, any>;
114
118
  scrollbar?: Record<string, any>;
119
+ tooltip?: any; // @todo
115
120
  filter?: (i: number) => boolean;
116
121
  children?: G2MarkChildrenCallback;
117
122
  dataDomain?: number;