当前位置:首页 > VUE

vue实现环形进度

2026-01-16 18:39:27VUE

Vue 实现环形进度条的方法

使用 SVG 和 CSS 实现

在 Vue 中创建一个环形进度条可以通过 SVG 和 CSS 结合实现。以下是一个简单的实现方式:

<template>
  <div class="circle-progress">
    <svg viewBox="0 0 36 36" class="circular-chart">
      <path
        class="circle-bg"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <path
        class="circle"
        :stroke-dasharray="`${progress}, 100`"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
    </svg>
    <div class="progress-text">{{ progress }}%</div>
  </div>
</template>

<script>
export default {
  props: {
    progress: {
      type: Number,
      default: 0,
      validator: value => value >= 0 && value <= 100
    }
  }
}
</script>

<style>
.circle-progress {
  position: relative;
  width: 100px;
  height: 100px;
}

.circular-chart {
  display: block;
  width: 100%;
  height: 100%;
}

.circle-bg {
  fill: none;
  stroke: #eee;
  stroke-width: 3;
}

.circle {
  fill: none;
  stroke: #4CAF50;
  stroke-width: 3;
  stroke-linecap: round;
  animation: progress 1s ease-out forwards;
}

.progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 1.2em;
  font-weight: bold;
}

@keyframes progress {
  0% {
    stroke-dasharray: 0, 100;
  }
}
</style>

使用第三方库

如果需要更复杂的功能,可以考虑使用专门的可视化库:

vue实现环形进度

npm install vue-css-donut-chart

使用示例:

vue实现环形进度

<template>
  <donut-chart
    :percent="progress"
    :size="100"
    :thickness="10"
    foreground-color="#4CAF50"
    background-color="#eee"
  />
</template>

<script>
import DonutChart from 'vue-css-donut-chart'

export default {
  components: {
    DonutChart
  },
  props: {
    progress: Number
  }
}
</script>

使用 Canvas 实现

对于需要动态绘制的情况,可以使用 Canvas:

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

<script>
export default {
  props: {
    progress: Number
  },
  mounted() {
    this.drawProgress()
  },
  watch: {
    progress() {
      this.drawProgress()
    }
  },
  methods: {
    drawProgress() {
      const canvas = this.$refs.canvas
      const ctx = canvas.getContext('2d')
      const centerX = canvas.width / 2
      const centerY = canvas.height / 2
      const radius = 80
      const lineWidth = 10

      // 清除画布
      ctx.clearRect(0, 0, canvas.width, canvas.height)

      // 绘制背景圆
      ctx.beginPath()
      ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI)
      ctx.lineWidth = lineWidth
      ctx.strokeStyle = '#eee'
      ctx.stroke()

      // 绘制进度圆
      ctx.beginPath()
      const endAngle = (2 * Math.PI * this.progress / 100) - (Math.PI / 2)
      ctx.arc(centerX, centerY, radius, -Math.PI / 2, endAngle)
      ctx.lineWidth = lineWidth
      ctx.strokeStyle = '#4CAF50'
      ctx.stroke()
    }
  }
}
</script>

实现动画效果

为环形进度条添加平滑的动画效果:

<template>
  <div class="animated-progress">
    <svg viewBox="0 0 36 36">
      <path
        class="track"
        d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <path
        class="fill"
        :style="{ 'stroke-dashoffset': dashOffset }"
        d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831"
      />
    </svg>
  </div>
</template>

<script>
export default {
  props: {
    progress: {
      type: Number,
      default: 0
    }
  },
  computed: {
    dashOffset() {
      return 100 - this.progress
    }
  }
}
</script>

<style>
.animated-progress {
  width: 100px;
  height: 100px;
}

.track {
  fill: none;
  stroke: #eee;
  stroke-width: 3;
}

.fill {
  fill: none;
  stroke: #4CAF50;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-dasharray: 100;
  transition: stroke-dashoffset 0.5s ease-out;
}
</style>

这些方法提供了不同复杂度的环形进度条实现方式,可以根据项目需求选择最适合的方案。SVG 方法通常是最灵活和响应式的,而 Canvas 方法则适合需要复杂动态绘制的场景。第三方库可以快速实现但可能带来额外的依赖。

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

相关文章

vue实现文件进度

vue实现文件进度

Vue 实现文件上传进度 使用 Vue 实现文件上传进度可以通过结合 axios 或原生 XMLHttpRequest 的进度事件来实现。以下是两种常见方法: 使用 axios 上传文件并显示进度…

php实现快递进度

php实现快递进度

实现快递进度查询的PHP方法 使用快递鸟API 快递鸟提供物流查询API接口,支持多家快递公司。需注册账号获取API Key和商户ID。 // 配置参数 $appKey = '你的AppKe…

vue前端实现下载进度

vue前端实现下载进度

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

css制作环形

css制作环形

使用CSS制作环形 制作环形有多种方法,以下是几种常见且实用的CSS实现方式: 使用border-radius和border属性 通过设置元素的border-radius为50%使其变为圆形,再通过…

css环形图制作

css环形图制作

使用 CSS 和 SVG 制作环形图 CSS 结合 SVG 可以轻松实现环形图效果。以下是一个简单的方法: <svg viewBox="0 0 100 100" class="donut"&g…

php实现快递进度

php实现快递进度

PHP实现快递进度查询 方法一:使用快递100 API 快递100提供了免费的快递查询API接口,可以通过PHP调用获取快递进度信息。 注册快递100开发者账号,获取API Key。 使用P…