vue项目中使用echarts做词云图

5 篇文章 0 订阅
4 篇文章 0 订阅

效果图  

安装依赖 

npm install echarts
npm i echarts-wordcloud

完整代码

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

  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
要在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绘制词云图的步骤,需要注意的是,词云图的配置项需要根据实际需求进行修改。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小•愿望

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值