当前位置:首页 > 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 的响应式特性动态更新扇形图:

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 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直接…

vue路由实现

vue路由实现

Vue 路由实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的基本实现步骤和核心功能。 安装 Vue Router 通过…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

vue实现频道

vue实现频道

Vue实现频道功能的方法 使用Vue Router动态路由 通过Vue Router的动态路由功能可以轻松实现频道切换。在路由配置中定义动态参数,组件内通过this.$route.params获取当前…

vue实现flvvideo

vue实现flvvideo

vue-flv-player 实现 FLV 视频播放 安装依赖包 npm install flv.js vue-flv-player 基础组件引入 <template> <v…

vue实现mui

vue实现mui

使用 Vue 实现 MUI(Material-UI)风格的界面 Vue 本身不直接提供 MUI(Material Design)组件,但可以通过集成第三方库或手动实现来达到类似效果。以下是几种常见方法…