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等

相关推荐
超哥--3 小时前
B站视频内容智能分析系统(九):React 前端与管理面板
前端·react.js·前端框架
Cutecat_5 小时前
视频字幕处理工具横向:提取模式 vs 编辑模式,该如何选择
android·前端·ios·语音识别
qq_422152576 小时前
PDF 加水印工具怎么选?2026 年文档版权保护方案对比
前端·pdf·github
kyriewen6 小时前
手写 Promise.all、race、any:不到 30 行代码,解决并发异步的所有姿势
前端·javascript·面试
brucelee1867 小时前
OpenClaw 浏览器控制(Chrome MCP)完整教程
前端·chrome
ct9787 小时前
React 状态管理方案深度对比
开发语言·前端·react
胡志辉的博客7 小时前
深入浅出理解浏览器事件循环:从一道输出题讲到 Chrome 源码
前端·javascript·chrome·chromium·event loop
代码不加糖7 小时前
js中不会冒泡的事件有哪些?
前端·javascript·vue.js
懂懂tty8 小时前
Vue2与Vue3之间API差异
前端·javascript·vue.js
AI焦点8 小时前
跨越协议鸿沟:Tool Use状态机从Anthropic到OpenAI兼容体系的适配要点
前端·人工智能