当前位置:首页 > VUE

vue实现多个圆环

2026-03-08 10:09:18VUE

Vue 实现多个圆环的方法

在 Vue 中实现多个圆环可以通过 SVG 或 Canvas 实现,以下是两种常见的实现方式:

使用 SVG 绘制圆环

SVG 是一种矢量图形格式,适合绘制圆环等图形。可以通过 vue-svg-circle 或手动实现。

vue实现多个圆环

<template>
  <svg width="200" height="200">
    <circle
      cx="100"
      cy="100"
      :r="radius"
      fill="transparent"
      stroke="#e0e0e0"
      stroke-width="strokeWidth"
    />
    <circle
      cx="100"
      cy="100"
      :r="radius"
      fill="transparent"
      :stroke="color"
      stroke-width="strokeWidth"
      :stroke-dasharray="dashArray"
      stroke-linecap="round"
    />
  </svg>
</template>

<script>
export default {
  props: {
    radius: { type: Number, default: 80 },
    strokeWidth: { type: Number, default: 10 },
    progress: { type: Number, default: 70 },
    color: { type: String, default: "#4CAF50" }
  },
  computed: {
    dashArray() {
      const circumference = 2 * Math.PI * this.radius;
      const dashValue = (this.progress / 100) * circumference;
      return `${dashValue} ${circumference}`;
    }
  }
};
</script>

使用 Canvas 绘制圆环

Canvas 适合动态或复杂的图形渲染,可以通过 Vue 的 ref 和生命周期钩子实现。

vue实现多个圆环

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

<script>
export default {
  props: {
    progress: { type: Number, default: 70 },
    color: { type: String, default: "#4CAF50" }
  },
  mounted() {
    this.drawCircle();
  },
  methods: {
    drawCircle() {
      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.beginPath();
      ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
      ctx.strokeStyle = "#e0e0e0";
      ctx.lineWidth = lineWidth;
      ctx.stroke();

      // 绘制进度圆环
      ctx.beginPath();
      ctx.arc(
        centerX,
        centerY,
        radius,
        -0.5 * Math.PI,
        (2 * Math.PI * this.progress) / 100 - 0.5 * Math.PI
      );
      ctx.strokeStyle = this.color;
      ctx.lineWidth = lineWidth;
      ctx.stroke();
    }
  }
};
</script>

使用第三方库(如 ECharts)

如果需要更复杂的圆环图表,可以使用 ECharts 或 Chart.js 等库。

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

<script>
import * as echarts from "echarts";
export default {
  mounted() {
    const chart = echarts.init(this.$refs.chart);
    chart.setOption({
      series: [
        {
          type: "pie",
          radius: ["60%", "70%"],
          label: { show: false },
          data: [
            { value: 70, name: "Progress", itemStyle: { color: "#4CAF50" } },
            { value: 30, name: "Remaining", itemStyle: { color: "#e0e0e0" } }
          ]
        }
      ]
    });
  }
};
</script>

多个圆环的实现

如果需要多个圆环,可以通过循环或组件复用的方式实现。

<template>
  <div class="circle-container">
    <CircleProgress
      v-for="(item, index) in circles"
      :key="index"
      :progress="item.progress"
      :color="item.color"
    />
  </div>
</template>

<script>
import CircleProgress from "./CircleProgress.vue";
export default {
  components: { CircleProgress },
  data() {
    return {
      circles: [
        { progress: 70, color: "#4CAF50" },
        { progress: 50, color: "#2196F3" },
        { progress: 30, color: "#FFC107" }
      ]
    };
  }
};
</script>

以上方法可以根据需求选择 SVG、Canvas 或第三方库实现多个圆环。

标签: 多个圆环
分享给朋友:

相关文章

vue实现多个页面

vue实现多个页面

Vue 实现多个页面的方法 在 Vue 中实现多个页面通常需要结合 Vue Router 进行路由管理。以下是实现多页面的核心步骤: 安装 Vue Router 通过 npm 或 yarn 安装 V…

react如何返回多个页面

react如何返回多个页面

在React中,返回多个页面通常涉及路由管理和状态控制。以下是几种常见的方法: 使用React Router管理多页面 通过React Router的BrowserRouter和Route组件定义多…

react如何编写多个页面

react如何编写多个页面

如何在React中编写多个页面 React本身是一个单页面应用(SPA)框架,但可以通过路由机制实现多页面效果。以下是实现多页面的几种方法: 使用React Router实现客户端路由 安装Reac…

react如何实现打开多个标签页

react如何实现打开多个标签页

在React中实现打开多个标签页可以通过多种方式实现,以下是一些常见的方法: 使用window.open方法 通过JavaScript的window.open方法可以在新标签页中打开链接。在Reac…

css圆环制作

css圆环制作

使用border-radius和边框制作圆环 通过设置元素的宽高相等,并利用border-radius: 50%将其变为圆形。通过调整边框宽度和颜色,可以形成圆环效果。透明背景或边框颜色差异能突出圆环…

css制作圆环扇形

css制作圆环扇形

使用CSS制作圆环扇形 方法一:使用border-radius和clip-path 通过设置border-radius创建圆形,再利用clip-path裁剪出扇形效果。 .circle-sector…