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等

相关推荐
AICoder码农王39 分钟前
Vitest 实战:从一个真实项目看懂测试套件
前端·前端框架
律宏阔1 小时前
Electron Forge 在 macOS M4 打包踩坑:Node.js 26 导致 make 异常中断
前端
木木爱研究1 小时前
elpis-里程碑四-基于Vue完成动态组件库建设
前端·后端
深念Y1 小时前
Nuxt 项目 Docker 构建:从 pnpm+node 迁移到全 bun
java·前端·docker
qq_426003961 小时前
UI自动化元素定位不到解决方案
前端·pycharm·自动化
EthanChou20201 小时前
AES67协议笔记
前端·笔记·es6
马优晨1 小时前
pom.xml 中 jquery webjars 依赖解释
xml·前端·jquery·jquery webjars·webjars依赖
里欧跑得慢1 小时前
AI 驱动的 UI 国际化方案自动适配:从单一语言到多语言布局的智能转换
前端·css·flutter·web
DolitD1 小时前
云流技术深度剖析:单服务器下如何实现3D应用的多实例并发?
java·服务器·前端·3d·云原生·云计算
芦柑4641 小时前
画布和3D导演台工具:短剧分镜从素材整理到空间预演的完整链路
服务器·前端·数据库