vue项目中使用echarts做词云图

安装依赖

js 复制代码
npm install echarts
npm i echarts-wordcloud

完整代码

js 复制代码
<template>
  <div id="chart-container"></div>
</template>

<script>
import * as echarts from "echarts";
import "echarts-wordcloud";

export default {
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById("chart-container"));
      var keywords = [];
      for (let index = 0; index < 20; index++) {
        let random = Math.floor(Math.random() * 100);
        keywords.push({
          name: random,
          value: random,
        });
      }
      var option = {
        series: [
          {
            type: "wordCloud",
            sizeRange: [12, 60],
            rotationRange: [-90, 90],
            rotationStep: 45,
            gridSize: 15,
            shape: "pentagon",
            width: "100%",
            height: "100%",
            drawOutOfBound: true,
            textStyle: {
              color: function () {
                return (
                  "rgb(" +
                  [
                    Math.round(Math.random() * 160),
                    Math.round(Math.random() * 160),
                    Math.round(Math.random() * 160),
                  ].join(",") +
                  ")"
                );
              },
            },
            emphasis: {
              textStyle: {
                shadowBlur: 10,
                shadowColor: "#333",
                color: "red",
              },
            },
            data: keywords,
          },
        ],
      };
      this.chart.setOption(option);
    },
  },
};
</script>
<style scoped>
#chart-container {
  height: 100%;
}
</style>

配置说明

shape:要绘制的"云"的形状。可以是表示为回调函数或存在的关键字。可用的礼物是circle

(默认),cardioid(苹果或心形曲线,最著名的极坐标方程),diamond(正方形)、triangle-forward、triangle(三角形)、star(星形)、pentagon (五边形)。

maskImage:词云轮廓图,图片需转成base64。

left、top、right、bottom:词云的位置,默认是 center。

width、height:词云的宽高,默认是 75%、80%。

sizeRange:词云的文字字号范围,默认是[12, 60]。

rotationRange:文本旋转范围和步长。文本将通过旋转步骤45在[-90,90]范围内随机旋转。

gridSize:每个词之间的间距。

drawOutOfBound:是否允许词云在边界外渲染。

textStyle:全局文本样式。

emphasis:鼠标悬浮样式。

data :数组,必须包含name、value属性,也可以有textStyle、emphasis等

相关推荐
李剑一7 分钟前
uni-app实现网络离线定位
前端·trae
鲨莎分不晴7 分钟前
Nginx 部署前端项目实战指南
运维·前端·nginx
码界奇点17 分钟前
基于Vue3与TypeScript的后台管理系统设计与实现
前端·javascript·typescript·vue·毕业设计·源代码管理
ashcn200122 分钟前
水滴按钮解析
前端·javascript·css
攀登的牵牛花22 分钟前
前端向架构突围系列 - 框架设计(五):契约继承原则
前端·架构
豆苗学前端1 小时前
你所不知道的前端知识,html篇(更新中)
前端·javascript·面试
一 乐1 小时前
绿色农产品销售|基于springboot + vue绿色农产品销售系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·宠物
zzjyr1 小时前
Webpack 生命周期原理深度解析
前端
xiaohe06011 小时前
💘 霸道女总裁爱上前端开发的我
前端·游戏开发·trae
sophie旭1 小时前
内存泄露排查之我的微感受
前端·javascript·性能优化