Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wordcloud): accept custom canvas #6354

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions __tests__/integration/api-chart-word-cloud-canvas.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { chartWordCloudCanvas as render } from '../plots/api/chart-word-cloud-canvas';
import './utils/useSnapshotMatchers';
import { createNodeGCanvas } from './utils/createNodeGCanvas';
import './utils/useCustomFetch';

describe('word cloud canvas', () => {
const canvas = createNodeGCanvas(640, 480);

it('word cloud should use custom canvas.', async () => {
// This no canvas in JSDOM environment, so it will throw error.
// But it's ok, we just need to test if the ref is called.
let ref;
try {
const rendered = render({
canvas,
container: document.createElement('div'),
});
ref = rendered.ref;
await rendered.finished;
} catch (e) {
expect(ref.called).toBe(true);
}
});

afterAll(() => {
canvas?.destroy();
});
});
30 changes: 30 additions & 0 deletions __tests__/plots/api/chart-word-cloud-canvas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Chart } from '../../../src';

export function chartWordCloudCanvas(context) {
const { container, canvas } = context;

const chart = new Chart({
container,
canvas,
});

const ref = { called: false };

chart.options({
type: 'wordCloud',
data: {
type: 'fetch',
value: 'data/philosophyWord.json',
},
layout: {
canvas: () => {
ref.called = true;
return document.createElement('canvas');
},
},
});

const finished = chart.render();

return { chart, ref, finished };
}
1 change: 1 addition & 0 deletions __tests__/plots/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ export { chartAutoFitHeight } from './chart-auto-fit-height';
export { chartAutoFitWidth } from './chart-auto-fit-width';
export { chartOnLabelClick } from './chart-on-label-click';
export { chartChangeSizeLabelRotate } from './chart-change-size-label-rotate';
export { chartWordCloudCanvas } from './chart-word-cloud-canvas';
15 changes: 8 additions & 7 deletions site/docs/spec/mark/wordcloud.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ chart.render();

## layout

| 属性 | 描述 | 类型 | 默认值 |
| --------- | ------------ | -------------------------------- | ------ |
| padding | 内间距 | `number` | `1` |
| rotate | 文字旋转角度 | `number \| (word => number)` | - |
| random | 随机方式 | `number \| (word => number)` | - |
| spiral | 外观图形 | `'archimedean' \| 'rectangular'` | - |
| imageMask | 图片蒙层 | `'HTMLImageElement \| string` | - |
| 属性 | 描述 | 类型 | 默认值 |
| --------- | ------------- | -------------------------------- | ---------------------------------------- |
| padding | 内间距 | `number` | `1` |
| rotate | 文字旋转角度 | `number \| (word => number)` | - |
| random | 随机方式 | `number \| (word => number)` | - |
| spiral | 外观图形 | `'archimedean' \| 'rectangular'` | - |
| imageMask | 图片蒙层 | `'HTMLImageElement \| string` | - |
| canvas | 自定义 canvas | `() => CanvasElement` | `() => document.createElement('canvas')` |

## style

Expand Down
8 changes: 6 additions & 2 deletions src/data/utils/d3-cloud/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ export function tagCloud() {
event = cloudDispatch,
words = [],
timer = null,
timeInterval = Infinity;
timeInterval = Infinity,
canvas = cloudCanvas;

const fontStyle = cloudFontNormal;
const canvas = cloudCanvas;
const cloud: any = {};

cloud.start = function () {
Expand Down Expand Up @@ -476,6 +476,10 @@ export function tagCloud() {
rotate = functor(_);
};

cloud.canvas = function (_) {
canvas = functor(_);
};

cloud.spiral = function (_) {
spiral = spirals[_] || _;
};
Expand Down
10 changes: 10 additions & 0 deletions src/data/wordCloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,16 @@ export type WordCloudOptions = {
* If not specified, returns the current random number generator, which defaults to Math.random.
*/
random: () => number;

/**
* If specified, sets the spiral used for positioning words.
*/
spiral: any;

/**
* If specified, sets the image mask used for positioning words.
*/
canvas: HTMLCanvasElement;
};

const DEFAULT_OPTIONS = {
Expand Down Expand Up @@ -172,6 +181,7 @@ export const WordCloud: DC<Partial<WordCloudOptions>> = (options) => {
.set('random')
.set('text')
.set('on')
.set('canvas')
.setAsync('imageMask', processImageMask, layout.createMask);

layout.words([...data]);
Expand Down
Loading