vue简单实现词云图组件

2024-04-28 20:08

本文主要是介绍vue简单实现词云图组件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

说在前面

JavaScript也有许多可以用来生成词云图的库,但我自己其实都没有使用过,之前使用python的时候倒是用过python的wordcloud库,wordcloud库配合jieba库就可以很好的满足词频统计的需求,但在JavaScript这边我还没有了解很多词频统计这块的相关知识,在网上搜索了一番好像都没有搜索到有有关词频统计的相关库,而在词云生成这一方面的相关库倒是发现有不少,如:js2wordcloudwordcloud2 等等……这些库都很好地实现了词云图片的展示,现在我也尝试着简单封装一个自己使用的简洁版词云图

思路

实现效果

词云图需要实现的效果大致可以总结如下:

  • 1、根据词频设置不同的字体大小
  • 2、给单词渲染不同的随机颜色
  • 3、随机排布单词的位置

在这里插入图片描述

关键代码
1、根据词频设置不同的字体大小

maxSize与minSize设置最大字体和最小字体的比例,并不是实际的字体大小,字体大小的计算方式如下:

  • 1、计算最大字体和最小字体的中位数最为基准(该基准的字体大小即为this.baseSize,其余均在此基准上下浮动)
const baseSize = (this.maxSize + this.minSize) / 2;
  • 2、通过词频比例计算出字体的大小的浮动值
const addSize =((this.maxSize - this.minSize) * (freq - this.minFreq)) /(this.maxFreq - this.minFreq);
  • 3、计算出具体的字体大小
    (字体最小值 + 浮动值) / 基准值 = 实际字体比例
    实际字体比例 * 实际设置基准值 = 实际字体大小
((this.minSize + addSize) / baseSize) * this.baseSize + "rem";
  • 4、完整代码如下
//通过词频计算字体大小
getSize(freq) {const baseSize = (this.maxSize + this.minSize) / 2;const addSize =((this.maxSize - this.minSize) * (freq - this.minFreq)) /(this.maxFreq - this.minFreq);return (((this.minSize + addSize) / baseSize) * this.baseSize + "rem");
},
2、给单词渲染不同的随机颜色

颜色的rgb值为3个0~255的数字组成,我们只要随机生成3个0~255范围的数字组合即可,具体代码如下:

//随机获取颜色
getRandomColor() {let res = "rgb(";res += this.getRandomNum(0, 255) + ",";res += this.getRandomNum(0, 255) + ",";res += this.getRandomNum(0, 255) + ")";return res;
},
//获取随机数
getRandomNum(minN, maxN) {return Math.floor(Math.random() * (maxN - minN + 1) + minN);
},
3、随机排布单词的位置

对于单词位置排布这边有两个实现方案,但两个方案各有好处与缺点。

  • 1、绝对布局设置单词位置
    在这里插入图片描述

优点:单词位置随机性高
缺点:需要处理单词位置重叠问题(选择了方案二,所以目前还没有去解决这个问题)
实现思路:
(1)在范围内随机获取坐标

//随机获取坐标
getRandomPoint() {const width = this.width,height = this.height,pointList = this.pointList,showTextList = this.showTextList;const x = this.getRandomNum(0, width - 20),y = this.getRandomNum(0, height - 20);return { x: x, y: y };
},

(2)组合对应样式

//组合样式
getStyle(item, index) {const height = parseFloat(item.size) * 16;const width = parseFloat(item.size) * 20 * item.text.length;let res = "";res += "font-size:" + item.size + ";";res += "position: absolute;";res +="top:" +Math.max(0, Math.min(item.point.y, this.width - width)) +"px;";res +="left:" +Math.max(0, Math.min(item.point.x, this.height - height)) +"px;";res += "color:" + item.color + ";";return res;
},
  • 2、浮动布局设置单词位置

在这里插入图片描述

优点:单词位置不会重叠
缺点:单词位置随机性低
实现思路:
(1)在范围内随机获取坐标

//随机获取坐标
getRandomPoint() {const width = this.width,height = this.height,pointList = this.pointList,showTextList = this.showTextList;const x = this.getRandomNum(0, width - 20),y = this.getRandomNum(0, height - 20);return { x: x, y: y };
},

(2)组合对应样式

//组合样式
getStyle(item, index) {const height = parseFloat(item.size) * 16;const width = parseFloat(item.size) * 20 * item.text.length;let res = "";res += "float: left;";res += "font-size:" + item.size + ";";res += "color:" + item.color + ";";return res;
},
完整代码

完整代码实现如下:

