当前位置:首页 > VUE

vue实现echarts波动效果

2026-02-22 02:14:22VUE

实现基础图表

在Vue项目中安装ECharts依赖:

npm install echarts --save

引入ECharts并初始化基础折线图:

import * as echarts from 'echarts';

export default {
  mounted() {
    const chartDom = this.$refs.chart;
    const myChart = echarts.init(chartDom);

    const option = {
      xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      },
      yAxis: {
        type: 'value'
      },
      series: [{
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        type: 'line'
      }]
    };

    myChart.setOption(option);
  }
}

添加波动动画效果

使用ECharts的animation配置实现基础动画:

vue实现echarts波动效果

series: [{
  data: [820, 932, 901, 934, 1290, 1330, 1320],
  type: 'line',
  smooth: true,
  animation: true,
  animationDuration: 2000,
  animationEasing: 'bounceOut'
}]

实现数据波动更新

创建定时更新数据的逻辑:

data() {
  return {
    chartData: [820, 932, 901, 934, 1290, 1330, 1320],
    myChart: null
  }
},
methods: {
  updateData() {
    this.chartData = this.chartData.map(
      item => item + (Math.random() * 200 - 100)
    );
    this.myChart.setOption({
      series: [{
        data: this.chartData
      }]
    });
  }
},
mounted() {
  this.myChart = echarts.init(this.$refs.chart);
  setInterval(this.updateData, 2000);
}

高级波动效果配置

实现更复杂的波浪效果:

vue实现echarts波动效果

series: [{
  type: 'line',
  showSymbol: false,
  data: this.chartData,
  smooth: true,
  lineStyle: {
    width: 3,
    color: '#5470C6'
  },
  areaStyle: {
    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
      { offset: 0, color: 'rgba(84, 112, 198, 0.5)' },
      { offset: 1, color: 'rgba(84, 112, 198, 0.1)' }
    ])
  },
  animationDurationUpdate: 1000,
  animationEasingUpdate: 'cubicInOut'
}]

性能优化建议

对于高频更新的图表,建议启用增量渲染:

setOption(newOption, {
  notMerge: false,
  lazyUpdate: true
});

添加resize监听确保响应式:

window.addEventListener('resize', () => {
  this.myChart.resize();
});

完整组件示例

<template>
  <div ref="chart" style="width: 600px; height: 400px;"></div>
</template>

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

export default {
  data() {
    return {
      chartData: [820, 932, 901, 934, 1290, 1330, 1320],
      myChart: null
    }
  },
  methods: {
    updateData() {
      this.chartData = this.chartData.map(
        item => Math.max(200, item + (Math.random() * 200 - 100))
      );
      this.myChart.setOption({
        series: [{
          data: this.chartData
        }]
      });
    },
    initChart() {
      const option = {
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: 'value',
          min: 0
        },
        series: [{
          data: this.chartData,
          type: 'line',
          smooth: true,
          animation: true,
          animationDurationUpdate: 1000,
          lineStyle: {
            width: 3
          },
          areaStyle: {
            opacity: 0.3
          }
        }]
      };

      this.myChart.setOption(option);
      setInterval(this.updateData, 2000);
    }
  },
  mounted() {
    this.myChart = echarts.init(this.$refs.chart);
    this.initChart();
  },
  beforeDestroy() {
    if (this.myChart) {
      this.myChart.dispose();
    }
  }
}
</script>

标签: 效果vue
分享给朋友:

相关文章

vue实现收起展开面板

vue实现收起展开面板

实现收起展开面板的方法 在Vue中实现收起展开面板功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起:…

vue 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue 实现表单

vue 实现表单

Vue 表单实现方法 Vue.js 提供了多种方式实现表单,包括双向数据绑定、表单验证和动态表单生成。以下是常见的实现方法: 基础表单绑定 使用 v-model 指令实现表单元素与数据的双向绑定:…

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template&…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现快手

vue实现快手

Vue 实现类似快手的功能 Vue.js 是一个渐进式 JavaScript 框架,适合构建复杂的单页应用。要实现类似快手的功能,可以结合 Vue 和相关技术栈进行开发。 核心功能模块 视频流展…