npm安装:

npm install js2wordcloud --save

用法

var wc = new Js2WordCloud(document.getElementById('container'))
wc.setOption({
    tooltip: {
        show: true
    },
    list: [['谈笑风生', 80], ['谈笑风生', 80], ['谈笑风生', 70], ['谈笑风生', 70], ['谈笑风生', 60], ['谈笑风生', 60]],
    color: '#15a4fa'
})

Document

API (https://github.com/timdream/wordcloud2.js/blob/gh-pages/API.md)

  • setOption(options)

    options必须通过此API进行设置,才能显示词云

    在wordcloud2.js原options基础上增加:

    {
    // ...
    imageShape: 'https://example.com/images/shape.png', // 提供一张图片,根据其形状进行词云渲染,默认为null
    fontSizeFactor: 0.1, // 当词云值相差太大,可设置此值进字体行大小微调,默认0.1
    maxFontSize: 60, // 最大fontSize,用来控制weightFactor,默认60
    minFontSize: 12, // 最小fontSize,用来控制weightFactor,默认12
    tooltip: {
    show: true, // 默认:false
    backgroundColor: 'rgba(0, 0, 0, 0.701961)', // 默认:'rgba(0, 0, 0, 0.701961)'
    formatter: function(item) { // 数据格式化函数,item为list的一项
    }
    },
    noDataLoadingOption: { // 无数据提示。
    backgroundColor: '#eee',
    text: '暂无数据',
    textStyle: {
    color: '#888',
    fontSize: 14
    }
    }
    // ...
    }
  • showLoading([loadingOption])

    过渡控制,显示loading(读取中)。可选。

    loadingOption:

    {
    backgroundColor: '#eee',
    text: '正在加载...',
    effect: 'spin' // 默认:null, { String | Function } 可选:'spin|normal';也可为回调函数,回调函数生成HTML
    }

     

  • hideLoading()

    • 隐藏loading

    • resize()

      当容器大小变化时,调用此方法进行重绘

  • wordcloud2函数说明

    wordcloud2(data, size = 1, minSize = 0, gridSize =  0,  

    fontFamily = NULL, fontWeight = 'normal',  

    color = 'random-dark', backgroundColor = "white",  

    minRotation = -pi/4, maxRotation = pi/4, rotateRatio = 0.4,  

    shape = 'circle', ellipticity = 0.65, widgetsize = NULL)  

    常用参数:

    (1)data:词云生成数据,包含具体词语以及频率;

    (2)size:字体大小,默认为1,一般来说该值越小,生成的形状轮廓越明显;

    (3)fontFamily:字体,如‘微软雅黑’;

    (4)fontWeight:字体粗细,包含‘normal’,‘bold’以及‘600’;;

    (5)color:字体颜色,可以选择‘random-dark’以及‘random-light’,其实就是颜色色系;

    (6)backgroundColor:背景颜色,支持R语言中的常用颜色,如‘gray’,‘blcak’,但是还支持不了更加具体的颜色选择,如‘gray20’

    (7)minRontatin与maxRontatin:字体旋转角度范围的最小值以及最大值,选定后,字体会在该范围内随机旋转;

    (8)rotationRation:字体旋转比例,如设定为1,则全部词语都会发生旋转;

    (9)shape:词云形状选择,默认是‘circle’,即圆形。还可以选择‘cardioid’(苹果形或心形),‘star’(星形),‘diamond’(钻石),‘triangle-forward’(三角形),‘triangle’(三角形),‘pentagon’(五边形);

  • 事例

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Demo</title>
    </head>
    <style>
    #container{
    width: 1000px;
    margin: 100px auto;
    border: 1px solid #ccc;
    }
    </style>
    <body>
    <h1>一般</h1>
    <div id="container" style="height: 400px;"></div>
    <!-- <h1>无数据</h1>
    <div id="container1" style="height: 400px;"></div> -->
    <script src="./node_modules/js2wordcloud/dist/js2wordcloud.js"></script>
    <script>
    function getnum(){
    return parseInt(Math.random()*100)
    }
    var arr1 = new Array(100);
    arr1[0] = ['减肥而附加费', 1]
    arr1[1] = ['v 发表观后感', 11]
    arr1[2] = ['我', 78]
    arr1[3] = ['解决国家', 18]
    arr1[4] = ['结构图然后 iu 越糊涂', 20]
    arr1[5] = ['就斤斤计较', 30]
    arr1[6] = ['的', 101]
    arr1[7] = ['给她', 100]
    arr1[8] = ['会议厅', 10]
    arr1[9] = ['非日任何个人和 u 研讨会', 90]
    for(var i=10;i<arr1.length;i++){
    var num = getnum()
    arr1[i] = ['谈笑风生', num];
    }
    console.log(arr1);
    var option = {
    // tooltip: {
    // show: true,
    // formatter: function(item) {
    // return item[0] + ': 价值¥' + item[1] + '<br>' + '词云图'
    // }
    // },
    // list: [['谈笑风生', 500], ['谈笑风生', 50], ['谈笑风生', 40], ['谈笑风生', 40], ['谈笑风生', 30], ['谈笑风生', 30], ['谈笑风生', 20], ['谈笑风生', 20], ['谈笑风生', 1], ['谈笑风生', 1], ['谈笑风生', 500000], ['谈笑风生', 50], ['谈笑风生', 40], ['谈笑风生', 1000], ['谈笑风生', 30], ['谈笑风生', 30], ['谈笑风生', 20], ['谈笑风生', 20], ['谈笑风生', 1], ['谈笑风生', 1], ['谈笑风生', 500000], ['谈笑风生', 50], ['谈笑风生', 40], ['谈笑风生', 40], ['谈笑风生', 30], ['谈笑风生', 30], ['谈笑风生', 20], ['谈笑风生', 20], ['谈笑风生', 1], ['谈笑风生', 1], ['谈笑风生', 500000], ['谈笑风生', 50], ['谈笑风生', 40], ['谈笑风生', 40], ['谈笑风生', 30], ['谈笑风生', 30], ['谈笑风生', 20], ['谈笑风生', 20], ['谈笑风生', 1], ['谈笑风生', 1], ['谈笑风生', 500000], ['谈笑风生', 50], ['谈笑风生', 40], ['谈笑风生', 40], ['谈笑风生', 30], ['谈笑风生', 30], ['谈笑风生', 20], ['谈笑风生', 20], ['谈笑风生', 1], ['谈笑风生', 1], ['谈笑风生', 500000], ['谈笑风生', 50], ['谈笑风生', 40], ['谈笑风生', 40], ['谈笑风生', 30], ['谈笑风生', 30], ['谈笑风生', 20], ['谈笑风生', 20], ['谈笑风生', 1], ['谈笑风生', 1], ['谈笑风生', 500000], ['谈笑风生', 50], ['谈笑风生', 40], ['谈笑风生', 40], ['谈笑风生', 30], ['谈笑风生', 30], ['谈笑风生', 20], ['谈笑风生', 20], ['谈笑风生', 1], ['谈笑风生', 1], ['谈笑风生', 500000], ['谈笑风生', 50], ['谈笑风生', 40], ['谈笑风生', 40], ['谈笑风生', 30], ['谈笑风生', 30], ['谈笑风生', 20], ['谈笑风生', 20], ['谈笑风生', 1], ['谈笑风生', 1], ['谈笑风生', 500000], ['谈笑风生', 50], ['谈笑风生', 40], ['谈笑风生', 40], ['谈笑风生', 30], ['谈笑风生', 30], ['谈笑风生', 20], ['谈笑风生', 20], ['谈笑风生', 1], ['谈笑风生', 1], ['谈笑风生', 500000], ['谈笑风生', 50], ['谈笑风生', 40], ['谈笑风生', 40], ['谈笑风生', 30], ['谈笑风生', 30], ['谈笑风生', 20], ['谈笑风生', 20], ['谈笑风生', 1], ['谈笑风生', 1], ['谈笑风生', 500000], ['谈笑风生', 50], ['谈笑风生', 40], ['谈笑风生', 40], ['谈笑风生', 30], ['谈笑风生', 30], ['谈笑风生', 20], ['谈笑风生', 20], ['谈笑风生', 1], ['谈笑风生', 1], ['谈笑风生', 500000], ['谈笑风生', 50], ['谈笑风生', 40], ['谈笑风生', 40], ['谈笑风生', 30], ['谈笑风生', 30], ['谈笑风生', 20], ['谈笑风生', 20], ['谈笑风生', 1], ['谈笑风生', 1]],
    list: arr1,
    color: 'random-dark',
    // shape: 'circle',
    ellipticity: 1,
    minRotation: -1.57080,
    maxRotation: -1.57080,
    rotateRatio: .4,
    fontSizeFactor: 5, // 当词云值相差太大,可设置此值进字体行大小微调,默认0.1
    maxFontSize: 60, // 最大fontSize,用来控制weightFactor,默认60
    minFontSize: 20,
    gridSize: 10,
    // ratationSteps: 2,
    // rotationRation: .5,
    // shuffle: 30
    // shape: 'circle',
    // cardioid: 'diamond'
    cardioid: 'square' }
    var wc = new Js2WordCloud(document.getElementById('container'))
    wc.setOption(option) window.onresize = function() {
    wc.resize()
    }
    </script>
    </body>
    </html>

      

     

