vue 词云

本文介绍了如何使用ECharts在Vue中创建一个动态词云图,通过数据绑定实现词频统计并自动调整词的大小和颜色。组件接收一个数组作为输入,实时更新词云效果。
摘要由CSDN通过智能技术生成
<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>

效果图在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值