@antv/g2 5.3.3-beta.0 → 5.3.4-alpha.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 +9 -0
- package/README.zh-CN.md +8 -0
- package/dist/g2.lite.min.js +30 -22
- package/dist/g2.min.js +30 -22
- package/esm/api/runtime.d.ts +12 -1
- package/esm/api/runtime.js +89 -6
- package/esm/api/runtime.js.map +1 -1
- package/esm/data/fetch.d.ts +2 -2
- package/esm/data/fetch.js +2 -2
- package/esm/data/fetch.js.map +1 -1
- package/esm/data/utils/venn/fmin/bisect.d.ts +3 -0
- package/esm/data/utils/venn/fmin/bisect.js +30 -0
- package/esm/data/utils/venn/fmin/bisect.js.map +1 -0
- package/esm/data/utils/venn/fmin/blas1.d.ts +7 -0
- package/esm/data/utils/venn/fmin/blas1.js +38 -0
- package/esm/data/utils/venn/fmin/blas1.js.map +1 -0
- package/esm/data/utils/venn/fmin/conjugateGradient.d.ts +6 -0
- package/esm/data/utils/venn/fmin/conjugateGradient.js +91 -0
- package/esm/data/utils/venn/fmin/conjugateGradient.js.map +1 -0
- package/esm/data/utils/venn/fmin/gradientDescent.d.ts +10 -0
- package/esm/data/utils/venn/fmin/gradientDescent.js +66 -0
- package/esm/data/utils/venn/fmin/gradientDescent.js.map +1 -0
- package/esm/data/utils/venn/fmin/index.d.ts +34 -0
- package/esm/data/utils/venn/fmin/index.js +35 -0
- package/esm/data/utils/venn/fmin/index.js.map +1 -0
- package/esm/data/utils/venn/fmin/linesearch.d.ts +1 -0
- package/esm/data/utils/venn/fmin/linesearch.js +60 -0
- package/esm/data/utils/venn/fmin/linesearch.js.map +1 -0
- package/esm/data/utils/venn/fmin/nelderMead.d.ts +5 -0
- package/esm/data/utils/venn/fmin/nelderMead.js +133 -0
- package/esm/data/utils/venn/fmin/nelderMead.js.map +1 -0
- package/esm/data/utils/venn/layout.d.ts +2 -2
- package/esm/data/utils/venn/layout.js +1 -1
- package/esm/data/utils/venn/layout.js.map +1 -1
- package/esm/index.d.ts +2 -2
- package/esm/index.js +2 -2
- package/esm/index.js.map +1 -1
- package/esm/interaction/elementSelect.js +32 -2
- package/esm/interaction/elementSelect.js.map +1 -1
- package/esm/interaction/tooltip.d.ts +57 -0
- package/esm/interaction/tooltip.js +147 -85
- package/esm/interaction/tooltip.js.map +1 -1
- package/esm/interaction/utils.d.ts +2 -2
- package/esm/interaction/utils.js +10 -2
- package/esm/interaction/utils.js.map +1 -1
- package/esm/mark/line.js +4 -1
- package/esm/mark/line.js.map +1 -1
- package/esm/runtime/component.js +5 -2
- package/esm/runtime/component.js.map +1 -1
- package/esm/runtime/plot.js +8 -3
- package/esm/runtime/plot.js.map +1 -1
- package/esm/runtime/render.d.ts +1 -1
- package/esm/runtime/render.js +4 -2
- package/esm/runtime/render.js.map +1 -1
- package/esm/runtime/types/common.d.ts +1 -0
- package/esm/shape/connector/connector.js +19 -15
- package/esm/shape/connector/connector.js.map +1 -1
- package/esm/spec/transform.d.ts +1 -0
- package/esm/theme/create.d.ts +0 -1
- package/esm/theme/create.js +0 -1
- package/esm/theme/create.js.map +1 -1
- package/esm/transform/group.d.ts +0 -1
- package/esm/transform/group.js.map +1 -1
- package/esm/transform/groupN.d.ts +1 -1
- package/esm/utils/style.js +11 -2
- package/esm/utils/style.js.map +1 -1
- package/esm/utils/traverse-elements.d.ts +2 -0
- package/esm/utils/traverse-elements.js +13 -0
- package/esm/utils/traverse-elements.js.map +1 -0
- package/lib/api/runtime.d.ts +12 -1
- package/lib/api/runtime.js +96 -13
- package/lib/api/runtime.js.map +1 -1
- package/lib/data/fetch.d.ts +2 -2
- package/lib/data/fetch.js +1 -1
- package/lib/data/fetch.js.map +1 -1
- package/lib/data/utils/venn/fmin/bisect.d.ts +3 -0
- package/lib/data/utils/venn/fmin/bisect.js +34 -0
- package/lib/data/utils/venn/fmin/bisect.js.map +1 -0
- package/lib/data/utils/venn/fmin/blas1.d.ts +7 -0
- package/lib/data/utils/venn/fmin/blas1.js +48 -0
- package/lib/data/utils/venn/fmin/blas1.js.map +1 -0
- package/lib/data/utils/venn/fmin/conjugateGradient.d.ts +6 -0
- package/lib/data/utils/venn/fmin/conjugateGradient.js +96 -0
- package/lib/data/utils/venn/fmin/conjugateGradient.js.map +1 -0
- package/lib/data/utils/venn/fmin/gradientDescent.d.ts +10 -0
- package/lib/data/utils/venn/fmin/gradientDescent.js +71 -0
- package/lib/data/utils/venn/fmin/gradientDescent.js.map +1 -0
- package/lib/data/utils/venn/fmin/index.d.ts +34 -0
- package/lib/data/utils/venn/fmin/index.js +49 -0
- package/lib/data/utils/venn/fmin/index.js.map +1 -0
- package/lib/data/utils/venn/fmin/linesearch.d.ts +1 -0
- package/lib/data/utils/venn/fmin/linesearch.js +64 -0
- package/lib/data/utils/venn/fmin/linesearch.js.map +1 -0
- package/lib/data/utils/venn/fmin/nelderMead.d.ts +5 -0
- package/lib/data/utils/venn/fmin/nelderMead.js +137 -0
- package/lib/data/utils/venn/fmin/nelderMead.js.map +1 -0
- package/lib/data/utils/venn/layout.d.ts +2 -2
- package/lib/data/utils/venn/layout.js +1 -1
- package/lib/data/utils/venn/layout.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -1
- package/lib/index.js.map +1 -1
- package/lib/interaction/elementSelect.js +32 -2
- package/lib/interaction/elementSelect.js.map +1 -1
- package/lib/interaction/tooltip.d.ts +57 -0
- package/lib/interaction/tooltip.js +150 -85
- package/lib/interaction/tooltip.js.map +1 -1
- package/lib/interaction/utils.d.ts +2 -2
- package/lib/interaction/utils.js +10 -2
- package/lib/interaction/utils.js.map +1 -1
- package/lib/mark/line.js +3 -0
- package/lib/mark/line.js.map +1 -1
- package/lib/runtime/component.js +5 -2
- package/lib/runtime/component.js.map +1 -1
- package/lib/runtime/plot.js +8 -3
- package/lib/runtime/plot.js.map +1 -1
- package/lib/runtime/render.d.ts +1 -1
- package/lib/runtime/render.js +4 -2
- package/lib/runtime/render.js.map +1 -1
- package/lib/runtime/types/common.d.ts +1 -0
- package/lib/shape/connector/connector.js +19 -15
- package/lib/shape/connector/connector.js.map +1 -1
- package/lib/spec/transform.d.ts +1 -0
- package/lib/theme/create.d.ts +0 -1
- package/lib/theme/create.js +0 -1
- package/lib/theme/create.js.map +1 -1
- package/lib/transform/group.d.ts +0 -1
- package/lib/transform/group.js.map +1 -1
- package/lib/transform/groupN.d.ts +1 -1
- package/lib/utils/style.js +11 -2
- package/lib/utils/style.js.map +1 -1
- package/lib/utils/traverse-elements.d.ts +2 -0
- package/lib/utils/traverse-elements.js +17 -0
- package/lib/utils/traverse-elements.js.map +1 -0
- package/package.json +21 -21
- package/src/api/runtime.ts +136 -6
- package/src/data/fetch.ts +5 -4
- package/src/data/utils/venn/fmin/bisect.ts +32 -0
- package/src/data/utils/venn/fmin/blas1.ts +42 -0
- package/src/data/utils/venn/fmin/conjugateGradient.ts +106 -0
- package/src/data/utils/venn/fmin/gradientDescent.ts +75 -0
- package/src/data/utils/venn/fmin/index.ts +35 -0
- package/src/data/utils/venn/fmin/linesearch.ts +70 -0
- package/src/data/utils/venn/fmin/nelderMead.ts +147 -0
- package/src/data/utils/venn/layout.ts +1 -1
- package/src/index.ts +2 -2
- package/src/interaction/elementSelect.ts +32 -3
- package/src/interaction/tooltip.ts +216 -135
- package/src/interaction/utils.ts +12 -5
- package/src/mark/line.ts +3 -1
- package/src/runtime/component.ts +5 -3
- package/src/runtime/plot.ts +7 -1
- package/src/runtime/render.ts +5 -1
- package/src/runtime/types/common.ts +1 -0
- package/src/shape/connector/connector.ts +30 -19
- package/src/spec/transform.ts +1 -0
- package/src/theme/create.ts +0 -1
- package/src/transform/group.ts +0 -1
- package/src/transform/groupN.ts +1 -1
- package/src/utils/style.ts +11 -1
- package/src/utils/traverse-elements.ts +16 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { dot, norm2, weightedSum } from './blas1';
|
|
2
|
+
|
|
3
|
+
/** minimizes a function using the downhill simplex method */
|
|
4
|
+
export function nelderMead(f, x0, parameters?: any) {
|
|
5
|
+
parameters = parameters || {};
|
|
6
|
+
|
|
7
|
+
const maxIterations = parameters.maxIterations || x0.length * 200;
|
|
8
|
+
const nonZeroDelta = parameters.nonZeroDelta || 1.05;
|
|
9
|
+
const zeroDelta = parameters.zeroDelta || 0.001;
|
|
10
|
+
const minErrorDelta = parameters.minErrorDelta || 1e-6;
|
|
11
|
+
const minTolerance = parameters.minErrorDelta || 1e-5;
|
|
12
|
+
const rho = parameters.rho !== undefined ? parameters.rho : 1;
|
|
13
|
+
const chi = parameters.chi !== undefined ? parameters.chi : 2;
|
|
14
|
+
const psi = parameters.psi !== undefined ? parameters.psi : -0.5;
|
|
15
|
+
const sigma = parameters.sigma !== undefined ? parameters.sigma : 0.5;
|
|
16
|
+
let maxDiff;
|
|
17
|
+
|
|
18
|
+
// initialize simplex.
|
|
19
|
+
const N = x0.length;
|
|
20
|
+
const simplex = new Array(N + 1);
|
|
21
|
+
simplex[0] = x0;
|
|
22
|
+
simplex[0].fx = f(x0);
|
|
23
|
+
simplex[0].id = 0;
|
|
24
|
+
for (let i = 0; i < N; ++i) {
|
|
25
|
+
const point = x0.slice();
|
|
26
|
+
point[i] = point[i] ? point[i] * nonZeroDelta : zeroDelta;
|
|
27
|
+
simplex[i + 1] = point;
|
|
28
|
+
simplex[i + 1].fx = f(point);
|
|
29
|
+
simplex[i + 1].id = i + 1;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function updateSimplex(value) {
|
|
33
|
+
for (let i = 0; i < value.length; i++) {
|
|
34
|
+
simplex[N][i] = value[i];
|
|
35
|
+
}
|
|
36
|
+
simplex[N].fx = value.fx;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const sortOrder = (a, b) => a.fx - b.fx;
|
|
40
|
+
|
|
41
|
+
const centroid = x0.slice();
|
|
42
|
+
const reflected = x0.slice();
|
|
43
|
+
const contracted = x0.slice();
|
|
44
|
+
const expanded = x0.slice();
|
|
45
|
+
|
|
46
|
+
for (let iteration = 0; iteration < maxIterations; ++iteration) {
|
|
47
|
+
simplex.sort(sortOrder);
|
|
48
|
+
|
|
49
|
+
if (parameters.history) {
|
|
50
|
+
// copy the simplex (since later iterations will mutate) and
|
|
51
|
+
// sort it to have a consistent order between iterations
|
|
52
|
+
const sortedSimplex = simplex.map((x) => {
|
|
53
|
+
const state = x.slice();
|
|
54
|
+
state.fx = x.fx;
|
|
55
|
+
state.id = x.id;
|
|
56
|
+
return state;
|
|
57
|
+
});
|
|
58
|
+
sortedSimplex.sort((a, b) => a.id - b.id);
|
|
59
|
+
|
|
60
|
+
parameters.history.push({
|
|
61
|
+
x: simplex[0].slice(),
|
|
62
|
+
fx: simplex[0].fx,
|
|
63
|
+
simplex: sortedSimplex,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
maxDiff = 0;
|
|
68
|
+
for (let i = 0; i < N; ++i) {
|
|
69
|
+
maxDiff = Math.max(maxDiff, Math.abs(simplex[0][i] - simplex[1][i]));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (
|
|
73
|
+
Math.abs(simplex[0].fx - simplex[N].fx) < minErrorDelta &&
|
|
74
|
+
maxDiff < minTolerance
|
|
75
|
+
) {
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// compute the centroid of all but the worst point in the simplex
|
|
80
|
+
for (let i = 0; i < N; ++i) {
|
|
81
|
+
centroid[i] = 0;
|
|
82
|
+
for (let j = 0; j < N; ++j) {
|
|
83
|
+
centroid[i] += simplex[j][i];
|
|
84
|
+
}
|
|
85
|
+
centroid[i] /= N;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// reflect the worst point past the centroid and compute loss at reflected
|
|
89
|
+
// point
|
|
90
|
+
const worst = simplex[N];
|
|
91
|
+
weightedSum(reflected, 1 + rho, centroid, -rho, worst);
|
|
92
|
+
reflected.fx = f(reflected);
|
|
93
|
+
|
|
94
|
+
// if the reflected point is the best seen, then possibly expand
|
|
95
|
+
if (reflected.fx < simplex[0].fx) {
|
|
96
|
+
weightedSum(expanded, 1 + chi, centroid, -chi, worst);
|
|
97
|
+
expanded.fx = f(expanded);
|
|
98
|
+
if (expanded.fx < reflected.fx) {
|
|
99
|
+
updateSimplex(expanded);
|
|
100
|
+
} else {
|
|
101
|
+
updateSimplex(reflected);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// if the reflected point is worse than the second worst, we need to
|
|
106
|
+
// contract
|
|
107
|
+
else if (reflected.fx >= simplex[N - 1].fx) {
|
|
108
|
+
let shouldReduce = false;
|
|
109
|
+
|
|
110
|
+
if (reflected.fx > worst.fx) {
|
|
111
|
+
// do an inside contraction
|
|
112
|
+
weightedSum(contracted, 1 + psi, centroid, -psi, worst);
|
|
113
|
+
contracted.fx = f(contracted);
|
|
114
|
+
if (contracted.fx < worst.fx) {
|
|
115
|
+
updateSimplex(contracted);
|
|
116
|
+
} else {
|
|
117
|
+
shouldReduce = true;
|
|
118
|
+
}
|
|
119
|
+
} else {
|
|
120
|
+
// do an outside contraction
|
|
121
|
+
weightedSum(contracted, 1 - psi * rho, centroid, psi * rho, worst);
|
|
122
|
+
contracted.fx = f(contracted);
|
|
123
|
+
if (contracted.fx < reflected.fx) {
|
|
124
|
+
updateSimplex(contracted);
|
|
125
|
+
} else {
|
|
126
|
+
shouldReduce = true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (shouldReduce) {
|
|
131
|
+
// if we don't contract here, we're done
|
|
132
|
+
if (sigma >= 1) break;
|
|
133
|
+
|
|
134
|
+
// do a reduction
|
|
135
|
+
for (let i = 1; i < simplex.length; ++i) {
|
|
136
|
+
weightedSum(simplex[i], 1 - sigma, simplex[0], sigma, simplex[i]);
|
|
137
|
+
simplex[i].fx = f(simplex[i]);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
updateSimplex(reflected);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
simplex.sort(sortOrder);
|
|
146
|
+
return { fx: simplex[0].fx, x: simplex[0] };
|
|
147
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { corelib, plotlib, graphlib, geolib, stdlib } from './lib';
|
|
1
|
+
import { litelib, corelib, plotlib, graphlib, geolib, stdlib } from './lib';
|
|
2
2
|
import { extend, Runtime } from './api';
|
|
3
3
|
import { API, CompositionAPI } from './api/extend';
|
|
4
4
|
import { G2Spec } from './spec';
|
|
5
5
|
import type { SymbolFactor } from './utils/marker';
|
|
6
6
|
|
|
7
|
-
export { corelib, plotlib, graphlib, geolib, stdlib };
|
|
7
|
+
export { litelib, corelib, plotlib, graphlib, geolib, stdlib };
|
|
8
8
|
|
|
9
9
|
export * from './exports';
|
|
10
10
|
|
|
@@ -2,6 +2,7 @@ import { DisplayObject } from '@antv/g';
|
|
|
2
2
|
import { group } from '@antv/vendor/d3-array';
|
|
3
3
|
import { deepMix } from '@antv/util';
|
|
4
4
|
import { subObject } from '../utils/helper';
|
|
5
|
+
import { traverseElements } from '../utils/traverse-elements';
|
|
5
6
|
import {
|
|
6
7
|
createValueof,
|
|
7
8
|
createDatumof,
|
|
@@ -41,7 +42,7 @@ export function elementSelect(
|
|
|
41
42
|
}: Record<string, any>,
|
|
42
43
|
) {
|
|
43
44
|
const elements = elementsof(root);
|
|
44
|
-
const elementSet = new Set(elements);
|
|
45
|
+
const elementSet: Set<DisplayObject> = new Set(elements);
|
|
45
46
|
const findElement = createFindElementByEvent({
|
|
46
47
|
elementsof,
|
|
47
48
|
root,
|
|
@@ -183,18 +184,46 @@ export function elementSelect(
|
|
|
183
184
|
});
|
|
184
185
|
};
|
|
185
186
|
|
|
187
|
+
const isClickElementOrGroup = (element: DisplayObject): boolean => {
|
|
188
|
+
if (elementSet.has(element)) return true;
|
|
189
|
+
|
|
190
|
+
for (const group of elementSet) {
|
|
191
|
+
const found = traverseElements(group, (el) => el === element);
|
|
192
|
+
if (found) return true;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return false;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const getRealElement = (element: DisplayObject): DisplayObject => {
|
|
199
|
+
if (elementSet.has(element)) return element;
|
|
200
|
+
for (const group of elementSet) {
|
|
201
|
+
let match: DisplayObject | null = null;
|
|
202
|
+
traverseElements(group, (el) => {
|
|
203
|
+
if (el === element) match = group;
|
|
204
|
+
});
|
|
205
|
+
if (match) return match;
|
|
206
|
+
}
|
|
207
|
+
return element;
|
|
208
|
+
};
|
|
209
|
+
|
|
186
210
|
const click = (event) => {
|
|
187
211
|
const { target: element, nativeEvent = true } = event;
|
|
188
212
|
|
|
189
213
|
const select = !isMultiSelectMode ? singleSelect : multipleSelect;
|
|
190
214
|
let el = element;
|
|
191
|
-
const isClickElement =
|
|
215
|
+
const isClickElement = isClickElementOrGroup(element);
|
|
192
216
|
|
|
193
217
|
if (!region || isClickElement) {
|
|
194
218
|
// Click non-element shape, reset.
|
|
195
219
|
// Such as the rest of content area(background).
|
|
196
220
|
if (!isClickElement) return clear();
|
|
197
|
-
return select({
|
|
221
|
+
return select({
|
|
222
|
+
event,
|
|
223
|
+
element: getRealElement(el),
|
|
224
|
+
nativeEvent,
|
|
225
|
+
groupBy: groupKey,
|
|
226
|
+
});
|
|
198
227
|
} else {
|
|
199
228
|
// Click background region area, select elements in the region.
|
|
200
229
|
// Get element at cursor.x position.
|
|
@@ -2,6 +2,7 @@ import { Circle, DisplayObject, IElement, Line } from '@antv/g';
|
|
|
2
2
|
import { sort, group, mean, bisector, minIndex } from '@antv/vendor/d3-array';
|
|
3
3
|
import { deepMix, lowerFirst, throttle } from '@antv/util';
|
|
4
4
|
import { Tooltip as TooltipComponent } from '@antv/component';
|
|
5
|
+
import { G2Element } from 'utils/selection';
|
|
5
6
|
import { defined, groupNameOf, subObject, dataOf } from '../utils/helper';
|
|
6
7
|
import { isTranspose, isPolar } from '../utils/coordinate';
|
|
7
8
|
import { angle, sub, dist } from '../utils/vector';
|
|
@@ -27,9 +28,9 @@ function getContainer(
|
|
|
27
28
|
if (mount) {
|
|
28
29
|
return typeof mount === 'string' ? document.querySelector(mount) : mount;
|
|
29
30
|
}
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
const view = group.ownerDocument?.defaultView;
|
|
32
|
+
if (!view) return;
|
|
33
|
+
const canvas: any = view.getContextService().getDomElement();
|
|
33
34
|
return canvas.parentElement as unknown as HTMLElement;
|
|
34
35
|
}
|
|
35
36
|
|
|
@@ -504,7 +505,7 @@ function interactionKeyof(markState, key) {
|
|
|
504
505
|
);
|
|
505
506
|
}
|
|
506
507
|
|
|
507
|
-
function maybeValue(specified, defaults) {
|
|
508
|
+
export function maybeValue(specified, defaults) {
|
|
508
509
|
return specified === undefined ? defaults : specified;
|
|
509
510
|
}
|
|
510
511
|
|
|
@@ -522,56 +523,111 @@ function hasSeries(markState): boolean {
|
|
|
522
523
|
}
|
|
523
524
|
|
|
524
525
|
/**
|
|
525
|
-
*
|
|
526
|
+
* Finds a single element based on the mouse event in a non-series context (e.g., single item tooltip).
|
|
527
|
+
* @param root - The root display object of the chart.
|
|
528
|
+
* @param event - The mouse event object (e.g., pointermove, pointerdown).
|
|
529
|
+
* @param elements - Array of chart elements to search within.
|
|
530
|
+
* @param coordinate - The coordinate system of the chart (e.g., Cartesian, polar).
|
|
531
|
+
* @param scale - The scale configurations (e.g., x, series scales).
|
|
532
|
+
* @param shared - Whether the tooltip is shared among multiple elements (e.g., grouped bars).
|
|
533
|
+
* @returns The matched display object or `undefined` if no element is found.
|
|
534
|
+
* @description
|
|
535
|
+
* - Handles bar charts by sorting elements and using bisector search for efficient lookup.
|
|
536
|
+
* - For non-bar charts, directly finds the target element from the event's target.
|
|
537
|
+
* - Adjusts for bar spacing in grouped charts when `shared` is false.
|
|
526
538
|
*/
|
|
527
|
-
export function
|
|
528
|
-
root
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
mount,
|
|
551
|
-
bounding,
|
|
552
|
-
theme,
|
|
553
|
-
offset,
|
|
554
|
-
disableNative = false,
|
|
555
|
-
marker = true,
|
|
556
|
-
preserve = false,
|
|
557
|
-
style: _style = {},
|
|
558
|
-
css = {},
|
|
559
|
-
...rest
|
|
560
|
-
}: Record<string, any>,
|
|
561
|
-
) {
|
|
562
|
-
const elements = elementsof(root);
|
|
563
|
-
const transposed = isTranspose(coordinate);
|
|
564
|
-
const polar = isPolar(coordinate);
|
|
565
|
-
const style = deepMix(_style, rest);
|
|
539
|
+
export function findSingleElement({
|
|
540
|
+
root,
|
|
541
|
+
event,
|
|
542
|
+
elements,
|
|
543
|
+
coordinate,
|
|
544
|
+
scale,
|
|
545
|
+
shared,
|
|
546
|
+
}): DisplayObject | undefined {
|
|
547
|
+
const inInterval = (d) => d.markType === 'interval';
|
|
548
|
+
const isBar = elements.every(inInterval) && !isPolar(coordinate);
|
|
549
|
+
const scaleX = scale.x;
|
|
550
|
+
const scaleSeries = scale.series;
|
|
551
|
+
const bandWidth = scaleX?.getBandWidth?.() ?? 0;
|
|
552
|
+
const xof = scaleSeries
|
|
553
|
+
? (d) => {
|
|
554
|
+
const seriesCount = Math.round(1 / scaleSeries.valueBandWidth);
|
|
555
|
+
return (
|
|
556
|
+
d.__data__.x +
|
|
557
|
+
d.__data__.series * bandWidth +
|
|
558
|
+
bandWidth / (seriesCount * 2)
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
: (d) => d.__data__.x + bandWidth / 2;
|
|
566
562
|
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
563
|
+
// Sort for bisector search.
|
|
564
|
+
if (isBar) elements.sort((a, b) => xof(a) - xof(b));
|
|
565
|
+
const findElementByTarget = (event) => {
|
|
566
|
+
const { target } = event;
|
|
567
|
+
return maybeRoot(target, (node) => {
|
|
568
|
+
if (!node.classList) return false;
|
|
569
|
+
return node.classList.includes('element');
|
|
570
|
+
});
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
const element = isBar
|
|
574
|
+
? (event) => {
|
|
575
|
+
const mouse = mousePosition(root, event);
|
|
576
|
+
if (!mouse) return;
|
|
577
|
+
const [abstractX] = coordinate.invert(mouse);
|
|
578
|
+
const search = bisector(xof).center;
|
|
579
|
+
const i = search(elements, abstractX);
|
|
580
|
+
const target = elements[i];
|
|
581
|
+
|
|
582
|
+
if (!shared) {
|
|
583
|
+
// For grouped bar chart without shared options.
|
|
584
|
+
const isGrouped = elements.find(
|
|
585
|
+
(d) => d !== target && xof(d) === xof(target),
|
|
586
|
+
);
|
|
587
|
+
if (isGrouped) return findElementByTarget(event);
|
|
588
|
+
}
|
|
589
|
+
return target;
|
|
590
|
+
}
|
|
591
|
+
: findElementByTarget;
|
|
592
|
+
|
|
593
|
+
return element(event);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* Finds series-related elements and data based on the mouse event for series tooltips.
|
|
598
|
+
* @param root - The root display object of the chart.
|
|
599
|
+
* @param event - The mouse event object (e.g., pointermove, pointerdown).
|
|
600
|
+
* @param elements - Array of chart elements to search within.
|
|
601
|
+
* @param coordinate - The coordinate system of the chart (e.g., Cartesian, polar).
|
|
602
|
+
* @param scale - The scale configurations (e.g., x, series scales).
|
|
603
|
+
* @param startX - The starting X position of the plot area.
|
|
604
|
+
* @param startY - The starting Y position of the plot area.
|
|
605
|
+
* @returns An object containing:
|
|
606
|
+
* - `selectedElements`: Matched display objects (series and item elements).
|
|
607
|
+
* - `selectedData`: Corresponding data records of the selected elements.
|
|
608
|
+
* - `filteredSeriesData`: Filtered series data closest to the mouse focus.
|
|
609
|
+
* - `abstractX`: A function to convert mouse coordinates to abstract X values.
|
|
610
|
+
* @description
|
|
611
|
+
* - Splits elements into series and item elements for targeted searching.
|
|
612
|
+
* - Handles bar charts and band scales using bisector search and coordinate inversion.
|
|
613
|
+
* - Sorts elements to ensure correct visual ordering (top-to-bottom or right-to-left in transposed mode).
|
|
614
|
+
* - Filters and groups data to provide accurate tooltip information for series.
|
|
615
|
+
*/
|
|
616
|
+
export function findSeriesElement({
|
|
617
|
+
root,
|
|
618
|
+
event,
|
|
619
|
+
elements,
|
|
620
|
+
coordinate,
|
|
621
|
+
scale,
|
|
622
|
+
startX,
|
|
623
|
+
startY,
|
|
624
|
+
}): {
|
|
625
|
+
selectedElements: DisplayObject[];
|
|
626
|
+
selectedData: Record<string, any>[];
|
|
627
|
+
filteredSeriesData: any[];
|
|
628
|
+
abstractX: (number) => number;
|
|
629
|
+
} {
|
|
630
|
+
const transposed = isTranspose(coordinate);
|
|
575
631
|
|
|
576
632
|
// Split elements into series elements and item elements.
|
|
577
633
|
const seriesElements = [];
|
|
@@ -698,7 +754,97 @@ export function seriesTooltip(
|
|
|
698
754
|
}),
|
|
699
755
|
);
|
|
700
756
|
};
|
|
757
|
+
const mouse = mousePosition(root, event);
|
|
758
|
+
if (!mouse) return;
|
|
759
|
+
const focus = [mouse[0] - startX, mouse[1] - startY];
|
|
760
|
+
if (!focus) return;
|
|
761
|
+
// Get selected item element.
|
|
762
|
+
const selectedItems = elementsByFocus(focus, itemElements);
|
|
763
|
+
|
|
764
|
+
// Get selected data item from both series element and item element.
|
|
765
|
+
const selectedSeriesElements = [];
|
|
766
|
+
const selectedSeriesData = [];
|
|
767
|
+
for (const element of seriesElements) {
|
|
768
|
+
const [sortedIndex, X] = elementSortedX.get(element);
|
|
769
|
+
const index = indexByFocus(event, focus, sortedIndex, X);
|
|
770
|
+
if (index !== null) {
|
|
771
|
+
selectedSeriesElements.push(element);
|
|
772
|
+
const d = seriesData(element, index);
|
|
773
|
+
const { x, y } = d;
|
|
774
|
+
const p = coordinate.map([(x || 0) + offsetX, y || 0]);
|
|
775
|
+
selectedSeriesData.push([{ ...d, element }, p] as const);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
// Filter selectedSeriesData with different x,
|
|
780
|
+
// make sure there is only one x closest to focusX.
|
|
781
|
+
const SX = Array.from(new Set(selectedSeriesData.map((d) => d[0].x)));
|
|
782
|
+
const closestX = SX[minIndex(SX, (x) => Math.abs(x - abstractX(focus)))];
|
|
783
|
+
const filteredSeriesData = selectedSeriesData.filter(
|
|
784
|
+
(d) => d[0].x === closestX,
|
|
785
|
+
);
|
|
786
|
+
|
|
787
|
+
const selectedData = [
|
|
788
|
+
...filteredSeriesData.map((d) => d[0]),
|
|
789
|
+
...selectedItems.map((d) => d.__data__),
|
|
790
|
+
];
|
|
791
|
+
|
|
792
|
+
// Get the displayed tooltip data.
|
|
793
|
+
const selectedElements = [...selectedSeriesElements, ...selectedItems];
|
|
701
794
|
|
|
795
|
+
return { selectedElements, selectedData, filteredSeriesData, abstractX };
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* Show tooltip for series item.
|
|
800
|
+
*/
|
|
801
|
+
export function seriesTooltip(
|
|
802
|
+
root: DisplayObject,
|
|
803
|
+
{
|
|
804
|
+
elements: elementsof,
|
|
805
|
+
sort: sortFunction,
|
|
806
|
+
filter: filterFunction,
|
|
807
|
+
scale,
|
|
808
|
+
coordinate,
|
|
809
|
+
crosshairs,
|
|
810
|
+
crosshairsX,
|
|
811
|
+
crosshairsY,
|
|
812
|
+
render,
|
|
813
|
+
groupName,
|
|
814
|
+
emitter,
|
|
815
|
+
wait = 50,
|
|
816
|
+
leading = true,
|
|
817
|
+
trailing = false,
|
|
818
|
+
startX = 0,
|
|
819
|
+
startY = 0,
|
|
820
|
+
body = true,
|
|
821
|
+
single = true,
|
|
822
|
+
position,
|
|
823
|
+
enterable,
|
|
824
|
+
mount,
|
|
825
|
+
bounding,
|
|
826
|
+
theme,
|
|
827
|
+
offset,
|
|
828
|
+
disableNative = false,
|
|
829
|
+
marker = true,
|
|
830
|
+
preserve = false,
|
|
831
|
+
style: _style = {},
|
|
832
|
+
css = {},
|
|
833
|
+
...rest
|
|
834
|
+
}: Record<string, any>,
|
|
835
|
+
) {
|
|
836
|
+
const elements = elementsof(root);
|
|
837
|
+
const style = deepMix(_style, rest);
|
|
838
|
+
const polar = isPolar(coordinate);
|
|
839
|
+
const transposed = isTranspose(coordinate);
|
|
840
|
+
const {
|
|
841
|
+
innerWidth: plotWidth,
|
|
842
|
+
innerHeight: plotHeight,
|
|
843
|
+
width: mainWidth,
|
|
844
|
+
height: mainHeight,
|
|
845
|
+
insetLeft,
|
|
846
|
+
insetTop,
|
|
847
|
+
} = coordinate.getOptions();
|
|
702
848
|
const update = throttle(
|
|
703
849
|
(event) => {
|
|
704
850
|
const mouse = mousePosition(root, event);
|
|
@@ -706,41 +852,16 @@ export function seriesTooltip(
|
|
|
706
852
|
const bbox = bboxOf(root);
|
|
707
853
|
const x = bbox.min[0];
|
|
708
854
|
const y = bbox.min[1];
|
|
709
|
-
const
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
const index = indexByFocus(event, focus, sortedIndex, X);
|
|
720
|
-
if (index !== null) {
|
|
721
|
-
selectedSeriesElements.push(element);
|
|
722
|
-
const d = seriesData(element, index);
|
|
723
|
-
const { x, y } = d;
|
|
724
|
-
const p = coordinate.map([(x || 0) + offsetX, y || 0]);
|
|
725
|
-
selectedSeriesData.push([{ ...d, element }, p] as const);
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
// Filter selectedSeriesData with different x,
|
|
730
|
-
// make sure there is only one x closest to focusX.
|
|
731
|
-
const SX = Array.from(new Set(selectedSeriesData.map((d) => d[0].x)));
|
|
732
|
-
const closestX = SX[minIndex(SX, (x) => Math.abs(x - abstractX(focus)))];
|
|
733
|
-
const filteredSeriesData = selectedSeriesData.filter(
|
|
734
|
-
(d) => d[0].x === closestX,
|
|
735
|
-
);
|
|
736
|
-
|
|
737
|
-
const selectedData = [
|
|
738
|
-
...filteredSeriesData.map((d) => d[0]),
|
|
739
|
-
...selectedItems.map((d) => d.__data__),
|
|
740
|
-
];
|
|
741
|
-
|
|
742
|
-
// Get the displayed tooltip data.
|
|
743
|
-
const selectedElements = [...selectedSeriesElements, ...selectedItems];
|
|
855
|
+
const { selectedElements, selectedData, filteredSeriesData, abstractX } =
|
|
856
|
+
findSeriesElement({
|
|
857
|
+
root,
|
|
858
|
+
event,
|
|
859
|
+
elements,
|
|
860
|
+
coordinate,
|
|
861
|
+
scale,
|
|
862
|
+
startX,
|
|
863
|
+
startY,
|
|
864
|
+
});
|
|
744
865
|
const tooltipData = groupItems(
|
|
745
866
|
selectedElements,
|
|
746
867
|
scale,
|
|
@@ -973,56 +1094,16 @@ export function tooltip(
|
|
|
973
1094
|
) {
|
|
974
1095
|
const elements = elementsof(root);
|
|
975
1096
|
const keyGroup = group(elements, groupKey);
|
|
976
|
-
const inInterval = (d) => d.markType === 'interval';
|
|
977
|
-
const isBar = elements.every(inInterval) && !isPolar(coordinate);
|
|
978
|
-
const scaleX = scale.x;
|
|
979
|
-
const scaleSeries = scale.series;
|
|
980
|
-
const bandWidth = scaleX?.getBandWidth?.() ?? 0;
|
|
981
|
-
const xof = scaleSeries
|
|
982
|
-
? (d) => {
|
|
983
|
-
const seriesCount = Math.round(1 / scaleSeries.valueBandWidth);
|
|
984
|
-
return (
|
|
985
|
-
d.__data__.x +
|
|
986
|
-
d.__data__.series * bandWidth +
|
|
987
|
-
bandWidth / (seriesCount * 2)
|
|
988
|
-
);
|
|
989
|
-
}
|
|
990
|
-
: (d) => d.__data__.x + bandWidth / 2;
|
|
991
|
-
|
|
992
|
-
// Sort for bisector search.
|
|
993
|
-
if (isBar) elements.sort((a, b) => xof(a) - xof(b));
|
|
994
|
-
|
|
995
|
-
const findElementByTarget = (event) => {
|
|
996
|
-
const { target } = event;
|
|
997
|
-
return maybeRoot(target, (node) => {
|
|
998
|
-
if (!node.classList) return false;
|
|
999
|
-
return node.classList.includes('element');
|
|
1000
|
-
});
|
|
1001
|
-
};
|
|
1002
|
-
|
|
1003
|
-
const findElement = isBar
|
|
1004
|
-
? (event) => {
|
|
1005
|
-
const mouse = mousePosition(root, event);
|
|
1006
|
-
if (!mouse) return;
|
|
1007
|
-
const [abstractX] = coordinate.invert(mouse);
|
|
1008
|
-
const search = bisector(xof).center;
|
|
1009
|
-
const i = search(elements, abstractX);
|
|
1010
|
-
const target = elements[i];
|
|
1011
|
-
|
|
1012
|
-
if (!shared) {
|
|
1013
|
-
// For grouped bar chart without shared options.
|
|
1014
|
-
const isGrouped = elements.find(
|
|
1015
|
-
(d) => d !== target && xof(d) === xof(target),
|
|
1016
|
-
);
|
|
1017
|
-
if (isGrouped) return findElementByTarget(event);
|
|
1018
|
-
}
|
|
1019
|
-
return target;
|
|
1020
|
-
}
|
|
1021
|
-
: findElementByTarget;
|
|
1022
|
-
|
|
1023
1097
|
const pointermove = throttle(
|
|
1024
1098
|
(event) => {
|
|
1025
|
-
const element =
|
|
1099
|
+
const element = findSingleElement({
|
|
1100
|
+
root,
|
|
1101
|
+
event,
|
|
1102
|
+
elements,
|
|
1103
|
+
coordinate,
|
|
1104
|
+
scale,
|
|
1105
|
+
shared,
|
|
1106
|
+
});
|
|
1026
1107
|
if (!element) {
|
|
1027
1108
|
hideTooltip({ root, single, emitter, event });
|
|
1028
1109
|
return;
|
package/src/interaction/utils.ts
CHANGED
|
@@ -17,6 +17,7 @@ import { isPolar, isTranspose } from '../utils/coordinate';
|
|
|
17
17
|
import { getStyle } from '../utils/style';
|
|
18
18
|
import { reorder } from '../shape/utils';
|
|
19
19
|
import { angle, angleBetween, sub } from '../utils/vector';
|
|
20
|
+
import { traverseElements } from '../utils/traverse-elements';
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
* Given root of chart returns elements to be manipulated
|
|
@@ -139,6 +140,14 @@ const STATE_GROUPS = {
|
|
|
139
140
|
highlight: ['active', 'inactive'],
|
|
140
141
|
};
|
|
141
142
|
|
|
143
|
+
const setElementAttribute = (element: DisplayObject, k: string, v: string) => {
|
|
144
|
+
traverseElements(element, (el) => {
|
|
145
|
+
if ('setAttribute' in el && typeof el.setAttribute === 'function') {
|
|
146
|
+
(el as DisplayObject).setAttribute(k, v);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
|
|
142
151
|
export function createUseState(
|
|
143
152
|
style: Record<string, any>,
|
|
144
153
|
elements: Element[],
|
|
@@ -157,16 +166,14 @@ export function createUseState(
|
|
|
157
166
|
}
|
|
158
167
|
});
|
|
159
168
|
|
|
160
|
-
return (
|
|
161
|
-
|
|
162
|
-
setAttribute = (element, key, v) => element.setAttribute(key, v),
|
|
163
|
-
) => useState(undefined, valueof, setAttribute);
|
|
169
|
+
return (valueof = (d, element) => d, setAttribute = setElementAttribute) =>
|
|
170
|
+
useState(undefined, valueof, setAttribute);
|
|
164
171
|
}
|
|
165
172
|
|
|
166
173
|
export function useState(
|
|
167
174
|
style: Record<string, any> | undefined,
|
|
168
175
|
valueof = (d, element) => d,
|
|
169
|
-
setAttribute =
|
|
176
|
+
setAttribute = setElementAttribute,
|
|
170
177
|
) {
|
|
171
178
|
const STATES = '__states__';
|
|
172
179
|
const ORIGINAL = '__ordinal__';
|
package/src/mark/line.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Vector } from '@antv/coord';
|
|
2
2
|
import { group } from '@antv/vendor/d3-array';
|
|
3
|
-
import { isParallel } from '../utils/coordinate';
|
|
3
|
+
import { isParallel, isPolar } from '../utils/coordinate';
|
|
4
4
|
import { Mark, MarkComponent as MC, SingleMark, Vector2 } from '../runtime';
|
|
5
5
|
import { LineMark } from '../spec';
|
|
6
6
|
import {
|
|
@@ -68,6 +68,8 @@ const parallel: Mark = (index, scale, value, coordinate) => {
|
|
|
68
68
|
if (PV.length === 0) {
|
|
69
69
|
throw new Error('Missing encode for position channel.');
|
|
70
70
|
}
|
|
71
|
+
// Close the loop for radar(= parallel + polar) coordinate.
|
|
72
|
+
if (isPolar(coordinate)) PV.push(PV[0]);
|
|
71
73
|
|
|
72
74
|
// One data corresponds to one line.
|
|
73
75
|
const P = Array.from(index, (i) => {
|