效果

<template>
<el-card
:body-style="{
padding: 0
}"
>
<div class="container" ref="target"></div>
</el-card>
</template>
<style lang="scss" scoped>
.container {
height: 240px;
}
</style>
<script setup>
export const randomRGB = () => {
const r = Math.floor(Math.random() * 255)
const g = Math.floor(Math.random() * 255)
const b = Math.floor(Math.random() * 255)
return `rgb(${r}, ${g}, ${b})`
}
import * as echarts from 'echarts'
import 'echarts-wordcloud'
import { ref, onMounted } from 'vue'
import { randomRGB } from '@/utils/color'
import { getChartWordCloud } from '@/api/chart.js'
import { useI18n } from 'vue-i18n'
import { watchSwitchLang } from '@/utils/i18n'
import wordcloudBg from '@/assets/wordcloud.png'
const i18n = useI18n()
const wordCloudData = ref([])
const getWordCloudData = async () => {
const res = await getChartWordCloud()
wordCloudData.value = res
renderChart()
}
getWordCloudData()
const target = ref(null)
let mChart = null
onMounted(() => {
mChart = echarts.init(target.value)
})
const renderChart = () => {
var maskImage = new Image()
maskImage.src = wordcloudBg
var option = {
title: {
text: i18n.t('msg.chart.cloudChartTitle')
},
series: [
{
type: 'wordCloud',
sizeRange: [4, 80],
rotationRange: [0, 0],
gridSize: 0,
maskImage: maskImage,
layoutAnimation: true,
textStyle: {
color: randomRGB
},
emphasis: {
textStyle: {
fontWeight: 'bold',
color: '#000'
}
},
data: wordCloudData.value
}
]
}
maskImage.onload = function () {
mChart.setOption(option)
}
}
watchSwitchLang(renderChart)
</script>
<style lang="scss" scoped>
.container {
height: 240px;
}
</style>