Skip to content

Commit

Permalink
chore: 处理之后的数据不与用户的数据合并
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhonghe committed Oct 21, 2020
1 parent 99580b0 commit e5f5e8a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 45 deletions.
4 changes: 2 additions & 2 deletions __tests__/unit/plots/word-cloud/index-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ describe('word-cloud', () => {
// x & y
expect(positionFields).toHaveLength(2);
// 数据经过 DataSet 处理过,这里是处理之后的数据中的 x 和 y 字段
expect(positionFields[0]).toBe('_x');
expect(positionFields[1]).toBe('_y');
expect(positionFields[0]).toBe('x');
expect(positionFields[1]).toBe('y');
});

it('imageMask', () => {
Expand Down
7 changes: 2 additions & 5 deletions __tests__/unit/utils/transform/word-cloud-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const data = ['Hello', 'world', 'normally', 'you', 'want', 'more', 'words', 'tha
return {
text: d,
value: 5 + Math.random() * 10,
test: 'haha',
};
});

Expand All @@ -38,8 +37,8 @@ function basicCommon(v: DataItem) {
expect(typeof v.padding).toBe('number');
expect(typeof v.width).toBe('number');
expect(typeof v.height).toBe('number');
expect(typeof v._x).toBe('number');
expect(typeof v._y).toBe('number');
expect(typeof v.x).toBe('number');
expect(typeof v.y).toBe('number');
}

describe('word-cloud', () => {
Expand All @@ -51,8 +50,6 @@ describe('word-cloud', () => {
function removeXY(v) {
delete v.x;
delete v.y;
delete v._x;
delete v._y;
}
const result1 = wordCloud(data, options as any);
const result2 = dv.rows;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"lint-staged": "lint-staged",
"test": "jest",
"test-live": "cross-env DEBUG_MODE=1 jest --watch ./__tests__",
"coverage": "jest --coverage",
"coverage": "jest --coverage ./__tests__/unit/utils/transform",
"ci": "run-s lint coverage build",
"changelog": "generate-changelog",
"prepublishOnly": "run-s lint test build",
Expand Down
6 changes: 3 additions & 3 deletions src/plots/word-cloud/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function geometry(params: Params<WordCloudOptions>): Params<WordCloudOptions> {
const data = transform(params);

chart.data(data);
chart.point().position('_x*_y').shape('word-cloud');
chart.point().position('x*y').shape('word-cloud');

return params;
}
Expand All @@ -24,10 +24,10 @@ function geometry(params: Params<WordCloudOptions>): Params<WordCloudOptions> {
*/
function color(params: Params<WordCloudOptions>): Params<WordCloudOptions> {
const { chart, options } = params;
const { wordField, color } = options;
const { color } = options;
const geometry = findGeometry(chart, 'point');

geometry.color(wordField, color);
geometry.color('text', color);

return params;
}
Expand Down
4 changes: 2 additions & 2 deletions src/plots/word-cloud/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export type DataItem = Row & {
/** 单词所占盒子的高度 */
height?: number;
/** x 轴坐标 */
_x?: number;
x?: number;
/** y 轴坐标 */
_y?: number;
y?: number;
};

/** 词云字体样式 */
Expand Down
62 changes: 30 additions & 32 deletions src/utils/transform/word-cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ export function transform(data: Data, options: Options) {
if (!isString(text) || !isString(value)) {
throw new TypeError('Invalid fields: must be an array with 2 strings (e.g. [ "text", "value" ])!');
}
const words = data.map((row) => {
row.text = row[text];
row.value = row[value];
return row;
});
const words = data.map((row) => ({
text: row[text],
value: row[value],
}));
layout.words(words);
if (options.imageMask) {
layout.createMask(options.imageMask);
Expand All @@ -76,23 +75,23 @@ export function transform(data: Data, options: Options) {
{ x: options.size[0], y: options.size[1] },
];
tags.forEach((tag) => {
tag._x += options.size[0] / 2;
tag._y += options.size[1] / 2;
tag.x += options.size[0] / 2;
tag.y += options.size[1] / 2;
});
const [w, h] = options.size;
const hasImage = result.hasImage;
tags.push({
text: '',
value: 0,
_x: hasImage ? 0 : bounds[0].x,
_y: hasImage ? 0 : bounds[0].y,
x: hasImage ? 0 : bounds[0].x,
y: hasImage ? 0 : bounds[0].y,
opacity: 0,
});
tags.push({
text: '',
value: 0,
_x: hasImage ? w : bounds[1].x,
_y: hasImage ? h : bounds[1].y,
x: hasImage ? w : bounds[1].x,
y: hasImage ? h : bounds[1].y,
opacity: 0,
});

Expand Down Expand Up @@ -237,11 +236,11 @@ function cloudCollide(tag, board, sw) {
sw >>= 5;
const sprite = tag.sprite,
w = tag.width >> 5,
lx = tag._x - (w << 4),
lx = tag.x - (w << 4),
sx = lx & 0x7f,
msx = 32 - sx,
h = tag.y1 - tag.y0;
let x = (tag._y + tag.y0) * sw + (lx >> 5),
let x = (tag.y + tag.y0) * sw + (lx >> 5),
last;
for (let j = 0; j < h; j++) {
last = 0;
Expand All @@ -256,14 +255,14 @@ function cloudCollide(tag, board, sw) {
function cloudBounds(bounds, d) {
const b0 = bounds[0],
b1 = bounds[1];
if (d._x + d.x0 < b0.x) b0.x = d._x + d.x0;
if (d._y + d.y0 < b0.y) b0.y = d._y + d.y0;
if (d._x + d.x1 > b1.x) b1.x = d._x + d.x1;
if (d._y + d.y1 > b1.y) b1.y = d._y + d.y1;
if (d.x + d.x0 < b0.x) b0.x = d.x + d.x0;
if (d.y + d.y0 < b0.y) b0.y = d.y + d.y0;
if (d.x + d.x1 > b1.x) b1.x = d.x + d.x1;
if (d.y + d.y1 > b1.y) b1.y = d.y + d.y1;
}

function collideRects(a, b) {
return a._x + a.x1 > b[0].x && a._x + a.x0 < b[1].x && a._y + a.y1 > b[0].y && a._y + a.y0 < b[1].y;
return a.x + a.x1 > b[0].x && a.x + a.x0 < b[1].x && a.y + a.y1 > b[0].y && a.y + a.y0 < b[1].y;
}

function archimedeanSpiral(size) {
Expand Down Expand Up @@ -380,8 +379,8 @@ function tagCloud() {
const start = Date.now();
while (Date.now() - start < timeInterval && ++i < n) {
const d = data[i];
d._x = (width * (random() + 0.5)) >> 1;
d._y = (height * (random() + 0.5)) >> 1;
d.x = (width * (random() + 0.5)) >> 1;
d.y = (height * (random() + 0.5)) >> 1;
cloudSprite(contextAndRatio, d, data, i);
if (d.hasText && place(board, d, bounds)) {
tags.push(d);
Expand All @@ -392,13 +391,13 @@ function tagCloud() {
}
} else {
bounds = [
{ x: d._x + d.x0, y: d._y + d.y0 },
{ x: d._x + d.x1, y: d._y + d.y1 },
{ x: d.x + d.x0, y: d.y + d.y0 },
{ x: d.x + d.x1, y: d.y + d.y1 },
];
}
// Temporary hack
d._x -= size[0] >> 1;
d._y -= size[1] >> 1;
d.x -= size[0] >> 1;
d.y -= size[1] >> 1;
}
}
cloud._tags = tags;
Expand All @@ -422,8 +421,8 @@ function tagCloud() {

function place(board, tag, bounds) {
// const perimeter = [{ x: 0, y: 0 }, { x: size[0], y: size[1] }],
const startX = tag._x,
startY = tag._y,
const startX = tag.x,
startY = tag.y,
maxDelta = Math.sqrt(size[0] * size[0] + size[1] * size[1]),
s = spiral(size),
dt = random() < 0.5 ? 1 : -1;
Expand All @@ -438,23 +437,22 @@ function tagCloud() {

if (Math.min(Math.abs(dx), Math.abs(dy)) >= maxDelta) break;

tag._x = startX + dx;
tag._y = startY + dy;
tag.x = startX + dx;
tag.y = startY + dy;

if (tag._x + tag.x0 < 0 || tag._y + tag.y0 < 0 || tag._x + tag.x1 > size[0] || tag._y + tag.y1 > size[1])
continue;
if (tag.x + tag.x0 < 0 || tag.y + tag.y0 < 0 || tag.x + tag.x1 > size[0] || tag.y + tag.y1 > size[1]) continue;
// TODO only check for collisions within current bounds.
if (!bounds || !cloudCollide(tag, board, size[0])) {
if (!bounds || collideRects(tag, bounds)) {
const sprite = tag.sprite,
w = tag.width >> 5,
sw = size[0] >> 5,
lx = tag._x - (w << 4),
lx = tag.x - (w << 4),
sx = lx & 0x7f,
msx = 32 - sx,
h = tag.y1 - tag.y0;
let last,
x = (tag._y + tag.y0) * sw + (lx >> 5);
x = (tag.y + tag.y0) * sw + (lx >> 5);
for (let j = 0; j < h; j++) {
last = 0;
for (let i = 0; i <= w; i++) {
Expand Down

0 comments on commit e5f5e8a

Please sign in to comment.