@antv/g2 5.0.0-beta.7 → 5.0.0-beta.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 (230) 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 +6 -2
  4. package/esm/api/chart.js +31 -14
  5. package/esm/api/chart.js.map +1 -1
  6. package/esm/api/composition/base.d.ts +18 -1
  7. package/esm/api/composition/base.js +44 -1
  8. package/esm/api/composition/base.js.map +1 -1
  9. package/esm/api/mark/base.d.ts +22 -0
  10. package/esm/api/mark/base.js +54 -0
  11. package/esm/api/mark/base.js.map +1 -0
  12. package/esm/api/mark/mark.d.ts +1 -7
  13. package/esm/api/mark/mark.js +1 -13
  14. package/esm/api/mark/mark.js.map +1 -1
  15. package/esm/api/props.js +0 -20
  16. package/esm/api/props.js.map +1 -1
  17. package/esm/component/axis.js +4 -0
  18. package/esm/component/axis.js.map +1 -1
  19. package/esm/component/legendContinuous.d.ts +1 -0
  20. package/esm/component/legendContinuous.js +14 -16
  21. package/esm/component/legendContinuous.js.map +1 -1
  22. package/esm/interaction/builtin/elementHighlight.d.ts +2 -0
  23. package/esm/interaction/builtin/elementHighlightByColor.d.ts +2 -0
  24. package/esm/interaction/builtin/elementHighlightByX.d.ts +2 -0
  25. package/esm/interaction/index.d.ts +1 -0
  26. package/esm/interaction/index.js +1 -0
  27. package/esm/interaction/index.js.map +1 -1
  28. package/esm/interaction/native/elementHighlight.d.ts +7 -2
  29. package/esm/interaction/native/elementHighlight.js +72 -19
  30. package/esm/interaction/native/elementHighlight.js.map +1 -1
  31. package/esm/interaction/native/elementHighlightByColor.d.ts +4 -1
  32. package/esm/interaction/native/elementHighlightByColor.js +19 -3
  33. package/esm/interaction/native/elementHighlightByColor.js.map +1 -1
  34. package/esm/interaction/native/elementHighlightByX.d.ts +3 -3
  35. package/esm/interaction/native/elementHighlightByX.js +19 -3
  36. package/esm/interaction/native/elementHighlightByX.js.map +1 -1
  37. package/esm/interaction/native/elementSelect.d.ts +1 -1
  38. package/esm/interaction/native/elementSelect.js +42 -21
  39. package/esm/interaction/native/elementSelect.js.map +1 -1
  40. package/esm/interaction/native/elementSelectByColor.js +3 -2
  41. package/esm/interaction/native/elementSelectByColor.js.map +1 -1
  42. package/esm/interaction/native/elementSelectByX.js +3 -2
  43. package/esm/interaction/native/elementSelectByX.js.map +1 -1
  44. package/esm/interaction/native/legendFilter.d.ts +4 -0
  45. package/esm/interaction/native/legendFilter.js +7 -4
  46. package/esm/interaction/native/legendFilter.js.map +1 -1
  47. package/esm/interaction/native/legendHighlight.d.ts +4 -0
  48. package/esm/interaction/native/legendHighlight.js +94 -0
  49. package/esm/interaction/native/legendHighlight.js.map +1 -0
  50. package/esm/interaction/native/utils.d.ts +13 -2
  51. package/esm/interaction/native/utils.js +151 -26
  52. package/esm/interaction/native/utils.js.map +1 -1
  53. package/esm/label-transform/overlapDodgeY.js +0 -8
  54. package/esm/label-transform/overlapDodgeY.js.map +1 -1
  55. package/esm/mark/area.js +1 -1
  56. package/esm/mark/area.js.map +1 -1
  57. package/esm/runtime/plot.js +4 -4
  58. package/esm/runtime/plot.js.map +1 -1
  59. package/esm/runtime/render.js +18 -13
  60. package/esm/runtime/render.js.map +1 -1
  61. package/esm/runtime/types/options.d.ts +4 -2
  62. package/esm/shape/area/hv.d.ts +3 -0
  63. package/esm/shape/area/hv.js +9 -0
  64. package/esm/shape/area/hv.js.map +1 -0
  65. package/esm/shape/area/hvh.d.ts +3 -0
  66. package/esm/shape/area/{step.js → hvh.js} +3 -3
  67. package/esm/shape/area/hvh.js.map +1 -0
  68. package/esm/shape/area/vh.d.ts +3 -0
  69. package/esm/shape/area/vh.js +9 -0
  70. package/esm/shape/area/vh.js.map +1 -0
  71. package/esm/shape/index.d.ts +6 -2
  72. package/esm/shape/index.js +3 -1
  73. package/esm/shape/index.js.map +1 -1
  74. package/esm/shape/interval/color.d.ts +1 -0
  75. package/esm/shape/interval/color.js +65 -53
  76. package/esm/shape/interval/color.js.map +1 -1
  77. package/esm/spec/composition.d.ts +0 -3
  78. package/esm/spec/interaction.d.ts +24 -11
  79. package/esm/spec/transform.d.ts +13 -3
  80. package/esm/stdlib/index.js +3 -8
  81. package/esm/stdlib/index.js.map +1 -1
  82. package/esm/transform/sample.js +42 -26
  83. package/esm/transform/sample.js.map +1 -1
  84. package/esm/transform/utils/lttb.d.ts +5 -0
  85. package/esm/transform/utils/lttb.js +56 -0
  86. package/esm/transform/utils/lttb.js.map +1 -0
  87. package/esm/utils/event.d.ts +0 -2
  88. package/esm/utils/event.js +0 -18
  89. package/esm/utils/event.js.map +1 -1
  90. package/esm/utils/scale.d.ts +1 -0
  91. package/esm/utils/scale.js +3 -3
  92. package/esm/utils/scale.js.map +1 -1
  93. package/esm/utils/vector.d.ts +1 -1
  94. package/esm/utils/vector.js +1 -1
  95. package/lib/api/chart.d.ts +6 -2
  96. package/lib/api/chart.js +33 -13
  97. package/lib/api/chart.js.map +1 -1
  98. package/lib/api/composition/base.d.ts +18 -1
  99. package/lib/api/composition/base.js +44 -1
  100. package/lib/api/composition/base.js.map +1 -1
  101. package/lib/api/mark/base.d.ts +22 -0
  102. package/lib/api/mark/base.js +58 -0
  103. package/lib/api/mark/base.js.map +1 -0
  104. package/lib/api/mark/mark.d.ts +1 -7
  105. package/lib/api/mark/mark.js +28 -41
  106. package/lib/api/mark/mark.js.map +1 -1
  107. package/lib/api/props.js +0 -20
  108. package/lib/api/props.js.map +1 -1
  109. package/lib/component/axis.js +4 -0
  110. package/lib/component/axis.js.map +1 -1
  111. package/lib/component/legendContinuous.d.ts +1 -0
  112. package/lib/component/legendContinuous.js +14 -16
  113. package/lib/component/legendContinuous.js.map +1 -1
  114. package/lib/interaction/builtin/elementHighlight.d.ts +2 -0
  115. package/lib/interaction/builtin/elementHighlightByColor.d.ts +2 -0
  116. package/lib/interaction/builtin/elementHighlightByX.d.ts +2 -0
  117. package/lib/interaction/index.d.ts +1 -0
  118. package/lib/interaction/index.js +3 -1
  119. package/lib/interaction/index.js.map +1 -1
  120. package/lib/interaction/native/elementHighlight.d.ts +7 -2
  121. package/lib/interaction/native/elementHighlight.js +71 -18
  122. package/lib/interaction/native/elementHighlight.js.map +1 -1
  123. package/lib/interaction/native/elementHighlightByColor.d.ts +4 -1
  124. package/lib/interaction/native/elementHighlightByColor.js +18 -2
  125. package/lib/interaction/native/elementHighlightByColor.js.map +1 -1
  126. package/lib/interaction/native/elementHighlightByX.d.ts +3 -3
  127. package/lib/interaction/native/elementHighlightByX.js +18 -2
  128. package/lib/interaction/native/elementHighlightByX.js.map +1 -1
  129. package/lib/interaction/native/elementSelect.d.ts +1 -1
  130. package/lib/interaction/native/elementSelect.js +41 -20
  131. package/lib/interaction/native/elementSelect.js.map +1 -1
  132. package/lib/interaction/native/elementSelectByColor.js +2 -1
  133. package/lib/interaction/native/elementSelectByColor.js.map +1 -1
  134. package/lib/interaction/native/elementSelectByX.js +2 -1
  135. package/lib/interaction/native/elementSelectByX.js.map +1 -1
  136. package/lib/interaction/native/legendFilter.d.ts +4 -0
  137. package/lib/interaction/native/legendFilter.js +10 -3
  138. package/lib/interaction/native/legendFilter.js.map +1 -1
  139. package/lib/interaction/native/legendHighlight.d.ts +4 -0
  140. package/lib/interaction/native/legendHighlight.js +98 -0
  141. package/lib/interaction/native/legendHighlight.js.map +1 -0
  142. package/lib/interaction/native/utils.d.ts +13 -2
  143. package/lib/interaction/native/utils.js +154 -27
  144. package/lib/interaction/native/utils.js.map +1 -1
  145. package/lib/label-transform/overlapDodgeY.js +0 -8
  146. package/lib/label-transform/overlapDodgeY.js.map +1 -1
  147. package/lib/mark/area.js +1 -1
  148. package/lib/mark/area.js.map +1 -1
  149. package/lib/runtime/plot.js +3 -3
  150. package/lib/runtime/plot.js.map +1 -1
  151. package/lib/runtime/render.js +20 -12
  152. package/lib/runtime/render.js.map +1 -1
  153. package/lib/runtime/types/options.d.ts +4 -2
  154. package/lib/shape/area/hv.d.ts +3 -0
  155. package/lib/shape/area/hv.js +13 -0
  156. package/lib/shape/area/hv.js.map +1 -0
  157. package/lib/shape/area/hvh.d.ts +3 -0
  158. package/lib/shape/area/{step.js → hvh.js} +5 -5
  159. package/lib/shape/area/hvh.js.map +1 -0
  160. package/lib/shape/area/vh.d.ts +3 -0
  161. package/lib/shape/area/vh.js +13 -0
  162. package/lib/shape/area/vh.js.map +1 -0
  163. package/lib/shape/index.d.ts +6 -2
  164. package/lib/shape/index.js +7 -3
  165. package/lib/shape/index.js.map +1 -1
  166. package/lib/shape/interval/color.d.ts +1 -0
  167. package/lib/shape/interval/color.js +67 -54
  168. package/lib/shape/interval/color.js.map +1 -1
  169. package/lib/spec/composition.d.ts +0 -3
  170. package/lib/spec/interaction.d.ts +24 -11
  171. package/lib/spec/transform.d.ts +13 -3
  172. package/lib/stdlib/index.js +1 -1
  173. package/lib/stdlib/index.js.map +1 -1
  174. package/lib/transform/sample.js +42 -26
  175. package/lib/transform/sample.js.map +1 -1
  176. package/lib/transform/utils/lttb.d.ts +5 -0
  177. package/lib/transform/utils/lttb.js +60 -0
  178. package/lib/transform/utils/lttb.js.map +1 -0
  179. package/lib/utils/event.d.ts +0 -2
  180. package/lib/utils/event.js +1 -21
  181. package/lib/utils/event.js.map +1 -1
  182. package/lib/utils/scale.d.ts +1 -0
  183. package/lib/utils/scale.js +5 -4
  184. package/lib/utils/scale.js.map +1 -1
  185. package/lib/utils/vector.d.ts +1 -1
  186. package/lib/utils/vector.js +1 -1
  187. package/package.json +4 -3
  188. package/src/.DS_Store +0 -0
  189. package/src/api/chart.ts +37 -16
  190. package/src/api/composition/base.ts +46 -1
  191. package/src/api/mark/base.ts +58 -0
  192. package/src/api/mark/mark.ts +1 -20
  193. package/src/api/props.ts +0 -23
  194. package/src/component/axis.ts +7 -0
  195. package/src/component/legendContinuous.ts +15 -5
  196. package/src/data/.DS_Store +0 -0
  197. package/src/interaction/index.ts +1 -0
  198. package/src/interaction/native/elementHighlight.ts +78 -12
  199. package/src/interaction/native/elementHighlightByColor.ts +9 -4
  200. package/src/interaction/native/elementHighlightByX.ts +9 -5
  201. package/src/interaction/native/elementSelect.ts +58 -15
  202. package/src/interaction/native/elementSelectByColor.ts +4 -1
  203. package/src/interaction/native/elementSelectByX.ts +4 -1
  204. package/src/interaction/native/legendFilter.ts +8 -4
  205. package/src/interaction/native/legendHighlight.ts +87 -0
  206. package/src/interaction/native/utils.ts +183 -26
  207. package/src/label-transform/overlapDodgeY.ts +0 -7
  208. package/src/mark/area.ts +1 -1
  209. package/src/runtime/plot.ts +4 -4
  210. package/src/runtime/render.ts +26 -13
  211. package/src/runtime/types/options.ts +10 -2
  212. package/src/shape/.DS_Store +0 -0
  213. package/src/shape/area/hv.ts +20 -0
  214. package/src/shape/area/{step.ts → hvh.ts} +3 -3
  215. package/src/shape/area/vh.ts +20 -0
  216. package/src/shape/index.ts +7 -2
  217. package/src/shape/interval/color.ts +86 -54
  218. package/src/spec/composition.ts +0 -1
  219. package/src/spec/interaction.ts +35 -11
  220. package/src/spec/transform.ts +27 -2
  221. package/src/stdlib/index.ts +8 -6
  222. package/src/transform/sample.ts +54 -18
  223. package/src/transform/utils/lttb.ts +73 -0
  224. package/src/utils/event.ts +0 -26
  225. package/src/utils/scale.ts +4 -4
  226. package/src/utils/vector.ts +1 -1
  227. package/esm/shape/area/step.d.ts +0 -3
  228. package/esm/shape/area/step.js.map +0 -1
  229. package/lib/shape/area/step.d.ts +0 -3
  230. package/lib/shape/area/step.js.map +0 -1
