Vue项目使用echarts实现词云图

2 篇文章 0 订阅

效果图
Vue

  1. 安装依赖
npm install echarts
npm install echarts-wordcloud
  1. 创建组件
<template>
 <div ref="wordcloud" class="wordcloud">
 </div>
</template>
<script>
import echarts from 'echarts'
import "echarts-wordcloud/dist/echarts-wordcloud";
import "echarts-wordcloud/dist/echarts-wordcloud.min";
 export default {
   data() {
     return {
       wordList:[
       {
         name: "珍珠奶茶",
         value: 15000
       },
       {
         name: "冰激凌红茶",
         value: 10081
       },
       {
         name: "椰果奶茶",
         value: 9386
       },
       {
         name: "波霸奶茶",
         value: 7500
       },
       {
         name: "金桔柠檬",
         value: 7500
       },
       {
         name: "乌龙奶茶",
         value: 6500
       },
       {
         name: "芒果欧蕾",
         value: 6500
       },
       {
         name: "仙草奶茶",
         value: 6000
       },
       {
         name: "翡翠柠檬",
         value: 4500
       },
       {
         name: "芒果养乐多",
         value: 3800
       },
       {
         name: "柠檬养乐多",
         value: 3000
       },
       {
         name: "波霸奶绿",
         value: 2500
       },
       {
         name: "四季春茶",
         value: 2300
       },
       {
         name: "茉莉绿茶",
         value: 2000
       },
       {
         name: "阿萨姆红茶",
         value: 1900
       },
       {
         name: "奶冻摇摇乐",
         value: 1800
       },
       {
         name: "冻顶乌龙茶",
         value: 1700
       },
       {
         name: "咖啡",
         value: 1600
       },
       {
         name: "焦糖玛奇朵",
         value: 1500
       },
       {
         name: "金桔柠檬",
         value: 1200
       },
     ]
     }
   },
   mounted(){
     this.initchart();
   },
   methods:{
     initchart(){
       let myChart = echarts.init(this.$refs.wordcloud);
       myChart.setOption({
         series: [
           {
             type: "wordCloud",
             //用来调整词之间的距离
             gridSize: 10,
             //用来调整字的大小范围
             // Text size range which the value in data will be mapped to.
             // Default to have minimum 12px and maximum 60px size.
             sizeRange: [14, 60],
             // Text rotation range and step in degree. Text will be rotated randomly in range [-90,                                                                             90] by rotationStep 45
             //用来调整词的旋转方向,,[0,0]--代表着没有角度,也就是词为水平方向,需要设置角度参考注释内容
             // rotationRange: [-45, 0, 45, 90],
             // rotationRange: [ 0,90],
             rotationRange: [0, 0],
             //随机生成字体颜色
             // maskImage: maskImage,
             textStyle: {
               normal: {
                 color: function() {
                   return (
                     "rgb(" +
                     Math.round(Math.random() * 255) +
                     ", " +
                     Math.round(Math.random() * 255) +
                     ", " +
                     Math.round(Math.random() * 255) +
                     ")"
                   );
                 }
               }
             },
             //位置相关设置
             // Folllowing left/top/width/height/right/bottom are used for positioning the word cloud
             // Default to be put in the center and has 75% x 80% size.
             left: "center",
             top: "center",
             right: null,
             bottom: null,
             width: "200%",
             height: "200%",
             //数据
             data: this.wordList
           }
         ]
       })
     }
   }
 }
</script>
<style scoped>
.wordcloud{
 width:100%;
 height:300px;
 margin:auto;
}
</style>
  1. 页面中引入组件
<wordcloud></wordcloud>
  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
要在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绘制词云图的步骤,需要注意的是,词云图的配置项需要根据实际需求进行修改。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值