Skip to content

Commit 24bd065

Browse files
authored
fix(word-cloud): 修复错误的边界值导致的重叠及间隙过大问题 (#1976)
* fix(word-cloud): 修复错误的边界值导致的重叠及间隙过大问题 * chore(word-cloud): 更改最小字体默认值
1 parent ad96609 commit 24bd065

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

__tests__/bugs/issue-1970-spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { WordCloud } from '../../src';
2+
import { CountryEconomy } from '../data/country-economy';
3+
import { createDiv } from '../utils/dom';
4+
5+
describe('issue 1970', () => {
6+
it('bounds', () => {
7+
const cloud = new WordCloud(createDiv(), {
8+
width: 400,
9+
height: 300,
10+
data: CountryEconomy,
11+
wordField: 'Country',
12+
weightField: 'GDP',
13+
});
14+
15+
cloud.render();
16+
17+
const width = cloud.chart.ele.clientWidth;
18+
const height = cloud.chart.ele.clientHeight;
19+
20+
// 最终的数据之中必须有这两个点表示画布的边界,才能正常渲染词云图
21+
// @ts-ignore
22+
expect(cloud.chart.filteredData.some((v) => v.x === 0 && v.y === 0)).toBe(true);
23+
// @ts-ignore
24+
expect(cloud.chart.filteredData.some((v) => v.x === width && v.y === height)).toBe(true);
25+
26+
cloud.destroy();
27+
});
28+
});

src/plots/word-cloud/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class WordCloud extends Plot<WordCloudOptions> {
3333
fontFamily: 'Verdana',
3434
fontWeight: 'normal',
3535
padding: 1,
36-
fontSize: [20, 60],
36+
fontSize: [12, 60],
3737
rotation: [0, 90],
3838
rotationSteps: 2,
3939
rotateRatio: 0.5,

src/utils/transform/word-cloud.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,28 @@ export function transform(words: Word[], options: Options) {
6363
const result = layout.start();
6464
const tags: any[] = result._tags;
6565

66-
const bounds = result._bounds || [
67-
{ x: 0, y: 0 },
68-
{ x: options.size[0], y: options.size[1] },
69-
];
70-
7166
tags.forEach((tag) => {
7267
tag.x += options.size[0] / 2;
7368
tag.y += options.size[1] / 2;
7469
});
7570

7671
const [w, h] = options.size;
77-
const hasImage = result.hasImage;
72+
// 添加两个参照数据,分别表示左上角和右下角。
73+
// 不添加的话不会按照真实的坐标渲染,而是以
74+
// 数据中的边界坐标为边界进行拉伸,以铺满画布。
75+
// 这样的后果会导致词语之间的重叠。
7876
tags.push({
7977
text: '',
8078
value: 0,
81-
x: hasImage ? 0 : bounds[0].x,
82-
y: hasImage ? 0 : bounds[0].y,
79+
x: 0,
80+
y: 0,
8381
opacity: 0,
8482
});
8583
tags.push({
8684
text: '',
8785
value: 0,
88-
x: hasImage ? w : bounds[1].x,
89-
y: hasImage ? h : bounds[1].y,
86+
x: w,
87+
y: h,
9088
opacity: 0,
9189
});
9290

0 commit comments

Comments
 (0)