@@ -8,6 +8,9 @@ import {
8
8
  useState,
9
9
  renderLink,
10
10
  applyDefaultsHighlightedStyle,
11
+ renderBackground,
12
+ selectPlotArea,
13
+ offsetTransform,
11
14
  } from './utils';
12
15
 
13
16
  /**
@@ -21,6 +24,10 @@ export function elementSelect(
21
24
  groupKey = (d) => d, // group elements by specified key
22
25
  link = false, // draw link or not
23
26
  single = false, // single select or not
27
+ coordinate,
28
+ background = false,
29
+ offset = 0,
30
+ scale,
24
31
  ...rest
25
32
  }: Record<string, any>,
26
33
  ) {
@@ -28,15 +35,39 @@ export function elementSelect(
28
35
  const elementSet = new Set(elements);
29
36
  const keyGroup = group(elements, groupKey);
30
37
  const valueof = createValueof(elements, datum);
31
- const [appendLink, removeLink] = renderLink(root, {
38
+ const [appendLink, removeLink] = renderLink({
39
+ link,
32
40
  elements,
33
41
  valueof,
42
+ coordinate,
34
43
  ...subObject(rest, 'link'),
35
44
  });
36
- const { setState, removeState, hasState } = useState(rest, valueof);
45
+
46
+ const [appendBackground, removeBackground] = renderBackground({
47
+ background,
48
+ coordinate,
49
+ scale,
50
+ valueof,
51
+ ...subObject(rest, 'background'),
52
+ });
53
+
54
+ const elementStyle = {
55
+ ...(offset !== 0 && {
56
+ // Apply translate to mock slice out.
57
+ selectedTransform: (_, i) => {
58
+ return offsetTransform(elements[i], offset, coordinate);
59
+ },
60
+ }),
61
+ ...rest,
62
+ };
63
+ const { setState, removeState, hasState } = useState(elementStyle, valueof);
64
+
37
65
  const clear = () => {
38
- for (const e of elements) removeState(e, 'selected', 'unselected');
39
- if (link) removeLink();
66
+ for (const e of elements) {
67
+ removeState(e, 'selected', 'unselected');
68
+ removeLink(e);
69
+ removeBackground(e);
70
+ }
40
71
  return;
41
72
  };
42
73
 
@@ -49,13 +80,14 @@ export function elementSelect(
49
80
  const groupSet = new Set(group);
50
81
  for (const e of elements) {
51
82
  if (groupSet.has(e)) setState(e, 'selected');
52
- else setState(e, 'unselected');
53
- }
54
- // Remove all links and append link for this group.
55
- if (link) {
56
- removeLink();
57
- appendLink(group);
83
+ else {
84
+ setState(e, 'unselected');
85
+ removeLink(e);
86
+ }
87
+ if (e !== element) removeBackground(e);
58
88
  }
89
+ appendLink(group);
90
+ appendBackground(element);
59
91
  }
60
92
  };
61
93
 
@@ -70,7 +102,8 @@ export function elementSelect(
70
102
  else if (!hasState(e, 'selected')) setState(e, 'unselected');
71
103
  }
72
104
  // Append link for each group only once.
73
- if (!hasSelectedGroup && link) appendLink(group, k);
105
+ if (!hasSelectedGroup && link) appendLink(group);
106
+ appendBackground(element);
74
107
  } else {
75
108
  // If there is no selected elements after resetting this group,
76
109
  // clear the states.
@@ -80,8 +113,11 @@ export function elementSelect(
80
113
  if (!hasSelected) return clear();
81
114
  // If there are still some selected elements after resetting this group,
82
115
  // only remove the link.
83
- for (const e of group) setState(e, 'unselected');
84
- if (link) removeLink(k);
116
+ for (const e of group) {
117
+ setState(e, 'unselected');
118
+ removeLink(e);
119
+ removeBackground(e);
120
+ }
85
121
  }
86
122
  };
87
123
 
@@ -98,17 +134,24 @@ export function elementSelect(
98
134
 
99
135
  return () => {
100
136
  root.removeEventListener('click', click);
101
- removeLink();
137
+ for (const e of elements) {
138
+ removeLink(e);
139
+ }
102
140
  };
103
141
  }
104
142
 
105
143
  export function ElementSelect(options) {
106
144
  return (context) => {
107
145
  const { container, view } = context;
108
- return elementSelect(container, {
146
+ const { coordinate, scale } = view;
147
+ const plotArea = selectPlotArea(container);
148
+ return elementSelect(plotArea, {
149
+ ...options,
109
150
  ...applyDefaultsHighlightedStyle(options),
110
151
  elements: selectG2Elements,
111
152
  datum: createDatumof(view),
153
+ coordinate,
154
+ scale,
112
155
  });
113
156
  };
114
157
  }
@@ -3,13 +3,16 @@ import {
3
3
  selectG2Elements,
4
4
  applyDefaultsHighlightedStyle,
5
5
  createColorKey,
6
+ selectPlotArea,
6
7
  } from './utils';
7
8
  import { elementSelect } from './elementSelect';
8
9
 
9
10
  export function ElementSelectByColor(options) {
10
11
  return (context) => {
11
12
  const { container, view } = context;
12
- return elementSelect(container, {
13
+ const plot = selectPlotArea(container);
14
+ return elementSelect(plot, {
15
+ ...options,
13
16
  ...applyDefaultsHighlightedStyle(options),
14
17
  elements: selectG2Elements,
15
18
  datum: createDatumof(view),
@@ -3,13 +3,16 @@ import {
3
3
  selectG2Elements,
4
4
  applyDefaultsHighlightedStyle,
5
5
  createXKey,
6
+ selectPlotArea,
6
7
  } from './utils';
7
8
  import { elementSelect } from './elementSelect';
8
9
 
9
10
  export function ElementSelectByX(options) {
10
11
  return (context) => {
11
12
  const { container, view } = context;
12
- return elementSelect(container, {
13
+ const plot = selectPlotArea(container);
14
+ return elementSelect(plot, {
15
+ ...options,
13
16
  ...applyDefaultsHighlightedStyle(options),
14
17
  elements: selectG2Elements,
15
18
  datum: createDatumof(view),
@@ -11,18 +11,22 @@ export const LEGEND_MAKER_CLASS_NAME = 'legend-category-item-marker';
11
11
 
12
12
  export const LEGEND_LABEL_CLASS_NAME = 'legend-category-item-label';
13
13
 
14
- function markerOf(item) {
14
+ export function markerOf(item) {
15
15
  return item.getElementsByClassName(LEGEND_MAKER_CLASS_NAME)[0];
16
16
  }
17
17
 
18
- function labelOf(item) {
18
+ export function labelOf(item) {
19
19
  return item.getElementsByClassName(LEGEND_LABEL_CLASS_NAME)[0];
20
20
  }
21
21
 
22
- function legendsOf(root) {
22
+ export function itemsOf(root) {
23
23
  return root.getElementsByClassName(LEGEND_ITEMS_CLASS_NAME);
24
24
  }
25
25
 
26
+ export function legendsOf(root) {
27
+ return root.getElementsByClassName(CATEGORY_LEGEND_CLASS_NAME);
28
+ }
29
+
26
30
  function builtInAccessors(selectedLegend) {
27
31
  // Get the value and scale type from legend.
28
32
  const { data } = selectedLegend.attributes;
@@ -31,7 +35,7 @@ function builtInAccessors(selectedLegend) {
31
35
  return [
32
36
  channel,
33
37
  {
34
- legends: legendsOf,
38
+ legends: itemsOf,
35
39
  marker: markerOf,
36
40
  label: labelOf,
37
41
  datum: (d) => {
@@ -0,0 +1,87 @@
1
+ import { group } from 'd3-array';
2
+ import { subObject } from '../../utils/helper';
3
+ import { selectG2Elements, useState } from './utils';
4
+ import { markerOf, labelOf, itemsOf, legendsOf } from './legendFilter';
5
+
6
+ export function LegendHighlight({ channel, ...rest }) {
7
+ return (context) => {
8
+ const { container, view } = context;
9
+ const legends = legendsOf(container);
10
+ const elements = selectG2Elements(container);
11
+ const channelOf = (legend) => {
12
+ const { __data__ } = legend.parentNode;
13
+ return __data__.scale.name;
14
+ };
15
+ const scaleOf = (channel) => {
16
+ const {
17
+ scale: { [channel]: scale },
18
+ } = view;
19
+ return scale;
20
+ };
21
+ for (const legend of legends) {
22
+ const datumOf = (item) => {
23
+ const { data } = legend.attributes;
24
+ const { __data__: datum } = item;
25
+ const { index } = datum;
26
+ return data[index].label;
27
+ };
28
+ const channel = channelOf(legend);
29
+ const items = itemsOf(legend);
30
+ const scale = scaleOf(channel);
31
+ const elementGroup = group<any, any>(elements, (d) =>
32
+ scale.invert(d.__data__[channel]),
33
+ );
34
+ const { setState, removeState } = useState(rest);
35
+ const markerStyle = subObject(rest, 'marker');
36
+ const labelStyle = subObject(rest, 'label');
37
+ const { setState: setM, removeState: removeM } = useState(markerStyle);
38
+ const { setState: setL, removeState: removeL } = useState(labelStyle);
39
+ const updateLegendState = (highlight) => {
40
+ for (const item of items) {
41
+ const marker = markerOf(item);
42
+ const label = labelOf(item);
43
+ if (item === highlight || highlight === null) {
44
+ removeM(marker, 'unhighlighted');
45
+ removeL(label, 'unhighlighted');
46
+ } else {
47
+ setM(marker, 'unhighlighted');
48
+ setL(label, 'unhighlighted');
49
+ }
50
+ }
51
+ };
52
+
53
+ const itemPointerover = new Map();
54
+
55
+ // Add listener for the legend items.
56
+ for (const item of items) {
57
+ const pointerover = () => {
58
+ const value = datumOf(item);
59
+ const elementSet = new Set(elementGroup.get(value));
60
+ for (const e of elements) {
61
+ if (elementSet.has(e)) setState(e, 'highlighted');
62
+ else setState(e, 'unhighlighted');
63
+ }
64
+ updateLegendState(item);
65
+ };
66
+ item.addEventListener('pointerover', pointerover);
67
+ itemPointerover.set(item, pointerover);
68
+ }
69
+
70
+ // Add listener for the legend group.
71
+ const pointerleave = () => {
72
+ for (const e of elements) {
73
+ removeState(e, 'unhighlighted', 'highlighted');
74
+ }
75
+ updateLegendState(null);
76
+ };
77
+ legend.addEventListener('pointerleave', pointerleave);
78
+
79
+ return () => {
80
+ legend.removeEventListener(pointerleave);
81
+ for (const [item, pointerover] of itemPointerover) {
82
+ item.removeEventListener(pointerover);
83
+ }
84
+ };
85
+ }
86
+ };
87
+ }
@@ -1,13 +1,20 @@
1
1
  import { DisplayObject, Path } from '@antv/g';
2
2
  import { path as d3Path } from 'd3-path';
3
+ import { sort } from 'd3-array';
4
+ import { Vector2 } from '@antv/coord';
3
5
  import { subObject } from '../../utils/helper';
4
- import { select } from '../../utils/selection';
6
+ import { G2Element, select } from '../../utils/selection';
5
7
  import { mapObject } from '../../utils/array';
6
8
  import {
7
9
  G2ViewDescriptor,
8
10
  ELEMENT_CLASS_NAME,
9
11
  PLOT_CLASS_NAME,
10
12
  } from '../../runtime';
13
+ import { isOrdinalScale } from '../../utils/scale';
14
+ import { rect } from '../../shape/interval/color';
15
+ import { isPolar, isTranspose } from '../../utils/coordinate';
16
+ import { reorder } from '../../shape/utils';
17
+ import { angle, angleBetween, sub } from '../../utils/vector';
11
18
 
12
19
  /**
13
20
  * Given root of chart returns elements to be manipulated
@@ -180,56 +187,206 @@ export function createValueof(elements, datum) {
180
187
  };
181
188
  }
182
189
 
183
- export function renderLink(
184
- root: DisplayObject,
185
- { valueof = (d, element) => d, ...style },
186
- ) {
190
+ export function renderLink({
191
+ link = false,
192
+ valueof = (d, element) => d,
193
+ coordinate,
194
+ ...style
195
+ }) {
187
196
  const LINK_CLASS_NAME = 'element-link';
188
- const append = (elements, key?) => {
197
+
198
+ if (!link) return [() => {}, () => {}];
199
+
200
+ const pointsOf = (element) => element.__data__.points;
201
+
202
+ const pathPointsOf = (P0, P1) => {
203
+ const [, p1, p2] = P0;
204
+ const [p0, , , p3] = P1;
205
+ const P: Vector2[] = [p1, p0, p3, p2];
206
+ return P;
207
+ };
208
+
209
+ const append = (elements) => {
189
210
  if (elements.length <= 1) return;
190
- const elementBBoxs = elements.map((d) => [d, d.getBBox()]);
191
211
 
192
- // Sort elements from top to bottom, left to right.
193
- elementBBoxs.sort(([, b0], [, b1]) => {
194
- const { x: x0, y: y0 } = b0;
195
- const { x: x1, y: y1 } = b1;
196
- return x0 - x1 === 0 ? y0 - y1 : x0 - x1;
212
+ // Sort elements by normalized x to avoid cross.
213
+ const sortedElements = sort<G2Element>(elements, (e0, e1) => {
214
+ const { x: x0 } = e0.__data__;
215
+ const { x: x1 } = e1.__data__;
216
+ const dx = x0 - x1;
217
+ return dx;
197
218
  });
198
219
 
199
- for (let i = 1; i < elementBBoxs.length; i++) {
220
+ for (let i = 1; i < sortedElements.length; i++) {
200
221
  const p = d3Path();
201
- const [e0, b0] = elementBBoxs[i - 1];
202
- const [, b1] = elementBBoxs[i];
203
- p.moveTo(b0.x + b0.width, b0.y);
204
- p.lineTo(b1.x, b1.y);
205
- p.lineTo(b1.x, b1.y + b1.height);
206
- p.lineTo(b0.x + b0.width, b0.y + b0.height);
222
+ const e0 = sortedElements[i - 1];
223
+ const e1 = sortedElements[i];
224
+ const [p0, p1, p2, p3] = pathPointsOf(pointsOf(e0), pointsOf(e1));
225
+ p.moveTo(...p0);
226
+ p.lineTo(...p1);
227
+ p.lineTo(...p2);
228
+ p.lineTo(...p3);
207
229
  p.closePath();
208
230
  const { fill = e0.getAttribute('fill'), ...rest } = mapObject(
209
231
  style,
210
232
  (d) => valueof(d, e0),
211
233
  );
212
- const path = new Path({
213
- className: `${LINK_CLASS_NAME}${key ? ' ' + key : ''}`,
234
+ const link = new Path({
235
+ className: LINK_CLASS_NAME,
214
236
  style: {
215
237
  d: p.toString(),
216
238
  fill,
239
+ zIndex: -2,
217
240
  ...rest,
218
241
  },
219
242
  });
220
- root.appendChild(path);
243
+ // @ts-ignore
244
+ e0.link?.remove();
245
+ e0.parentNode.appendChild(link);
246
+ // @ts-ignore
247
+ e0.link = link;
221
248
  }
222
249
  };
223
250
 
224
- const remove = (key?) => {
225
- const className = key || LINK_CLASS_NAME;
226
- const links = root.getElementsByClassName(className) || [];
227
- for (const link of links) link.remove();
251
+ const remove = (element) => {
252
+ element.link?.remove();
253
+ element.link = null;
228
254
  };
229
255
 
230
256
  return [append, remove] as const;
231
257
  }
232
258
 
259
+ // Apply translate to mock slice out.
260
+ export function offsetTransform(element, offset, coordinate) {
261
+ const append = (t) => {
262
+ const { transform } = element.style;
263
+ return transform ? `${transform} ${t}` : t;
264
+ };
265
+ if (isPolar(coordinate)) {
266
+ const { points } = element.__data__;
267
+ const [p0, p1] = isTranspose(coordinate) ? reorder(points) : points;
268
+ const center = coordinate.getCenter();
269
+ const v0 = sub(p0, center);
270
+ const v1 = sub(p1, center);
271
+ const a0 = angle(v0);
272
+ const da = angleBetween(v0, v1);
273
+ const amid = a0 + da / 2;
274
+ const dx = offset * Math.cos(amid);
275
+ const dy = offset * Math.sin(amid);
276
+ return append(`translate(${dx}, ${dy})`);
277
+ }
278
+ if (isTranspose(coordinate)) return append(`translate(${offset}, 0)`);
279
+ return append(`translate(0, ${-offset})`);
280
+ }
281
+
282
+ export function renderBackground({
283
+ background,
284
+ scale,
285
+ coordinate,
286
+ valueof,
287
+ padding = 0.001,
288
+ ...rest
289
+ }) {
290
+ const BACKGROUND_CLASS_NAME = 'element-background';
291
+
292
+ // Don't have background.
293
+ if (!background) return [() => {}, () => {}];
294
+
295
+ const extentOf = (scale, x) => {
296
+ const ax = scale.invert(x);
297
+ const mid = x + scale.getBandWidth(ax) / 2;
298
+ const half = scale.getStep(ax) / 2;
299
+ const offset = half * padding;
300
+ return [mid - half + offset, mid + half - offset];
301
+ };
302
+
303
+ const sizeXOf = (element) => {
304
+ const { x: scaleX } = scale;
305
+ if (!isOrdinalScale(scaleX)) return [0, 1];
306
+ const { __data__: data } = element;
307
+ const { x } = data;
308
+ const [e1, e2] = extentOf(scaleX, x);
309
+ return [e1, e2];
310
+ };
311
+
312
+ const sizeYOf = (element) => {
313
+ const { y: scaleY } = scale;
314
+ if (!isOrdinalScale(scaleY)) return [0, 1];
315
+ const { __data__: data } = element;
316
+ const { y } = data;
317
+ const [e1, e2] = extentOf(scaleY, y);
318
+ return [e1, e2];
319
+ };
320
+
321
+ const bandShapeOf = (element, style) => {
322
+ const [x1, x2] = sizeXOf(element);
323
+ const [y1, y2] = sizeYOf(element);
324
+ const points = [
325
+ [x1, y1],
326
+ [x2, y1],
327
+ [x2, y2],
328
+ [x1, y2],
329
+ ].map((d) => coordinate.map(d));
330
+ const { __data__: data } = element;
331
+ const { y: dy, y1: dy1 } = data;
332
+ return rect(points, { y: dy, y1: dy1 }, coordinate, style);
333
+ };
334
+
335
+ // Shape without ordinal style.
336
+ // Clone and scale it.
337
+ const cloneShapeOf = (element, style) => {
338
+ const {
339
+ transform = 'scale(1.2, 1.2)',
340
+ transformOrigin = 'center center',
341
+ stroke = '',
342
+ ...rest
343
+ } = style;
344
+ const finalStyle = { transform, transformOrigin, stroke, ...rest };
345
+ const shape = element.cloneNode(true);
346
+ for (const [key, value] of Object.entries(finalStyle)) {
347
+ shape.style[key] = value;
348
+ }
349
+ return shape;
350
+ };
351
+
352
+ const isOrdinalShape = () => {
353
+ const { x, y } = scale;
354
+ return [x, y].some(isOrdinalScale);
355
+ };
356
+
357
+ const append = (element) => {
358
+ if (element.background) element.background.remove();
359
+ const {
360
+ fill = '#CCD6EC',
361
+ fillOpacity = 0.3,
362
+ zIndex = -2,
363
+ ...style
364
+ } = mapObject(rest, (d) => valueof(d, element));
365
+ const finalStyle = {
366
+ ...style,
367
+ fill,
368
+ fillOpacity,
369
+ zIndex,
370
+ };
371
+ const shapeOf = isOrdinalShape() ? bandShapeOf : cloneShapeOf;
372
+ const shape = shapeOf(element, finalStyle);
373
+ shape.className = BACKGROUND_CLASS_NAME;
374
+ element.parentNode.appendChild(shape);
375
+ element.background = shape;
376
+ };
377
+
378
+ const remove = (element) => {
379
+ element.background?.remove();
380
+ element.background = null;
381
+ };
382
+
383
+ const is = (element) => {
384
+ return element.className === BACKGROUND_CLASS_NAME;
385
+ };
386
+
387
+ return [append, remove, is] as const;
388
+ }
389
+
233
390
  export function setCursor(root, cursor) {
234
391
  // @ts-ignore
235
392
  const canvas = root.getRootNode().defaultView;
@@ -17,14 +17,7 @@ function useMap<K, V>() {
17
17
  return [get, set] as const;
18
18
  }
19
19
 
20
- /**
21
- * @todo fixme
22
- */
23
20
  function getBoundsWithoutConnector(shape: DisplayObject) {
24
- // Remove this line when @antv/g fixed
25
- // https://github.com/antvis/G/pull/1269#pullrequestreview-1251209956
26
- if (!defined(shape.style.stroke)) shape.style.stroke = '';
27
-
28
21
  const node = shape.cloneNode(true);
29
22
  const connectorShape = node.getElementById('connector');
30
23
  connectorShape && node.removeChild(connectorShape);
package/src/mark/area.ts CHANGED
@@ -62,7 +62,7 @@ export const Area: MC<AreaOptions> = () => {
62
62
  };
63
63
  };
