当前位置:首页 > VUE

vue实现扇形图

2026-02-18 18:08:02VUE

Vue 实现扇形图的方法

使用 ECharts 实现

安装 ECharts 依赖:

npm install echarts --save

在 Vue 组件中引入并使用:

<template>
  <div ref="chart" style="width: 400px; 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 = {
        series: [{
          type: 'pie',
          radius: ['50%', '70%'],
          avoidLabelOverlap: false,
          itemStyle: {
            borderRadius: 10,
            borderColor: '#fff',
            borderWidth: 2
          },
          label: {
            show: false,
            position: 'center'
          },
          emphasis: {
            label: {
              show: true,
              fontSize: '18',
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: false
          },
          data: [
            { value: 1048, name: 'Search Engine' },
            { value: 735, name: 'Direct' },
            { value: 580, name: 'Email' },
            { value: 484, name: 'Union Ads' },
            { value: 300, name: 'Video Ads' }
          ]
        }]
      };

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

使用 D3.js 实现

安装 D3.js 依赖:

npm install d3 --save

Vue 组件实现:

<template>
  <svg ref="svg" width="400" height="400"></svg>
</template>

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

export default {
  mounted() {
    this.drawChart();
  },
  methods: {
    drawChart() {
      const data = [10, 20, 30, 40, 50];
      const svg = d3.select(this.$refs.svg);
      const width = +svg.attr('width');
      const height = +svg.attr('height');
      const radius = Math.min(width, height) / 2;

      const g = svg.append('g')
        .attr('transform', `translate(${width / 2}, ${height / 2})`);

      const color = d3.scaleOrdinal(['#98abc5', '#8a89a6', '#7b6888', '#6b486b', '#a05d56']);

      const pie = d3.pie();
      const arc = d3.arc()
        .innerRadius(0)
        .outerRadius(radius);

      const arcs = g.selectAll('.arc')
        .data(pie(data))
        .enter()
        .append('g')
        .attr('class', 'arc');

      arcs.append('path')
        .attr('d', arc)
        .attr('fill', (d, i) => color(i));
    }
  }
};
</script>

使用 Canvas API 手动绘制

Vue 组件实现:

<template>
  <canvas ref="canvas" width="400" height="400"></canvas>
</template>

<script>
export default {
  mounted() {
    this.drawSector();
  },
  methods: {
    drawSector() {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');
      const centerX = canvas.width / 2;
      const centerY = canvas.height / 2;
      const radius = 150;

      const data = [
        { value: 30, color: '#FF6384' },
        { value: 50, color: '#36A2EB' },
        { value: 100, color: '#FFCE56' },
        { value: 40, color: '#4BC0C0' }
      ];

      let total = data.reduce((sum, item) => sum + item.value, 0);
      let startAngle = 0;

      data.forEach(item => {
        const sliceAngle = (item.value / total) * 2 * Math.PI;

        ctx.beginPath();
        ctx.fillStyle = item.color;
        ctx.moveTo(centerX, centerY);
        ctx.arc(centerX, centerY, radius, startAngle, startAngle + sliceAngle);
        ctx.closePath();
        ctx.fill();

        startAngle += sliceAngle;
      });
    }
  }
};
</script>

注意事项

  • ECharts 方案适合需要丰富交互和动画效果的场景
  • D3.js 方案提供更底层的控制,适合自定义需求
  • Canvas API 方案最轻量,但需要手动处理所有绘制逻辑
  • 响应式设计需要考虑在窗口大小变化时重新绘制图表
  • 大数据量场景下需要注意性能优化

vue实现扇形图

标签: 扇形vue
分享给朋友:

相关文章

vue路由实现内部切换

vue路由实现内部切换

Vue 路由实现内部切换的方法 Vue Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-lin…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &l…

实现.vue文件

实现.vue文件

创建.vue文件的基本结构 一个标准的.vue文件包含三个部分:模板(template)、脚本(script)和样式(style)。这种结构允许开发者在一个文件中管理组件的HTML、JavaScrip…

vue实现标签

vue实现标签

Vue 实现标签的方法 在 Vue 中实现标签功能可以通过多种方式完成,以下是几种常见的方法: 使用动态组件 动态组件可以根据不同的条件渲染不同的标签内容。通过 :is 属性动态绑定组件名,实现标…