Skip to content

Commit

Permalink
feat(wordcloud): accept custom canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed Jul 11, 2024
1 parent b7bb946 commit 9d0a4a4
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
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();
});
});
31 changes: 31 additions & 0 deletions __tests__/plots/api/chart-word-cloud-canvas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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: {
// random,
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';
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

0 comments on commit 9d0a4a4

Please sign in to comment.