@antv/g2 5.2.11 → 5.3.0
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.
- package/README.md +3 -3
- package/README.zh-CN.md +3 -3
- package/dist/g2.lite.min.js +22 -22
- package/dist/g2.min.js +22 -22
- package/esm/animation/morphing.js +1 -2
- package/esm/animation/morphing.js.map +1 -1
- package/esm/animation/scaleInY.js +2 -20
- package/esm/animation/scaleInY.js.map +1 -1
- package/esm/api/runtime.js +2 -0
- package/esm/api/runtime.js.map +1 -1
- package/esm/component/legendCategory.js +5 -1
- package/esm/component/legendCategory.js.map +1 -1
- package/esm/data/utils/arc/arc.d.ts +25 -2
- package/esm/data/utils/arc/arc.js.map +1 -1
- package/esm/data/utils/arc/types.d.ts +27 -2
- package/esm/exports.d.ts +2 -0
- package/esm/exports.js +2 -0
- package/esm/exports.js.map +1 -1
- package/esm/interaction/elementSelect.d.ts +5 -2
- package/esm/interaction/elementSelect.js +80 -21
- package/esm/interaction/elementSelect.js.map +1 -1
- package/esm/interaction/event.d.ts +0 -1
- package/esm/interaction/event.js +1 -12
- package/esm/interaction/event.js.map +1 -1
- package/esm/interaction/legendHighlight.js.map +1 -1
- package/esm/interaction/tooltip.js +63 -42
- package/esm/interaction/tooltip.js.map +1 -1
- package/esm/interaction/utils.d.ts +12 -0
- package/esm/interaction/utils.js +48 -1
- package/esm/interaction/utils.js.map +1 -1
- package/esm/runtime/layout.js +1 -1
- package/esm/runtime/layout.js.map +1 -1
- package/esm/runtime/plot.js +9 -4
- package/esm/runtime/plot.js.map +1 -1
- package/esm/runtime/render.js +4 -2
- package/esm/runtime/render.js.map +1 -1
- package/esm/runtime/transform.d.ts +1 -0
- package/esm/runtime/transform.js +2 -1
- package/esm/runtime/transform.js.map +1 -1
- package/esm/shape/point/color.js +2 -1
- package/esm/shape/point/color.js.map +1 -1
- package/esm/spec/interaction.d.ts +3 -0
- package/esm/spec/transform.d.ts +0 -1
- package/esm/transform/bin.js.map +1 -1
- package/esm/transform/diffY.js +2 -5
- package/esm/transform/diffY.js.map +1 -1
- package/esm/transform/sort.js +19 -51
- package/esm/transform/sort.js.map +1 -1
- package/esm/transform/utils/helper.js +1 -0
- package/esm/transform/utils/helper.js.map +1 -1
- package/esm/utils/expr.d.ts +8 -0
- package/esm/utils/expr.js +51 -0
- package/esm/utils/expr.js.map +1 -0
- package/esm/utils/helper.d.ts +25 -0
- package/esm/utils/helper.js +77 -0
- package/esm/utils/helper.js.map +1 -1
- package/lib/animation/morphing.js +1 -2
- package/lib/animation/morphing.js.map +1 -1
- package/lib/animation/scaleInY.js +1 -19
- package/lib/animation/scaleInY.js.map +1 -1
- package/lib/api/runtime.js +2 -0
- package/lib/api/runtime.js.map +1 -1
- package/lib/component/legendCategory.js +5 -1
- package/lib/component/legendCategory.js.map +1 -1
- package/lib/data/utils/arc/arc.d.ts +25 -2
- package/lib/data/utils/arc/arc.js.map +1 -1
- package/lib/data/utils/arc/types.d.ts +27 -2
- package/lib/exports.d.ts +2 -0
- package/lib/exports.js +7 -1
- package/lib/exports.js.map +1 -1
- package/lib/interaction/elementSelect.d.ts +5 -2
- package/lib/interaction/elementSelect.js +79 -20
- package/lib/interaction/elementSelect.js.map +1 -1
- package/lib/interaction/event.d.ts +0 -1
- package/lib/interaction/event.js +3 -15
- package/lib/interaction/event.js.map +1 -1
- package/lib/interaction/legendHighlight.js.map +1 -1
- package/lib/interaction/tooltip.js +65 -44
- package/lib/interaction/tooltip.js.map +1 -1
- package/lib/interaction/utils.d.ts +12 -0
- package/lib/interaction/utils.js +49 -1
- package/lib/interaction/utils.js.map +1 -1
- package/lib/runtime/layout.js.map +1 -1
- package/lib/runtime/plot.js +9 -4
- package/lib/runtime/plot.js.map +1 -1
- package/lib/runtime/render.js +4 -2
- package/lib/runtime/render.js.map +1 -1
- package/lib/runtime/transform.d.ts +1 -0
- package/lib/runtime/transform.js +3 -2
- package/lib/runtime/transform.js.map +1 -1
- package/lib/shape/point/color.js +2 -1
- package/lib/shape/point/color.js.map +1 -1
- package/lib/spec/interaction.d.ts +3 -0
- package/lib/spec/transform.d.ts +0 -1
- package/lib/transform/bin.js.map +1 -1
- package/lib/transform/diffY.js +1 -4
- package/lib/transform/diffY.js.map +1 -1
- package/lib/transform/sort.js +19 -51
- package/lib/transform/sort.js.map +1 -1
- package/lib/transform/utils/helper.js +1 -0
- package/lib/transform/utils/helper.js.map +1 -1
- package/lib/utils/expr.d.ts +8 -0
- package/lib/utils/expr.js +55 -0
- package/lib/utils/expr.js.map +1 -0
- package/lib/utils/helper.d.ts +25 -0
- package/lib/utils/helper.js +82 -1
- package/lib/utils/helper.js.map +1 -1
- package/package.json +4 -3
- package/src/animation/morphing.ts +1 -2
- package/src/animation/scaleInY.ts +2 -45
- package/src/api/runtime.ts +3 -0
- package/src/component/legendCategory.ts +9 -1
- package/src/data/utils/arc/arc.ts +5 -5
- package/src/data/utils/arc/types.ts +28 -2
- package/src/exports.ts +3 -0
- package/src/interaction/elementSelect.ts +101 -15
- package/src/interaction/event.ts +1 -14
- package/src/interaction/legendHighlight.ts +4 -2
- package/src/interaction/tooltip.ts +89 -44
- package/src/interaction/utils.ts +60 -1
- package/src/runtime/layout.ts +15 -2
- package/src/runtime/plot.ts +8 -3
- package/src/runtime/render.ts +4 -3
- package/src/runtime/transform.ts +4 -1
- package/src/shape/point/color.ts +2 -1
- package/src/spec/interaction.ts +3 -0
- package/src/spec/transform.ts +0 -1
- package/src/transform/bin.ts +2 -2
- package/src/transform/diffY.ts +2 -6
- package/src/transform/sort.ts +20 -42
- package/src/transform/utils/helper.ts +1 -0
- package/src/utils/expr.ts +70 -0
- package/src/utils/helper.ts +84 -0
|
@@ -13,6 +13,9 @@ import {
|
|
|
13
13
|
offsetTransform,
|
|
14
14
|
mergeState,
|
|
15
15
|
selectElementByData,
|
|
16
|
+
createXKey,
|
|
17
|
+
createFindElementByEvent,
|
|
18
|
+
VALID_FIND_BY_X_MARKS,
|
|
16
19
|
} from './utils';
|
|
17
20
|
|
|
18
21
|
/**
|
|
@@ -24,19 +27,31 @@ export function elementSelect(
|
|
|
24
27
|
elements: elementsof, // given the root of chart returns elements to be manipulated
|
|
25
28
|
datum, // given each element returns the datum of it
|
|
26
29
|
groupKey = (d) => d, // group elements by specified key
|
|
30
|
+
regionGroupKey = (d) => d, // how to group elements when click region
|
|
27
31
|
link = false, // draw link or not
|
|
28
32
|
single = false, // single select or not
|
|
33
|
+
multipleSelectHotkey, // hotkey for multi-select mode
|
|
29
34
|
coordinate,
|
|
30
35
|
background = false,
|
|
31
36
|
scale,
|
|
32
37
|
emitter,
|
|
33
38
|
state = {},
|
|
39
|
+
region = false,
|
|
34
40
|
}: Record<string, any>,
|
|
35
41
|
) {
|
|
36
42
|
const elements = elementsof(root);
|
|
37
43
|
const elementSet = new Set(elements);
|
|
44
|
+
const findElement = createFindElementByEvent({
|
|
45
|
+
elementsof,
|
|
46
|
+
root,
|
|
47
|
+
coordinate,
|
|
48
|
+
scale,
|
|
49
|
+
});
|
|
38
50
|
const keyGroup = group(elements, groupKey);
|
|
51
|
+
const regionGroup = group(elements, regionGroupKey);
|
|
52
|
+
|
|
39
53
|
const valueof = createValueof(elements, datum);
|
|
54
|
+
|
|
40
55
|
const [appendLink, removeLink] = renderLink({
|
|
41
56
|
link,
|
|
42
57
|
elements,
|
|
@@ -57,7 +72,7 @@ export function elementSelect(
|
|
|
57
72
|
const elementStyle = deepMix(state, {
|
|
58
73
|
selected: {
|
|
59
74
|
...(state.selected?.offset && {
|
|
60
|
-
//Apply translate to mock slice out.
|
|
75
|
+
// Apply translate to mock slice out.
|
|
61
76
|
transform: (...params) => {
|
|
62
77
|
const value = state.selected.offset(...params);
|
|
63
78
|
const [, i] = params;
|
|
@@ -68,6 +83,8 @@ export function elementSelect(
|
|
|
68
83
|
});
|
|
69
84
|
|
|
70
85
|
const { setState, removeState, hasState } = useState(elementStyle, valueof);
|
|
86
|
+
let isMultiSelectMode = !single; // "single" determines whether to multi-select by default
|
|
87
|
+
let activeHotkey = null; // Track the currently active hotkey
|
|
71
88
|
|
|
72
89
|
const clear = (nativeEvent = true) => {
|
|
73
90
|
for (const e of elements) {
|
|
@@ -79,14 +96,22 @@ export function elementSelect(
|
|
|
79
96
|
return;
|
|
80
97
|
};
|
|
81
98
|
|
|
82
|
-
const singleSelect = (
|
|
99
|
+
const singleSelect = ({
|
|
100
|
+
event,
|
|
101
|
+
element,
|
|
102
|
+
nativeEvent = true,
|
|
103
|
+
filter = (el) => true,
|
|
104
|
+
groupBy = groupKey,
|
|
105
|
+
groupMap = keyGroup,
|
|
106
|
+
}) => {
|
|
107
|
+
const filteredElements = elements.filter(filter);
|
|
83
108
|
// Clear states if clicked selected element.
|
|
84
109
|
if (hasState(element, 'selected')) clear();
|
|
85
110
|
else {
|
|
86
|
-
const k =
|
|
87
|
-
const group =
|
|
111
|
+
const k = groupBy(element);
|
|
112
|
+
const group = groupMap.get(k);
|
|
88
113
|
const groupSet = new Set(group);
|
|
89
|
-
for (const e of
|
|
114
|
+
for (const e of filteredElements) {
|
|
90
115
|
if (groupSet.has(e)) setState(e, 'selected');
|
|
91
116
|
else {
|
|
92
117
|
setState(e, 'unselected');
|
|
@@ -108,13 +133,22 @@ export function elementSelect(
|
|
|
108
133
|
}
|
|
109
134
|
};
|
|
110
135
|
|
|
111
|
-
const multipleSelect = (
|
|
112
|
-
|
|
113
|
-
|
|
136
|
+
const multipleSelect = ({
|
|
137
|
+
event,
|
|
138
|
+
element,
|
|
139
|
+
nativeEvent = true,
|
|
140
|
+
filter = (el) => true,
|
|
141
|
+
groupBy = groupKey,
|
|
142
|
+
groupMap = keyGroup,
|
|
143
|
+
}) => {
|
|
144
|
+
const k = groupBy(element);
|
|
145
|
+
const group = groupMap.get(k);
|
|
114
146
|
const groupSet = new Set(group);
|
|
147
|
+
const filteredElements = elements.filter(filter);
|
|
148
|
+
|
|
115
149
|
if (!hasState(element, 'selected')) {
|
|
116
150
|
const hasSelectedGroup = group.some((e) => hasState(e, 'selected'));
|
|
117
|
-
for (const e of
|
|
151
|
+
for (const e of filteredElements) {
|
|
118
152
|
if (groupSet.has(e)) setState(e, 'selected');
|
|
119
153
|
else if (!hasState(e, 'selected')) setState(e, 'unselected');
|
|
120
154
|
}
|
|
@@ -148,19 +182,63 @@ export function elementSelect(
|
|
|
148
182
|
|
|
149
183
|
const click = (event) => {
|
|
150
184
|
const { target: element, nativeEvent = true } = event;
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
185
|
+
|
|
186
|
+
const select = !isMultiSelectMode ? singleSelect : multipleSelect;
|
|
187
|
+
let el = element;
|
|
188
|
+
const isClickElement = elementSet.has(element);
|
|
189
|
+
|
|
190
|
+
if (!region || isClickElement) {
|
|
191
|
+
// Click non-element shape, reset.
|
|
192
|
+
// Such as the rest of content area(background).
|
|
193
|
+
if (!isClickElement) return clear();
|
|
194
|
+
return select({ event, element: el, nativeEvent, groupBy: groupKey });
|
|
195
|
+
} else {
|
|
196
|
+
// Click background region area, select elements in the region.
|
|
197
|
+
// Get element at cursor.x position.
|
|
198
|
+
el = findElement(event);
|
|
199
|
+
|
|
200
|
+
if (!elementSet.has(el)) return clear();
|
|
201
|
+
|
|
202
|
+
return select({
|
|
203
|
+
event,
|
|
204
|
+
element: el,
|
|
205
|
+
nativeEvent,
|
|
206
|
+
filter: (el) => VALID_FIND_BY_X_MARKS.includes(el.markType),
|
|
207
|
+
groupBy: regionGroupKey,
|
|
208
|
+
groupMap: regionGroup,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// Handle keyboard events for multi-select mode
|
|
214
|
+
const hotkeys = Array.isArray(multipleSelectHotkey)
|
|
215
|
+
? multipleSelectHotkey
|
|
216
|
+
: [multipleSelectHotkey];
|
|
217
|
+
const handleKeyDown = (event) => {
|
|
218
|
+
if (hotkeys.includes(event.code) && !activeHotkey) {
|
|
219
|
+
activeHotkey = event.code;
|
|
220
|
+
isMultiSelectMode = true;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
const handleKeyUp = (event) => {
|
|
224
|
+
if (event.code === activeHotkey) {
|
|
225
|
+
activeHotkey = null;
|
|
226
|
+
isMultiSelectMode = false;
|
|
227
|
+
}
|
|
156
228
|
};
|
|
157
229
|
|
|
158
230
|
root.addEventListener('click', click);
|
|
231
|
+
if (multipleSelectHotkey) {
|
|
232
|
+
// If a hotkey is set, the initial state should be single mode
|
|
233
|
+
isMultiSelectMode = false;
|
|
234
|
+
document.addEventListener('keydown', handleKeyDown);
|
|
235
|
+
document.addEventListener('keyup', handleKeyUp);
|
|
236
|
+
}
|
|
159
237
|
|
|
160
238
|
const onSelect = (e) => {
|
|
161
239
|
const { nativeEvent, data } = e;
|
|
162
240
|
if (nativeEvent) return;
|
|
163
|
-
const selectedData =
|
|
241
|
+
const selectedData = !isMultiSelectMode ? data.data.slice(0, 1) : data.data;
|
|
164
242
|
for (const d of selectedData) {
|
|
165
243
|
const element = selectElementByData(elements, d, datum);
|
|
166
244
|
click({ target: element, nativeEvent: false });
|
|
@@ -177,6 +255,10 @@ export function elementSelect(
|
|
|
177
255
|
return () => {
|
|
178
256
|
for (const e of elements) removeLink(e);
|
|
179
257
|
root.removeEventListener('click', click);
|
|
258
|
+
if (multipleSelectHotkey) {
|
|
259
|
+
document.removeEventListener('keydown', handleKeyDown);
|
|
260
|
+
document.removeEventListener('keyup', handleKeyUp);
|
|
261
|
+
}
|
|
180
262
|
emitter.off('element:select', onSelect);
|
|
181
263
|
emitter.off('element:unselect', onUnSelect);
|
|
182
264
|
};
|
|
@@ -184,6 +266,7 @@ export function elementSelect(
|
|
|
184
266
|
|
|
185
267
|
export function ElementSelect({
|
|
186
268
|
createGroup,
|
|
269
|
+
createRegionGroup,
|
|
187
270
|
background = false,
|
|
188
271
|
link = false,
|
|
189
272
|
...rest
|
|
@@ -196,6 +279,9 @@ export function ElementSelect({
|
|
|
196
279
|
elements: selectG2Elements,
|
|
197
280
|
datum: createDatumof(view),
|
|
198
281
|
groupKey: createGroup ? createGroup(view) : undefined,
|
|
282
|
+
regionGroupKey: createRegionGroup
|
|
283
|
+
? createRegionGroup(view)
|
|
284
|
+
: createXKey(view),
|
|
199
285
|
coordinate,
|
|
200
286
|
scale,
|
|
201
287
|
state: mergeState(options, [
|
package/src/interaction/event.ts
CHANGED
|
@@ -1,20 +1,7 @@
|
|
|
1
1
|
import { ChartEvent } from '../utils/event';
|
|
2
|
+
import { dataOf } from '../utils/helper';
|
|
2
3
|
import { maybeRoot } from './utils';
|
|
3
4
|
|
|
4
|
-
export function dataOf(element, view) {
|
|
5
|
-
const { __data__: datum } = element;
|
|
6
|
-
const { markKey, index, seriesIndex } = datum;
|
|
7
|
-
const { markState } = view;
|
|
8
|
-
const selectedMark: any = Array.from(markState.keys()).find(
|
|
9
|
-
(mark) => (mark as any).key === markKey,
|
|
10
|
-
);
|
|
11
|
-
if (!selectedMark) return;
|
|
12
|
-
if (seriesIndex) {
|
|
13
|
-
return seriesIndex.map((i) => selectedMark.data[i]);
|
|
14
|
-
}
|
|
15
|
-
return selectedMark.data[index];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
5
|
// For extended component
|
|
19
6
|
function maybeComponentRoot(node) {
|
|
20
7
|
return maybeRoot(node, (node) => node.className === 'component');
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { group } from '@antv/vendor/d3-array';
|
|
1
|
+
import { InternMap, group } from '@antv/vendor/d3-array';
|
|
2
2
|
import { subObject } from '../utils/helper';
|
|
3
3
|
import {
|
|
4
4
|
mergeState,
|
|
@@ -66,7 +66,9 @@ export function LegendHighlight() {
|
|
|
66
66
|
const highlightItem = (event, item) => {
|
|
67
67
|
// Update UI.
|
|
68
68
|
const value = datumOf(item);
|
|
69
|
-
const elementSet = new Set(
|
|
69
|
+
const elementSet = new Set(
|
|
70
|
+
(elementGroup as InternMap<unknown, any[]>).get(value),
|
|
71
|
+
);
|
|
70
72
|
for (const e of elements) {
|
|
71
73
|
if (elementSet.has(e)) setState(e, 'active');
|
|
72
74
|
else setState(e, 'inactive');
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import { Circle, DisplayObject, IElement, Line } from '@antv/g';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
sort,
|
|
4
|
+
group,
|
|
5
|
+
mean,
|
|
6
|
+
bisector,
|
|
7
|
+
minIndex,
|
|
8
|
+
bisect,
|
|
9
|
+
} from '@antv/vendor/d3-array';
|
|
3
10
|
import { deepMix, lowerFirst, throttle } from '@antv/util';
|
|
4
11
|
import { Tooltip as TooltipComponent } from '@antv/component';
|
|
5
|
-
import {
|
|
6
|
-
import { defined, subObject } from '../utils/helper';
|
|
12
|
+
import { defined, groupNameOf, subObject, dataOf } from '../utils/helper';
|
|
7
13
|
import { isTranspose, isPolar } from '../utils/coordinate';
|
|
8
14
|
import { angle, sub, dist } from '../utils/vector';
|
|
9
15
|
import { invert } from '../utils/scale';
|
|
10
16
|
import { BBox } from '../runtime';
|
|
17
|
+
import { CALLBACK_ITEM_SYMBOL } from '../runtime/transform';
|
|
11
18
|
import {
|
|
12
19
|
selectG2Elements,
|
|
13
20
|
createXKey,
|
|
@@ -19,7 +26,6 @@ import {
|
|
|
19
26
|
bboxOf,
|
|
20
27
|
maybeRoot,
|
|
21
28
|
} from './utils';
|
|
22
|
-
import { dataOf } from './event';
|
|
23
29
|
|
|
24
30
|
function getContainer(
|
|
25
31
|
group: IElement,
|
|
@@ -208,39 +214,6 @@ function singleItem(element) {
|
|
|
208
214
|
};
|
|
209
215
|
}
|
|
210
216
|
|
|
211
|
-
function groupNameOf(scale, datum) {
|
|
212
|
-
const { color: scaleColor, series: scaleSeries, facet = false } = scale;
|
|
213
|
-
const { color, series } = datum;
|
|
214
|
-
const invertAble = (scale) => {
|
|
215
|
-
return (
|
|
216
|
-
scale &&
|
|
217
|
-
scale.invert &&
|
|
218
|
-
!(scale instanceof Band) &&
|
|
219
|
-
!(scale instanceof Constant)
|
|
220
|
-
);
|
|
221
|
-
};
|
|
222
|
-
// For non constant color channel.
|
|
223
|
-
if (invertAble(scaleSeries)) {
|
|
224
|
-
const cloned = scaleSeries.clone();
|
|
225
|
-
return cloned.invert(series);
|
|
226
|
-
}
|
|
227
|
-
if (
|
|
228
|
-
series &&
|
|
229
|
-
scaleSeries instanceof Band &&
|
|
230
|
-
scaleSeries.invert(series) !== color &&
|
|
231
|
-
!facet
|
|
232
|
-
) {
|
|
233
|
-
return scaleSeries.invert(series);
|
|
234
|
-
}
|
|
235
|
-
if (invertAble(scaleColor)) {
|
|
236
|
-
const name = scaleColor.invert(color);
|
|
237
|
-
// For threshold scale.
|
|
238
|
-
if (Array.isArray(name)) return null;
|
|
239
|
-
return name;
|
|
240
|
-
}
|
|
241
|
-
return null;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
217
|
function itemColorOf(element) {
|
|
245
218
|
const fill = element.getAttribute('fill');
|
|
246
219
|
const stroke = element.getAttribute('stroke');
|
|
@@ -268,7 +241,11 @@ function groupItems(
|
|
|
268
241
|
).filter(defined);
|
|
269
242
|
const newItems = data
|
|
270
243
|
.flatMap((datum, i) => {
|
|
271
|
-
const element = elements
|
|
244
|
+
const element = elements.find(
|
|
245
|
+
(element) =>
|
|
246
|
+
element.__data__.seriesKey?.includes(datum.key) ||
|
|
247
|
+
element.__data__.key === datum.key,
|
|
248
|
+
);
|
|
272
249
|
const { items = [], title } = datum;
|
|
273
250
|
const definedItems = items.filter(defined);
|
|
274
251
|
|
|
@@ -279,7 +256,12 @@ function groupItems(
|
|
|
279
256
|
return definedItems.map(
|
|
280
257
|
({ color = itemColorOf(element) || theme.color, name, ...item }) => {
|
|
281
258
|
const groupName = groupNameOf(scale, datum);
|
|
282
|
-
|
|
259
|
+
// callback's priority is higher than groupName.
|
|
260
|
+
const name1 =
|
|
261
|
+
useGroupName && !(CALLBACK_ITEM_SYMBOL in item)
|
|
262
|
+
? groupName || name
|
|
263
|
+
: name || groupName;
|
|
264
|
+
|
|
283
265
|
return {
|
|
284
266
|
...item,
|
|
285
267
|
color,
|
|
@@ -508,6 +490,8 @@ function updateMarker(root, { data, style, theme }) {
|
|
|
508
490
|
r: 4,
|
|
509
491
|
stroke,
|
|
510
492
|
lineWidth: 2,
|
|
493
|
+
// Prevents blocking clicks on elements behind.
|
|
494
|
+
pointerEvents: 'none',
|
|
511
495
|
...style,
|
|
512
496
|
},
|
|
513
497
|
});
|
|
@@ -659,6 +643,7 @@ export function seriesTooltip(
|
|
|
659
643
|
);
|
|
660
644
|
|
|
661
645
|
const { x: scaleX } = scale;
|
|
646
|
+
const stepWidth = scaleX?.getStep?.() ?? 0;
|
|
662
647
|
|
|
663
648
|
// Apply offset for band scale x.
|
|
664
649
|
const offsetX = scaleX?.getBandWidth ? scaleX.getBandWidth() / 2 : 0;
|
|
@@ -669,6 +654,9 @@ export function seriesTooltip(
|
|
|
669
654
|
};
|
|
670
655
|
|
|
671
656
|
const indexByFocus = (event, focus, I, X) => {
|
|
657
|
+
const isOutRange = (x, min, max) => x < min || x > max;
|
|
658
|
+
const shouldSkip = (closest, isRangeViolation, isInvalid) =>
|
|
659
|
+
!closest && isRangeViolation && isInvalid;
|
|
672
660
|
// _x is from emit event, to find the right element.
|
|
673
661
|
const { _x } = event;
|
|
674
662
|
const finalX = _x !== undefined ? scaleX.map(_x) : abstractX(focus);
|
|
@@ -679,20 +667,63 @@ export function seriesTooltip(
|
|
|
679
667
|
|
|
680
668
|
// If closest is true, always find at least one element.
|
|
681
669
|
// Otherwise, skip element out of plot area.
|
|
682
|
-
if (
|
|
670
|
+
if (shouldSkip(closest, isOutRange(finalX, minX, maxX), !isOnlyOneElement))
|
|
683
671
|
return null;
|
|
672
|
+
|
|
684
673
|
const search = bisector((i) => X[+i]).center;
|
|
685
674
|
const i = search(I, finalX);
|
|
675
|
+
if (isOnlyOneElement) {
|
|
676
|
+
// if is only one element, find the closest x to focusX
|
|
677
|
+
const sortedDomain: number[] = scaleX.adjustedRange;
|
|
678
|
+
if (
|
|
679
|
+
// consider oneElementLine, if only one element of one XDomain, must return one element, related test case: tooltip/one-element-line
|
|
680
|
+
// else if multi elements, determine whether it is between the minimum scope and the maximum scope
|
|
681
|
+
shouldSkip(
|
|
682
|
+
closest,
|
|
683
|
+
isOutRange(
|
|
684
|
+
finalX,
|
|
685
|
+
sortedDomain[0],
|
|
686
|
+
sortedDomain[sortedDomain.length - 1],
|
|
687
|
+
),
|
|
688
|
+
!(sortedDomain.length === 1),
|
|
689
|
+
)
|
|
690
|
+
)
|
|
691
|
+
return null;
|
|
692
|
+
|
|
693
|
+
return I[i];
|
|
694
|
+
}
|
|
686
695
|
return I[i];
|
|
687
696
|
};
|
|
688
697
|
|
|
698
|
+
// get x domain by point
|
|
699
|
+
const getDomainXByPoint = (point) => {
|
|
700
|
+
const [pointX] = coordinate.invert(point);
|
|
701
|
+
const rangeIndexMap = scaleX.rangeIndexMap;
|
|
702
|
+
const domainXs = Array.from(rangeIndexMap.keys()).map(
|
|
703
|
+
(d: number) => d - (stepWidth - scaleX.getBandWidth()) / 2,
|
|
704
|
+
);
|
|
705
|
+
const index = bisect(domainXs as number[], pointX);
|
|
706
|
+
return scaleX.sortedDomain[index - 1];
|
|
707
|
+
};
|
|
708
|
+
|
|
689
709
|
const elementsByFocus = isBar
|
|
690
710
|
? (focus, elements) => {
|
|
691
711
|
const search = bisector(xof).center;
|
|
692
|
-
const
|
|
693
|
-
const
|
|
694
|
-
const
|
|
695
|
-
const selected =
|
|
712
|
+
const pointX = abstractX(focus);
|
|
713
|
+
const parentOf = (d) => d.parentNode;
|
|
714
|
+
const elementGroups = group(elements, parentOf);
|
|
715
|
+
const selected = [];
|
|
716
|
+
|
|
717
|
+
const domainX = getDomainXByPoint(focus);
|
|
718
|
+
elementGroups.forEach((elements) => {
|
|
719
|
+
const i = search(elements, pointX);
|
|
720
|
+
const find = elements[i];
|
|
721
|
+
|
|
722
|
+
if (domainX === (find as any).__data__.title) {
|
|
723
|
+
selected.push(find);
|
|
724
|
+
}
|
|
725
|
+
});
|
|
726
|
+
|
|
696
727
|
return selected;
|
|
697
728
|
}
|
|
698
729
|
: (focus, elements) => {
|
|
@@ -1001,6 +1032,7 @@ export function tooltip(
|
|
|
1001
1032
|
const scaleX = scale.x;
|
|
1002
1033
|
const scaleSeries = scale.series;
|
|
1003
1034
|
const bandWidth = scaleX?.getBandWidth?.() ?? 0;
|
|
1035
|
+
const stepWidth = scaleX?.getStep?.() ?? 0;
|
|
1004
1036
|
const xof = scaleSeries
|
|
1005
1037
|
? (d) => {
|
|
1006
1038
|
const seriesCount = Math.round(1 / scaleSeries.valueBandWidth);
|
|
@@ -1032,6 +1064,19 @@ export function tooltip(
|
|
|
1032
1064
|
const i = search(elements, abstractX);
|
|
1033
1065
|
const target = elements[i];
|
|
1034
1066
|
|
|
1067
|
+
// Handle the case where stepWidth is negative.
|
|
1068
|
+
const isStepWidthPositive = stepWidth > 0;
|
|
1069
|
+
const targetLeftBoundary = isStepWidthPositive
|
|
1070
|
+
? xof(target) - stepWidth / 2
|
|
1071
|
+
: xof(target) + stepWidth / 2;
|
|
1072
|
+
const targetRightBoundary = isStepWidthPositive
|
|
1073
|
+
? xof(target) + stepWidth / 2
|
|
1074
|
+
: xof(target) - stepWidth / 2;
|
|
1075
|
+
|
|
1076
|
+
if (abstractX < targetLeftBoundary || abstractX > targetRightBoundary) {
|
|
1077
|
+
return;
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1035
1080
|
if (!shared) {
|
|
1036
1081
|
// For grouped bar chart without shared options.
|
|
1037
1082
|
const isGrouped = elements.find(
|
package/src/interaction/utils.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DisplayObject, Path, AABB } from '@antv/g';
|
|
2
2
|
import { path as d3Path } from '@antv/vendor/d3-path';
|
|
3
|
-
import { sort } from '@antv/vendor/d3-array';
|
|
3
|
+
import { sort, bisector } from '@antv/vendor/d3-array';
|
|
4
4
|
import { Vector2 } from '@antv/coord';
|
|
5
5
|
import { filter } from '@antv/util';
|
|
6
6
|
import type { PathArray } from '@antv/util';
|
|
@@ -532,3 +532,62 @@ export function maybeRoot(node, rootOf) {
|
|
|
532
532
|
while (root && !rootOf(root)) root = root.parent;
|
|
533
533
|
return root;
|
|
534
534
|
}
|
|
535
|
+
|
|
536
|
+
export const VALID_FIND_BY_X_MARKS = ['interval', 'point', 'density'];
|
|
537
|
+
/**
|
|
538
|
+
* @description Create function that can find element by event.
|
|
539
|
+
* @returns Element find function.
|
|
540
|
+
*/
|
|
541
|
+
export function createFindElementByEvent({
|
|
542
|
+
elementsof,
|
|
543
|
+
root,
|
|
544
|
+
coordinate,
|
|
545
|
+
scale,
|
|
546
|
+
validFindByXMarks = VALID_FIND_BY_X_MARKS,
|
|
547
|
+
}) {
|
|
548
|
+
let elements = elementsof(root);
|
|
549
|
+
const getValidFindByXMarks = (d) => validFindByXMarks.includes(d.markType);
|
|
550
|
+
const hasValidFindByXMarks = elements.find(getValidFindByXMarks);
|
|
551
|
+
|
|
552
|
+
// Try to find element by x position.
|
|
553
|
+
if (hasValidFindByXMarks) {
|
|
554
|
+
elements = elements.filter(getValidFindByXMarks);
|
|
555
|
+
|
|
556
|
+
const scaleX = scale.x;
|
|
557
|
+
const scaleSeries = scale.series;
|
|
558
|
+
const bandWidth = scaleX?.getBandWidth?.() ?? 0;
|
|
559
|
+
const xof = scaleSeries
|
|
560
|
+
? (d) => {
|
|
561
|
+
const seriesCount = Math.round(1 / scaleSeries.valueBandWidth);
|
|
562
|
+
return (
|
|
563
|
+
d.__data__.x +
|
|
564
|
+
d.__data__.series * bandWidth +
|
|
565
|
+
bandWidth / (seriesCount * 2)
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
: (d) => d.__data__.x + bandWidth / 2;
|
|
569
|
+
|
|
570
|
+
// Sort for bisector search.
|
|
571
|
+
elements.sort((a, b) => xof(a) - xof(b));
|
|
572
|
+
|
|
573
|
+
return (event) => {
|
|
574
|
+
const mouse = mousePosition(root, event);
|
|
575
|
+
if (!mouse) return;
|
|
576
|
+
const [abstractX] = coordinate.invert(mouse);
|
|
577
|
+
const search = bisector(xof).center;
|
|
578
|
+
const i = search(elements, abstractX);
|
|
579
|
+
const target = elements[i];
|
|
580
|
+
|
|
581
|
+
return target;
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// If there is no valid element find by x, just return the target element.
|
|
586
|
+
return (event) => {
|
|
587
|
+
const { target } = event;
|
|
588
|
+
return maybeRoot(target, (node) => {
|
|
589
|
+
if (!node.classList) return false;
|
|
590
|
+
return node.classList.includes('element');
|
|
591
|
+
});
|
|
592
|
+
};
|
|
593
|
+
}
|
package/src/runtime/layout.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { Coordinate } from '@antv/coord';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
NestedInternMap,
|
|
4
|
+
ascending,
|
|
5
|
+
group,
|
|
6
|
+
max,
|
|
7
|
+
min,
|
|
8
|
+
sum,
|
|
9
|
+
} from '@antv/vendor/d3-array';
|
|
3
10
|
import { deepMix } from '@antv/util';
|
|
4
11
|
import { isParallel, isPolar, isRadar, radiusOf } from '../utils/coordinate';
|
|
5
12
|
import { capitalizeFirst, defined } from '../utils/helper';
|
|
@@ -389,10 +396,16 @@ export function placeComponents(
|
|
|
389
396
|
layout: Layout,
|
|
390
397
|
): void {
|
|
391
398
|
// Group components by plane & position.
|
|
392
|
-
|
|
399
|
+
|
|
400
|
+
const positionComponents: NestedInternMap<
|
|
401
|
+
G2GuideComponentOptions,
|
|
402
|
+
G2GuideComponentOptions[],
|
|
403
|
+
[string]
|
|
404
|
+
> = group<G2GuideComponentOptions, [string]>(
|
|
393
405
|
components,
|
|
394
406
|
(d) => `${d.plane || 'xy'}-${d.position}`,
|
|
395
407
|
);
|
|
408
|
+
|
|
396
409
|
const {
|
|
397
410
|
paddingLeft,
|
|
398
411
|
paddingRight,
|
package/src/runtime/plot.ts
CHANGED
|
@@ -1078,7 +1078,7 @@ async function plotView(
|
|
|
1078
1078
|
maybeFacetElement(element, parent, origin);
|
|
1079
1079
|
const node = shapeFunction(data, index);
|
|
1080
1080
|
const animation = updateFunction(data, [element], [node]);
|
|
1081
|
-
if (animation
|
|
1081
|
+
if (animation?.length) return animation;
|
|
1082
1082
|
if (
|
|
1083
1083
|
element.nodeName === node.nodeName &&
|
|
1084
1084
|
node.nodeName !== 'g'
|
|
@@ -1631,8 +1631,13 @@ function createAnimationFunction(
|
|
|
1631
1631
|
const animateFunction = useAnimation(options, context);
|
|
1632
1632
|
const value = { delay, duration, easing };
|
|
1633
1633
|
const A = animateFunction(from, to, deepMix(defaultEffectTiming, value));
|
|
1634
|
-
|
|
1635
|
-
|
|
1634
|
+
let an: GAnimation[] = [];
|
|
1635
|
+
if (!Array.isArray(A)) {
|
|
1636
|
+
an = [A];
|
|
1637
|
+
} else {
|
|
1638
|
+
an = A;
|
|
1639
|
+
}
|
|
1640
|
+
return an.filter(Boolean);
|
|
1636
1641
|
};
|
|
1637
1642
|
}
|
|
1638
1643
|
|
package/src/runtime/render.ts
CHANGED
|
@@ -6,11 +6,11 @@ import EventEmitter from '@antv/event-emitter';
|
|
|
6
6
|
import { select } from '../utils/selection';
|
|
7
7
|
import { ChartEvent } from '../utils/event';
|
|
8
8
|
import { error } from '../utils/helper';
|
|
9
|
+
import { parseOptionsExpr } from '../utils/expr';
|
|
9
10
|
import { G2Context, G2ViewTree } from './types/options';
|
|
10
11
|
import { plot } from './plot';
|
|
11
12
|
import { VIEW_CLASS_NAME } from './constant';
|
|
12
13
|
import { preprocessOption } from './option-preprocess';
|
|
13
|
-
|
|
14
14
|
/**
|
|
15
15
|
* Infer key for each node of view tree.
|
|
16
16
|
* Each key should be unique in the entire view tree.
|
|
@@ -70,10 +70,11 @@ export function render<T extends G2ViewTree = G2ViewTree>(
|
|
|
70
70
|
throw e;
|
|
71
71
|
},
|
|
72
72
|
): HTMLElement {
|
|
73
|
+
const afterParsedOptions = parseOptionsExpr(options);
|
|
73
74
|
// Initialize the context if it is not provided.
|
|
74
|
-
const { width = 640, height = 480, depth = 0 } =
|
|
75
|
+
const { width = 640, height = 480, depth = 0 } = afterParsedOptions;
|
|
75
76
|
// Preprocessing here, such as syntactic sugar.
|
|
76
|
-
const preprocessedOption = preprocessOption(
|
|
77
|
+
const preprocessedOption = preprocessOption(afterParsedOptions);
|
|
77
78
|
const keyed = inferKeys(preprocessedOption);
|
|
78
79
|
const {
|
|
79
80
|
canvas = Canvas(width, height),
|
package/src/runtime/transform.ts
CHANGED
|
@@ -15,6 +15,8 @@ import { Data, DataComponent } from './types/data';
|
|
|
15
15
|
import { G2Mark, G2DataOptions, G2Context } from './types/options';
|
|
16
16
|
import { isPosition } from './scale';
|
|
17
17
|
|
|
18
|
+
export const CALLBACK_ITEM_SYMBOL = Symbol('CALLBACK_ITEM');
|
|
19
|
+
|
|
18
20
|
// @todo Add more defaults.
|
|
19
21
|
export function applyDefaults(
|
|
20
22
|
I: number[],
|
|
@@ -192,7 +194,8 @@ export function extractTooltip(
|
|
|
192
194
|
const values = [];
|
|
193
195
|
for (const i of I) {
|
|
194
196
|
const v = item(data[i], i, data, encode);
|
|
195
|
-
if (isStrictObject(v))
|
|
197
|
+
if (isStrictObject(v))
|
|
198
|
+
values[i] = { ...v, [CALLBACK_ITEM_SYMBOL]: true };
|
|
196
199
|
else values[i] = { value: v };
|
|
197
200
|
}
|
|
198
201
|
return values;
|
package/src/shape/point/color.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { ShapeComponent as SC } from '../../runtime';
|
|
|
3
3
|
import { isFisheye } from '../../utils/coordinate';
|
|
4
4
|
import { Symbols } from '../../utils/marker';
|
|
5
5
|
import { select } from '../../utils/selection';
|
|
6
|
+
import { camelCase } from '../../utils/string';
|
|
6
7
|
import { applyStyle, getOrigin, toOpacityKey } from '../utils';
|
|
7
8
|
|
|
8
9
|
export type ColorOptions = {
|
|
@@ -36,7 +37,7 @@ export function getRadius(
|
|
|
36
37
|
export const Color: SC<ColorOptions> = (options, context) => {
|
|
37
38
|
// Render border only when colorAttribute is stroke.
|
|
38
39
|
const { colorAttribute, symbol, mode = 'auto', ...style } = options;
|
|
39
|
-
const path = Symbols.get(symbol) || Symbols.get('point');
|
|
40
|
+
const path = Symbols.get(camelCase(symbol)) || Symbols.get('point');
|
|
40
41
|
const { coordinate, document } = context;
|
|
41
42
|
return (points, value, defaults) => {
|
|
42
43
|
const { lineWidth, color: defaultColor } = defaults;
|
package/src/spec/interaction.ts
CHANGED
|
@@ -107,6 +107,8 @@ export type ElementSelectInteraction = {
|
|
|
107
107
|
single?: boolean;
|
|
108
108
|
background?: boolean;
|
|
109
109
|
offset?: number;
|
|
110
|
+
multipleSelectHotkey?: string;
|
|
111
|
+
region?: boolean;
|
|
110
112
|
} & Record<`${'link' | 'background'}${any}`, any>;
|
|
111
113
|
|
|
112
114
|
export type ElementSelectByColorInteraction = {
|
|
@@ -118,6 +120,7 @@ export type ElementSelectByColorInteraction = {
|
|
|
118
120
|
|
|
119
121
|
export type ElementSelectByXInteraction = {
|
|
120
122
|
type?: 'elementSelectByX';
|
|
123
|
+
region?: boolean;
|
|
121
124
|
single?: boolean;
|
|
122
125
|
background?: boolean;
|
|
123
126
|
offset?: number;
|