Skip to content

Commit

Permalink
fix: 修复词云图 legend 配置不起效 修复 词云图 color 回调缺少其他数据 (#2787)
Browse files Browse the repository at this point in the history
* fix: 修复词云图 legend 配置不起效 修复 词云图 color 回调缺少其他数据

* fix: 修复词云图 legend 配置不起效 修复 词云图 color 回调缺少其他数据 -2

* fix: 修复词云图 legend 配置不起效 修复 词云图 color 回调缺少其他数据 -3

Co-authored-by: ai-qing-hai <wb-xcf804241@antgroup.com>
  • Loading branch information
ai-qing-hai and ai-qing-hai authored Aug 13, 2021
1 parent c64270e commit ec47b76
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
19 changes: 19 additions & 0 deletions __tests__/unit/plots/word-cloud/color-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,23 @@ describe('word-cloud color option', () => {

cloud.destroy();
});

it('callback color', () => {
const cloud = new WordCloud(createDiv(), {
width: 400,
height: 300,
data: CountryEconomy,
wordField: 'Country',
weightField: 'GDP',
colorField: 'x',
color: (data) => {
expect(!!data['datum'] || !data['color']).toBe(true);
return 'red';
},
});

cloud.render();

cloud.destroy();
});
});
29 changes: 29 additions & 0 deletions __tests__/unit/plots/word-cloud/legend-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,33 @@ describe('word-cloud', () => {

cloud.destroy();
});

it('配置 legend', async () => {
const cloud = new WordCloud(createDiv('x*y'), {
width: 400,
height: 300,
data: CountryEconomy,
wordField: 'Country',
weightField: 'GDP',
colorField: 'continent',
legend: {
position: 'left',
marker: { style: { fill: 'red' } },
},
animation: false,
wordStyle: {
// 本地跑 live 也会丢失一个 series,故此加上 font-size
fontSize: [12, 20],
},
});

cloud.render();

expect(cloud.options.legend).toMatchObject({
position: 'left',
marker: { style: { fill: 'red' } },
});

cloud.destroy();
});
});
24 changes: 22 additions & 2 deletions src/plots/word-cloud/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { isFunction, get } from '@antv/util';
import { Params } from '../../core/adaptor';
import { tooltip, interaction, animation, theme, scale, state, legend } from '../../adaptor/common';
import { tooltip, interaction, animation, theme, scale, state } from '../../adaptor/common';
import { flow, deepAssign } from '../../utils';
import { point } from '../../adaptor/geometries';
import { WordCloudOptions } from './types';
import { transform } from './utils';
import { WORD_CLOUD_COLOR_FIELD } from './constant';

/**
* geometry 配置处理
Expand All @@ -20,7 +22,8 @@ function geometry(params: Params<WordCloudOptions>): Params<WordCloudOptions> {
options: {
xField: 'x',
yField: 'y',
seriesField: colorField && 'color',
seriesField: colorField && WORD_CLOUD_COLOR_FIELD,
rawFields: isFunction(color) && [...get(options, 'rawFields', []), 'datum'],
point: {
color,
shape: 'word-cloud',
Expand Down Expand Up @@ -50,6 +53,23 @@ function meta(params: Params<WordCloudOptions>): Params<WordCloudOptions> {
)(params);
}

/**
* 词云图 legend 配置
* @param params
*/
export function legend(params: Params<WordCloudOptions>): Params<WordCloudOptions> {
const { chart, options } = params;
const { legend, colorField } = options;

if (legend === false) {
chart.legend(false);
} else if (colorField) {
chart.legend(WORD_CLOUD_COLOR_FIELD, legend);
}

return params;
}

/**
* 词云图适配器
* @param chart
Expand Down
7 changes: 5 additions & 2 deletions src/plots/word-cloud/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { Plot } from '../../core/plot';
import { Datum } from '../../types';
import { deepAssign } from '../../utils';

/** 词云图 color 通道映射字段 */
export const WORD_CLOUD_COLOR_FIELD = 'color';

/**
* 旭日图 默认配置项
* 词云图 默认配置项
*/
export const DEFAULT_OPTIONS = deepAssign({}, Plot.getDefaultOptions(), {
timeInterval: 2000,
Expand All @@ -12,7 +15,7 @@ export const DEFAULT_OPTIONS = deepAssign({}, Plot.getDefaultOptions(), {
showTitle: false,
showMarkers: false,
showCrosshairs: false,
fields: ['text', 'value', 'color'],
fields: ['text', 'value', WORD_CLOUD_COLOR_FIELD],
formatter: (datum: Datum) => {
return { name: datum.text, value: datum.value };
},
Expand Down

0 comments on commit ec47b76

Please sign in to comment.