echarts实现词云

最终效果图
在这里插入图片描述
实现思路:
1、前端引入echarts.min.js和echarts-wordcloud.js
实现:
前端代码:

<body>
  <section class="mainBox">
      <div class="column">
          <div class="panel bar">
              <h2>词云</h2>
              <div class="charts"></div>
              <div class="pane-footer"></div>
          </div>
      </div>
  </section>
   <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
    <script src="./js/flexble.js"></script>
    //到官网下载并引入echarts
    <script src='https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js'></script>
    //这里做词云图时需要
    <script src='./js/echarts-wordcloud.js'></script>
    //引入js代码
    <script src="./js/index.js"></script>
</body>

js部分(这里的js的内容就是index.js文件里的内容)


(function(data){
    var chart = echarts.init(document.querySelector('.bar .charts'));
    $.get('./weibo_hot_word.json',function(data){//引入本地json文件
        console.log("data")
        console.log(data)
		var bseries=[];
        data.map(item=>{
            console.log("item")
            bseries.push({
                'name':item.word,
                'value': item.weight
            })
            console.log(bseries)
    })
       option = {
            tooltip: {},
            series: [ 
            {
                type: 'wordCloud',
                gridSize: 2,
                sizeRange: [12, 50],
                rotationRange: [-90, 90],
                shape: 'pentagon',
                width: 1000,
                height: 1000,
                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'
                    }
                },
                data:bseries,
            }
        ]
        }
        chart.setOption(option);
    })

注意:在vscode中运行index.html文件是,应该运行到open with live server,不然$.get()会出现跨域问题。
在这里插入图片描述

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值