64
64
 
65
- const shapes = ['area', 'smooth', 'step'];
65
+ const shapes = ['area', 'smooth', 'hvh', 'hv', 'vh'];
66
66
 
67
67
  Area.props = {
68
68
  defaultShape: 'area',
@@ -4,7 +4,7 @@ import { deepMix, upperFirst } from '@antv/util';
4
4
  import { group } from 'd3-array';
5
5
  import { format } from 'd3-format';
6
6
  import { mapObject } from '../utils/array';
7
- import { CHART_LIFE_CIRCLE, emitEvent } from '../utils/event';
7
+ import { CHART_LIFE_CIRCLE } from '../utils/event';
8
8
  import {
9
9
  appendTransform,
10
10
  compose,
@@ -165,8 +165,7 @@ export async function plot<T extends G2ViewTree>(
165
165
  }
166
166
  }
167
167
 
168
- const { on } = options;
169
- emitEvent(on, CHART_LIFE_CIRCLE.BEFORE_PAINT);
168
+ context.emitter.emit(CHART_LIFE_CIRCLE.BEFORE_PAINT);
170
169
 
171
170
  // Plot chart.
172
171
  const enterContainer = new Map<G2ViewDescriptor, DisplayObject>();
@@ -266,9 +265,10 @@ export async function plot<T extends G2ViewTree>(
266
265
  keyframes.push(keyframe);
267
266
  }
268
267
 
268
+ context.views = views;
269
269
  context.animations = transitions;
270
270
 
271
- emitEvent(on, CHART_LIFE_CIRCLE.AFTER_PAINT);
271
+ context.emitter.emit(CHART_LIFE_CIRCLE.AFTER_PAINT);
272
272
 
273
273
  // Note!!!
274
274
  // The returned promise will never resolved if one of nodeGenerator