当前位置:首页 > VUE

vue实现动态图表

2026-02-24 10:33:31VUE

使用 Vue 和 ECharts 实现动态图表

安装 ECharts 依赖

npm install echarts --save

在 Vue 组件中引入 ECharts

import * as echarts from 'echarts';

创建图表容器

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

初始化图表

vue实现动态图表

export default {
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      const chartDom = this.$refs.chartRef;
      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);
    }
  }
}

实现动态数据更新

添加定时更新数据的方法

methods: {
  // ...其他方法
  updateData() {
    const newData = Array(7).fill(0).map(() => Math.round(Math.random() * 1000));
    this.myChart.setOption({
      series: [{
        data: newData
      }]
    });
  }
},
mounted() {
  this.initChart();
  this.timer = setInterval(this.updateData, 2000);
},
beforeDestroy() {
  clearInterval(this.timer);
  this.myChart.dispose();
}

使用 Vue-ECharts 封装组件

安装 vue-echarts

vue实现动态图表

npm install vue-echarts

创建可复用的图表组件

import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { LineChart } from 'echarts/charts';
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  GridComponent
} from 'echarts/components';
import VChart from 'vue-echarts';

use([
  CanvasRenderer,
  LineChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  GridComponent
]);

export default {
  components: {
    VChart
  },
  props: {
    chartData: {
      type: Array,
      required: true
    }
  },
  computed: {
    options() {
      return {
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: this.chartData,
          type: 'line'
        }]
      };
    }
  }
};

响应式图表实现

监听窗口大小变化

mounted() {
  window.addEventListener('resize', this.handleResize);
  this.handleResize();
},
methods: {
  handleResize() {
    this.myChart && this.myChart.resize();
  }
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
}

多图表类型支持

配置不同图表类型的选项

const option = {
  tooltip: {},
  legend: {
    data: ['销量']
  },
  xAxis: {
    data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
  },
  yAxis: {},
  series: [
    {
      name: '销量',
      type: 'bar', // 可改为 'pie', 'line', 'scatter' 等
      data: [5, 20, 36, 10, 10, 20]
    }
  ]
};

标签: 图表动态
分享给朋友:

相关文章

vue实现多个图表展示

vue实现多个图表展示

使用 Vue 实现多个图表展示 安装图表库 推荐使用 ECharts 或 Chart.js,两者均支持 Vue 集成。以 ECharts 为例,安装依赖: npm install echarts…

vue实现动态表单

vue实现动态表单

Vue 动态表单实现方法 使用 v-for 动态渲染表单字段 通过遍历数组动态生成表单项,数据驱动视图变化。表单字段配置可存储在数组中,修改数组即可实现表单动态更新。 <template>…

vue实现动态卡片

vue实现动态卡片

实现动态卡片的基本思路 在Vue中实现动态卡片通常涉及数据绑定、循环渲染和交互逻辑。动态卡片的内容、样式或数量可以根据数据变化而更新。 数据驱动的卡片列表 使用v-for指令循环渲染卡片,数据存储在…

vue 实现动态连线

vue 实现动态连线

Vue 实现动态连线的方法 动态连线通常用于可视化工具、流程图或关系图中,Vue 结合 SVG 或 Canvas 可以高效实现这一功能。 使用 SVG 实现动态连线 SVG 的 <line&g…

vue实现动态修改代码

vue实现动态修改代码

Vue 动态修改代码的实现方法 Vue 提供了多种方式实现动态修改代码的需求,以下是几种常见场景和解决方案: 动态组件加载 使用 Vue 的 <component> 标签配合 is 属性…

vue动态大屏实现

vue动态大屏实现

Vue动态大屏实现方法 响应式布局与适配 使用CSS的vw、vh单位结合flex或grid布局实现基础适配。通过监听窗口变化动态调整根字体大小,结合rem实现等比缩放: // 在main.js或Ap…