当前位置:首页 > VUE

vue实现复杂饼图

2026-01-23 06:30:49VUE

使用 ECharts 实现复杂饼图

ECharts 是一个功能强大的数据可视化库,支持复杂的饼图配置,包括嵌套饼图、环形图、自定义标签等。

安装 ECharts:

npm install echarts --save

基础饼图实现:

<template>
  <div ref="chart" style="width: 600px; height: 400px;"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      const chartDom = this.$refs.chart;
      const myChart = echarts.init(chartDom);

      const option = {
        title: {
          text: '基础饼图示例',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        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)'
              }
            }
          }
        ]
      };

      myChart.setOption(option);
    }
  }
};
</script>

实现环形饼图

修改 radius 属性即可创建环形图:

vue实现复杂饼图

series: [
  {
    name: '访问来源',
    type: 'pie',
    radius: ['40%', '70%'],  // 内半径40%,外半径70%
    data: [
      // 数据同上
    ]
  }
]

实现嵌套饼图(多层饼图)

通过多个 series 叠加实现:

series: [
  {
    name: '外层',
    type: 'pie',
    radius: ['50%', '70%'],
    label: {
      show: false
    },
    data: [
      { value: 335, name: 'A' },
      { value: 310, name: 'B' },
      { value: 234, name: 'C' }
    ]
  },
  {
    name: '内层',
    type: 'pie',
    radius: ['0', '30%'],
    label: {
      position: 'inner'
    },
    data: [
      { value: 100, name: 'A1' },
      { value: 90, name: 'A2' },
      { value: 145, name: 'A3' }
    ]
  }
]

自定义饼图样式

可以自定义每个扇区的样式:

data: [
  {
    value: 1048,
    name: '搜索引擎',
    itemStyle: {
      color: '#c23531',
      borderWidth: 2,
      borderColor: '#fff'
    }
  },
  // 其他数据项...
]

添加交互效果

配置 tooltip 和 emphasis 增强交互:

vue实现复杂饼图

tooltip: {
  trigger: 'item',
  formatter: function(params) {
    return `${params.seriesName}<br/>
            ${params.marker} ${params.name}<br/>
            数值: ${params.value}<br/>
            占比: ${params.percent}%`;
  }
},
emphasis: {
  scale: true,
  scaleSize: 5,
  label: {
    show: true,
    fontSize: '20',
    fontWeight: 'bold'
  }
}

响应式处理

添加窗口大小变化监听:

mounted() {
  this.initChart();
  window.addEventListener('resize', this.handleResize);
},
methods: {
  handleResize() {
    this.myChart && this.myChart.resize();
  }
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
}

使用 Vue-ECharts 组件

对于更简单的集成,可以使用 vue-echarts 封装组件:

npm install vue-echarts echarts --save

使用示例:

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

<script>
import { use } from 'echarts/core';
import { PieChart } from 'echarts/charts';
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent
} from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
import VChart from 'vue-echarts';

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

export default {
  components: {
    VChart
  },
  data() {
    return {
      chartOption: {
        // 同上配置
      }
    };
  }
};
</script>

以上方法提供了从基础到高级的 Vue 中实现复杂饼图的完整方案,可根据实际需求选择合适的实现方式。

标签: 复杂vue
分享给朋友:

相关文章

vue 实现长列表

vue 实现长列表

Vue 实现长列表的优化方案 虚拟滚动技术 虚拟滚动是处理长列表的高效方式,仅渲染可视区域内的元素。通过计算滚动位置动态加载和卸载DOM节点,大幅减少内存占用和渲染压力。 <template&…

vue实现微博发布动态

vue实现微博发布动态

使用Vue实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="we…

vue实现gps

vue实现gps

Vue 中实现 GPS 定位功能 在 Vue 中实现 GPS 定位功能通常依赖于浏览器的 Geolocation API 或第三方地图服务(如高德、百度地图等)。以下是两种常见的实现方式: 使用浏览…

vue 实现平面

vue 实现平面

Vue 实现平面的方法 在 Vue 中实现平面效果通常涉及 CSS 样式、UI 框架或自定义组件的使用。以下是几种常见方法: 使用 CSS 样式 通过 Vue 的样式绑定或 scoped CSS 为…

vue 实现折叠

vue 实现折叠

Vue 实现折叠功能的方法 在 Vue 中实现折叠功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-if 或 v-show 控制显示隐藏 通过绑定一个布尔值变量,结合 v-if 或 v…

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.define…