当前位置:首页 > 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实现画布

Vue实现画布

Vue 实现画布的方法 在 Vue 中实现画布功能可以通过 HTML5 的 <canvas> 元素结合 Vue 的响应式特性来完成。以下是几种常见的实现方式: 使用原生 <canv…

Vue实现lagger页面

Vue实现lagger页面

Vue实现懒加载页面的方法 懒加载(Lazy Loading)是一种优化技术,用于延迟加载非关键资源,从而提升页面初始加载速度。在Vue中可以通过以下方式实现懒加载: 路由懒加载 使用Vue Rou…

Vue 实现下载

Vue 实现下载

Vue 实现文件下载的方法 前端下载(纯前端生成文件) 适用于需要动态生成文件内容并下载的场景,如导出表格数据为 CSV 或 Excel。 // 创建 Blob 对象并触发下载 const dow…

Vue实现多个echarts排版

Vue实现多个echarts排版

实现多个 ECharts 实例的排版布局 在 Vue 中实现多个 ECharts 实例的排版布局,可以通过 CSS 布局结合 ECharts 的响应式设计来完成。以下是几种常见方法: 使用 Flex…

Vue实现禁止截屏

Vue实现禁止截屏

防止截屏的常见方法 在Vue中实现禁止截屏的功能,可以通过以下几种方式来实现。需要注意的是,完全防止截屏在技术上是不现实的,但可以增加截屏的难度或通过其他手段进行限制。 使用CSS禁止用户选择文本和…

Vue使用transform实现轮播

Vue使用transform实现轮播

使用 transform 实现 Vue 轮播 在 Vue 中,可以通过 transform 和 CSS 过渡效果实现平滑的轮播效果。以下是具体实现方法: 核心思路 利用 transform: tra…