<template>
<div id="word-chart" :style="{width: '100%', height: '300px'}"></div>
</template>
<script>
import * as echarts from 'echarts'
import 'echarts-wordcloud'
export default {
name: "wordCloud",
props: {
wordData: {
type: Array,
default: () => [
{name: '张三', value: 10},
{name: '李四', value: 20},
]
}
},
data() {
return {
wordChart: null,
options: {
backgroundColor: "#FFFFFF",
tooltip: {},
series: [{
type: 'wordCloud',
//用来调整词之间的距离
gridSize: 40,
//词的大小,最小24px,最大60px,可以在这个范围调整词的大小
sizeRange: [24, 60],
//用来调整词的旋转方向,,[0,0]--代表着没有角度,也就是词为水平方向,需要设置角度参考注释内容
// rotationRange: [-45, 0, 45, 90],
// rotationRange: [ 0,90],
rotationRange: [0, 0],
//随机生成字体颜色
textStyle: {
color: function (e) {
let color = 'rgba(93,112,146,' + (0.7 +
3 * e.data.rate) + ')'
return color
}
},
// 位置相关设置
left: "center",
top: "center",
right: null,
bottom: null,
width: "100%",
height: "100%",
//数据
data: []
}]
}
}
},
mounted() {
this.wordChart = echarts.init(document.getElementById('word-chart'));
this.initCharts()
},
watch: {
wordData: {
handler() {
this.initCharts()
},
deep: true
}
},
methods: {
initCharts() {
this.options.series[0].data = this.wordData
if(!this.wordData || this.wordData.length === 0){
this.wordChart.setOption(this.options)
const _this = this
window.addEventListener("resize", function () {
_this.wordChart.resize();
});
return
}
let total = this.wordData.map(x=>x.value).reduce((x,y)=>x+y)
this.wordData.map(x=>x.rate = x.value/total)
this.wordChart.setOption(this.options)
const _this = this
window.addEventListener("resize", function () {
_this.wordChart.resize();
});
}
}
}
</script>
<style lang="scss" scoped>
</style>
效果图