react中Js2WordCloud画词云

一、效果

在这里插入图片描述

二、源码

1、npm安装

npm install js2wordcloud --save

2、引入

import Js2WordCloud from 'js2wordcloud/dist/js2wordcloud.js'

3、使用

optionChart1=()=>{
        var self = this;
        // let div = echarts.init(document.getElementById('yjgb-box-1'));
        // var div = new window.Js2WordCloud(document.getElementById('yjgb-box-1'))//在index.html中引入时的写法
        var div = new Js2WordCloud(document.getElementById('yjgb-box-1'))
        // div.resize();
        let textList=[
            ['服务'], ['细致'], ['意识'], ['踏实'], ['责任感'], ['耐劳'],['自觉'], ['主动'], ['分享'], ['严谨'],
            ['热情'], ['上进心'],['及时'], ['经验'], ['刻苦'], ['沟通'], ['抗压'], ['精通'],['操作'],['熟练'],
            ['资格'], ['成本'],['英语'], ['提供'], ['分析'], ['开发'], ['反馈'], ['协助'],['使用'],['谈判']
        ]
        let cyList=[]
        for(let i=0;i<60;i++){
            cyList.push([textList[parseInt(Math.random()*textList.length)],Math.round(Math.random()*10)+1])
        }
        div.setOption({
            backgroundColor:'transparent',
            shape: 'circle',
            ellipticity: 1,//椭圆
            maxFontSize: 36,//最大字号
            minFontSize: 12,//最小字号
            tooltip: {
                show: true
            },
            list: cyList,
            color(word, weight, fontSize, distance, theta) { //自定义颜色、透明度
                if (fontSize>=12 && fontSize<=20) {
                    return 'rgb(29,227,250,0.3)';
                } else if(fontSize>20 && fontSize<=30){
                    return 'rgb(29,227,250,0.6)';
                }else if(fontSize>30 && fontSize<=36){
                    return 'rgb(29,227,250)';
                }
           },
        })
    }

三、参考

https://github.com/liangbizhi/js2wordcloud/tree/master/

四、有坑

法1:
create-react-app中,在index.html中引入js插件后,使用时,要加上window.xxx,否则会报“xxx is not defined”的错,具体原因还不是很清楚,可能是webpack配置的问题。
法2:
npm 安装、import 引入
这样就可以直接new Js2WordCloud了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 ECharts 实现,需要先引入 ECharts 库。可以通过以下代码在 HTML 文件引入 ECharts: ```html <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script> ``` 接下来,需要准备一个用于显示的 HTML 元素。可以使用以下代码创建一个 div 元素: ```html <div id="wordcloud" style="width: 600px;height:400px;"></div> ``` 然后,在 JavaScript 文件,可以通过以下代码创建: ```javascript // 创建图表 var myChart = echarts.init(document.getElementById('wordcloud')); // 设置的配置项 var option = { series: [{ type: 'wordCloud', shape: 'circle', left: 'center', top: 'center', width: '80%', height: '80%', right: null, bottom: null, sizeRange: [12, 60], rotationRange: [-90, 90], rotationStep: 45, gridSize: 8, drawOutOfBound: false, textStyle: { normal: { fontFamily: 'sans-serif', fontWeight: 'bold', color: function () { return 'rgb(' + [ Math.round(Math.random() * 160), Math.round(Math.random() * 160), Math.round(Math.random() * 160) ].join(',') + ')'; } }, emphasis: { shadowBlur: 10, shadowColor: '#333' } }, // 设置的数据 data: [ { name: 'ECharts', value: 10000, textStyle: { normal: { color: 'black' }, emphasis: { color: 'red' } } }, { name: 'JavaScript', value: 6181, textStyle: { normal: { color: 'black' }, emphasis: { color: 'red' } } }, { name: 'HTML', value: 4386, textStyle: { normal: { color: 'black' }, emphasis: { color: 'red' } } }, { name: 'CSS', value: 4055, textStyle: { normal: { color: 'black' }, emphasis: { color: 'red' } } }, { name: 'Vue.js', value: 2467, textStyle: { normal: { color: 'black' }, emphasis: { color: 'red' } } }, { name: 'React', value: 2244, textStyle: { normal: { color: 'black' }, emphasis: { color: 'red' } } }, { name: 'AngularJS', value: 1898, textStyle: { normal: { color: 'black' }, emphasis: { color: 'red' } } }, { name: 'TypeScript', value: 1484, textStyle: { normal: { color: 'black' }, emphasis: { color: 'red' } } } ] }] }; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); ``` 这里的代码使用 `series` 配置项的 `type` 属性设置类型为 `wordCloud`,然后设置 `data` 属性为显示的数据,包括语名称和对应的权重值。 `textStyle` 属性设置语的样式。 的其他配置项还包括语的形状、大小、排列方式、字体等,可以根据需要进行设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值