Skip to content

Commit 02b7604

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

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ describe('scatter', () => {
2020
const legendController = scatter.chart.getController('legend');
2121
// @ts-ignore
2222
const { option } = legendController;
23-
expect(option).toBe(false);
23+
expect(option).toBe(undefined);
2424
scatter.update({
2525
shapeField: 'gender',
2626
});
2727
// @ts-ignore
2828
expect(scatter.chart.getController('legend').option).toEqual({
2929
gender: undefined,
30-
height: false,
31-
weight: false,
3230
});
31+
expect(legendController.getComponents().length).toBe(1);
32+
3333
scatter.update({
3434
shapeField: '',
3535
colorField: 'g',

src/plots/scatter/adaptor.ts

+23-11
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,18 +120,30 @@ 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);
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 = sizeField && sizeLegend;
132+
126133
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 {
134+
chart.legend(colorField, legend);
135+
}
136+
if (showShapeLegend) {
137+
// 优先取 shapeLegend,否则取 legend
138+
chart.legend(shapeField, shapeLegend || legend);
139+
}
140+
if (showSizeLegend) {
141+
chart.legend(sizeField, sizeLegend);
142+
}
143+
if (!showLegend && !showShapeLegend && !showSizeLegend) {
134144
chart.legend(false);
145+
} else if (!showSizeLegend && sizeField) {
146+
chart.legend(sizeField, false);
135147
}
136148

137149
return params;

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)