Skip to content

Commit 6fbc8b4

Browse files
sersishen荊芥
andauthored
fix: 修复分面图下多图例显示配置 (#3865)
* fix: 修复分面图下多图例显示配置 --------- Co-authored-by: 荊芥 <[email protected]>
1 parent 7426dd3 commit 6fbc8b4

File tree

4 files changed

+216
-2
lines changed

4 files changed

+216
-2
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import { groupBy, size } from '@antv/util';
2+
import { Facet } from '../../../../src';
3+
import { createDiv } from '../../../utils/dom';
4+
5+
describe('facet - legend', () => {
6+
const data = [
7+
{
8+
name: 14513,
9+
carat: 1.35,
10+
cut: 'Ideal',
11+
color: 'J',
12+
clarity: 'VS2',
13+
depth: 61.4,
14+
table: 57,
15+
price: 5862,
16+
x: 7.1,
17+
y: 7.13,
18+
z: 4.37,
19+
},
20+
{
21+
name: 28685,
22+
carat: 0.3,
23+
cut: 'Good',
24+
color: 'G',
25+
clarity: 'VVS1',
26+
depth: 64,
27+
table: 57,
28+
price: 678,
29+
x: 4.23,
30+
y: 4.27,
31+
z: 2.72,
32+
},
33+
{
34+
name: 50368,
35+
carat: 0.75,
36+
cut: 'Ideal',
37+
color: 'F',
38+
clarity: 'SI2',
39+
depth: 59.2,
40+
table: 60,
41+
price: 2248,
42+
x: 5.87,
43+
y: 5.92,
44+
z: 3.49,
45+
},
46+
{
47+
name: 7721,
48+
carat: 0.26,
49+
cut: 'Ideal',
50+
color: 'F',
51+
clarity: 'VS1',
52+
depth: 60.9,
53+
table: 57,
54+
price: 580,
55+
x: 4.13,
56+
y: 4.11,
57+
z: 2.51,
58+
},
59+
{
60+
name: 31082,
61+
carat: 0.33,
62+
cut: 'Premium',
63+
color: 'H',
64+
clarity: 'VVS1',
65+
depth: 61.4,
66+
table: 59,
67+
price: 752,
68+
x: 4.42,
69+
y: 4.44,
70+
z: 2.72,
71+
},
72+
{
73+
name: 26429,
74+
carat: 1.52,
75+
cut: 'Ideal',
76+
color: 'G',
77+
clarity: 'VVS1',
78+
depth: 62.4,
79+
table: 55,
80+
price: 15959,
81+
x: 7.3,
82+
y: 7.39,
83+
z: 4.58,
84+
},
85+
];
86+
87+
const plot = new Facet(createDiv(), {
88+
data,
89+
type: 'rect',
90+
fields: ['cut', 'clarity'],
91+
cols: 3, // 超过3个换行
92+
padding: [0, 10, 10],
93+
appendPadding: 30,
94+
axes: {},
95+
meta: {
96+
carat: {
97+
sync: true,
98+
},
99+
price: {
100+
sync: true,
101+
},
102+
cut: {
103+
// 设置 sync 同步之后,可以按照 'cut' 进行颜色映射分类
104+
sync: true,
105+
},
106+
},
107+
colorField: 'color',
108+
shapeField: 'cut',
109+
sizeField: 'price',
110+
// shapeLegend: false,
111+
// legend: false,
112+
// sizeLegend: { position: 'left' },
113+
eachView: (view, f) => {
114+
return {
115+
type: 'scatter',
116+
options: {
117+
data: f?.data,
118+
xField: 'carat',
119+
yField: 'price',
120+
colorField: 'color',
121+
shapeField: 'cut',
122+
sizeField: 'price',
123+
pointStyle: { fillOpacity: 0.3, stroke: null },
124+
},
125+
};
126+
},
127+
});
128+
plot.render();
129+
130+
it('bubble - default', () => {
131+
const legendLen = plot.chart.getController('legend').getComponents()?.length;
132+
// 气泡图默认只画 color legend 和 shape legend 且分开
133+
expect(legendLen).toBe(2);
134+
});
135+
136+
it('bubble - sizeLegend开启', () => {
137+
plot.update({ sizeLegend: {} });
138+
const legendLen = plot.chart.getController('legend').getComponents()?.length;
139+
expect(legendLen).toBe(3);
140+
});
141+
142+
it('bubble - legend关闭', () => {
143+
plot.update({ legend: false });
144+
expect(plot.chart.getController('legend').getComponents().length).toBe(2);
145+
});
146+
147+
afterAll(() => {
148+
plot.destroy();
149+
});
150+
});

jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ module.exports = {
2222
'lodash-es': 'lodash',
2323
'd3-color': 'd3-color/dist/d3-color.min.js',
2424
'd3-interpolate': 'd3-interpolate/dist/d3-interpolate.min.js',
25+
'd3-geo': 'd3-geo/build/d3-geo.min.js',
26+
'd3-path': 'd3-path/dist/d3-path.min.js'
2527
},
2628
};

