Skip to content

Commit 906411d

Browse files
committed
fix: fix duplicated label renderde for line/area/radar plot
1 parent 960b741 commit 906411d

File tree

11 files changed

+163
-43
lines changed

11 files changed

+163
-43
lines changed

__tests__/bugs/issue-2064-spec.ts

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { Line, Area, Radar } from '../../src';
2+
import { createDiv } from '../utils/dom';
3+
4+
const data = [
5+
{
6+
type: '家具家电',
7+
sales: 38,
8+
},
9+
{
10+
type: '粮油副食',
11+
sales: 52,
12+
},
13+
];
14+
15+
describe('#2064', () => {
16+
it('#2064 line', () => {
17+
const plot = new Line(createDiv(), {
18+
width: 400,
19+
height: 400,
20+
data,
21+
xField: 'type',
22+
yField: 'sales',
23+
point: {},
24+
label: {},
25+
});
26+
27+
plot.render();
28+
29+
const line = plot.chart.geometries.find((geom) => geom.type === 'line');
30+
const point = plot.chart.geometries.find((geom) => geom.type === 'point');
31+
32+
expect(line.labelsContainer.getChildren()).toHaveLength(data.length);
33+
expect(point.labelsContainer.getChildren()).toHaveLength(0);
34+
35+
plot.destroy();
36+
});
37+
38+
it('#2064 area', () => {
39+
const plot = new Area(createDiv(), {
40+
width: 400,
41+
height: 400,
42+
data,
43+
xField: 'type',
44+
yField: 'sales',
45+
line: {},
46+
point: {},
47+
label: {},
48+
});
49+
50+
plot.render();
51+
52+
const area = plot.chart.geometries.find((geom) => geom.type === 'area');
53+
const line = plot.chart.geometries.find((geom) => geom.type === 'line');
54+
const point = plot.chart.geometries.find((geom) => geom.type === 'point');
55+
56+
expect(area.labelsContainer.getChildren()).toHaveLength(data.length);
57+
expect(line.labelsContainer.getChildren()).toHaveLength(0);
58+
expect(point.labelsContainer.getChildren()).toHaveLength(0);
59+
60+
plot.destroy();
61+
});
62+
63+
it('#2064 radar', () => {
64+
const plot = new Radar(createDiv(), {
65+
width: 400,
66+
height: 400,
67+
data,
68+
xField: 'type',
69+
yField: 'sales',
70+
area: {},
71+
point: {},
72+
label: {},
73+
radius: 0.8,
74+
});
75+
76+
plot.render();
77+
78+
const area = plot.chart.geometries.find((geom) => geom.type === 'area');
79+
const line = plot.chart.geometries.find((geom) => geom.type === 'line');
80+
const point = plot.chart.geometries.find((geom) => geom.type === 'point');
81+
82+
expect(area.labelsContainer.getChildren()).toHaveLength(0);
83+
expect(line.labelsContainer.getChildren()).toHaveLength(data.length);
84+
expect(point.labelsContainer.getChildren()).toHaveLength(0);
85+
});
86+
});

src/adaptor/geometries/area.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Params } from '../../core/adaptor';
22
import { getTooltipMapping } from '../../utils/tooltip';
33
import { deepAssign } from '../../utils';
4-
import { GeometryOptions, geometry, MappingOptions } from './base';
4+
import { GeometryOptions, geometry, MappingOptions, GeometryAdaptorConfig } from './base';
55

