当前位置:首页 > 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 实现多图片预览功能 使用第三方库(推荐) 安装 viewerjs 或 vue-photo-preview 这类专门处理图片预览的库。 npm install viewerjs 在组件中引入并…

react如何渲染多个页面

react如何渲染多个页面

渲染多个页面的方法 在React中渲染多个页面通常涉及路由配置和组件组织。以下是几种常见方法: 使用React Router React Router是处理多页面应用最流行的库。安装后配置路由:…

react多个单击事件如何切换

react多个单击事件如何切换

切换多个单击事件的实现方法 在React中切换多个单击事件可以通过状态管理或条件渲染来实现。以下是几种常见的方法: 使用状态变量控制事件切换 通过维护一个状态变量来记录当前激活的事件类型,根据状态…

php实现多个网站同步

php实现多个网站同步

PHP实现多个网站同步的方法 使用PHP实现多个网站的数据同步可以通过多种方式实现,以下是几种常见的方法: 数据库同步 通过共享数据库或数据库复制技术实现数据同步。多个网站连接到同一个数据库,或使用…

uniapp多个吸顶

uniapp多个吸顶

实现多个吸顶效果的方法 在UniApp中实现多个吸顶效果,可以通过以下方式完成: 使用uni-sticky组件 UniApp官方提供了uni-sticky组件,可以轻松实现吸顶效果。对于多个…

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 使用动态组件切换 通过 Vue 的 <component> 和 v-bind:is 动态切换组件实现 Tab 内容切换。定义一个数组存储 Tab 数据,使…