Skip to content

Commit ec47b76

Browse files
authored
fix: 修复词云图 legend 配置不起效 修复 词云图 color 回调缺少其他数据 (#2787)
* fix: 修复词云图 legend 配置不起效 修复 词云图 color 回调缺少其他数据 * fix: 修复词云图 legend 配置不起效 修复 词云图 color 回调缺少其他数据 -2 * fix: 修复词云图 legend 配置不起效 修复 词云图 color 回调缺少其他数据 -3 Co-authored-by: ai-qing-hai <[email protected]>
1 parent c64270e commit ec47b76

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

__tests__/unit/plots/word-cloud/color-spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,23 @@ describe('word-cloud color option', () => {
7373

7474
cloud.destroy();
7575
});
76+
77+
it('callback color', () => {
78+
const cloud = new WordCloud(createDiv(), {
79+
width: 400,
80+
height: 300,
81+
data: CountryEconomy,
82+
wordField: 'Country',
83+
weightField: 'GDP',
84+
colorField: 'x',
85+
color: (data) => {
86+
expect(!!data['datum'] || !data['color']).toBe(true);
87+
return 'red';
88+
},
89+
});
90+
91+
cloud.render();
92+
93+
cloud.destroy();
94+
});
7695
});

__tests__/unit/plots/word-cloud/legend-spec.ts

+29
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,33 @@ describe('word-cloud', () => {
5353

5454
cloud.destroy();
5555
});
56+
57+
it('配置 legend', async () => {
58+
const cloud = new WordCloud(createDiv('x*y'), {
59+
width: 400,
60+
height: 300,
61+
data: CountryEconomy,
62+
wordField: 'Country',
63+
weightField: 'GDP',
64+
colorField: 'continent',
65+
legend: {
66+
position: 'left',
67+
marker: { style: { fill: 'red' } },
68+
},
69+
animation: false,
70+
wordStyle: {
71+
// 本地跑 live 也会丢失一个 series,故此加上 font-size
72+
fontSize: [12, 20],
73+
},
74+
});
75+
76+
cloud.render();
77+
78+
expect(cloud.options.legend).toMatchObject({
79+
position: 'left',
80+
marker: { style: { fill: 'red' } },
81+
});
82+
83+
cloud.destroy();
84+
});
5685
});

src/plots/word-cloud/adaptor.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { isFunction, get } from '@antv/util';
12
import { Params } from '../../core/adaptor';
2-
import { tooltip, interaction, animation, theme, scale, state, legend } from '../../adaptor/common';
3+
import { tooltip, interaction, animation, theme, scale, state } from '../../adaptor/common';
34
import { flow, deepAssign } from '../../utils';
45
import { point } from '../../adaptor/geometries';
56
import { WordCloudOptions } from './types';
67
import { transform } from './utils';
8+
import { WORD_CLOUD_COLOR_FIELD } from './constant';
79

810
/**
911
* geometry 配置处理
@@ -20,7 +22,8 @@ function geometry(params: Params<WordCloudOptions>): Params<WordCloudOptions> {
2022
options: {
2123
xField: 'x',
2224
yField: 'y',
23-
seriesField: colorField && 'color',
25+
seriesField: colorField && WORD_CLOUD_COLOR_FIELD,
26+
rawFields: isFunction(color) && [...get(options, 'rawFields', []), 'datum'],
2427
point: {
2528
color,
2629
shape: 'word-cloud',
@@ -50,6 +53,23 @@ function meta(params: Params<WordCloudOptions>): Params<WordCloudOptions> {
5053
)(params);
5154
}
5255

56+
/**
57+
* 词云图 legend 配置
58+
* @param params
59+
*/
60+
export function legend(params: Params<WordCloudOptions>): Params<WordCloudOptions> {
61+
const { chart, options } = params;
62+
const { legend, colorField } = options;
63+
64+
if (legend === false) {
65+
chart.legend(false);
66+
} else if (colorField) {
67+
chart.legend(WORD_CLOUD_COLOR_FIELD, legend);
68+
}
69+
70+
return params;
71+
}
72+
5373
/**
5474
* 词云图适配器
5575
* @param chart

src/plots/word-cloud/constant.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { Plot } from '../../core/plot';
22
import { Datum } from '../../types';
33
import { deepAssign } from '../../utils';
44

5+
/** 词云图 color 通道映射字段 */
6+
export const WORD_CLOUD_COLOR_FIELD = 'color';
7+
58
/**
6-
* 旭日图 默认配置项
9+
* 词云图 默认配置项
710
*/
811
export const DEFAULT_OPTIONS = deepAssign({}, Plot.getDefaultOptions(), {
912
timeInterval: 2000,
@@ -12,7 +15,7 @@ export const DEFAULT_OPTIONS = deepAssign({}, Plot.getDefaultOptions(), {
1215
showTitle: false,
1316
showMarkers: false,
1417
showCrosshairs: false,
15-
fields: ['text', 'value', 'color'],
18+
fields: ['text', 'value', WORD_CLOUD_COLOR_FIELD],
1619
formatter: (datum: Datum) => {
1720
return { name: datum.text, value: datum.value };
1821
},

0 commit comments

Comments
 (0)