@antv/g2 5.2.0 → 5.2.2
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 +6 -5
- package/README.zh-CN.md +2 -1
- package/dist/g2.lite.min.js +1 -1
- package/dist/g2.min.js +1 -1
- package/esm/api/runtime.d.ts +1 -0
- package/esm/api/runtime.js +2 -1
- package/esm/api/runtime.js.map +1 -1
- package/esm/composition/facetRect.js +4 -6
- package/esm/composition/facetRect.js.map +1 -1
- package/esm/composition/utils.d.ts +2 -0
- package/esm/composition/utils.js +10 -2
- package/esm/composition/utils.js.map +1 -1
- package/esm/data/utils/d3-cloud/index.js +4 -2
- package/esm/data/utils/d3-cloud/index.js.map +1 -1
- package/esm/data/wordCloud.d.ts +7 -0
- package/esm/data/wordCloud.js +5 -2
- package/esm/data/wordCloud.js.map +1 -1
- package/esm/interaction/legendFilter.js +0 -2
- package/esm/interaction/legendFilter.js.map +1 -1
- package/esm/interaction/sliderFilter.js +0 -2
- package/esm/interaction/sliderFilter.js.map +1 -1
- package/esm/interaction/tooltip.js +1 -0
- package/esm/interaction/tooltip.js.map +1 -1
- package/esm/label-transform/exceedAdjust.js +5 -0
- package/esm/label-transform/exceedAdjust.js.map +1 -1
- package/esm/runtime/coordinate.js +5 -1
- package/esm/runtime/coordinate.js.map +1 -1
- package/esm/runtime/mark.d.ts +1 -1
- package/esm/runtime/mark.js +1 -2
- package/esm/runtime/mark.js.map +1 -1
- package/esm/runtime/plot.d.ts +2 -2
- package/esm/runtime/plot.js +50 -35
- package/esm/runtime/plot.js.map +1 -1
- package/esm/runtime/render.js +2 -2
- package/esm/runtime/render.js.map +1 -1
- package/esm/runtime/scale.d.ts +1 -0
- package/esm/runtime/scale.js +13 -2
- package/esm/runtime/scale.js.map +1 -1
- package/esm/runtime/transform.d.ts +12 -13
- package/esm/runtime/transform.js +1 -1
- package/esm/runtime/transform.js.map +1 -1
- package/esm/runtime/types/data.d.ts +2 -1
- package/esm/runtime/types/transform.d.ts +2 -5
- package/esm/shape/label/label.js +4 -4
- package/esm/shape/label/label.js.map +1 -1
- package/esm/shape/label/position/spider.d.ts +3 -2
- package/esm/shape/label/position/spider.js +22 -6
- package/esm/shape/label/position/spider.js.map +1 -1
- package/esm/shape/label/position/utils.d.ts +2 -0
- package/esm/shape/label/position/utils.js +68 -0
- package/esm/shape/label/position/utils.js.map +1 -0
- package/esm/shape/text/advance.js +65 -32
- package/esm/shape/text/advance.js.map +1 -1
- package/esm/spec/scale.d.ts +4 -1
- package/esm/transform/diffY.js +1 -1
- package/esm/transform/diffY.js.map +1 -1
- package/esm/transform/dodgeX.js +14 -9
- package/esm/transform/dodgeX.js.map +1 -1
- package/esm/transform/filter.js +25 -3
- package/esm/transform/filter.js.map +1 -1
- package/esm/transform/stackY.js +2 -1
- package/esm/transform/stackY.js.map +1 -1
- package/esm/transform/utils/order.d.ts +1 -1
- package/esm/transform/utils/order.js +1 -1
- package/esm/transform/utils/order.js.map +1 -1
- package/lib/api/runtime.d.ts +1 -0
- package/lib/api/runtime.js +2 -1
- package/lib/api/runtime.js.map +1 -1
- package/lib/composition/facetRect.js +4 -6
- package/lib/composition/facetRect.js.map +1 -1
- package/lib/composition/utils.d.ts +2 -0
- package/lib/composition/utils.js +12 -3
- package/lib/composition/utils.js.map +1 -1
- package/lib/data/utils/d3-cloud/index.js +4 -2
- package/lib/data/utils/d3-cloud/index.js.map +1 -1
- package/lib/data/wordCloud.d.ts +7 -0
- package/lib/data/wordCloud.js +5 -2
- package/lib/data/wordCloud.js.map +1 -1
- package/lib/interaction/legendFilter.js +0 -2
- package/lib/interaction/legendFilter.js.map +1 -1
- package/lib/interaction/sliderFilter.js +0 -2
- package/lib/interaction/sliderFilter.js.map +1 -1
- package/lib/interaction/tooltip.js +1 -0
- package/lib/interaction/tooltip.js.map +1 -1
- package/lib/label-transform/exceedAdjust.js +5 -0
- package/lib/label-transform/exceedAdjust.js.map +1 -1
- package/lib/runtime/coordinate.js +5 -1
- package/lib/runtime/coordinate.js.map +1 -1
- package/lib/runtime/mark.d.ts +1 -1
- package/lib/runtime/mark.js +1 -2
- package/lib/runtime/mark.js.map +1 -1
- package/lib/runtime/plot.d.ts +2 -2
- package/lib/runtime/plot.js +49 -34
- package/lib/runtime/plot.js.map +1 -1
- package/lib/runtime/render.js +2 -2
- package/lib/runtime/render.js.map +1 -1
- package/lib/runtime/scale.d.ts +1 -0
- package/lib/runtime/scale.js +14 -2
- package/lib/runtime/scale.js.map +1 -1
- package/lib/runtime/transform.d.ts +12 -13
- package/lib/runtime/transform.js +1 -1
- package/lib/runtime/transform.js.map +1 -1
- package/lib/runtime/types/data.d.ts +2 -1
- package/lib/runtime/types/transform.d.ts +2 -5
- package/lib/shape/label/label.js +4 -4
- package/lib/shape/label/label.js.map +1 -1
- package/lib/shape/label/position/spider.d.ts +3 -2
- package/lib/shape/label/position/spider.js +22 -6
- package/lib/shape/label/position/spider.js.map +1 -1
- package/lib/shape/label/position/utils.d.ts +2 -0
- package/lib/shape/label/position/utils.js +73 -0
- package/lib/shape/label/position/utils.js.map +1 -0
- package/lib/shape/text/advance.js +65 -32
- package/lib/shape/text/advance.js.map +1 -1
- package/lib/spec/scale.d.ts +4 -1
- package/lib/transform/diffY.js +1 -1
- package/lib/transform/diffY.js.map +1 -1
- package/lib/transform/dodgeX.js +14 -9
- package/lib/transform/dodgeX.js.map +1 -1
- package/lib/transform/filter.js +25 -3
- package/lib/transform/filter.js.map +1 -1
- package/lib/transform/stackY.js +2 -1
- package/lib/transform/stackY.js.map +1 -1
- package/lib/transform/utils/order.d.ts +1 -1
- package/lib/transform/utils/order.js +1 -1
- package/lib/transform/utils/order.js.map +1 -1
- package/package.json +1 -1
- package/src/api/runtime.ts +4 -1
- package/src/composition/facetRect.ts +7 -4
- package/src/composition/utils.ts +10 -2
- package/src/data/utils/d3-cloud/index.ts +6 -2
- package/src/data/wordCloud.ts +14 -2
- package/src/interaction/legendFilter.ts +0 -1
- package/src/interaction/sliderFilter.ts +0 -3
- package/src/interaction/tooltip.ts +1 -0
- package/src/label-transform/exceedAdjust.ts +5 -0
- package/src/runtime/coordinate.ts +6 -1
- package/src/runtime/mark.ts +4 -9
- package/src/runtime/plot.ts +54 -45
- package/src/runtime/render.ts +3 -4
- package/src/runtime/scale.ts +28 -2
- package/src/runtime/transform.ts +13 -14
- package/src/runtime/types/data.ts +3 -1
- package/src/runtime/types/transform.ts +2 -6
- package/src/shape/label/label.ts +4 -3
- package/src/shape/label/position/spider.ts +29 -10
- package/src/shape/label/position/utils.ts +77 -0
- package/src/shape/text/advance.ts +76 -38
- package/src/spec/scale.ts +8 -0
- package/src/transform/diffY.ts +2 -2
- package/src/transform/dodgeX.ts +16 -10
- package/src/transform/filter.ts +55 -5
- package/src/transform/stackY.ts +1 -1
- package/src/transform/utils/order.ts +4 -2
package/src/runtime/transform.ts
CHANGED
|
@@ -12,15 +12,14 @@ import { isFullTooltip } from '../utils/mark';
|
|
|
12
12
|
import { useLibrary } from './library';
|
|
13
13
|
import { createColumnOf } from './mark';
|
|
14
14
|
import { Data, DataComponent } from './types/data';
|
|
15
|
-
import { G2Mark, G2DataOptions } from './types/options';
|
|
16
|
-
import { TransformContext } from './types/transform';
|
|
15
|
+
import { G2Mark, G2DataOptions, G2Context } from './types/options';
|
|
17
16
|
import { isPosition } from './scale';
|
|
18
17
|
|
|
19
18
|
// @todo Add more defaults.
|
|
20
19
|
export function applyDefaults(
|
|
21
20
|
I: number[],
|
|
22
21
|
mark: G2Mark,
|
|
23
|
-
context:
|
|
22
|
+
context: G2Context,
|
|
24
23
|
): [number[], G2Mark] {
|
|
25
24
|
const { encode = {}, scale = {}, transform = [], ...rest } = mark;
|
|
26
25
|
return [I, { ...rest, encode, scale, transform }];
|
|
@@ -29,7 +28,7 @@ export function applyDefaults(
|
|
|
29
28
|
export async function applyDataTransform(
|
|
30
29
|
I: number[],
|
|
31
30
|
mark: G2Mark,
|
|
32
|
-
context:
|
|
31
|
+
context: G2Context,
|
|
33
32
|
): Promise<[number[], G2Mark]> {
|
|
34
33
|
const { library } = context;
|
|
35
34
|
const { data } = mark;
|
|
@@ -40,7 +39,7 @@ export async function applyDataTransform(
|
|
|
40
39
|
const descriptor = normalizedDataSource(data);
|
|
41
40
|
const { transform: T = [], ...connector } = descriptor;
|
|
42
41
|
const transform = [connector, ...T];
|
|
43
|
-
const transformFunctions = transform.map(useData);
|
|
42
|
+
const transformFunctions = transform.map((t) => useData(t, context));
|
|
44
43
|
const transformedData = await composeAsync(transformFunctions)(data);
|
|
45
44
|
|
|
46
45
|
// Maintain the consistency of shape between input and output data.
|
|
@@ -62,7 +61,7 @@ export async function applyDataTransform(
|
|
|
62
61
|
export function flatEncode(
|
|
63
62
|
I: number[],
|
|
64
63
|
mark: G2Mark,
|
|
65
|
-
context:
|
|
64
|
+
context: G2Context,
|
|
66
65
|
): [number[], G2Mark] {
|
|
67
66
|
const { encode } = mark;
|
|
68
67
|
if (!encode) return [I, mark];
|
|
@@ -83,7 +82,7 @@ export function flatEncode(
|
|
|
83
82
|
export function inferChannelsType(
|
|
84
83
|
I: number[],
|
|
85
84
|
mark: G2Mark,
|
|
86
|
-
context:
|
|
85
|
+
context: G2Context,
|
|
87
86
|
): [number[], G2Mark] {
|
|
88
87
|
const { encode, data } = mark;
|
|
89
88
|
if (!encode) return [I, mark];
|
|
@@ -98,7 +97,7 @@ export function inferChannelsType(
|
|
|
98
97
|
export function maybeVisualChannel(
|
|
99
98
|
I: number[],
|
|
100
99
|
mark: G2Mark,
|
|
101
|
-
context:
|
|
100
|
+
context: G2Context,
|
|
102
101
|
): [number[], G2Mark] {
|
|
103
102
|
const { encode } = mark;
|
|
104
103
|
if (!encode) return [I, mark];
|
|
@@ -113,7 +112,7 @@ export function maybeVisualChannel(
|
|
|
113
112
|
export function extractColumns(
|
|
114
113
|
I: number[],
|
|
115
114
|
mark: G2Mark,
|
|
116
|
-
context:
|
|
115
|
+
context: G2Context,
|
|
117
116
|
): [number[], G2Mark] {
|
|
118
117
|
const { encode, data } = mark;
|
|
119
118
|
if (!encode) return [I, mark];
|
|
@@ -129,7 +128,7 @@ export function extractColumns(
|
|
|
129
128
|
export function normalizeTooltip(
|
|
130
129
|
I: number[],
|
|
131
130
|
mark: G2Mark,
|
|
132
|
-
context:
|
|
131
|
+
context: G2Context,
|
|
133
132
|
): [number[], G2Mark] {
|
|
134
133
|
const { tooltip = {} } = mark;
|
|
135
134
|
if (isUnset(tooltip)) return [I, mark];
|
|
@@ -145,7 +144,7 @@ export function normalizeTooltip(
|
|
|
145
144
|
export function extractTooltip(
|
|
146
145
|
I: number[],
|
|
147
146
|
mark: G2Mark,
|
|
148
|
-
context:
|
|
147
|
+
context: G2Context,
|
|
149
148
|
): [number[], G2Mark] {
|
|
150
149
|
const { data, encode, tooltip = {} } = mark;
|
|
151
150
|
if (isUnset(tooltip)) return [I, mark];
|
|
@@ -212,7 +211,7 @@ export function extractTooltip(
|
|
|
212
211
|
export function maybeArrayField(
|
|
213
212
|
I: number[],
|
|
214
213
|
mark: G2Mark,
|
|
215
|
-
context:
|
|
214
|
+
context: G2Context,
|
|
216
215
|
): [number[], G2Mark] {
|
|
217
216
|
const { encode, ...rest } = mark;
|
|
218
217
|
if (!encode) return [I, mark];
|
|
@@ -250,7 +249,7 @@ export function maybeArrayField(
|
|
|
250
249
|
export function addGuideToScale(
|
|
251
250
|
I: number[],
|
|
252
251
|
mark: G2Mark,
|
|
253
|
-
context:
|
|
252
|
+
context: G2Context,
|
|
254
253
|
): [number[], G2Mark] {
|
|
255
254
|
const { axis = {}, legend = {}, slider = {}, scrollbar = {} } = mark;
|
|
256
255
|
const normalize = (guide: boolean | Record<string, any>, channel: string) => {
|
|
@@ -298,7 +297,7 @@ export function addGuideToScale(
|
|
|
298
297
|
export function maybeNonAnimate(
|
|
299
298
|
I: number[],
|
|
300
299
|
mark: G2Mark,
|
|
301
|
-
context:
|
|
300
|
+
context: G2Context,
|
|
302
301
|
): [number[], G2Mark] {
|
|
303
302
|
const { animate } = mark;
|
|
304
303
|
if (animate || animate === undefined) return [I, mark];
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { G2Context } from './options';
|
|
2
|
+
|
|
1
3
|
export type DataOptions = Record<string, any>;
|
|
2
4
|
|
|
3
5
|
export type DataProps = Record<string, never>;
|
|
4
6
|
|
|
5
7
|
export type DataComponent<O extends DataOptions = DataOptions> = {
|
|
6
|
-
(options?: O): Data;
|
|
8
|
+
(options?: O, context?: G2Context): Data;
|
|
7
9
|
props: DataProps;
|
|
8
10
|
};
|
|
9
11
|
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { ColumnValue, NormalizedEncodeSpec } from './encode';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export type TransformContext = {
|
|
5
|
-
library: G2Library;
|
|
6
|
-
};
|
|
2
|
+
import { G2Context, G2Mark } from './options';
|
|
7
3
|
|
|
8
4
|
export type TransformOptions = Record<string, any>;
|
|
9
5
|
|
|
@@ -18,7 +14,7 @@ export type TransformComponent<O extends TransformOptions = TransformOptions> =
|
|
|
18
14
|
export type Transform = (
|
|
19
15
|
I: number[],
|
|
20
16
|
mark: G2Mark,
|
|
21
|
-
context:
|
|
17
|
+
context: G2Context,
|
|
22
18
|
) => [number[], G2Mark] | Promise<[number[], G2Mark]>;
|
|
23
19
|
|
|
24
20
|
export type TransformSpec = {
|
package/src/shape/label/label.ts
CHANGED
|
@@ -29,6 +29,7 @@ function getDefaultStyle(
|
|
|
29
29
|
coordinate: Coordinate,
|
|
30
30
|
theme: G2Theme,
|
|
31
31
|
options: LabelOptions,
|
|
32
|
+
labels: Vector2[][],
|
|
32
33
|
): Record<string, any> {
|
|
33
34
|
// For non-series mark, calc position for label based on
|
|
34
35
|
// position and the bounds of shape.
|
|
@@ -48,7 +49,7 @@ function getDefaultStyle(
|
|
|
48
49
|
}
|
|
49
50
|
return {
|
|
50
51
|
...t,
|
|
51
|
-
...processor(p, points, v, coordinate, options),
|
|
52
|
+
...processor(p, points, v, coordinate, options, labels),
|
|
52
53
|
};
|
|
53
54
|
}
|
|
54
55
|
|
|
@@ -59,7 +60,7 @@ function getDefaultStyle(
|
|
|
59
60
|
export const Label: SC<LabelOptions> = (options, context) => {
|
|
60
61
|
const { coordinate, theme } = context;
|
|
61
62
|
const { render } = options;
|
|
62
|
-
return (points, value) => {
|
|
63
|
+
return (points, value, style, labels) => {
|
|
63
64
|
const {
|
|
64
65
|
text,
|
|
65
66
|
x,
|
|
@@ -73,7 +74,7 @@ export const Label: SC<LabelOptions> = (options, context) => {
|
|
|
73
74
|
rotate = 0,
|
|
74
75
|
transform = '',
|
|
75
76
|
...defaultStyle
|
|
76
|
-
} = getDefaultStyle(points, value, coordinate, theme, options);
|
|
77
|
+
} = getDefaultStyle(points, value, coordinate, theme, options, labels);
|
|
77
78
|
|
|
78
79
|
return select(new Advance())
|
|
79
80
|
.call(applyStyle, defaultStyle)
|
|
@@ -3,19 +3,16 @@ import { Vector2 } from '../../../runtime';
|
|
|
3
3
|
import { isCircular } from '../../../utils/coordinate';
|
|
4
4
|
import { LabelPosition } from './default';
|
|
5
5
|
import { inferOutsideCircularStyle, radiusOf, angleOf } from './outside';
|
|
6
|
+
import { hideAndDodgeY } from './utils';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export function spider(
|
|
11
|
-
position: LabelPosition,
|
|
8
|
+
const styleByPoints = new WeakMap();
|
|
9
|
+
|
|
10
|
+
function compute(
|
|
12
11
|
points: Vector2[],
|
|
13
12
|
value: Record<string, any>,
|
|
14
13
|
coordinate: Coordinate,
|
|
15
14
|
) {
|
|
16
|
-
if (!isCircular(coordinate)) return {};
|
|
17
15
|
const { connectorLength, connectorLength2, connectorDistance } = value;
|
|
18
|
-
|
|
19
16
|
const { ...style }: any = inferOutsideCircularStyle(
|
|
20
17
|
'outside',
|
|
21
18
|
points,
|
|
@@ -27,13 +24,35 @@ export function spider(
|
|
|
27
24
|
const angle = angleOf(points, value, coordinate);
|
|
28
25
|
const radius1 = radius + connectorLength + connectorLength2;
|
|
29
26
|
const sign = Math.sin(angle) > 0 ? 1 : -1;
|
|
30
|
-
|
|
31
27
|
const newX = center[0] + (radius1 + +connectorDistance) * sign;
|
|
32
|
-
|
|
33
28
|
const { x: originX } = style;
|
|
34
29
|
const dx = newX - originX;
|
|
35
30
|
style.x += dx;
|
|
36
31
|
style.connectorPoints[0][0] -= dx;
|
|
37
|
-
|
|
38
32
|
return style;
|
|
39
33
|
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Spider label transform only suitable for the labels in polar coordinate,
|
|
37
|
+
* labels should distinguish coordinate type.
|
|
38
|
+
*/
|
|
39
|
+
export function spider(
|
|
40
|
+
position: LabelPosition,
|
|
41
|
+
points: Vector2[],
|
|
42
|
+
value: Record<string, any>,
|
|
43
|
+
coordinate: Coordinate,
|
|
44
|
+
options: Record<string, any>,
|
|
45
|
+
labels: Vector2[][],
|
|
46
|
+
) {
|
|
47
|
+
if (!isCircular(coordinate)) return {};
|
|
48
|
+
if (styleByPoints.has(points)) return styleByPoints.get(points);
|
|
49
|
+
const computed = labels.map((points) => compute(points, value, coordinate));
|
|
50
|
+
const { width, height } = coordinate.getOptions();
|
|
51
|
+
const left = computed.filter((d) => d.x < width / 2);
|
|
52
|
+
const right = computed.filter((d) => d.x >= width / 2);
|
|
53
|
+
const extendedOptions = { ...options, height };
|
|
54
|
+
hideAndDodgeY(left, extendedOptions);
|
|
55
|
+
hideAndDodgeY(right, extendedOptions);
|
|
56
|
+
computed.forEach((style, i) => styleByPoints.set(labels[i], style));
|
|
57
|
+
return styleByPoints.get(points);
|
|
58
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { sort } from 'd3-array';
|
|
2
|
+
|
|
3
|
+
// Optimize antiCollision from: https://github.com/antvis/G2/blob/master/src/geometry/label/layout/pie/util.ts
|
|
4
|
+
export function dodgeY(
|
|
5
|
+
labels: Record<string, any>[],
|
|
6
|
+
options: Record<string, any> = {},
|
|
7
|
+
) {
|
|
8
|
+
const { labelHeight = 14, height } = options;
|
|
9
|
+
|
|
10
|
+
// Sort labels by y and init boxes (one box for each label)
|
|
11
|
+
const sortedLabels = sort(labels, (d) => d.y);
|
|
12
|
+
const n = sortedLabels.length;
|
|
13
|
+
const boxes = new Array(n);
|
|
14
|
+
for (let i = 0; i < n; i++) {
|
|
15
|
+
const label = sortedLabels[i];
|
|
16
|
+
const { y } = label;
|
|
17
|
+
boxes[i] = { y, y1: y + labelHeight, labels: [y] };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Merge boxes until no overlapping boxes or only one box left.
|
|
21
|
+
// All the boxes should start higher than 0, but maybe higher than height.
|
|
22
|
+
let overlap = true;
|
|
23
|
+
while (overlap) {
|
|
24
|
+
overlap = false;
|
|
25
|
+
// Scan backward because boxes maybe deleted.
|
|
26
|
+
for (let i = boxes.length - 1; i > 0; i--) {
|
|
27
|
+
const box = boxes[i];
|
|
28
|
+
const preBox = boxes[i - 1];
|
|
29
|
+
if (preBox.y1 > box.y) {
|
|
30
|
+
overlap = true;
|
|
31
|
+
preBox.labels.push(...box.labels);
|
|
32
|
+
boxes.splice(i, 1);
|
|
33
|
+
|
|
34
|
+
// Compute new y1 to contain the current box.
|
|
35
|
+
preBox.y1 += box.y1 - box.y;
|
|
36
|
+
|
|
37
|
+
// Make sure the new box is in the range of [0, height].
|
|
38
|
+
const newHeight = preBox.y1 - preBox.y;
|
|
39
|
+
preBox.y1 = Math.max(Math.min(preBox.y1, height), newHeight);
|
|
40
|
+
preBox.y = preBox.y1 - newHeight;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let i = 0;
|
|
46
|
+
for (const box of boxes) {
|
|
47
|
+
const { y, labels } = box;
|
|
48
|
+
let prevY = y - labelHeight;
|
|
49
|
+
for (const curY of labels) {
|
|
50
|
+
const label = sortedLabels[i++];
|
|
51
|
+
const expectedY = prevY + labelHeight;
|
|
52
|
+
const dy = expectedY - curY;
|
|
53
|
+
label.connectorPoints[0][1] -= dy;
|
|
54
|
+
label.y = prevY + labelHeight;
|
|
55
|
+
prevY += labelHeight;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function hideAndDodgeY(
|
|
61
|
+
unsorted: Record<string, any>[],
|
|
62
|
+
options: Record<string, any>,
|
|
63
|
+
) {
|
|
64
|
+
const labels = sort(unsorted, (d) => d.y);
|
|
65
|
+
const { height, labelHeight = 14 } = options;
|
|
66
|
+
const maxCount = Math.ceil(height / labelHeight);
|
|
67
|
+
if (labels.length <= maxCount) return dodgeY(labels, options);
|
|
68
|
+
const filtered = [];
|
|
69
|
+
for (let i = 0; i < labels.length; i++) {
|
|
70
|
+
// Hide labels out of range.
|
|
71
|
+
if (i < labels.length - maxCount) {
|
|
72
|
+
labels[i].opacity = 0;
|
|
73
|
+
labels[i].connector = false;
|
|
74
|
+
} else filtered.push(labels[i]);
|
|
75
|
+
}
|
|
76
|
+
dodgeY(filtered, options);
|
|
77
|
+
}
|
|
@@ -50,7 +50,7 @@ type TextShapeStyleProps = Omit<TextStyleProps, 'text'> &
|
|
|
50
50
|
text?: string;
|
|
51
51
|
};
|
|
52
52
|
|
|
53
|
-
function getConnectorPoint(shape: GText | Rect) {
|
|
53
|
+
function getConnectorPoint(shape: GText | Rect): [number, number] {
|
|
54
54
|
const {
|
|
55
55
|
min: [x0, y0],
|
|
56
56
|
max: [x1, y1],
|
|
@@ -61,7 +61,6 @@ function getConnectorPoint(shape: GText | Rect) {
|
|
|
61
61
|
if (x1 < 0) x = x1;
|
|
62
62
|
if (y0 > 0) y = y0;
|
|
63
63
|
if (y1 < 0) y = y1;
|
|
64
|
-
|
|
65
64
|
return [x, y];
|
|
66
65
|
}
|
|
67
66
|
|
|
@@ -91,41 +90,76 @@ const cos = (p0: Vector2, p1: Vector2, p2: Vector2) => {
|
|
|
91
90
|
return (a ** 2 + b ** 2 - c ** 2) / (2 * a * b);
|
|
92
91
|
};
|
|
93
92
|
|
|
93
|
+
// A path from element to label.
|
|
94
|
+
// Adapted drawLabelLine from https://github.com/antvis/G2/blob/master/src/geometry/label/layout/pie/spider.ts
|
|
94
95
|
function inferConnectorPath(
|
|
95
96
|
shape: DisplayObject,
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
coordCenter,
|
|
97
|
+
end: Vector2,
|
|
98
|
+
control: Vector2[],
|
|
99
|
+
coordCenter: Vector2,
|
|
100
|
+
left = true,
|
|
101
|
+
top = true,
|
|
99
102
|
) {
|
|
100
|
-
const
|
|
101
|
-
const [x, y] = getConnectorPoint(shape);
|
|
102
|
-
// Straight connector line.
|
|
103
|
-
if (x0 === x1 && y0 === y1) {
|
|
104
|
-
return line()([
|
|
105
|
-
[0, 0],
|
|
106
|
-
[x, y],
|
|
107
|
-
]);
|
|
108
|
-
}
|
|
103
|
+
const path = (points) => line()(points);
|
|
109
104
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
105
|
+
if (!end[0] && !end[1]) return path([getConnectorPoint(shape), end]);
|
|
106
|
+
if (!control.length) return path([[0, 0], end]);
|
|
107
|
+
|
|
108
|
+
const [inflection, start] = control;
|
|
109
|
+
const p1 = [...start];
|
|
110
|
+
const p2 = [...inflection];
|
|
111
|
+
|
|
112
|
+
// Label has been adjusted, so add offset to the label.
|
|
113
|
+
if (start[0] !== inflection[0]) {
|
|
114
|
+
const offset = left ? -4 : 4;
|
|
115
|
+
p1[1] = start[1];
|
|
113
116
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
117
|
+
// For the label in the first quadrant.
|
|
118
|
+
if (top && !left) {
|
|
119
|
+
p1[0] = Math.max(inflection[0], start[0] - offset);
|
|
120
|
+
if (start[1] < inflection[1]) {
|
|
121
|
+
p2[1] = p1[1];
|
|
122
|
+
} else {
|
|
123
|
+
p2[1] = inflection[1];
|
|
124
|
+
p2[0] = Math.max(p2[0], p1[0] - offset);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// For the label in the second quadrant.
|
|
129
|
+
if (!top && !left) {
|
|
130
|
+
p1[0] = Math.max(inflection[0], start[0] - offset);
|
|
131
|
+
if (start[1] > inflection[1]) {
|
|
132
|
+
p2[1] = p1[1];
|
|
133
|
+
} else {
|
|
134
|
+
p2[1] = inflection[1];
|
|
135
|
+
p2[0] = Math.max(p2[0], p1[0] - offset);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// For the label in the third quadrant.
|
|
140
|
+
if (!top && left) {
|
|
141
|
+
p1[0] = Math.min(inflection[0], start[0] - offset);
|
|
142
|
+
if (start[1] > inflection[1]) {
|
|
143
|
+
p2[1] = p1[1];
|
|
144
|
+
} else {
|
|
145
|
+
p2[1] = inflection[1];
|
|
146
|
+
p2[0] = Math.min(p2[0], p1[0] - offset);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// For the label in the fourth quadrant.
|
|
151
|
+
if (top && left) {
|
|
152
|
+
p1[0] = Math.min(inflection[0], start[0] - offset);
|
|
153
|
+
if (start[1] < inflection[1]) {
|
|
154
|
+
p2[1] = p1[1];
|
|
155
|
+
} else {
|
|
156
|
+
p2[1] = inflection[1];
|
|
157
|
+
p2[0] = Math.min(p2[0], p1[0] - offset);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
126
160
|
}
|
|
127
161
|
|
|
128
|
-
return
|
|
162
|
+
return path([start, p1, p2, inflection, end]);
|
|
129
163
|
}
|
|
130
164
|
|
|
131
165
|
export const Advance = createElement((g) => {
|
|
@@ -161,14 +195,12 @@ export const Advance = createElement((g) => {
|
|
|
161
195
|
}
|
|
162
196
|
|
|
163
197
|
const { padding, ...backgroundStyle } = subObject(rest, 'background');
|
|
164
|
-
const { points = [], ...connectorStyle } = subObject(
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
];
|
|
198
|
+
const { points: controlPoints = [], ...connectorStyle } = subObject(
|
|
199
|
+
rest,
|
|
200
|
+
'connector',
|
|
201
|
+
);
|
|
169
202
|
|
|
170
203
|
let textShape;
|
|
171
|
-
// Use html to customize advance text.
|
|
172
204
|
if (innerHTML) {
|
|
173
205
|
textShape = select(g)
|
|
174
206
|
.maybeAppend('html', 'html', className)
|
|
@@ -201,12 +233,18 @@ export const Advance = createElement((g) => {
|
|
|
201
233
|
.call(applyStyle, background ? backgroundStyle : {})
|
|
202
234
|
.node();
|
|
203
235
|
|
|
236
|
+
const left = +x0 < coordCenter[0];
|
|
237
|
+
const top = +y0 < coordCenter[1];
|
|
238
|
+
const end: [number, number] = [+x0 - +x, +y0 - +y];
|
|
204
239
|
const connectorPath = inferConnectorPath(
|
|
205
240
|
rect,
|
|
206
|
-
|
|
207
|
-
|
|
241
|
+
end,
|
|
242
|
+
controlPoints,
|
|
208
243
|
coordCenter,
|
|
244
|
+
left,
|
|
245
|
+
top,
|
|
209
246
|
);
|
|
247
|
+
|
|
210
248
|
const markerStart =
|
|
211
249
|
startMarker &&
|
|
212
250
|
new Marker({
|
package/src/spec/scale.ts
CHANGED
|
@@ -13,6 +13,11 @@ import {
|
|
|
13
13
|
SqrtOptions,
|
|
14
14
|
SequentialOptions,
|
|
15
15
|
ConstantOptions,
|
|
16
|
+
Linear,
|
|
17
|
+
Log,
|
|
18
|
+
Pow,
|
|
19
|
+
Sqrt,
|
|
20
|
+
Time,
|
|
16
21
|
} from '@antv/scale';
|
|
17
22
|
import { ScaleComponent } from '../runtime';
|
|
18
23
|
import { Palette } from './palette';
|
|
@@ -51,6 +56,8 @@ export type ScaleTypes =
|
|
|
51
56
|
| 'constant'
|
|
52
57
|
| ScaleComponent;
|
|
53
58
|
|
|
59
|
+
type QuantitativeScale = Linear | Log | Pow | Sqrt | Time;
|
|
60
|
+
|
|
54
61
|
export type BaseScale<T extends ScaleTypes, O> = {
|
|
55
62
|
type?: T;
|
|
56
63
|
palette?: Palette['type'] | string;
|
|
@@ -64,6 +71,7 @@ export type BaseScale<T extends ScaleTypes, O> = {
|
|
|
64
71
|
zero?: boolean;
|
|
65
72
|
offset?: (t: number) => number;
|
|
66
73
|
relations?: [any, any][];
|
|
74
|
+
groupTransform?: (scales: QuantitativeScale[]) => void;
|
|
67
75
|
} & O;
|
|
68
76
|
|
|
69
77
|
export type LinearScale = BaseScale<'linear', LinearOptions>;
|
package/src/transform/diffY.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { deepMix } from '@antv/util';
|
|
|
2
2
|
import { TransformComponent as TC } from '../runtime';
|
|
3
3
|
import { DiffYTransform } from '../spec';
|
|
4
4
|
import { column, columnOf, maybeColumnOf } from './utils/helper';
|
|
5
|
-
import {
|
|
5
|
+
import { createGroups } from './utils/order';
|
|
6
6
|
|
|
7
7
|
export type DiffYOptions = Omit<DiffYTransform, 'type'>;
|
|
8
8
|
|
|
@@ -34,7 +34,7 @@ export const DiffY: TC<DiffYOptions> = (options = {}) => {
|
|
|
34
34
|
// Get the max Y of current group with current Y exclude.
|
|
35
35
|
const max = Math.max(...YG.filter((_, _i) => _i !== idx));
|
|
36
36
|
// Diff Y value.
|
|
37
|
-
newY1[i] = Y[i] > max ? max : Y[i];
|
|
37
|
+
newY1[i] = +Y[i] > max ? max : Y[i];
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
package/src/transform/dodgeX.ts
CHANGED
|
@@ -24,14 +24,26 @@ export const DodgeX: TC<DodgeXOptions> = (options = {}) => {
|
|
|
24
24
|
const [Y] = columnOf(encode, 'y');
|
|
25
25
|
const [S] = maybeColumnOf(encode, 'series', 'color');
|
|
26
26
|
const domainSeries = domainOf(S, scaleSeries);
|
|
27
|
+
const newMark = deepMix({}, mark, {
|
|
28
|
+
scale: {
|
|
29
|
+
series: {
|
|
30
|
+
domain: domainSeries,
|
|
31
|
+
paddingInner: padding,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
});
|
|
27
35
|
|
|
28
36
|
// Create groups and apply specified order for each group.
|
|
29
37
|
const groups = createGroups(groupBy, I, mark);
|
|
30
38
|
const createComparator = normalizeComparator(orderBy);
|
|
39
|
+
|
|
40
|
+
if (!createComparator) {
|
|
41
|
+
return [I, deepMix(newMark, { encode: { series: column(S) } })];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Sort and Update series for each mark related to series domain.
|
|
31
45
|
const comparator = createComparator(data, Y, S);
|
|
32
46
|
if (comparator) applyOrder(groups, comparator);
|
|
33
|
-
|
|
34
|
-
// Update series for each mark related to series domain.
|
|
35
47
|
const newS = new Array(I.length);
|
|
36
48
|
for (const G of groups) {
|
|
37
49
|
if (reverse) G.reverse();
|
|
@@ -42,15 +54,9 @@ export const DodgeX: TC<DodgeXOptions> = (options = {}) => {
|
|
|
42
54
|
|
|
43
55
|
return [
|
|
44
56
|
I,
|
|
45
|
-
deepMix(
|
|
46
|
-
scale: {
|
|
47
|
-
series: {
|
|
48
|
-
domain: domainSeries,
|
|
49
|
-
paddingInner: padding,
|
|
50
|
-
},
|
|
51
|
-
},
|
|
57
|
+
deepMix(newMark, {
|
|
52
58
|
encode: {
|
|
53
|
-
series: column(newS),
|
|
59
|
+
series: column(orderBy ? newS : S),
|
|
54
60
|
},
|
|
55
61
|
}),
|
|
56
62
|
];
|
package/src/transform/filter.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { deepMix } from '@antv/util';
|
|
2
|
-
import { TransformComponent as TC } from '../runtime';
|
|
2
|
+
import { G2Mark, TransformComponent as TC } from '../runtime';
|
|
3
3
|
import { FilterTransform } from '../spec';
|
|
4
4
|
import { defined } from '../utils/helper';
|
|
5
5
|
import { columnOf } from './utils/helper';
|
|
@@ -11,6 +11,54 @@ function normalizeValue(value) {
|
|
|
11
11
|
else return [value, true];
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
function filterWhenNoElements(mark: G2Mark) {
|
|
15
|
+
const { encode } = mark;
|
|
16
|
+
|
|
17
|
+
// keep y-axis
|
|
18
|
+
const noElementMark = {
|
|
19
|
+
...mark,
|
|
20
|
+
encode: {
|
|
21
|
+
...mark.encode,
|
|
22
|
+
y: {
|
|
23
|
+
...mark.encode.y,
|
|
24
|
+
value: [],
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const targetField = encode?.color?.field;
|
|
30
|
+
if (!encode || !targetField) {
|
|
31
|
+
return noElementMark;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 获取color的筛选源
|
|
35
|
+
let filterObject;
|
|
36
|
+
|
|
37
|
+
for (const [key, v] of Object.entries(encode)) {
|
|
38
|
+
if ((key === 'x' || key === 'y') && v.field === targetField) {
|
|
39
|
+
filterObject = {
|
|
40
|
+
...filterObject,
|
|
41
|
+
[key]: {
|
|
42
|
+
...v,
|
|
43
|
+
value: [],
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!filterObject) {
|
|
50
|
+
return noElementMark;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
...mark,
|
|
55
|
+
encode: {
|
|
56
|
+
...mark.encode,
|
|
57
|
+
...filterObject,
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
14
62
|
/**
|
|
15
63
|
* The Filter transform filter channels.
|
|
16
64
|
*/
|
|
@@ -35,14 +83,16 @@ export const Filter: TC<FilterOptions> = (options = {}) => {
|
|
|
35
83
|
}
|
|
36
84
|
})
|
|
37
85
|
.filter(defined);
|
|
38
|
-
|
|
39
|
-
// Skip empty filters.
|
|
40
|
-
if (filters.length === 0) return [I, mark];
|
|
41
|
-
|
|
42
86
|
// Filter index and channels.
|
|
43
87
|
const totalFilter = (i) => filters.every((f) => f(i));
|
|
44
88
|
const FI = I.filter(totalFilter);
|
|
45
89
|
const newIndex = FI.map((_, i) => i);
|
|
90
|
+
|
|
91
|
+
if (filters.length === 0) {
|
|
92
|
+
const targetMark = filterWhenNoElements(mark);
|
|
93
|
+
return [I, targetMark];
|
|
94
|
+
}
|
|
95
|
+
|
|
46
96
|
const newEncodes = Object.entries(encode).map(([key, encode]) => {
|
|
47
97
|
return [
|
|
48
98
|
key,
|
package/src/transform/stackY.ts
CHANGED
|
@@ -35,7 +35,7 @@ export const StackY: TC<StackYOptions> = (options = {}) => {
|
|
|
35
35
|
|
|
36
36
|
// Create groups and apply specified order for each group.
|
|
37
37
|
const groups = createGroups(groupBy, I, mark);
|
|
38
|
-
const createComparator = normalizeComparator(orderBy);
|
|
38
|
+
const createComparator = normalizeComparator(orderBy) ?? (() => null);
|
|
39
39
|
const comparator = createComparator(data, Y, S);
|
|
40
40
|
if (comparator) applyOrder(groups, comparator);
|
|
41
41
|
|
|
@@ -34,14 +34,16 @@ export function createGroups(
|
|
|
34
34
|
return Array.from(group(I, key).values());
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
export function normalizeComparator(
|
|
37
|
+
export function normalizeComparator(
|
|
38
|
+
order: Order,
|
|
39
|
+
): IndexComparatorFactory | null {
|
|
38
40
|
if (Array.isArray(order)) return createFieldsOrder(order);
|
|
39
41
|
if (typeof order === 'function') return createFunctionOrder(order);
|
|
40
42
|
if (order === 'series') return createSeriesOrder;
|
|
41
43
|
if (order === 'value') return createValueOrder;
|
|
42
44
|
if (order === 'sum') return createSumOrder;
|
|
43
45
|
if (order === 'maxIndex') return createMaxIndexOrder;
|
|
44
|
-
return
|
|
46
|
+
return null;
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
export function applyOrder(groups: number[][], comparator: IndexComparator) {
|