vue3.0 中使用 echarts 的 echarts-wordcloud 插件开发词云图

1.安装

npm install echarts echarts-wordcloud

注意:
echarts-wordcloud 是基于 echarts 的,所以要先安装 echarts,若已经安装,则直接安装 echarts-wordcloud

另外,echarts4.7.0 或 4.9.0 适用安装 echarts-wordcloud@1.1.2 版本,echarts5.0.1 适用安装 echarts-wordcloud@2.0.0 版本

<script setup lang="ts">
import { ref, nextTick, onMounted } from 'vue'
import * as echarts from "echarts"
import 'echarts-wordcloud'
const wordcloudRef = ref()
onMounted(() => {
  const data = [
    {
      name: '前端工程师',
      value: 100
    },
    {
      name: '数据可视化',
      value: 50
    },
    {
      name: '大耳朵图图',
      value: 20
    },
    {
      name: '前端工程师',
      value: 150
    },
    {
      name: '数据可视化',
      value: 75
    },
    {
      name: '大耳朵图图',
      value: 55
    }
  ]
  initWordCloud(data)
})
// 初始化词云
const initWordCloud = (data: any, max = 72) => {
  var myChart = echarts.init(wordcloudRef.value);
  const option = {
    title: {
      text: '关键词',
      show: false
    },
    tooltip: {},
    series: [{
      type: 'wordCloud',
      shape: 'circle',
      left: 'center',
      top: 'center',
      width: '75%',
      height: '60%',
      right: null,
      bottom: null,
      sizeRange: [14, max],
      rotationRange: [-45, 45],
      rotationStep: 15, // 0 15 30 45 度倾斜
      gridSize: 12,
      drawOutOfBound: false,
        // 这是全局的文字样式,相对应的还可以对每个词设置字体样式
        textStyle: {
          fontFamily: 'sans-serif',
          fontWeight: 'bold',
          // 颜色可以用一个函数来返回字符串
          color: function () {
            // Random color
            return 'rgb(' + [
              Math.round(Math.random() * 160),
              Math.round(Math.random() * 160),
              Math.round(Math.random() * 160)
            ].join(',') + ')';
          },
          emphasis: {
            shadowBlur: 2,
            shadowColor: '#333'
          }
        },
        // Data is an array. Each array item must have name and value property.
        data: data
      }]
  }
  option && myChart.setOption(option);
  window.addEventListener("resize", () => {
    if (myChart) {
      myChart.resize();
    }
  });
}
</script>

<template>
  <div ref="wordcloudRef" class="wordcloud"></div>
</template>

<style lang="scss" scoped>
.wordcloud {
  width: 700px;
  height: 500px;
}
</style>

效果如下:
在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Vue项目使用echarts-wordcloud绘制词云图,可以按照以下步骤操作: 1. 安装echartsecharts-wordcloud插件 ```bash npm install echarts --save npm install echarts-wordcloud --save ``` 2. 在需要使用词云图的组件引入echarts ```javascript import echarts from 'echarts' ``` 3. 在mounted生命周期初始化echarts实例,并设置词云图的配置项 ```javascript mounted() { const chartDom = this.$refs.chart const myChart = echarts.init(chartDom) const option = { series: [{ type: 'wordCloud', shape: 'circle', sizeRange: [20, 80], rotationRange: [-90, 90], rotationStep: 45, gridSize: 2, textStyle: { normal: { fontFamily: 'sans-serif', fontWeight: 'bold', color: function () { return 'rgb(' + [ Math.round(Math.random() * 255), Math.round(Math.random() * 255), Math.round(Math.random() * 255) ].join(',') + ')' } } }, data: [ { name: 'Apple', value: 10000 }, { name: 'Banana', value: 6181 }, { name: 'Orange', value: 4386 }, { name: 'Watermelon', value: 4055 }, { name: 'Pineapple', value: 2467 }, { name: 'Grape', value: 2244 }, { name: 'Mango', value: 1898 }, { name: 'Pear', value: 1484 }, { name: 'Cherry', value: 1001 }, { name: 'Peach', value: 987 }, { name: 'Kiwi', value: 900 } ] }] } myChart.setOption(option) } ``` 4. 在模板添加echarts实例的容器 ```html <template> <div> <div ref="chart" style="width: 600px; height: 400px;"></div> </div> </template> ``` 以上就是在Vue项目使用echarts-wordcloud绘制词云图的步骤,需要注意的是,词云图的配置项需要根据实际需求进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值