当前位置:首页 > VUE

vue实现饼状图

2026-02-23 16:04:42VUE

vue3 使用 ECharts 实现饼状图

安装 ECharts 依赖

npm install echarts --save

在组件中引入 ECharts

import * as echarts from 'echarts';
import { onMounted } from 'vue';

准备 DOM 容器

<div id="pieChart" style="width: 600px; height: 400px;"></div>

初始化图表

const initPieChart = () => {
  const chartDom = document.getElementById('pieChart');
  const myChart = echarts.init(chartDom);

  const option = {
    title: {
      text: '示例饼图',
      subtext: '数据展示',
      left: 'center'
    },
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      left: 'left',
      data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
    },
    series: [
      {
        name: '访问来源',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 335, name: '直接访问' },
          { value: 310, name: '邮件营销' },
          { value: 234, name: '联盟广告' },
          { value: 135, name: '视频广告' },
          { value: 1548, name: '搜索引擎' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  };

  myChart.setOption(option);
};

onMounted(() => {
  initPieChart();
});

vue2 使用 vue-echarts 实现饼状图

安装 vue-echarts

vue实现饼状图

npm install echarts vue-echarts

全局注册组件

import Vue from 'vue';
import ECharts from 'vue-echarts/components/ECharts.vue';
import 'echarts/lib/chart/pie';

Vue.component('v-chart', ECharts);

组件中使用

<template>
  <v-chart :options="pieOptions" autoresize />
</template>

<script>
export default {
  data() {
    return {
      pieOptions: {
        title: {
          text: '销售统计',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        series: [
          {
            type: 'pie',
            radius: '55%',
            data: [
              { value: 235, name: '视频广告' },
              { value: 274, name: '搜索引擎' },
              { value: 310, name: '邮件营销' }
            ]
          }
        ]
      }
    }
  }
}
</script>

<style scoped>
.echarts {
  width: 100%;
  height: 400px;
}
</style>

自定义饼图样式

设置环形饼图

vue实现饼状图

series: [{
  type: 'pie',
  radius: ['40%', '70%'],
  avoidLabelOverlap: false,
  label: {
    show: false,
    position: 'center'
  },
  emphasis: {
    label: {
      show: true,
      fontSize: '18',
      fontWeight: 'bold'
    }
  },
  data: [...]
}]

添加动画效果

animationType: 'scale',
animationEasing: 'elasticOut',
animationDelay: function (idx) {
  return Math.random() * 200;
}

响应式处理

监听窗口变化

window.addEventListener('resize', function() {
  myChart.resize();
});

使用 vue-echarts 时自动响应

<v-chart :options="pieOptions" autoresize />

数据动态更新

使用 watch 监听数据变化

watch: {
  chartData: {
    handler(newVal) {
      this.updateChart(newVal);
    },
    deep: true
  }
},
methods: {
  updateChart(data) {
    this.pieOptions.series[0].data = data;
  }
}

标签: vue饼状图
分享给朋友:

相关文章

vue懒加载实现难吗

vue懒加载实现难吗

vue懒加载的实现难度 Vue懒加载的实现并不复杂,核心逻辑是通过动态导入(Dynamic Imports)和路由配置或组件异步加载完成。以下是具体实现方法: 路由懒加载实现 在Vue Router…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue插槽实现

vue插槽实现

插槽的基本概念 Vue插槽(Slot)是一种内容分发机制,允许父组件向子组件传递模板片段,子组件通过<slot>标签定义接收位置。插槽的核心作用是增强组件的灵活性和复用性。 默认插槽…

vue权限实现

vue权限实现

Vue 权限实现方案 基于路由的权限控制 通过路由守卫实现权限验证,在路由配置中添加meta字段标记权限要求。在router.beforeEach中检查用户权限与路由要求的匹配情况。 // rout…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

vue实现hovuer

vue实现hovuer

Vue 实现 Hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 直接控制 通过 :hover 伪类实现,无需额外的 JavaScri…