echarts词云图

效果

在这里插入图片描述

注意点

需要下载一个第三方的包 echarts-wordcloud

代码块

<template>
   <div ref="target" class="w-full h-full"></div>
</template>

<script setup>
import { onMounted, ref, watch } from "vue";
import * as echarts from "echarts";
import "echarts-wordcloud"; // 需要下载第三方的包

const props = defineProps({
  data: {
    type: Object,
    required: true,
  },
});

// 获取 dom 实例
const target = ref(null);

// echarts 实例变量
let mChart = null;
// 在 mounted 生命周期之后,实例化 echarts
onMounted(() => {
  mChart = echarts.init(target.value);
  // 渲染 echarts
  renderChart();
});

/**
 * 生成随机色值
 */
const randomRGB = () => {
  const r = Math.floor(Math.random() * 255);
  const g = Math.floor(Math.random() * 255);
  const b = Math.floor(Math.random() * 255);
  return `rgb(${r}, ${g}, ${b})`;
};

// 渲染图表
const renderChart = () => {
  const options = {
    series: [
      {
        // 类型
        type: "wordCloud",
        // 数据映射文本的大小范围
        sizeRange: [8, 46],
        // 文字旋转范围
        rotationRange: [0, 0],
        // 单词之间的间距
        gridSize: 0,
        // 渲染动画
        layoutAnimation: true,
        // 字体
        textStyle: {
          // 随机色值
          color: randomRGB,
        },
        // 高亮字体
        emphasis: {
          textStyle: {
            fontWeight: "bold",
            color: "#000",
          },
        },
        // 数据
        data: props.data.datas,
      },
    ],
  };

  mChart.setOption(options);
};

// 监听数据的变化,重新渲染图表
watch(
  () => props.data,
  () => {
    renderChart();
  }
);
</script>

<style lang="scss" scoped></style>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Echarts 是一个非常流行的数据可视化库,它支持多种图表类型,包括词云图。下面是一个简单的使用 Echarts 绘制词云图的示例: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Echarts 词云图</title> <!-- 引入 Echarts 库 --> <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script> </head> <body> <!-- 使用 Echarts 绘制词云图的容器 --> <div id="wordcloud" style="width: 600px; height: 400px;"></div> <script> // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('wordcloud')); // 定义词云图的数据 var data = [ {name: '张三', value: 100}, {name: '李四', value: 80}, {name: '王五', value: 70}, {name: '赵六', value: 60}, {name: '钱七', value: 50}, {name: '孙八', value: 40}, {name: '周九', value: 30}, {name: '吴十', value: 20}, ]; // 配置词云图的参数 var option = { series: [{ type: 'wordCloud', shape: 'circle', left: 'center', top: 'center', width: '80%', height: '80%', right: null, bottom: null, sizeRange: [12, 60], rotationRange: [-90, 90], rotationStep: 45, gridSize: 8, drawOutOfBound: false, textStyle: { normal: { fontFamily: 'sans-serif', fontWeight: 'bold', color: function () { return 'rgb(' + [ Math.round(Math.random() * 160), Math.round(Math.random() * 160), Math.round(Math.random() * 160) ].join(',') + ')'; } }, emphasis: { shadowBlur: 10, shadowColor: '#333' } }, data: data }] }; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); </script> </body> </html> ``` 这个示例中,我们首先引入了 Echarts 库,然后在页面中创建一个容器用于显示词云图。接着,我们定义了词云图的数据,包括每个词的名称和权重。最后,我们使用 Echarts 的 `wordCloud` 类型来创建词云图,并通过配置参数来指定词云图的样式和数据。最终,我们将配置项和数据传递给 Echarts 的实例对象,并通过调用 `setOption` 方法显示图表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值