当前位置:首页 > VUE

vue实现svg心电图

2026-02-19 17:11:59VUE

Vue 中实现 SVG 心电图

使用 SVG 路径绘制动态心电图

在 Vue 中可以通过 <svg><path> 元素实现动态心电图效果。关键点在于动态更新路径的 d 属性,模拟心电图波形。

<template>
  <div class="ecg-container">
    <svg width="100%" height="100" viewBox="0 0 500 100">
      <path 
        :d="pathData" 
        fill="none" 
        stroke="#FF0000" 
        stroke-width="2"
      />
    </svg>
  </div>
</template>

<script>
export default {
  data() {
    return {
      points: [],
      maxPoints: 50,
      amplitude: 20,
      frequency: 0.1
    }
  },
  computed: {
    pathData() {
      if (this.points.length === 0) return ''

      let d = `M ${this.points[0].x} ${this.points[0].y}`
      for (let i = 1; i < this.points.length; i++) {
        d += ` L ${this.points[i].x} ${this.points[i].y}`
      }
      return d
    }
  },
  mounted() {
    this.generatePoints()
    setInterval(this.updatePoints, 100)
  },
  methods: {
    generatePoints() {
      for (let i = 0; i < this.maxPoints; i++) {
        this.points.push({
          x: i * (500 / this.maxPoints),
          y: 50 + Math.sin(i * this.frequency) * this.amplitude
        })
      }
    },
    updatePoints() {
      // 移除第一个点
      this.points.shift()

      // 添加新点
      const lastX = this.points[this.points.length - 1].x
      this.points.push({
        x: lastX + (500 / this.maxPoints),
        y: 50 + Math.sin(lastX * this.frequency) * this.amplitude
      })
    }
  }
}
</script>

<style>
.ecg-container {
  width: 100%;
  background: #f0f0f0;
  padding: 20px;
}
</style>

使用 SVG 动画增强效果

可以通过 SVG 的动画特性使心电图看起来更真实:

vue实现svg心电图

<template>
  <svg width="100%" height="100" viewBox="0 0 500 100">
    <defs>
      <linearGradient id="gradient" x1="0%" y1="0%" x2="100%" y2="0%">
        <stop offset="0%" stop-color="#FF0000" />
        <stop offset="100%" stop-color="#FF0000" stop-opacity="0" />
      </linearGradient>
    </defs>

    <path 
      :d="pathData" 
      fill="none" 
      stroke="url(#gradient)" 
      stroke-width="2"
    >
      <animate 
        attributeName="stroke-dashoffset"
        values="500;0" 
        dur="5s" 
        repeatCount="indefinite"
      />
    </path>
  </svg>
</template>

使用第三方库优化性能

对于更复杂的心电图效果,可以考虑使用专门的图表库:

  1. 安装 Chart.js 和 vue-chartjs:

    vue实现svg心电图

    npm install chart.js vue-chartjs
  2. 创建心电图组件:

    
    import { Line } from 'vue-chartjs'

export default { extends: Line, data() { return { chartData: { labels: Array(100).fill(''), datasets: [{ label: 'ECG', borderColor: '#FF0000', pointRadius: 0, borderWidth: 2, data: Array(100).fill(0) }] }, options: { responsive: true, maintainAspectRatio: false, animation: { duration: 0 }, scales: { xAxes: [{ display: false }], yAxes: [{ display: false, ticks: { min: -100, max: 100 } }] } } } }, mounted() { this.renderChart(this.chartData, this.options) setInterval(this.updateChart, 100) }, methods: { updateChart() { const newData = this.chartData.datasets[0].data.slice(1) newData.push(Math.random() * 100 - 50) this.chartData.datasets[0].data = newData this.$data._chart.update() } } }


#### 真实心电图数据模拟

要模拟更真实的心电图波形,可以使用数学函数组合:

```javascript
function generateECGPoint(x) {
  // 基础心率
  const base = Math.sin(x * 0.1) * 10

  // P波
  const pWave = x % 50 < 5 ? Math.sin((x % 50) * 0.6) * 8 : 0

  // QRS复合波
  let qrs = 0
  if (x % 50 >= 5 && x % 50 < 15) {
    const pos = (x % 50) - 5
    if (pos < 2) qrs = -pos * 15
    else if (pos < 4) qrs = -30 + (pos-2) * 25
    else if (pos < 6) qrs = 20 - (pos-4) * 10
    else qrs = 0
  }

  // T波
  const tWave = x % 50 >= 15 && x % 50 < 25 ? 
    Math.sin((x % 50 - 15) * 0.3) * 5 : 0

  return base + pWave + qrs + tWave
}

这些方法可以根据实际需求进行调整和组合,实现不同复杂度的心电图效果。

标签: 心电图vue
分享给朋友:

相关文章

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成列…

vue datepicker 实现

vue datepicker 实现

实现 Vue Datepicker 的基本方法 在 Vue 项目中实现日期选择功能,可以使用第三方库如 vue-datepicker 或 v-calendar。以下是两种常见实现方式: 安装 vu…

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…

vue实现tag

vue实现tag

Vue 实现标签(Tag)功能 在 Vue 中实现标签(Tag)功能可以通过多种方式完成,以下是一些常见的方法和实现步骤: 使用动态组件和 v-for 通过 v-for 指令动态渲染标签列表,结合…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…