src/plots/facet/adaptor.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ function facetAdaptor(params: Params<FacetOptions>): Params<FacetOptions> {
4141
} else {
4242
const plot = viewOptions as IPlot;
4343
const plotOptions = plot.options;
44+
45+
// 如果 view 内部没有指定映射字段,则使用 options 内指定的映射字段
46+
['seriesField', 'colorField', 'shapeField', 'sizeField', 'groupField'].forEach((key) => {
47+
if (options[key] && typeof plotOptions[key] === 'undefined') {
48+
plotOptions[key] = options[key];
49+
}
50+
});
51+
4452
// @ts-ignore 仪表盘没 tooltip
4553
if (plotOptions.tooltip) {
4654
// 配置 tooltip 交互
@@ -56,7 +64,20 @@ function facetAdaptor(params: Params<FacetOptions>): Params<FacetOptions> {
5664

5765
function component(params: Params<FacetOptions>): Params<FacetOptions> {
5866
const { chart, options } = params;
59-
const { axes, meta, tooltip, coordinate, theme, legend, interactions, annotations } = options;
67+
const {
68+
axes,
69+
meta,
70+
tooltip,
71+
coordinate,
72+
theme,
73+
legend,
74+
interactions,
75+
annotations,
76+
shapeField,
77+
sizeField,
78+
shapeLegend,
79+
sizeLegend,
80+
} = options;
6081

6182
// 3. meta 配置
6283
let scales: Record<string, any> = {};
@@ -91,6 +112,22 @@ function component(params: Params<FacetOptions>): Params<FacetOptions> {
91112

92113
// 7. legend 配置(默认展示)
93114
chart.legend(legend);
115+
const showLegend = legend !== false;
116+
if (shapeField) {
117+
if (shapeLegend) {
118+
chart.legend(shapeField, shapeLegend);
119+
} else {
120+
chart.legend(shapeField, shapeLegend === false ? false : legend);
121+
}
122+
}
123+
124+
if (sizeField) {
125+
chart.legend(sizeField, sizeLegend ? sizeLegend : false);
126+
}
127+
// /** 默认不展示 shape 图例,当 shapeLegend 为 undefined 也不展示图例 */
128+
if (!showLegend && !shapeLegend && !sizeLegend) {
129+
chart.legend(false);
130+
}
94131

95132
// theme 配置
96133
if (theme) {

src/plots/facet/types.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,32 @@ export interface FacetOptions<T extends keyof Types.FacetCfgMap = keyof Types.Fa
146146
*/
147147
readonly legend?: Legend;
148148
/**
149-
* @title 图例配置
149+
* @title 交互配置
150150
*/
151151
readonly interactions?: Interaction[];
152+
/**
153+
* @title 形状字段
154+
* @description 形状映射字段
155+
*/
156+
readonly shapeField?: string;
157+
/**
158+
* @title 颜色字段
159+
* @description 颜色映射字段
160+
*/
161+
readonly colorField?: string;
162+
/**
163+
* @title 大小字段
164+
* @description 大小映射字段
165+
*/
166+
readonly sizeField?: string;
167+
/**
168+
* @title 形状图例配置
169+
* @description 形状图例的配置项
170+
*/
171+
readonly shapeLegend?: Legend | false;
172+
/**
173+
* @title 大小图例配置
174+
* @description 大小图例的配置项
175+
*/
176+
readonly sizeLegend?: Legend | false;
152177
}

0 commit comments

Comments
 (0)