echarts-wordcloud 热词云使用

1.安装

npm install echarts

npm install echarts-wordcloud

注意版本:echarts版本5只能和wordcloud版本2的一起使用 ;echarts版本4只能和wordcloud版本1的一起使用

2.在main.js引入

import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts
require('echarts-wordcloud')

3.使用

<template>
    <div id="echartsWordcloud" style="width:200px;height:200px;background-color:#0d071f"></div>
</template>
<script>
var run = 999
 var createRandomItemStyle2 = function () {
    var colorArr = ['#fda67e', '#81cacc', '#cca8ba', "#88cc81", "#82a0c5", '#fddb7e', '#735ba1', '#bda29a', '#6e7074', '#546570', '#c4ccd3'];
    var flag = parseInt(Math.random() * 10);
    return {
        normal: {
            fontFamily: '微软雅黑',
            color:colorArr[flag]
        }
    };
}
var createRandomItemStyle1 = function (params) {    //此方法与下方配置中的第一个textStle下的color等同
    var colors = ['#fda67e', '#81cacc', '#cca8ba', "#88cc81", "#82a0c5", '#fddb7e', '#735ba1', '#bda29a', '#6e7074', '#546570', '#c4ccd3'];
    return colors[parseInt(Math.random() * 10)];
}
export default{
  data() {
    return {
      data : [{name: "小区",value: "283"},{name: "留言板",value: "101"},{name: "业主",value: "148"},
      {name: "同学",value: "283"},{name: "老师",value: "101"},{name: "话痨",value: "148"}
      ]
    }
  },
  mounted(){
    this.initEcharts(this.data)
  },
  methods:{
    initEcharts(data){
        let echartsWordcloud = this.$echarts.init(document.getElementById("echartsWordcloud"));
        let option = {
          title: {
            text: "标题",
            x: "center"
          },
          // backgroundColor: "#fff",
          series: [
            {
              type: "wordCloud",
              //用来调整词之间的距离
              gridSize: 10,
              //用来调整字的大小范围
              sizeRange: [14, 26],
              rotationRange: [0, 0],
              //随机生成字体颜色
              // textStyle: {
              //     fontFamily: 'sans-serif',
              //     // fontWeight: 'bold',
              //     // Color can be a callback function or a color string
              //     color: function () {
              //         // Random color
              //         return 'rgb(' + [
              //             Math.round(Math.random() * 160),
              //             Math.round(Math.random() * 160),
              //             Math.round(Math.random() * 160)
              //         ].join(',') + ')';
              //     }
              // },
              // 也可以自己定制颜色
              textStyle: {
                  fontFamily: '微软雅黑',
                  color: function () {
                      var colors = ['#fda67e', '#81cacc', '#cca8ba', "#88cc81", "#82a0c5", '#fddb7e', '#735ba1', '#bda29a', '#6e7074', '#546570', '#c4ccd3'];
                      return colors[parseInt(Math.random() * 10)];
                  }
                },
              // textStyle: {
              //     color: function() {
              //       return (
              //         "rgb(" +
              //         Math.round(Math.random() * 255) +
              //         ", " +
              //         Math.round(Math.random() * 255) +
              //         ", " +
              //         Math.round(Math.random() * 255) +
              //         ")"
              //       );
              //     }
              // },
              //位置相关设置
              left: "center",
              top: "center",
              right: null,
              bottom: null,
              width: "100%",
              height: "100%",
              //数据
              data: data
            }
          ]
        };
        echartsWordcloud.setOption(option);
        //点击事件
        echartsWordcloud.on("click",function(e){
          alert(e)
        })
    }
  }
}
</script>

效果图:

插件地址:echarts-wordcloud

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
ECharts-WordCloud 是 Apache ECharts(incubating)的一个扩展,用于绘制词云图。下面我给出一个简单的使用示例: 1. 引入 echartsecharts-wordcloud 库 ```html <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/echarts-wordcloud/dist/echarts-wordcloud.min.js"></script> ``` 2. 创建一个容器 ```html <div id="wordcloud" style="width: 600px;height:400px;"></div> ``` 3. 初始化 ECharts 实例 ```javascript var myChart = echarts.init(document.getElementById('wordcloud')); ``` 4. 配置词云图选项 ```javascript var option = { series: [{ type: 'wordCloud', gridSize: 20, sizeRange: [12, 50], rotationRange: [-90, 90], shape: 'pentagon', width: 600, height: 400, textStyle: { normal: { color: function() { return 'rgb(' + [ Math.round(Math.random() * 255), Math.round(Math.random() * 255), Math.round(Math.random() * 255) ].join(',') + ')'; } } }, data: [ { name: 'JavaScript', value: 10000, }, { name: 'Java', value: 6181, }, { name: 'Python', value: 4386, }, { name: 'PHP', value: 4055, }, { name: 'Ruby', value: 2467, }, { name: 'C++', value: 2244, }, { name: 'C#', value: 1898, }, { name: 'HTML', value: 1484, }, { name: 'CSS', value: 1112, }, { name: 'TypeScript', value: 965, }, { name: 'Swift', value: 847, }, { name: 'Objective-C', value: 582, }, { name: 'SQL', value: 555, }, { name: 'Go', value: 550, }, { name: 'Kotlin', value: 462, }, { name: 'Perl', value: 367, } ] }] }; ``` 5. 使用刚指定的配置项和数据显示图表。 ```javascript myChart.setOption(option); ``` 这样就可以在页面上展示一个简单的词云图了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值