当前位置:首页 > VUE

vue实现饼图

2026-01-08 07:13:49VUE

使用 ECharts 实现 Vue 饼图

安装 ECharts 依赖

npm install echarts --save

在 Vue 组件中引入 ECharts

import * as echarts from 'echarts'

创建饼图容器

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

初始化饼图

mounted() {
  this.initPieChart()
},
methods: {
  initPieChart() {
    const chart = echarts.init(document.getElementById('pieChart'))
    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)'
            }
          }
        }
      ]
    }
    chart.setOption(option)
  }
}

使用 Vue-ECharts 组件

安装 vue-echarts

npm install echarts vue-echarts

注册组件

import { provide } from 'vue'
import { createApp } from 'vue'
import App from './App.vue'
import ECharts from 'vue-echarts'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { PieChart } from 'echarts/charts'
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent
} from 'echarts/components'

use([
  CanvasRenderer,
  PieChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent
])

const app = createApp(App)
app.component('v-chart', ECharts)
provide('$echarts', echarts)
app.mount('#app')

使用组件

<template>
  <v-chart :option="chartOption" style="height: 400px" />
</template>

<script>
export default {
  data() {
    return {
      chartOption: {
        title: {
          text: 'Vue-ECharts 饼图',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: '50%',
            data: [
              { value: 1048, name: '搜索引擎' },
              { value: 735, name: '直接访问' },
              { value: 580, name: '邮件营销' },
              { value: 484, name: '联盟广告' },
              { value: 300, name: '视频广告' }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
    }
  }
}
</script>

响应式饼图实现

监听窗口变化

mounted() {
  this.initPieChart()
  window.addEventListener('resize', this.handleResize)
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize)
},
methods: {
  handleResize() {
    this.chart && this.chart.resize()
  }
}

动态数据更新

updateChartData(newData) {
  this.chart.setOption({
    series: [{
      data: newData
    }]
  })
}

自定义饼图样式

环形饼图配置

series: [{
  type: 'pie',
  radius: ['40%', '70%'],
  avoidLabelOverlap: false,
  label: {
    show: false,
    position: 'center'
  },
  emphasis: {
    label: {
      show: true,
      fontSize: '18',
      fontWeight: 'bold'
    }
  },
  labelLine: {
    show: false
  },
  data: [
    {value: 1048, name: '搜索引擎'},
    {value: 735, name: '直接访问'}
  ]
}]

玫瑰图效果

vue实现饼图

series: [{
  type: 'pie',
  radius: [20, 140],
  roseType: 'radius',
  itemStyle: {
    borderRadius: 5
  },
  data: [
    {value: 40, name: 'rose 1'},
    {value: 33, name: 'rose 2'}
  ]
}]

标签: vue
分享给朋友:

相关文章

vue实现autocomplete

vue实现autocomplete

Vue 实现 Autocomplete 功能 Vue 中实现 Autocomplete(自动完成)功能可以通过自定义组件或使用现有库完成。以下是几种常见方法: 方法一:使用 Vue 原生实现 创建…

vue实现注销

vue实现注销

实现用户注销功能 在Vue应用中实现用户注销功能通常涉及清除用户会话、重置应用状态并跳转到登录页面。以下是具体实现方法: 清除用户认证信息 使用Vuex或Pinia管理用户状态时,需在注销时重置存储…

vue实现布局

vue实现布局

Vue 实现布局方法 Vue 提供了多种方式实现页面布局,可以通过组件化、CSS 框架或自定义样式完成。以下是几种常见方法: 使用 CSS Flexbox 或 Grid 通过 Vue 单文件组件结合…

vue 分页 实现

vue 分页 实现

Vue 分页实现方法 使用第三方库(如 Element UI) Element UI 提供了现成的分页组件 el-pagination,适合快速集成。 安装 Element UI: npm ins…

vue实现刷新

vue实现刷新

Vue 实现页面刷新的方法 在Vue中实现页面刷新可以通过多种方式,以下是几种常见的方法: 使用 window.location.reload() 通过调用浏览器的原生方法强制刷新当前页面: me…

vue实现双击

vue实现双击

Vue 实现双击事件的方法 在Vue中实现双击事件可以通过以下几种方式完成,具体选择取决于项目需求和开发习惯。 使用 @dblclick 指令 Vue提供了内置的@dblclick指令,可以直接在模…