Skip to content

Commit f47bf6a

Browse files
committed
feat(scatter): 散点图支持配置 shapeLegend & sizeLegend
1 parent ce0c61b commit f47bf6a

File tree

4 files changed

+78
-18
lines changed

4 files changed

+78
-18
lines changed

__tests__/unit/plots/scatter/color-spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ describe('scatter', () => {
108108
});
109109

110110
it('color mapping with shape mapping, legends', () => {
111+
scatter.update({ shapeLegend: {} });
111112
const legendController = scatter.chart.getController('legend');
112113
expect(legendController.getComponents().length).toBe(2);
113114
expect(legendController.getComponents()[0].component.get('items').length).toBe(3);

__tests__/unit/plots/scatter/legend-spec.ts

+46-6
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ describe('scatter', () => {
2020
const legendController = scatter.chart.getController('legend');
2121
// @ts-ignore
2222
const { option } = legendController;
23-
expect(option).toBe(false);
23+
expect(option).toEqual({ height: false, weight: false });
2424
scatter.update({
2525
shapeField: 'gender',
2626
});
2727
// @ts-ignore
28-
expect(scatter.chart.getController('legend').option).toEqual({
29-
gender: undefined,
30-
height: false,
31-
weight: false,
32-
});
28+
expect(scatter.chart.getController('legend').option).toEqual({ height: false, weight: false });
29+
30+
expect(legendController.getComponents().length).toBe(1);
31+
3332
scatter.update({
3433
shapeField: '',
3534
colorField: 'g',
@@ -197,4 +196,45 @@ describe('scatter', () => {
197196

198197
scatter.destroy();
199198
});
199+
200+
const scatter = new Scatter(createDiv(), {
201+
width: 400,
202+
height: 300,
203+
appendPadding: 10,
204+
data: data.map((d) => ({ ...d, ['gender-1']: d.gender })),
205+
xField: 'weight',
206+
yField: 'height',
207+
shapeField: 'gender',
208+
xAxis: {
209+
nice: true,
210+
},
211+
});
212+
213+
scatter.render();
214+
215+
it('not colorField, but shapeField, legend 会取 shapeField', () => {
216+
const legendController = scatter.chart.getController('legend');
217+
expect(legendController.getComponents().length).toBe(1);
218+
expect(legendController.getComponents()[0].id).toBe('legend-gender');
219+
});
220+
221+
it('shapeLegend toBe false', () => {
222+
scatter.update({ shapeLegend: false });
223+
const legendController = scatter.chart.getController('legend');
224+
expect(legendController.getComponents().length).toBe(0);
225+
});
226+
227+
it('当 colorField & shapeLegend 同字段时,shapeLegend false 会导致 colorLegend 关闭', () => {
228+
scatter.update({ colorField: 'gender', shapeLegend: {} });
229+
let legendController = scatter.chart.getController('legend');
230+
expect(legendController.getComponents().length).toBe(1);
231+
232+
scatter.update({ shapeLegend: false });
233+
legendController = scatter.chart.getController('legend');
234+
expect(legendController.getComponents().length).toBe(0);
235+
});
236+
237+
afterAll(() => {
238+
scatter.destroy();
239+
});
200240
});

src/plots/scatter/adaptor.ts

+27-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isBoolean, isNumber } from '@antv/util';
1+
import { isNumber } from '@antv/util';
22
import { Params } from '../../core/adaptor';
33
import { flow, deepAssign } from '../../utils';
44
import { point } from '../../adaptor/geometries';
@@ -120,17 +120,32 @@ function axis(params: Params<ScatterOptions>): Params<ScatterOptions> {
120120
*/
121121
function legend(params: Params<ScatterOptions>): Params<ScatterOptions> {
122122
const { chart, options } = params;
123-
const { legend, colorField, shapeField, sizeField } = options;
124-
// legend 没有指定时根据 shapeField 和 colorField 来设置默认值
125-
const showLegend = isBoolean(legend) ? legend : legend || !!(shapeField || colorField);
126-
if (showLegend) {
127-
colorField && chart.legend(colorField, legend);
128-
shapeField && chart.legend(shapeField, legend);
129-
// 隐藏连续图例
130-
if (sizeField) {
131-
chart.legend(sizeField, false);
132-
}
133-
} else {
123+
const { legend, colorField, shapeField, sizeField, shapeLegend, sizeLegend } = options;
124+
125+
/** 1. legend 不为 false, 则展示图例, 优先展示 color 分类图例 */
126+
const showLegend = legend !== false;
127+
// 1. shapeLegend 为 false, 强制关闭 shape 映射图例
128+
// 2.1 shapeLegend 不为空时,展示 shape 图例; 2.2 否则colorField 不存在,且 legend 存在时,可展示 shape 图例
129+
const showShapeLegend = shapeField && shapeLegend !== false && ((showLegend && !colorField) || shapeLegend);
130+
/** 默认没有 sizeField,则隐藏连续图例 */
131+
const showSizeLegend = sizeLegend;
132+
133+
if (colorField) {
134+
showLegend ? chart.legend(colorField, legend) : chart.legend(colorField, false);
135+
}
136+
137+
// 优先取 shapeLegend,否则取 legend
138+
if (showShapeLegend) {
139+
chart.legend(shapeField, shapeLegend || legend);
140+
} else if (shapeField && shapeLegend === false) {
141+
chart.legend(shapeField, false);
142+
}
143+
144+
if (sizeField) {
145+
showSizeLegend ? chart.legend(sizeField, sizeLegend) : chart.legend(sizeField, false);
146+
}
147+
148+
if (!showLegend && !showShapeLegend && !showSizeLegend) {
134149
chart.legend(false);
135150
}
136151

src/plots/scatter/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ export interface ScatterOptions extends Options {
4646
readonly type?: 'jitter' | 'stack' | 'symmetric' | 'dodge';
4747
/** 点大小映射对应的数据字段名 */
4848
readonly sizeField?: string;
49+
/** size 对应的图例 */
50+
readonly sizeLegend?: Options['legend'];
4951
/** 散点图大小 */
5052
readonly size?: SizeAttr;
5153
/** 点形状映射对应的数据字段名 */
5254
readonly shapeField?: string;
55+
/** shape 对应的图例 */
56+
readonly shapeLegend?: Options['legend'];
5357
/** 散点图形状 */
5458
readonly shape?: ShapeAttr;
5559
/** 散点图样式 */

0 commit comments

Comments
 (0)