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.

154 lines
3.7 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
},
3 years ago
lvl: {
3 years ago
type: [Number, String],
value: 18
3 years ago
},
3 years ago
color: {
3 years ago
type: String,
value: 'random-dark'
3 years ago
},
canvasId: {
3 years ago
type: String,
value: 'myCanvas'
3 years ago
}
},
lifetimes: {
3 years ago
attached: function () {
3 years ago
// 在组件实例进入页面节点树时执行
3 years ago
const query = wx.createSelectorQuery()
3 years ago
query.select('#' + this.data.canvasId)
3 years ago
.fields({
node: true,
size: true
})
3 years ago
.exec((res) => {
3 years ago
this.setData({
3 years ago
canvas: res[0].node
3 years ago
})
3 years ago
// this.data.canvas = res[0].node
3 years ago
})
3 years ago
},
},
3 years ago
observers: {
3 years ago
list(newVal) {
if (!newVal || !Array.isArray(newVal) || newVal.length === 0) return
let {
width,
height,
list,
wordData,
color
} = this.data
3 years ago
list = newVal;
const dpr = wx.getSystemInfoSync().pixelRatio
3 years ago
this.setData({
options: {
3 years ago
"list": this.doData(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": 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": 375 * dpr
3 years ago
}
})
3 years ago
setTimeout(() => {
3 years ago
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: {
3 years ago
"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
},
3 years ago
/**
* 组件的方法列表
*/
methods: {
bindWord(event) {
3 years ago
const {
detail
} = event.currentTarget.dataset
3 years ago
this.triggerEvent('detail', detail);
},
3 years ago
doData(arr1 = []) {
if (arr1.length === 0) {
3 years ago
return []
3 years ago
}
3 years ago
let list = this.doList(arr1);
3 years ago
let arr = [];
let maxVal = list[0][1];
3 years ago
let minVal = list[list.length - 1][1];
3 years ago
let l = list.length;
let x = l / (375 * this.data.height)
3 years ago
let fx = Math.sqrt(x);
let max = fx * maxVal;
let fy = maxVal / minVal;
3 years ago
let fz = fy >= 5 ? 5 : fy;
let y = fz * 100/ max;
3 years ago
list.forEach(ele => {
3 years ago
let a = [ele[0], ele[1] * x * y ];
3 years ago
arr.push(a)
})
return arr;
3 years ago
},
doList(arr = []) {
let list = [];
if(arr.length > 140) {
list = arr.slice(139)
} else {
list = arr;
}
return list
3 years ago
}
3 years ago
}
3 years ago
})