当前位置:首页 > VUE

vue实现扇形进度

2026-01-16 03:03:26VUE

Vue 实现扇形进度条

使用 SVG 绘制扇形

SVG 的 path 元素可以用于绘制扇形。通过计算扇形的路径数据,结合 Vue 的动态数据绑定实现进度变化。

<template>
  <svg width="200" height="200" viewBox="0 0 100 100">
    <path :d="getSectorPath(50, 50, 40, 0, endAngle)" fill="#4CAF50"/>
  </svg>
</template>

<script>
export default {
  data() {
    return {
      progress: 75, // 进度百分比 (0-100)
    };
  },
  computed: {
    endAngle() {
      return (this.progress / 100) * 2 * Math.PI;
    },
  },
  methods: {
    getSectorPath(cx, cy, r, startAngle, endAngle) {
      const x1 = cx + r * Math.cos(startAngle);
      const y1 = cy + r * Math.sin(startAngle);
      const x2 = cx + r * Math.cos(endAngle);
      const y2 = cy + r * Math.sin(endAngle);
      const largeArcFlag = endAngle - startAngle <= Math.PI ? 0 : 1;
      return `M ${cx} ${cy} L ${x1} ${y1} A ${r} ${r} 0 ${largeArcFlag} 1 ${x2} ${y2} Z`;
    },
  },
};
</script>

使用 Canvas 绘制扇形

通过 Canvas 的 arc 方法绘制扇形,结合 Vue 的响应式特性更新绘制。

vue实现扇形进度

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

<script>
export default {
  data() {
    return {
      progress: 75,
    };
  },
  mounted() {
    this.drawSector();
  },
  watch: {
    progress() {
      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 = 80;
      const startAngle = 0;
      const endAngle = (this.progress / 100) * 2 * Math.PI;

      ctx.clearRect(0, 0, canvas.width, canvas.height);
      ctx.beginPath();
      ctx.moveTo(centerX, centerY);
      ctx.arc(centerX, centerY, radius, startAngle, endAngle);
      ctx.closePath();
      ctx.fillStyle = '#4CAF50';
      ctx.fill();
    },
  },
};
</script>

使用第三方库

可以借助第三方库如 vue-awesome-progressecharts 快速实现扇形进度条。

vue实现扇形进度

使用 echarts 的示例:

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

<script>
import * as echarts from 'echarts';
export default {
  data() {
    return {
      progress: 75,
    };
  },
  mounted() {
    this.initChart();
  },
  watch: {
    progress() {
      this.initChart();
    },
  },
  methods: {
    initChart() {
      const chart = echarts.init(this.$refs.chart);
      chart.setOption({
        series: [{
          type: 'pie',
          radius: ['60%', '80%'],
          avoidLabelOverlap: false,
          itemStyle: {
            borderRadius: 0,
            borderColor: '#fff',
            borderWidth: 2
          },
          label: {
            show: false,
          },
          data: [
            { value: this.progress, name: 'Progress' },
            { value: 100 - this.progress, name: 'Remaining' }
          ]
        }]
      });
    },
  },
};
</script>

动画效果

通过 CSS 或 JavaScript 为扇形进度条添加动画效果,提升用户体验。

// 在 drawSector 方法中添加动画
drawSector() {
  const canvas = this.$refs.canvas;
  const ctx = canvas.getContext('2d');
  const centerX = canvas.width / 2;
  const centerY = canvas.height / 2;
  const radius = 80;
  let currentAngle = 0;
  const targetAngle = (this.progress / 100) * 2 * Math.PI;

  const animate = () => {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.beginPath();
    ctx.moveTo(centerX, centerY);
    ctx.arc(centerX, centerY, radius, 0, currentAngle);
    ctx.closePath();
    ctx.fillStyle = '#4CAF50';
    ctx.fill();

    if (currentAngle < targetAngle) {
      currentAngle += 0.05;
      requestAnimationFrame(animate);
    }
  };
  animate();
}

标签: 扇形进度
分享给朋友:

相关文章

vue实现进度审批

vue实现进度审批

Vue 实现进度审批功能 进度审批功能通常涉及表单提交、状态跟踪和交互反馈。以下是实现的基本思路和代码示例。 审批流程数据结构设计 使用对象数组存储审批步骤,每个步骤包含状态、审批人等信息: da…

vue实现环形进度组件

vue实现环形进度组件

实现环形进度组件 环形进度组件可以通过SVG或Canvas实现,以下是基于SVG的实现方法: 定义组件模板 <template> <div class="circ…

vue前端实现下载进度

vue前端实现下载进度

实现下载进度显示的方法 在Vue中实现下载进度显示,可以通过监听XMLHttpRequest或fetch的progress事件来获取下载进度数据。以下是具体实现方式: 使用XMLHttpReques…

css制作扇形图

css制作扇形图

使用CSS制作扇形图 利用conic-gradient实现基础扇形 通过CSS的conic-gradient可以快速创建扇形效果。定义一个圆形元素并应用角度渐变色: .pie-chart { w…

php如何实现下载进度

php如何实现下载进度

使用 cURL 获取下载进度 在 PHP 中,可以通过 cURL 的 CURLOPT_PROGRESSFUNCTION 和 CURLOPT_NOPROGRESS 选项来实现下载进度跟踪。需要启用进度回…

php下载进度实现

php下载进度实现

PHP 下载进度实现 在 PHP 中实现下载进度显示通常需要结合前端和后端技术,以下是一种常见的实现方法: 使用 PHP 的 readfile() 或 fread() 结合 JavaScript 这…