diff --git a/examples/word-cloud/basic/demo/custom-placement.ts b/examples/word-cloud/basic/demo/custom-placement.ts new file mode 100644 index 0000000000..7c8b609d29 --- /dev/null +++ b/examples/word-cloud/basic/demo/custom-placement.ts @@ -0,0 +1,29 @@ +import { WordCloud } from '@antv/g2plot'; + +const data = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '.split('').map((item) => ({ name: item, value: 1 })); + +const wordCloud = new WordCloud('container', { + data, + width: 600, + height: 400, + wordField: 'name', + weightField: 'value', + colorField: 'name', + wordStyle: { + fontFamily: 'Verdana', + fontSize: 16, + rotation: 0, + }, + customPlacement(word, index, words) { + const width = this.ele.clientWidth; + const height = this.ele.clientHeight; + const length = words.length; + + return { + x: (width / length) * index, + y: (height / length) * index, + }; + }, +}); + +wordCloud.render(); diff --git a/examples/word-cloud/basic/demo/custom-tooltip.ts b/examples/word-cloud/basic/demo/custom-tooltip.ts index 54c28acdcc..8be0dc6b45 100644 --- a/examples/word-cloud/basic/demo/custom-tooltip.ts +++ b/examples/word-cloud/basic/demo/custom-tooltip.ts @@ -9,6 +9,7 @@ fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/antv-keywords.json') height: 400, wordField: 'name', weightField: 'value', + colorField: 'name', imageMask: 'https://gw.alipayobjects.com/mdn/rms_2274c3/afts/img/A*07tdTIOmvlYAAAAAAAAAAABkARQnAQ', wordStyle: { fontFamily: 'Verdana', diff --git a/examples/word-cloud/basic/demo/image-mask-base64.ts b/examples/word-cloud/basic/demo/image-mask-base64.ts index 2e96f2657e..04b940c3b9 100644 --- a/examples/word-cloud/basic/demo/image-mask-base64.ts +++ b/examples/word-cloud/basic/demo/image-mask-base64.ts @@ -9,6 +9,7 @@ fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/antv-keywords.json') height: 400, wordField: 'name', weightField: 'value', + colorField: 'name', imageMask: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcoAAADHCAIAAAAWF4ThAAAAAXNSR0IArs4c6QAAAAR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u076crRVyCvx8hrm2nuSzo/FNZKtodOn9fNIo3dHnVQDrkaeT1GXtsNT2xQWm9Eg32PvN7kBTNpZBvvWO+R12Pktc+o2xLDduinKOw75fVmqjvZ20fqypH1Ia/HyKtB7IYZUtZv7l92fr+8FvlD0MMym9aQ2yIir8fIq5l8u2xfGZtgmr48x/m2ef1n62xAaNPysd+qPkFej5FXL7e7Es/dcnsoSvnrZ+HydUTlwJbJ653ytJt0Zs3nNv8Vn6mn1Q+LBZaSQ7vtlbLiW2y3Gop7DWfbEBtHmDe+fmgr5vVRSmNO7k1aTE+kgBbbbyaleEPb0r9tg4dtku8xsqV3x/fw8iXll+/9Hzu6c+QVAKZDXgFAgrwCgAR5BQAJ8goAEuQVACTIKwBIkFcAkCCvACBBXgFAgrwCgAR5BQAJ8goAEuQVACTIKwBIkFcAkCCvACBBXgFAgrwCgAR5BQAJ8goAEuQVACTIKwBIkFcAkCCvACBBXgFAgrwCgAR5BQAJ8goAEuQVACTIKwBIkFcAkCCvACBBXgFAgrwCgAR5BQAJ8goAEuQVACTIKwBIkFcAkCCvACBBXgFAgrwCgAR5BQAJ8goAEuQVAAT+++//CT3TAOpn61kAAAAASUVORK5CYII=', wordStyle: { diff --git a/examples/word-cloud/basic/demo/image-mask.ts b/examples/word-cloud/basic/demo/image-mask.ts index e088f3863e..2ab8294567 100644 --- a/examples/word-cloud/basic/demo/image-mask.ts +++ b/examples/word-cloud/basic/demo/image-mask.ts @@ -9,6 +9,7 @@ fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/antv-keywords.json') height: 400, wordField: 'name', weightField: 'value', + colorField: 'name', imageMask: 'https://gw.alipayobjects.com/mdn/rms_2274c3/afts/img/A*07tdTIOmvlYAAAAAAAAAAABkARQnAQ', wordStyle: { fontFamily: 'Verdana', diff --git a/examples/word-cloud/basic/demo/meta.json b/examples/word-cloud/basic/demo/meta.json index bbd91014b6..54803cf617 100644 --- a/examples/word-cloud/basic/demo/meta.json +++ b/examples/word-cloud/basic/demo/meta.json @@ -51,6 +51,22 @@ "en": "Word Cloud chart - colorField" }, "screenshot": "https://gw.alipayobjects.com/mdn/rms_f5c722/afts/img/A*id4CSZIMCtsAAAAAAAAAAABkARQnAQ" + }, + { + "filename": "same-place.ts", + "title": { + "zh": "词云图-每次渲染保持位置不变", + "en": "Word Cloud chart - keep the position unchanged" + }, + "screenshot": "https://gw.alipayobjects.com/mdn/rms_f5c722/afts/img/A*id4CSZIMCtsAAAAAAAAAAABkARQnAQ" + }, + { + "filename": "custom-placement.ts", + "title": { + "zh": "词云图-自定义每个词语位置", + "en": "Word Cloud chart - custom placement" + }, + "screenshot": "https://gw.alipayobjects.com/mdn/rms_f5c722/afts/img/A*id4CSZIMCtsAAAAAAAAAAABkARQnAQ" } ] } diff --git a/examples/word-cloud/basic/demo/same-place.ts b/examples/word-cloud/basic/demo/same-place.ts new file mode 100644 index 0000000000..77b1e94f02 --- /dev/null +++ b/examples/word-cloud/basic/demo/same-place.ts @@ -0,0 +1,24 @@ +import { WordCloud } from '@antv/g2plot'; + +fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/antv-keywords.json') + .then((res) => res.json()) + .then((data) => { + const wordCloud = new WordCloud('container', { + data, + width: 600, + height: 400, + wordField: 'name', + weightField: 'value', + colorField: 'name', + wordStyle: { + fontFamily: 'Verdana', + fontSize: [8, 32], + rotation: 0, + }, + // 返回值设置成一个 [0, 1) 区间内的值, + // 可以让每次渲染的位置相同(前提是每次的宽高一致)。 + random: () => 0.5, + }); + + wordCloud.render(); + }); diff --git a/src/plots/word-cloud/utils.ts b/src/plots/word-cloud/utils.ts index bc13c9113d..e855c82291 100644 --- a/src/plots/word-cloud/utils.ts +++ b/src/plots/word-cloud/utils.ts @@ -11,7 +11,7 @@ import { Tag, Word, WordCloudOptions } from './types'; * @param params */ export function transform(params: Params): Tag[] { - const { options: rawOptions } = params; + const { options: rawOptions, chart } = params; const { data, imageMask, @@ -58,14 +58,14 @@ export function transform(params: Params): Tag[] { // 自定义布局函数 if (isFunction(customPlacement)) { return words.map((word: Word, index: number, words: Word[]) => ({ + ...word, hasText: !!word.text, font: functor(options.font)(word, index, words), weight: functor(options.fontWeight)(word, index, words), rotate: functor(options.rotate)(word, index, words), size: functor(options.fontSize)(word, index, words), style: 'normal', - ...word, - ...customPlacement(word, index, words), + ...customPlacement.call(chart, word, index, words), })); }