@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
package/src/transform/bin.ts
CHANGED
|
@@ -39,7 +39,7 @@ export const Bin: TC<BinOptions> = (options = {}) => {
|
|
|
39
39
|
const channelIndexKey = {};
|
|
40
40
|
|
|
41
41
|
// Group indexes and update channelIndexKey.
|
|
42
|
-
const groupBy = (I, mark) => {
|
|
42
|
+
const groupBy = (I, mark): number[][] => {
|
|
43
43
|
const { encode } = mark;
|
|
44
44
|
const binValues = binChannels.map((channel) => {
|
|
45
45
|
const [V] = columnOf(encode, channel);
|
|
@@ -80,7 +80,7 @@ export const Bin: TC<BinOptions> = (options = {}) => {
|
|
|
80
80
|
|
|
81
81
|
// Group by indexes by channel keys.
|
|
82
82
|
const key = (i: number) => groupKeys.map((key) => key(i)).join('-');
|
|
83
|
-
return Array.from(group(DI, key).values());
|
|
83
|
+
return Array.from(group(DI, key).values()) as number[][];
|
|
84
84
|
};
|
|
85
85
|
|
|
86
86
|
return GroupN({
|
package/src/transform/diffY.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { deepMix } from '@antv/util';
|
|
2
2
|
import { TransformComponent as TC } from '../runtime';
|
|
3
3
|
import { DiffYTransform } from '../spec';
|
|
4
|
-
import { column, columnOf
|
|
4
|
+
import { column, columnOf } from './utils/helper';
|
|
5
5
|
import { createGroups } from './utils/order';
|
|
6
6
|
|
|
7
7
|
export type DiffYOptions = Omit<DiffYTransform, 'type'>;
|
|
@@ -11,16 +11,12 @@ export type DiffYOptions = Omit<DiffYTransform, 'type'>;
|
|
|
11
11
|
* Keep y unchanged, set y1 = max(otherY), if y1 > y, remove the data.
|
|
12
12
|
*/
|
|
13
13
|
export const DiffY: TC<DiffYOptions> = (options = {}) => {
|
|
14
|
-
const { groupBy = 'x'
|
|
14
|
+
const { groupBy = 'x' } = options;
|
|
15
15
|
return (I, mark) => {
|
|
16
16
|
const { encode } = mark;
|
|
17
17
|
const [Y] = columnOf(encode, 'y');
|
|
18
18
|
const [_, fy1] = columnOf(encode, 'y1');
|
|
19
19
|
|
|
20
|
-
const [S] = series
|
|
21
|
-
? maybeColumnOf(encode, 'series', 'color')
|
|
22
|
-
: columnOf(encode, 'color');
|
|
23
|
-
|
|
24
20
|
// Create groups and apply specified order for each group.
|
|
25
21
|
const groups = createGroups(groupBy, I, mark);
|
|
26
22
|
|
package/src/transform/sort.ts
CHANGED
|
@@ -43,16 +43,6 @@ export type SortOptions = {
|
|
|
43
43
|
| ((I: number[], V: Primitive[]) => Primitive);
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
-
function sortQuantitative(I, mark, options): [number[], G2Mark] {
|
|
47
|
-
const { reverse, channel } = options;
|
|
48
|
-
const { encode } = mark;
|
|
49
|
-
const [V] = columnOf(encode, channel);
|
|
50
|
-
const sortedI = sort(I, (i: number) => V[i]);
|
|
51
|
-
if (reverse) sortedI.reverse();
|
|
52
|
-
// const s = typeof slice === 'number' ? [0, slice] : slice;
|
|
53
|
-
return [sortedI, mark];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
46
|
// If domain is specified, only sort data in the domain.
|
|
57
47
|
function filterIndex(I, values, specifiedDomain): number[] {
|
|
58
48
|
if (!Array.isArray(specifiedDomain)) return I;
|
|
@@ -60,44 +50,32 @@ function filterIndex(I, values, specifiedDomain): number[] {
|
|
|
60
50
|
return I.filter((i) => domain.has(values[i]));
|
|
61
51
|
}
|
|
62
52
|
|
|
63
|
-
function sortOrdinal(I, mark, options): [number[], G2Mark] {
|
|
64
|
-
const { reverse, slice, channel, ...rest } = options;
|
|
65
|
-
const { encode, scale = {} } = mark;
|
|
66
|
-
const domain = scale[channel]?.domain;
|
|
67
|
-
const [T] = columnOf(encode, channel);
|
|
68
|
-
const normalizeReducer = createReducer(channel, rest, encode);
|
|
69
|
-
const SI = filterIndex(I, T, domain);
|
|
70
|
-
const sortedDomain = groupSort(SI, normalizeReducer, (i: number) => T[i]);
|
|
71
|
-
if (reverse) sortedDomain.reverse();
|
|
72
|
-
const s = typeof slice === 'number' ? [0, slice] : slice;
|
|
73
|
-
const slicedDomain = slice ? sortedDomain.slice(...s) : sortedDomain;
|
|
74
|
-
return [
|
|
75
|
-
I,
|
|
76
|
-
deepMix(mark, {
|
|
77
|
-
scale: {
|
|
78
|
-
[channel]: {
|
|
79
|
-
domain: slicedDomain,
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
}),
|
|
83
|
-
];
|
|
84
|
-
}
|
|
85
|
-
|
|
86
53
|
/**
|
|
87
54
|
* Sort marks groups by groups.
|
|
88
55
|
*/
|
|
89
56
|
export const Sort: TC<SortOptions> = (options = {}) => {
|
|
90
|
-
const { reverse = false, slice, channel, ordinal = true, ...rest } = options;
|
|
91
57
|
return (I, mark) => {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
58
|
+
const { reverse, slice, channel, by, ordinal = true, reducer } = options;
|
|
59
|
+
const { encode, scale = {} } = mark;
|
|
60
|
+
const domain = scale[channel].domain;
|
|
61
|
+
const [V] = columnOf(encode, by ?? channel);
|
|
62
|
+
const [T] = columnOf(encode, channel);
|
|
63
|
+
const normalizeReducer = createReducer(channel, { by, reducer }, encode);
|
|
64
|
+
const SI = filterIndex(I, T, domain);
|
|
65
|
+
const sortedDomain = groupSort(SI, normalizeReducer, (i: number) => T[i]);
|
|
66
|
+
|
|
67
|
+
// when ordinal is true, do not change the index of the data.
|
|
68
|
+
const sortedI = !ordinal ? sort(I, (i: number) => V[i]) : I;
|
|
69
|
+
if (reverse) {
|
|
70
|
+
!ordinal && sortedI.reverse();
|
|
71
|
+
sortedDomain.reverse();
|
|
99
72
|
}
|
|
100
|
-
|
|
73
|
+
const s = typeof slice === 'number' ? [0, slice] : slice;
|
|
74
|
+
const slicedDomain = slice ? sortedDomain.slice(...s) : sortedDomain;
|
|
75
|
+
return [
|
|
76
|
+
sortedI,
|
|
77
|
+
deepMix(mark, { scale: { [channel]: { domain: slicedDomain } } }),
|
|
78
|
+
];
|
|
101
79
|
};
|
|
102
80
|
};
|
|
103
81
|
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { compile } from '@antv/expr';
|
|
2
|
+
import { mapValues } from '@antv/util';
|
|
3
|
+
import { lru } from './lru';
|
|
4
|
+
|
|
5
|
+
// Whitelist of properties that can contain expressions.
|
|
6
|
+
export const EXPR_WHITE_LIST = ['style', 'encode', 'labels', 'children'];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Compiles an expression string into a function.
|
|
10
|
+
* @param expr Expression string to compile.
|
|
11
|
+
* @returns Compiled function or original string if empty.
|
|
12
|
+
*/
|
|
13
|
+
const compileExpression = lru(
|
|
14
|
+
(expr: string): (() => any) | string => {
|
|
15
|
+
const evaluator = compile(expr);
|
|
16
|
+
|
|
17
|
+
return (...args) => {
|
|
18
|
+
const paramNames = Array.from({ length: args.length }, (_, i) =>
|
|
19
|
+
String.fromCharCode(97 + i),
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
const namedParams = Object.fromEntries(
|
|
23
|
+
args.map((value, index) => [paramNames[index], value]),
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
// global is used to overview what can i get in props.
|
|
27
|
+
return evaluator({
|
|
28
|
+
...namedParams,
|
|
29
|
+
global: { ...namedParams },
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
(expr) => expr,
|
|
34
|
+
128,
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Processes options object to convert expressions to functions.
|
|
39
|
+
* @param options Options object to process.
|
|
40
|
+
* @param isSpecRoot Whether the options is the root of the spec.
|
|
41
|
+
* @returns Processed options object with expressions converted to functions.
|
|
42
|
+
*/
|
|
43
|
+
export function parseOptionsExpr(options: any, isSpecRoot = true): any {
|
|
44
|
+
if (Array.isArray(options)) {
|
|
45
|
+
return options.map((_, i) => parseOptionsExpr(options[i], isSpecRoot));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (typeof options === 'object' && options) {
|
|
49
|
+
return mapValues(options, (value, key) => {
|
|
50
|
+
// if options is root and the key is in the white list, parse the expression.
|
|
51
|
+
if (isSpecRoot && EXPR_WHITE_LIST.includes(key)) {
|
|
52
|
+
return parseOptionsExpr(value, key === 'children');
|
|
53
|
+
}
|
|
54
|
+
if (!isSpecRoot) {
|
|
55
|
+
return parseOptionsExpr(value, false);
|
|
56
|
+
}
|
|
57
|
+
return value;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// if options is a string and is a valid expression.
|
|
62
|
+
if (typeof options === 'string') {
|
|
63
|
+
const trimmed = options.trim();
|
|
64
|
+
if (trimmed.startsWith('{') && trimmed.endsWith('}')) {
|
|
65
|
+
return compileExpression(trimmed.slice(1, -1));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return options;
|
|
70
|
+
}
|
package/src/utils/helper.ts
CHANGED
|
@@ -1,5 +1,89 @@
|
|
|
1
1
|
import { DisplayObject } from '@antv/g';
|
|
2
2
|
import { lowerFirst, upperFirst, isPlainObject } from '@antv/util';
|
|
3
|
+
import { Band, Base, Constant } from '@antv/scale';
|
|
4
|
+
import { G2Element } from './selection';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @description Get element's ancestor view node.
|
|
8
|
+
* @param elemenet G2 element.
|
|
9
|
+
* @returns Element's ancestor view node.
|
|
10
|
+
*/
|
|
11
|
+
export function getViewFromElement(element: G2Element) {
|
|
12
|
+
let current = element as G2Element;
|
|
13
|
+
while (current) {
|
|
14
|
+
if (current.attributes?.class === 'view') return current;
|
|
15
|
+
current = current.parentNode as G2Element;
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @description Get element's original data.
|
|
22
|
+
* @param elemenet G2 element.
|
|
23
|
+
* @param elemenet View data, if not provided, will get from element's ancestor view.
|
|
24
|
+
* @returns The original data of the element.
|
|
25
|
+
*/
|
|
26
|
+
export function dataOf(element: G2Element, viewData?: any) {
|
|
27
|
+
const view = viewData ?? getViewFromElement(element).__data__;
|
|
28
|
+
const datum = element.__data__;
|
|
29
|
+
const { markKey, index, seriesIndex } = datum;
|
|
30
|
+
const { markState } = view;
|
|
31
|
+
const selectedMark: any = Array.from(markState.keys()).find(
|
|
32
|
+
(mark) => (mark as any).key === markKey,
|
|
33
|
+
);
|
|
34
|
+
if (!selectedMark) return;
|
|
35
|
+
if (seriesIndex) {
|
|
36
|
+
return seriesIndex.map((i) => selectedMark.data[i]);
|
|
37
|
+
}
|
|
38
|
+
return selectedMark.data[index];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @description Get element's series name.
|
|
43
|
+
* @param elemenet G2 element.
|
|
44
|
+
* @returns The series name of the element.
|
|
45
|
+
*/
|
|
46
|
+
export function seriesOf(elemenet: G2Element): string {
|
|
47
|
+
const viewData = getViewFromElement(elemenet).__data__;
|
|
48
|
+
const { scale } = viewData;
|
|
49
|
+
return groupNameOf(scale, elemenet.__data__);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Get group name with view's scale and element's datum.
|
|
54
|
+
*/
|
|
55
|
+
export function groupNameOf(scale: Record<string, Base<any>>, datum) {
|
|
56
|
+
const { color: scaleColor, series: scaleSeries, facet = false } = scale;
|
|
57
|
+
const { color, series } = datum;
|
|
58
|
+
const invertAble = (scale) => {
|
|
59
|
+
return (
|
|
60
|
+
scale &&
|
|
61
|
+
scale.invert &&
|
|
62
|
+
!(scale instanceof Band) &&
|
|
63
|
+
!(scale instanceof Constant)
|
|
64
|
+
);
|
|
65
|
+
};
|
|
66
|
+
// For non constant color channel.
|
|
67
|
+
if (invertAble(scaleSeries)) {
|
|
68
|
+
const cloned = scaleSeries.clone();
|
|
69
|
+
return cloned.invert(series);
|
|
70
|
+
}
|
|
71
|
+
if (
|
|
72
|
+
series &&
|
|
73
|
+
scaleSeries instanceof Band &&
|
|
74
|
+
scaleSeries.invert(series) !== color &&
|
|
75
|
+
!facet
|
|
76
|
+
) {
|
|
77
|
+
return scaleSeries.invert(series);
|
|
78
|
+
}
|
|
79
|
+
if (invertAble(scaleColor)) {
|
|
80
|
+
const name = scaleColor.invert(color);
|
|
81
|
+
// For threshold scale.
|
|
82
|
+
if (Array.isArray(name)) return null;
|
|
83
|
+
return name;
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
3
87
|
|
|
4
88
|
export function identity<T>(x: T): T {
|
|
5
89
|
return x;
|