66
export interface AreaGeometryOptions extends GeometryOptions {
77
/** x 轴字段 */
@@ -19,8 +19,9 @@ export interface AreaGeometryOptions extends GeometryOptions {
1919
/**
2020
* area geometry 的配置处理
2121
* @param params
22+
* @param config
2223
*/
23-
export function area<O extends AreaGeometryOptions>(params: Params<O>): Params<O> {
24+
export function area<O extends AreaGeometryOptions>(params: Params<O>, config?: GeometryAdaptorConfig): Params<O> {
2425
const { options } = params;
2526
const { area, xField, yField, seriesField, smooth, tooltip } = options;
2627

src/adaptor/geometries/base.ts

+22-10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export type Geometry = {
5959
*/
6060
export type GeometryOptions = Geometry & Partial<Options>;
6161

62+
/** geometry adaptor 配置 */
63+
export type GeometryAdaptorConfig = {
64+
/** 是否允许 label 配置 */
65+
label: boolean;
66+
};
67+
6268
/**
6369
* 获得映射的字段列表
6470
* @param options
@@ -119,8 +125,12 @@ export function getMappingFunction(mappingFields: string[], func: (datum: Datum)
119125
/**
120126
* 通用 geometry 的配置处理的 adaptor
121127
* @param params
128+
* @param config
122129
*/
123-
export function geometry<O extends GeometryOptions>(params: Params<O>): Params<O> {
130+
export function geometry<O extends GeometryOptions>(
131+
params: Params<O>,
132+
config: GeometryAdaptorConfig = { label: false }
133+
): Params<O> {
124134
const { chart, options } = params;
125135
const {
126136
type,
@@ -220,15 +230,17 @@ export function geometry<O extends GeometryOptions>(params: Params<O>): Params<O
220230
/**
221231
* label 的映射
222232
*/
223-
if (label === false) {
224-
geometry.label(false);
225-
} else if (label) {
226-
const { callback, fields, ...cfg } = label;
227-
geometry.label({
228-
fields: fields || [yField],
229-
callback,
230-
cfg: transformLabel(cfg),
231-
});
233+
if (config?.label) {
234+
if (label === false) {
235+
geometry.label(false);
236+
} else if (label) {
237+
const { callback, fields, ...cfg } = label;
238+
geometry.label({
239+
fields: fields || [yField],
240+
callback,
241+
cfg: transformLabel(cfg),
242+
});
243+
}
232244
}
233245

234246
/**

src/adaptor/geometries/edge.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Params } from '../../core/adaptor';
22
import { getTooltipMapping } from '../../utils/tooltip';
33
import { deepAssign } from '../../utils';
4-
import { geometry, MappingOptions, GeometryOptions } from './base';
4+
import { geometry, MappingOptions, GeometryOptions, GeometryAdaptorConfig } from './base';
55

66
export interface EdgeGeometryOptions extends GeometryOptions {
77
/** x 轴字段 */
@@ -17,8 +17,9 @@ export interface EdgeGeometryOptions extends GeometryOptions {
1717
/**
1818
* edge 的配置处理
1919
* @param params
20+
* @param config
2021
*/
21-
export function edge<O extends EdgeGeometryOptions>(params: Params<O>): Params<O> {
22+
export function edge<O extends EdgeGeometryOptions>(params: Params<O>, config?: GeometryAdaptorConfig): Params<O> {
2223
const { options } = params;
2324
const { edge, xField, yField, seriesField, tooltip } = options;
2425

src/adaptor/geometries/interval.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isNil, isObject } from '@antv/util';
33
import { Params } from '../../core/adaptor';
44
import { deepAssign } from '../../utils';
55
import { getTooltipMapping } from '../../utils/tooltip';
6-
import { GeometryOptions, MappingOptions, geometry } from './base';
6+
import { GeometryOptions, MappingOptions, geometry, GeometryAdaptorConfig } from './base';
77

88
export interface IntervalGeometryOptions extends GeometryOptions {
99
/** x 轴字段 */
@@ -74,7 +74,10 @@ function otherAdaptor<O extends IntervalGeometryOptions>(params: Params<O>): Par
7474
return params;
7575
}
7676

77-
export function interval<O extends IntervalGeometryOptions>(params: Params<O>): Params<O> {
77+
export function interval<O extends IntervalGeometryOptions>(
78+
params: Params<O>,
79+
config?: GeometryAdaptorConfig
80+
): Params<O> {
7881
const { options } = params;
7982
const { xField, yField, interval, seriesField, tooltip } = options;
8083

@@ -93,7 +96,8 @@ export function interval<O extends IntervalGeometryOptions>(params: Params<O>):
9396
...interval,
9497
},
9598
},
96-
})
99+
}),
100+
config
97101
)
98102
: params;
99103

src/adaptor/geometries/line.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Params } from '../../core/adaptor';
33
import { ColorAttr, StyleAttr, SizeAttr, ShapeAttr } from '../../types';
44
import { getTooltipMapping } from '../../utils/tooltip';
55
import { deepAssign } from '../../utils';
6-
import { GeometryOptions, geometry, MappingOptions } from './base';
6+
import { GeometryOptions, geometry, MappingOptions, GeometryAdaptorConfig } from './base';
77

88
export interface LineGeometryOptions extends GeometryOptions {
99
/** x 轴字段 */
@@ -25,8 +25,9 @@ export interface LineGeometryOptions extends GeometryOptions {
2525
/**
2626
* line 辅助点的配置处理
2727
* @param params
28+
* @param config
2829
*/
29-
export function line<O extends LineGeometryOptions>(params: Params<O>): Params<O> {
30+
export function line<O extends LineGeometryOptions>(params: Params<O>, config?: GeometryAdaptorConfig): Params<O> {
3031
const { options } = params;
3132
const { line, stepType, xField, yField, seriesField, smooth, connectNulls, tooltip } = options;
3233

@@ -49,7 +50,8 @@ export function line<O extends LineGeometryOptions>(params: Params<O>): Params<O
4950
),
5051
args: { connectNulls },
5152
},
52-
})
53+
}),
54+
config
5355
)
5456
: params;
5557
}

src/adaptor/geometries/point.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Params } from '../../core/adaptor';
22
import { getTooltipMapping } from '../../utils/tooltip';
33
import { deepAssign } from '../../utils';
4-
import { geometry, MappingOptions, GeometryOptions } from './base';
4+
import { geometry, MappingOptions, GeometryOptions, GeometryAdaptorConfig } from './base';
55

