当前位置:首页 > VUE

vue实现扇形图

2026-03-10 07:22:20VUE

使用 SVG 绘制扇形图

在 Vue 中可以通过 SVG 的 <path> 元素绘制扇形。计算扇形的路径需要用到极坐标公式,结合 d 属性生成路径数据。

计算公式

  • 扇形路径由圆心、半径、起始角度和结束角度决定。
  • 路径命令 M(移动到起点)、A(画弧)、L(画线到圆心)、Z(闭合路径)。

示例代码

<template>
  <svg width="200" height="200" viewBox="0 0 200 200">
    <path :d="getSectorPath(100, 100, 80, 0, 90)" fill="#42b983" />
    <path :d="getSectorPath(100, 100, 80, 90, 180)" fill="#ff7e67" />
  </svg>
</template>

<script>
export default {
  methods: {
    getSectorPath(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);
      const largeArcFlag = endAngle - startAngle <= 180 ? 0 : 1;
      return `M ${cx} ${cy} L ${x1} ${y1} A ${r} ${r} 0 ${largeArcFlag} 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 { TitleComponent, TooltipComponent } from "echarts/components";
import VChart from "vue-echarts";
use([PieChart, TitleComponent, TooltipComponent]);

export default {
  components: { VChart },
  data() {
    return {
      chartOption: {
        title: { text: "扇形图示例" },
        series: [
          {
            type: "pie",
            radius: ["50%", "70%"],
            data: [
              { value: 40, name: "A" },
              { value: 30, name: "B" },
              { value: 20, name: "C" },
            ],
          },
        ],
      },
    };
  },
};
</script>

使用 D3.js 动态生成扇形图

D3.js 适合需要高度自定义的场景,结合 Vue 实现动态数据绑定。

示例代码

vue实现扇形图

<template>
  <svg ref="chart" width="200" height="200"></svg>
</template>

<script>
import * as d3 from "d3";

export default {
  mounted() {
    this.drawChart();
  },
  methods: {
    drawChart() {
      const data = [30, 70];
      const svg = d3.select(this.$refs.chart);
      const pie = d3.pie()(data);
      const arc = d3
        .arc()
        .innerRadius(0)
        .outerRadius(80);

      svg
        .selectAll("path")
        .data(pie)
        .enter()
        .append("path")
        .attr("d", arc)
        .attr("fill", (d, i) => (i === 0 ? "#42b983" : "#ff7e67"))
        .attr("transform", "translate(100, 100)");
    },
  },
};
</script>

注意事项

  • 性能优化:大数据量时考虑使用 Canvas(如 ECharts)替代 SVG。
  • 响应式设计:通过监听窗口大小变化动态调整图表尺寸。
  • 动画效果:ECharts 或 D3.js 支持过渡动画,增强用户体验。

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

相关文章

vue实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

vue nexttrick实现

vue nexttrick实现

Vue 中 nextTick 的实现原理 nextTick 是 Vue 提供的一个异步方法,用于在下次 DOM 更新循环结束之后执行延迟回调。它利用了 JavaScript 的事件循环机制,确保回调在…