Skip to content

Commit f3d6d7e

Browse files
yp0413150120沈建斌
and
沈建斌
authored
feat: 词云图重写changeData方法 && 添加单测 (#2214)
Co-authored-by: 沈建斌 <[email protected]>
1 parent 8473559 commit f3d6d7e

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { WordCloud } from '../../../../src';
2+
import { CountryEconomy } from '../../../data/country-economy';
3+
import { createDiv } from '../../../utils/dom';
4+
5+
describe('word-cloud changeData', () => {
6+
it('changeData: normal', () => {
7+
const cloud = new WordCloud(createDiv(), {
8+
width: 1024,
9+
height: 1024,
10+
data: CountryEconomy,
11+
wordField: 'Country',
12+
weightField: 'GDP',
13+
});
14+
15+
cloud.render();
16+
17+
expect(cloud.chart.geometries[0].elements.length).toEqual(CountryEconomy.length + 2);
18+
19+
const newData = CountryEconomy.slice(0, 20);
20+
cloud.changeData(newData);
21+
expect(cloud.chart.geometries[0].elements.length).toEqual(newData.length + 2);
22+
expect(cloud.options.data).toEqual(newData);
23+
24+
cloud.destroy();
25+
});
26+
27+
it('changeData: from empty to have data', () => {
28+
const cloud = new WordCloud(createDiv(), {
29+
width: 1024,
30+
height: 1024,
31+
data: [],
32+
wordField: 'Country',
33+
weightField: 'GDP',
34+
});
35+
36+
cloud.render();
37+
38+
cloud.changeData(CountryEconomy);
39+
expect(cloud.chart.geometries[0].elements.length).toEqual(CountryEconomy.length + 2);
40+
expect(cloud.options.data).toEqual(CountryEconomy);
41+
42+
cloud.destroy();
43+
});
44+
});

src/plots/word-cloud/index.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Datum } from '../../types';
44
import { deepAssign } from '../../utils';
55
import { WordCloudOptions } from './types';
66
import { adaptor } from './adaptor';
7-
import { processImageMask } from './utils';
7+
import { processImageMask, transform } from './utils';
88
// 注册的shape
99
import './shapes/word-cloud';
1010

@@ -14,6 +14,15 @@ export class WordCloud extends Plot<WordCloudOptions> {
1414
/** 词云图 */
1515
public type: string = 'word-cloud';
1616

17+
/**
18+
* @override
19+
* @param data
20+
*/
21+
public changeData(data) {
22+
this.updateOption({ data });
23+
this.chart.changeData(transform({ chart: this.chart, options: this.options }));
24+
}
25+
1726
/**
1827
* 获取默认的 options 配置项
1928
*/

0 commit comments

Comments
 (0)