当前位置:首页 > VUE

vue实现扇形图

2026-01-18 01:40:15VUE

Vue 实现扇形图的方法

使用 SVG 和 Vue 动态计算路径

通过 SVG 的 path 元素和 Vue 的计算属性动态生成扇形路径。以下是一个基础实现示例:

<template>
  <svg width="200" height="200" viewBox="0 0 100 100">
    <path 
      :d="sectorPath(50, 50, 40, 0, 90)" 
      fill="#FF6384"
    />
  </svg>
</template>

<script>
export default {
  methods: {
    sectorPath(cx, cy, r, startAngle, endAngle) {
      const startRad = (startAngle * Math.PI) / 180;
      const endRad = (endAngle * Math.PI) / 180;
      const x1 = cx + r * Math.cos(startRad);
      const y1 = cy + r * Math.sin(startRad);
      const x2 = cx + r * Math.cos(endRad);
      const y2 = cy + r * Math.sin(endRad);

      return `M ${cx} ${cy} L ${x1} ${y1} A ${r} ${r} 0 0 1 ${x2} ${y2} Z`;
    }
  }
};
</script>

使用第三方图表库(如 ECharts)

安装 ECharts 和 Vue-ECharts 封装库:

npm install echarts vue-echarts

示例代码:

<template>
  <v-chart :option="chartOption" style="height: 400px"/>
</template>

<script>
import { use } from 'echarts/core';
import { PieChart } from 'echarts/charts';
import { CanvasRenderer } from 'echarts/renderers';
import VChart from 'vue-echarts';

use([PieChart, CanvasRenderer]);

export default {
  components: { VChart },
  data() {
    return {
      chartOption: {
        series: [{
          type: 'pie',
          radius: ['50%', '70%'],
          data: [
            { value: 35, name: 'Category A' },
            { value: 25, name: 'Category B' }
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }]
      }
    };
  }
};
</script>

使用 Canvas 绘制

通过 Vue 的生命周期钩子和 Canvas API 实现:

<template>
  <canvas ref="pieCanvas" width="300" height="300"></canvas>
</template>

<script>
export default {
  mounted() {
    this.drawPieChart();
  },
  methods: {
    drawPieChart() {
      const canvas = this.$refs.pieCanvas;
      const ctx = canvas.getContext('2d');
      const data = [30, 70];
      const colors = ['#FF6384', '#36A2EB'];

      let startAngle = 0;
      data.forEach((value, i) => {
        const sliceAngle = (2 * Math.PI * value) / 100;
        ctx.beginPath();
        ctx.fillStyle = colors[i];
        ctx.moveTo(150, 150);
        ctx.arc(150, 150, 100, startAngle, startAngle + sliceAngle);
        ctx.closePath();
        ctx.fill();
        startAngle += sliceAngle;
      });
    }
  }
};
</script>

响应式数据处理

结合 Vue 的响应式特性动态更新扇形图:

<template>
  <div>
    <svg width="200" height="200">
      <path 
        v-for="(item, index) in sectors" 
        :key="index"
        :d="item.path" 
        :fill="item.color"
      />
    </svg>
    <button @click="updateData">Update Data</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [45, 30, 25],
      colors: ['#FF6384', '#36A2EB', '#FFCE56']
    };
  },
  computed: {
    sectors() {
      let total = this.data.reduce((a, b) => a + b, 0);
      let startAngle = 0;
      return this.data.map((value, i) => {
        const angle = (value / total) * 360;
        const path = this.sectorPath(100, 100, 80, startAngle, startAngle + angle);
        startAngle += angle;
        return { path, color: this.colors[i] };
      });
    }
  },
  methods: {
    sectorPath(cx, cy, r, startAngle, endAngle) {
      // 同第一个示例的 path 计算方法
    },
    updateData() {
      this.data = this.data.map(() => Math.floor(Math.random() * 50) + 10);
    }
  }
};
</script>

关键注意事项

  • SVG 方案适合轻量级定制需求,但需要手动计算几何路径
  • ECharts 等库提供丰富的交互功能(如 tooltip、动画),适合复杂场景
  • Canvas 方案性能较好,但需要手动处理分辨率适配
  • 动态数据更新时,确保通过 Vue 的响应式系统触发重新渲染

vue实现扇形图

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

相关文章

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed: {…

vue实现全局遮罩层

vue实现全局遮罩层

实现全局遮罩层的方法 在Vue中实现全局遮罩层可以通过多种方式完成,以下是几种常见的实现方法: 使用Vue组件创建遮罩层 创建一个遮罩层组件,通过全局注册或动态挂载的方式实现全局调用。以下是一个简单…

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现发送值接受值

vue实现发送值接受值

Vue 实现父子组件传值 父组件向子组件传值(props) 父组件通过 v-bind 或简写 : 向子组件传递数据。子组件通过 props 接收数据。 父组件代码: <template&g…

用vue实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…