66
export interface PointGeometryOptions extends GeometryOptions {
77
/** x 轴字段 */
@@ -21,8 +21,9 @@ export interface PointGeometryOptions extends GeometryOptions {
2121
/**
2222
* point 辅助点的配置处理
2323
* @param params
24+
* @param config
2425
*/
25-
export function point<O extends PointGeometryOptions>(params: Params<O>): Params<O> {
26+
export function point<O extends PointGeometryOptions>(params: Params<O>, config?: GeometryAdaptorConfig): Params<O> {
2627
const { options } = params;
2728
const { point, xField, yField, seriesField, sizeField, shapeField, tooltip } = options;
2829

src/adaptor/geometries/polygon.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Params } from '../../core/adaptor';
22
import { getTooltipMapping } from '../../utils/tooltip';
33
import { deepAssign } from '../../utils';
4-
import { geometry, MappingOptions, GeometryOptions } from './base';
4+
import { geometry, MappingOptions, GeometryOptions, GeometryAdaptorConfig } from './base';
55

66
export interface PolygonGeometryOptions extends GeometryOptions {
77
/** x 轴字段 */
@@ -17,8 +17,12 @@ export interface PolygonGeometryOptions extends GeometryOptions {
1717
/**
1818
* polygon 的配置处理
1919
* @param params
20+
* @param config
2021
*/
21-
export function polygon<O extends PolygonGeometryOptions>(params: Params<O>): Params<O> {
22+
export function polygon<O extends PolygonGeometryOptions>(
23+
params: Params<O>,
24+
config?: GeometryAdaptorConfig
25+
): Params<O> {
2226
const { options } = params;
2327
const { polygon, xField, yField, seriesField, tooltip } = options;
2428

@@ -36,7 +40,8 @@ export function polygon<O extends PolygonGeometryOptions>(params: Params<O>): Pa
3640
...polygon,
3741
},
3842
},
39-
})
43+
}),
44+
config
4045
)
4146
: params;
4247
}

src/plots/dual-axes/util/geometry.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export function drawSingleGeometry<O extends { xField: string; yField: string; g
3030
style: geometryOption.lineStyle,
3131
},
3232
},
33-
})
33+
}),
34+
{ label: true }
3435
);
3536
// 绘制点
3637
point(
@@ -79,7 +80,8 @@ export function drawSingleGeometry<O extends { xField: string; yField: string; g
7980
style: geometryOption.columnStyle,
8081
},
8182
},
82-
})
83+
}),
84+
{ label: true }
8385
);
8486
}
8587

src/plots/multi-view/adaptor.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ function multiView(params: Params<MultiViewOptions>): Params<MultiViewOptions> {
5252

5353
// 6. geometry
5454
each(geometries, (geometry: IGeometry) => {
55-
const { ext } = geometryAdaptor({
56-
chart: viewOfG2,
57-
options: geometry,
58-
});
55+
const { ext } = geometryAdaptor(
56+
{
57+
chart: viewOfG2,
58+
options: geometry,
59+
},
60+
{ label: true }
61+
);
5962

6063
// adjust
6164
const { adjust } = geometry;

src/plots/sankey/adaptor.ts

+15-12
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,23 @@ function geometry(params: Params<SankeyOptions>): Params<SankeyOptions> {
7171
const nodeView = chart.createView();
7272
nodeView.data(nodesData);
7373

74-
polygon({
75-
chart: nodeView,
76-
options: {
77-
xField: X_FIELD,
78-
yField: Y_FIELD,
79-
seriesField: COLOR_FIELD,
80-
polygon: {
81-
color,
82-
style: nodeStyle,
74+
polygon(
75+
{
76+
chart: nodeView,
77+
options: {
78+
xField: X_FIELD,
79+
yField: Y_FIELD,
80+
seriesField: COLOR_FIELD,
81+
polygon: {
82+
color,
83+
style: nodeStyle,
84+
},
85+
label,
86+
tooltip: false,
8387
},
84-
label,
85-
tooltip: false,
8688
},
87-
});
89+
{ label: true }
90+
);
8891

8992
// edge view
9093
const edgeView = chart.createView();

0 commit comments

Comments
 (0)