当前位置:首页 > VUE

Vue实现词云图

2026-02-19 19:00:43VUE

使用vue-wordcloud插件实现词云图

安装vue-wordcloud插件

npm install vue-wordcloud

在Vue组件中引入并使用

<template>
  <div>
    <word-cloud
      :data="wordData"
      :color="myColors"
      nameKey="word"
      valueKey="count"
    />
  </div>
</template>

<script>
import WordCloud from 'vue-wordcloud'

export default {
  components: { WordCloud },
  data() {
    return {
      wordData: [
        { word: 'Vue', count: 100 },
        { word: 'React', count: 85 },
        { word: 'Angular', count: 70 }
      ],
      myColors: ['#1f77b4', '#ff7f0e', '#2ca02c']
    }
  }
}
</script>

使用echarts实现词云图

安装echarts和echarts-wordcloud

Vue实现词云图

npm install echarts echarts-wordcloud

创建词云组件

<template>
  <div ref="wordcloud" style="width: 600px; height: 400px;"></div>
</template>

<script>
import * as echarts from 'echarts'
import 'echarts-wordcloud'

export default {
  mounted() {
    this.initWordCloud()
  },
  methods: {
    initWordCloud() {
      const chart = echarts.init(this.$refs.wordcloud)
      const option = {
        series: [{
          type: 'wordCloud',
          data: [
            { name: 'Vue', value: 100 },
            { name: 'React', value: 85 },
            { name: 'Angular', value: 70 }
          ],
          shape: 'circle',
          sizeRange: [12, 60]
        }]
      }
      chart.setOption(option)
    }
  }
}
</script>

自定义实现简单词云

使用CSS和JavaScript创建基础词云效果

Vue实现词云图

<template>
  <div class="word-cloud">
    <span 
      v-for="(word, index) in words" 
      :key="index"
      :style="{
        fontSize: `${word.size}px`,
        color: word.color,
        transform: `rotate(${word.rotate}deg)`
      }"
    >
      {{ word.text }}
    </span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      words: [
        { text: 'Vue', size: 40, color: '#42b983', rotate: 0 },
        { text: 'React', size: 35, color: '#61dafb', rotate: -15 },
        { text: 'Angular', size: 30, color: '#dd0031', rotate: 10 }
      ]
    }
  }
}
</script>

<style>
.word-cloud {
  width: 500px;
  height: 300px;
  position: relative;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
}
.word-cloud span {
  margin: 5px;
  padding: 5px;
  display: inline-block;
}
</style>

使用d3-cloud实现高级词云

安装d3-cloud

npm install d3-cloud

创建基于d3的词云组件

<template>
  <div ref="cloudContainer"></div>
</template>

<script>
import * as d3 from 'd3'
import cloud from 'd3-cloud'

export default {
  props: {
    words: {
      type: Array,
      default: () => [
        { text: 'Vue', size: 60 },
        { text: 'React', size: 50 },
        { text: 'Angular', size: 40 }
      ]
    }
  },
  mounted() {
    this.drawWordCloud()
  },
  methods: {
    drawWordCloud() {
      const layout = cloud()
        .size([500, 300])
        .words(this.words)
        .padding(5)
        .rotate(() => Math.floor(Math.random() * 2) * 90)
        .font('Impact')
        .fontSize(d => d.size)
        .on('end', this.drawWords)

      layout.start()
    },
    drawWords(words) {
      d3.select(this.$refs.cloudContainer)
        .append('svg')
        .attr('width', 500)
        .attr('height', 300)
        .append('g')
        .attr('transform', 'translate(250,150)')
        .selectAll('text')
        .data(words)
        .enter()
        .append('text')
        .style('font-size', d => `${d.size}px`)
        .style('font-family', 'Impact')
        .style('fill', () => `hsl(${Math.random() * 360}, 70%, 50%)`)
        .attr('text-anchor', 'middle')
        .attr('transform', d => `translate(${[d.x, d.y]})rotate(${d.rotate})`)
        .text(d => d.text)
    }
  }
}
</script>

响应式词云实现

添加窗口大小变化监听

<script>
export default {
  // ...其他代码
  mounted() {
    this.initWordCloud()
    window.addEventListener('resize', this.handleResize)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize)
  },
  methods: {
    handleResize() {
      this.$nextTick(() => {
        this.initWordCloud()
      })
    }
    // ...其他方法
  }
}
</script>

标签: 云图Vue
分享给朋友:

相关文章

Vue实现ping

Vue实现ping

Vue 实现 Ping 功能的方法 在 Vue 中实现 Ping 功能通常需要结合前端和后端技术。以下是几种常见的方法: 使用 JavaScript 的 fetch API 或 axios 通过发送…

Vue数据依赖实现

Vue数据依赖实现

Vue数据依赖实现原理 Vue的数据依赖实现基于响应式系统,核心是通过Object.defineProperty或Proxy拦截数据读写操作,结合发布-订阅模式自动追踪依赖和触发更新。 核心机制 依…

Vue语法实现

Vue语法实现

Vue 语法实现 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 的核心语法实现方式: 模板语法 Vue 使用基于 HTML 的模板语法,允许开发者声明式…

Vue 实现左右滑动

Vue 实现左右滑动

Vue 实现左右滑动的方法 使用 touch 事件监听 通过监听 touchstart、touchmove 和 touchend 事件实现基础滑动逻辑。在 Vue 组件中声明这些事件处理函数,计算滑动…

Vue项目实现rem

Vue项目实现rem

Vue 项目中实现 rem 适配方案 在 Vue 项目中实现 rem 适配,主要目的是让页面元素根据屏幕尺寸自动调整大小。以下是几种常见方法: 使用 postcss-pxtorem 插件 安装 p…

Vue实现词云图

Vue实现词云图

Vue 中实现词云图的方法 使用第三方库 vue-wordcloud vue-wordcloud 是一个专为 Vue 设计的轻量级词云组件,支持自定义颜色、大小和交互事件。 安装依赖: npm…