Vue实现词云
<template>
<div>
<svg style="background-color: white;border-radius: 50%;" :width='width' :height='height' @mousemove='listener($event)'>
<a class="fontA" :href="tag.href" v-for='tag in tags'>
<text :x='tag.x' :y='tag.y' :font-size='20 * (600/(600-tag.z))' :fill-opacity='((400+tag.z)/600)'>{{tag.text}}</text>
</a>
</svg>
</div>
</template>
<script>
export default {
name: "word-cloud",
props: ['data','width','height','RADIUS'],
data() {
return {
tagsNum:0,
speedX:Math.PI/360/1.5,
speedY:Math.PI/360/1.5,
tags: []
}
},
computed:{
CX(){
return this.width/2;
},
CY(){
return this.height/2;
}
},
created(){
let tags=[];
this.tagsNum = this.data.length
for(let i =0;i<this.data.length;i++){
let tag = {};
let k = -1 + (2 * (i + 1) - 1) / this.tagsNum;
let a = Math.acos(k);
let b = a * Math.sqrt(this.tagsNum * Math.PI)
tag.text = this.data[i];
tag.x = this.CX + this.RADIUS * Math.sin(a) * Math.cos(b);
tag.y = this.CY + this.RADIUS * Math.sin(a) * Math.sin(b);
tag.z = this.RADIUS * Math.cos(a);
tags.push(tag);
console.log(tag)
}
this.tags = tags;
},
methods:{
rotateX(angleX){
},
rotateY(angleY){
var cos = Math.cos(angleY);
var sin = Math.sin(angleY);
for(let tag of this.tags){
var x1 = (tag.x - this.CX) * cos - tag.z * sin + this.CX;
var z1 = tag.z * cos + (tag.x - this.CX) * sin;
tag.x = x1;
tag.z = z1;
}
},
listener(e){
}
},
mounted() {
setInterval(() => {
this.rotateX(this.speedX);
this.rotateY(this.speedY);
}, 17)
},
};
</script>
<style scoped>
.fontA{
fill: green;
font-weight: bold;
font-family: Apple LiGothic Medium;
}
.fontA:hover{
fill: red;
}
</style>