词云(wordcloud2.js js2wordcloud.js)的更多相关文章

  1. R语言︱文本挖掘——词云wordcloud2包

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 笔者看到微信公众号探数寻理中提到郎大为Chif ...

  2. 使用R语言将微信记录制作成词云(简洁)--情人节奥义

    一.导出并读入微信聊天记录     参照百度的方法,使用同步助手.安装同步助手--连接手机(安卓苹果均可)--点击"其他功能"--点击微信图标即可进入聊天记录导出界面(非常简单). ...

  3. NLP实现文本分词+在线词云实现工具

    实现文本分词+在线词云实现工具 词云是NLP中比较简单而且效果较好的一种表达方式,说到可视化,R语言当仍不让,可见R语言︱文本挖掘——词云wordcloud2包 当然用代码写词云还是比较费劲的,网上也 ...

  4. Python之手把手教你用JS逆向爬取网易云40万+评论并用stylecloud炫酷词云进行情感分析

    本文借鉴了@平胸小仙女的知乎回复 https://www.zhihu.com/question/36081767 写在前面: 文章有点长,操作有点复杂,需要代码的直接去文末即可.想要学习的需要有点耐心 ...

  5. js2wordcloud 词云包的使用

    js文件下载: https://github.com/liangbizhi/js2wordcloud/tree/master/ dist文件夹内 引用: <script src="** ...

  6. R语言之词云:wordcloud&wordcloud2安装及参数说明

    一.wordcloud安装说明 install.packages("wordcloud"); 二.wordcloud2安装说明 install.packages("dev ...

  7. 微信小程序存放视频文件到阿里云用到算法js脚本文件

           peterhuang007/weixinFileToaliyun: 微信小程序存放视频文件到阿里云用到算法js脚本文件 https://github.com/peterhuang007/ ...

  8. echarts之词云随机颜色的配置

    echarts中的词云字体产生随机颜色,最主演的是要引入worldcloud.js,另外还要有jquery.js文件与echarts.js文件的引入,通过配置即可实现词云随机颜色的产生.下面为大家介绍 ...

  9. echarts词云引用

    最近项目中需要使用echarts的词云图,因为几经波折才引用成功,所以想记下来跟大家分享,(我的随笔不会写那么多让人需要动脑子去理解的东西,就是记录一下步骤,因为经验甚少,底层原理懂得不多,所以就先记 ...

随机推荐

  1. Spring Boot 引入org.springframework.boot.SpringApplication出错

    今天新建的一个spring boot maven项目, 写启动类时发现无法引入SpringApplication, 经查原来是冲突了,我早些时候用了比较低版本的spring boot创建了项目 ,导致 ...

  2. ffmpeg日志调式

    1.播放器打印输出调试日志:ffplay -v debug $URL2.播放器打开详细调试日志:./ffplay -loglevel 563.修改源码修改日志级别:    1)log.c中:stati ...

  3. 反射RelectionDemo

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. 错误提示:未处理的“System.NullReferenceException”类型的异常出现在 system.data.dll 中。 其他信息: 未将对象引用设置到对象的实例。

    这种情况的发生一般有3种情况:1.代码中有一个对象没有初始化.例如Form A=new Form();2.连接数据库的问题.连接和打开连接的语句.3.T-SQL语句不正确,引起在访问数据库时有问题.

  5. Spring Boot2.1.3全局跨域

    /** * 配置跨域访问 * * @author Terwer */ @Bean public WebMvcConfigurer corsConfigurer() { return new WebMv ...

  6. python3 参数*args 、 **args 在函数定义和调用中的应用

    一.函数调用时 说明:*args 表示解包(解包 列表.元组.字符串类型) #定义函数cn_musql def cn_musql(host,port,user,pwd,db): print(host) ...

  7. VSCode打开多个项目文件夹的解决方法

    最近从sublime转vscode,自然而然就会把sublime的一些习惯带过来,其中有一点让人头疼的是: 当把一个文件夹拖进vscode里面的时候,会把原来的文件夹覆盖掉,这就意味着不能同时在vsc ...

  8. LeetCode 705 Design HashSet 解题报告

    题目要求 Design a HashSet without using any built-in hash table libraries. To be specific, your design s ...

  9. rand和srand的用法

    首先我们要对rand&srand有个总体的看法:srand初始化随机种子,rand产生随机数,下面将详细说明. rand(产生随机数)表头文件: #include<stdlib.h> ...

  10. Python004-数据处理示例:以某个数据(字段)为基准从数据中获取不同的字段行数

    数据源样式如下所示: 需求: 读取文本,以第一列为基准参考系,每个基准仅输出满足需要条数的数据:不满足,全部输出. 比如,基准为 6236683970000018780,输出条数要求为 5.若文本中含 ...