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

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery 或…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue computed实现

vue computed实现

计算属性的基本用法 在Vue中,计算属性通过computed选项定义,用于声明依赖于其他属性的动态值。计算属性基于它们的依赖进行缓存,只有依赖发生变化时才会重新计算。 new Vue({ dat…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <label…

vue实现Siri

vue实现Siri

Vue 实现 Siri 风格语音交互 实现类似 Siri 的语音交互功能需要结合语音识别、语音合成和前端交互逻辑。Vue 作为前端框架,可以很好地管理这些功能的交互状态。 语音识别集成 使用 Web…