You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.3 KiB

3 years ago
// components/word-cloud/index.js
3 years ago
import WordCloud from './WordCloud'
3 years ago
Component({
/**
* 组件的属性列表
*/
properties: {
3 years ago
width: {
type: Number,
value: 375
3 years ago
},
height: {
3 years ago
type: Number,
value: 450
3 years ago
},
3 years ago
bgColor: {
3 years ago
type: String,
3 years ago
value: 'transparent',
3 years ago
},
3 years ago
list: {
type: Array,
value: [],
3 years ago
},
color: {
type: String,
value: 'random-dark'
},
canvasId: {
type: String,
value: 'myCanvas'
3 years ago
}
},
lifetimes: {
attached: function() {
// 在组件实例进入页面节点树时执行
const query = this.createSelectorQuery()
3 years ago
query.select('#' + this.data.canvasId)
3 years ago
.fields({ node: true, size: true })
.exec((res) => {
this.data.canvas = res[0].node
})
3 years ago
},
},
3 years ago
observers: {
list(newVal) {
if(!newVal || !Array.isArray(newVal) || newVal.length === 0) return
3 years ago
let { width, height, list, wordData, color } = this.data
3 years ago
list = newVal;
if (!width || isNaN(Number(width))) {
width = 375
}
if (!height || isNaN(Number(height))) {
height = 450
}
const dpr = wx.getSystemInfoSync().pixelRatio
3 years ago
this.setData({
options: {
3 years ago
"list": this.doData(list),
3 years ago
"gridSize": 10, // size of the grid in pixels
"weightFactor": 4, // number to multiply for size of each word in the list
"fontWeight": 'normal', // 'normal', 'bold' or a callback
"fontFamily": 'Times, serif', // font to use
"color": color, // 'random-dark' or 'random-light'
"backgroundColor": 'transparent', // the color of canvas
"rotateRatio": 0.2, // probability for the word to rotate. 1 means always
"relativeScaling": 0.1,
"height": height * dpr,
"width": width * dpr
}
})
3 years ago
setTimeout(()=>{
if (this.data.canvas) {
3 years ago
const wordCloud = new WordCloud(this.data.canvas, this.data.options)
3 years ago
wordData = wordCloud.start()
this.setData({
wordData,
width,
height,
})
}
}, 500)
}
},
/**
* 组件的初始数据
*/
3 years ago
data: {
3 years ago
wordData: [],
canvas: null,
3 years ago
options: {
"list": [],
"gridSize": 10, // size of the grid in pixels
"weightFactor": 4, // number to multiply for size of each word in the list
"fontWeight": 'normal', // 'normal', 'bold' or a callback
"fontFamily": 'Times, serif', // font to use
"color": 'random-dark', // 'random-dark' or 'random-light'
"backgroundColor": 'transparent', // the color of canvas
"rotateRatio": 0.2, // probability for the word to rotate. 1 means always
"relativeScaling": 0.1
}
3 years ago
},
3 years ago
/**
* 组件的方法列表
*/
methods: {
bindWord(event) {
const { detail } = event.currentTarget.dataset
this.triggerEvent('detail', detail);
},
3 years ago
doData(list = []) {
if(list.length === 0) {
return []
}
let arr = [];
3 years ago
let max = 18;
3 years ago
let maxVal = list[0][1];
list.forEach(ele => {
let a = [ele[0], ele[1] / maxVal * max];
arr.push(a)
})
return arr;
}
3 years ago
}
3 years ago
})