<template><div><divclass="j-word-cloud":style="'min-height:' + height + 'px;width:' + width + 'px;'"><spanv-for="(item, index) in showTextList":key="index":id="'word-' + index":style="getStyle(item, index)">{{ item.text }}</span></div><div @click="init()">刷新</div></div>
</template><script>
export default {name: "JWordCloud",props: {textList: {type: Array,default: () => {return [{ text: "单词", freq: 10 },{ text: "单词1", freq: 5 },{ text: "单词2", freq: 7 },{ text: "单词3", freq: 2 },{ text: "单词5", freq: 3 },{ text: "单词6", freq: 4 },{ text: "单词7", freq: 5 },{ text: "单词8", freq: 6 },{ text: "单词9", freq: 6 },{ text: "单词10", freq: 8 },{ text: "单词11", freq: 4 },{ text: "单词12", freq: 2 },{ text: "单词13", freq: 4 },{ text: "单词14", freq: 3 },{ text: "单词15", freq: 1 },{ text: "单词16", freq: 5 }];}},width: {type: Number,default: 300},colorList: {type: Array,default: () => []},baseSize: {type: Number,default: 2},maxSize: {type: Number,default: 5},minSize: {type: Number,default: 1},transformDeg: {type: Array,default: () => {return [-45, 45];}}},data() {return {maxFreq: 0,minFreq: 0,showTextList: [],pointList: [],height: 200};},mounted() {this.init();},methods: {init() {this.initData();this.comShowtexList();this.getFourPoints();},//组合样式getStyle(item, index) {const height = parseFloat(item.size) * 16;const width = parseFloat(item.size) * 20 * item.text.length;let res = "";res += "font-size:" + item.size + ";";// res += "position: absolute;";res += "float: left;";// res +=//     "top:" +//     Math.max(0, Math.min(item.point.y, this.width - width)) +//     "px;";// res +=//     "left:" +//     Math.max(0, Math.min(item.point.x, this.height - height)) +//     "px;";res += "color:" + item.color + ";";// res += "transform:rotate(" + item.deg + "deg);";return res;},//计算旋转后的坐标getTransformPoint(x, y, deg) {deg = parseFloat(deg);let rx = (Math.cos(deg * Math.PI) / 180) * x,ry = (Math.sin(deg * Math.PI) / 180) * x;return { tx: x + rx, ty: y + ry };},//随机获取坐标getRandomPoint() {const width = this.width,height = this.height,pointList = this.pointList,showTextList = this.showTextList;const x = this.getRandomNum(0, width - 20),y = this.getRandomNum(0, height - 20);return { x: x, y: y };},//随机获取颜色getRandomColor() {let res = "rgb(";res += this.getRandomNum(0, 255) + ",";res += this.getRandomNum(0, 255) + ",";res += this.getRandomNum(0, 255) + ")";return res;},//随机获取角度getRandomdeg() {let res = "";res += this.getRandomNum(this.transformDeg[0],this.transformDeg[1]);return res;},//获取随机数getRandomNum(minN, maxN) {return Math.floor(Math.random() * (maxN - minN + 1) + minN);},//初始化initData() {const textList = this.textList;let maxF = textList[0].freq,minF = textList[0].freq;textList.map(item => {maxF = Math.max(maxF, item.freq);minF = Math.min(minF, item.freq);});this.maxFreq = maxF;this.minFreq = minF;},//通过词频计算字体大小getSize(freq) {const baseSize = (this.maxSize + this.minSize) / 2;const addSize =((this.maxSize - this.minSize) * (freq - this.minFreq)) /(this.maxFreq - this.minFreq);return (((this.minSize + addSize) / baseSize) * this.baseSize + "rem");},//获取四个顶点坐标getFourPoints() {this.$nextTick(() => {let showTextList = this.showTextList;let newHeight = 0;for (let i = 0; i < showTextList.length; i++) {let id = "word-" + i;let dom = document.getElementById(id);let tl = { x: dom.offsetLeft, y: dom.offsetTop };let tr = {x: dom.offsetLeft + dom.offsetWidth,y: dom.offsetTop};let bl = {x: dom.offsetLeft,y: dom.offsetTop + dom.offsetHeight};let br = {x: dom.offsetLeft + dom.offsetWidth,y: dom.offsetTop};newHeight = Math.max(newHeight, bl.y);showTextList[i].fourPoints = {tl: tl,tr: tr,bl: bl,br: br};}this.height = newHeight;});},//组装显示列表属性数据comShowtexList() {let showTextList = [];this.textList.map(item => {let temp = item;temp.size = this.getSize(item.freq);const point = this.getRandomPoint();const color = this.getRandomColor();const deg = this.getRandomdeg();this.pointList.push(point);temp.point = point;temp.color = color;temp.deg = deg;showTextList.push(temp);});showTextList = showTextList.sort((a, b) => {return 0.5 - Math.random();});this.showTextList = showTextList;}}
};
</script><style lang="scss" scoped>
.j-word-cloud {border: 1px solid black;position: relative;span {transform-origin: 0 0;padding: 0.1rem;margin: 0 auto;}
}
</style>
代码地址

预览(文档):http://120.79.163.94/JYeontuComponents/#/JWordCloudView

gitee源码:https://gitee.com/zheng_yongtao/jyeontu-component-warehouse

这篇关于vue简单实现词云图组件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/944140

相关文章

python实现幸运大转盘 python实现抽奖

欢迎关注我👆,收藏下次不迷路┗|`O′|┛ 嗷~~ 目录 一.前言 二.代码 三.使用 四.总结 一.前言 幸运大转盘是一种活动形式,广泛应用于各种场合,如商业促销、展览活动、企业庆典以及体育课堂等,旨在增加活动的趣味性和参与度。以下是对幸运大转盘的详细介

Ableton Live 10 Sound Packs Collection 声音乐器组件拓展包合集

格式: ALP / aif 参数: 24 bit 48 kHz stereo 要求: Live 9.7 标准版 (Live 10 或更新版本) 大小:105GB 适用于Ableton Live 9.7 标准版 (Live 10 或更新版本)的声音乐器组件拓展音色库内容。 包含以下内容(及时更新) Gravitas Create – Indian Raga ProjectSAM – Big Ba

https://www.cloudmidi.net/ableton-live-11-suite-v11-0-macos.html

macOS | 2021年2月23日| 2.5 GB 使用Live的新设备创建更大胆的声音。 通过大量工作流程改进来保持发展。 借助Push,可以使笔记本计算机与计算机的距离更远。 使用精选库来建立声音。 并获得无缝内置的Max for Live的无限潜力。 使用新设备进行创建 满足Wavetable,Echo,Drum Bus和Pedal的需求:借助Live的乐器和效果,可以带来丰富多彩

前端手写练习题及概念加深

通过举例和实际例子来加深前端的概念形式  1. 手写reduce 用法: // 手写 reduceconst a = [1, 3, 6, 8]const sum = a.reduce((accu, curr) => accu + curr, 0)console.log('sum', sum ) 在注释中解释 reduce 的手写方法 // 1. 明确参数累加函数,接受两个参数,回调函

vc小程序源码:利用opencv 实现九宫格切图

#include "stdafx.h"#include<opencv2/opencv.hpp>using namespace std;using namespace cv;int main(){Mat src = imread("福利.png");if (src.empty()){cout << "No Image!" << endl;system("pause");return -1

通过flask搭建,简单的网站,实现注册登录效果,初步了解搭建网页的基本架构。

网站架构了解 通过flask搭建,简单的网站,实现注册登录效果,初步了解搭建网页的基本架构。 前提准备 html在开发中最主要的一些标签知识flask中自带的接收信息给后台的语法 1)html基础标签的使用 <h1>用于强调文本内容,放大字体</h1>/* 行内标签&&块状标签 */1. 行内标签:字符多长就占多长的空间,内容会不断的添加在行后<span>行内标签</span>2.

锁和MVCC如何实现mysql的隔离级别

概述 MVCC解决读的隔离性,加锁解决写的隔离性。 读未提交 读未提交,更新数据大概率使用的是独享锁吧。 读已提交 在 Read Committed(读已提交)隔离级别下,每次执行读操作时都会生成一个新的 read view。这是因为在读已提交隔离级别下,每次读取操作都应该反映出其他事务已经提交的变更,因此需要生成新的 read view 来确保事务能够看到最新的已提交版本。 而

如何通过香港站群服务器高效实现网站内容的快速更新?

如何通过香港站群服务器高效实现网站内容的快速更新? 在当今激烈的数字市场竞争中,网站内容的快速更新对于吸引用户和保持竞争优势至关重要。而利用香港站群服务器实现这一目标,则具备诸多优势。下面将详细探讨如何通过香港站群服务器高效实现网站内容的快速更新。 如何通过香港站群服务器高效实现网站内容的快速更新? 分布式部署策略 通过采用分布式部署策略,可以将网站的不同部分部署在不同的服务

document.getElementsByClassName 的理想实现

各种实现方式 Pure DOM 通常先使用getElementsByTagName("*")取出文档中所有元素,然后进行遍历,使用正则表达式找出匹配的元素放入一个数组返回。由于IE5不支持document.getElementsByTagName("*"),要使用分支document.all以防错误。 The Ultimate getElementsByClassName方案,作者为Rob

Html5前端图片压缩

function compress(img, type) { var width = img.width; var height = img.height; // 如果图片大于四百万像素,计算压缩比并将大小压至400万以下 // var ratio; // if ((ratio = width * height / 4000000) > 1) { // ratio =