当前位置:首页 > VUE

vue实现心电图

2026-02-10 09:05:01VUE

Vue 实现心电图效果

实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘制,以下是一个基于 Vue 和 Canvas 的实现方案:

安装依赖(如需要)

若使用第三方库(如 chart.jsecharts),需先安装:

npm install echarts

基础 Canvas 实现

创建一个 Vue 组件,通过 Canvas 动态绘制心电图折线:

<template>
  <div class="ecg-container">
    <canvas ref="ecgCanvas" :width="width" :height="height"></canvas>
  </div>
</template>

<script>
export default {
  data() {
    return {
      width: 800,
      height: 200,
      dataPoints: [], // 存储心电图数据
      animationId: null,
    };
  },
  mounted() {
    this.initData();
    this.drawECG();
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId);
  },
  methods: {
    initData() {
      // 模拟心电图数据(正弦波 + 脉冲)
      for (let i = 0; i < 200; i++) {
        const x = (i / 10) * Math.PI;
        let y = Math.sin(x) * 20;
        // 模拟QRS波群
        if (i % 50 === 0) y = 60;
        if (i % 50 === 1) y = -40;
        this.dataPoints.push(y);
      }
    },
    drawECG() {
      const canvas = this.$refs.ecgCanvas;
      const ctx = canvas.getContext('2d');
      const { width, height } = this;

      const animate = () => {
        ctx.clearRect(0, 0, width, height);

        // 绘制网格背景
        ctx.strokeStyle = '#eee';
        ctx.lineWidth = 1;
        for (let i = 0; i < width; i += 20) {
          ctx.beginPath();
          ctx.moveTo(i, 0);
          ctx.lineTo(i, height);
          ctx.stroke();
        }
        for (let j = 0; j < height; j += 20) {
          ctx.beginPath();
          ctx.moveTo(0, j);
          ctx.lineTo(width, j);
          ctx.stroke();
        }

        // 绘制心电图
        ctx.strokeStyle = 'red';
        ctx.lineWidth = 2;
        ctx.beginPath();

        const offset = Date.now() / 100 % width;
        for (let i = 0; i < width; i++) {
          const dataIndex = Math.floor((i + offset) % this.dataPoints.length);
          const y = height / 2 - this.dataPoints[dataIndex];
          if (i === 0) ctx.moveTo(i, y);
          else ctx.lineTo(i, y);
        }
        ctx.stroke();

        this.animationId = requestAnimationFrame(animate);
      };
      animate();
    },
  },
};
</script>

<style>
.ecg-container {
  background: #fff;
  border: 1px solid #ddd;
}
</style>

使用 ECharts 实现

若需要更复杂的图表功能,可通过 ECharts 实现:

vue实现心电图

<template>
  <div ref="ecgChart" style="width: 800px; height: 200px;"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      const chart = echarts.init(this.$refs.ecgChart);
      const data = this.generateECGData();

      const option = {
        animation: false,
        grid: { top: 10, right: 10, bottom: 10, left: 10 },
        xAxis: { show: false, type: 'value' },
        yAxis: { show: false, min: -1.5, max: 1.5 },
        series: [{
          type: 'line',
          showSymbol: false,
          data: data,
          lineStyle: { color: 'red', width: 2 },
        }]
      };
      chart.setOption(option);

      // 动态更新数据
      setInterval(() => {
        data.shift();
        data.push(this.generateECGPoint(data.length));
        chart.setOption({ series: [{ data }] });
      }, 100);
    },
    generateECGData() {
      const data = [];
      for (let i = 0; i < 200; i++) {
        data.push(this.generateECGPoint(i));
      }
      return data;
    },
    generateECGPoint(i) {
      // 模拟心电图波形
      const x = i / 10;
      if (i % 50 === 0) return 1.2;
      if (i % 50 === 1) return -1.2;
      return Math.sin(x) * 0.5;
    }
  }
};
</script>

关键点说明

  • 数据生成:通过正弦波模拟基础心律,添加脉冲模拟 QRS 波群。
  • 动态渲染:使用 requestAnimationFrame 或定时器实现动画效果。
  • 性能优化:对于长序列数据,可采用视窗渲染(仅显示可见区域数据)。

扩展功能

  • 添加实时数据接口,连接真实 ECG 设备。
  • 实现缩放、平移等交互功能。
  • 添加心率计算等分析功能。

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

相关文章

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &l…

vue实现画图

vue实现画图

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

vue 实现拖动

vue 实现拖动

Vue 实现拖动的几种方法 在Vue中实现拖动功能可以通过多种方式,包括原生HTML5的拖放API、第三方库如vuedraggable等。以下是几种常见的实现方法: 使用HTML5拖放API HTM…

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…

vue 实现报价

vue 实现报价

Vue 实现报价功能 在 Vue 中实现报价功能通常涉及前端表单交互、数据计算和与后端 API 的通信。以下是实现报价功能的几种常见方法: 表单设计与数据绑定 使用 Vue 的 v-model 指…