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等 。

相关推荐
栉甜17 分钟前
Vue2学习内容(上)
前端·javascript·vue.js·学习
梦曦i26 分钟前
v0.2.0 架构升级:class + reactive 重塑状态管理
前端·uni-app x
布丁爱干饭27 分钟前
寄给午后
前端
java1234_小锋1 小时前
【技术专题】CSS3 网页技术 - CSS3 布局进阶
前端·css·css3
zhippt_10141 小时前
只剩截图没有源文件?图转PPT把图片还原成可编辑幻灯片
前端·人工智能·powerpoint
shmily麻瓜小菜鸡2 小时前
TypeScript 知识点补充(第三部分:工程化与高级类型)
前端·javascript·typescript
10岁的博客2 小时前
Chrome 扩展安全攻防实录:从权限模型到真实漏洞复盘
前端·chrome·安全
QianhangQianping2 小时前
WebSocket实时推送+ HTTP 定时轮询:一块大屏两套取数怎么不打架
前端·websocket·网络协议·http·低代码·架构·数据可视化
Eider19982 小时前
Python基础知识之并发相关-2
开发语言·前端·python
用户69371750013842 小时前
AI 能写代码,但工程架构还是应该由人来定
前端·后端