VUE3 ECharts5 实现动态柱状图(附源码和效果)

javascript 复制代码
<template>
  <div id="main" class="echart-style">
  </div>
</template>

<script setup lang="ts">
import * as echarts from 'echarts';
import { onMounted, ref } from 'vue';
let myChart = ref()
let option = ref({})
let data = ref<any>([])
for (let i = 0; i < 5; ++i) {
    data.value.push(Math.round(Math.random() * 200));
  }
onMounted(() => {
  init()
})
const init = () => { 
  // 基于准备好的dom,初始化echarts实例
  myChart.value = echarts.init(document.getElementById('main'));
  // 绘制图表
  option.value = {
    xAxis: {
      max: 'dataMax'
    },
    yAxis: {
      type: 'category',
      data: ['A', 'B', 'C', 'D', 'E'],
      inverse: true,
      animationDuration: 300,
      animationDurationUpdate: 300,
      max:5// only the largest 3 bars will be displayed
    },
    series: [
      {
        realtimeSort: true,
        name: 'X',
        type: 'bar',
        data: data.value,
        label: {
          show: true,
          position: 'right',
          valueAnimation: true
        }
      }
    ],
    legend: {
      show: true
    },
    animationDuration: 3000,
    animationDurationUpdate: 3000,
    animationEasing: 'linear',
    animationEasingUpdate: 'linear'

  };
  myChart.value.setOption(option.value)
};
function update() {
 data.value = option.value.series[0].data;
  for (var i = 0; i < data.value.length; ++i) {
    if (Math.random() > 0.9) {
      data.value[i] += Math.round(Math.random() * 2000);
    } else {
      data.value[i] += Math.round(Math.random() * 200);
    }
  }
  init()
}
setInterval(function () {
  update();
}, 3000);
</script>
<style scoped>
.echart-style {
  width: 1000px;
  height: 900px;
  background: skyblue;
}
</style>

相关推荐
Linux运维技术栈3 分钟前
Cloudflare Argo Smart Routing全球加速:优化跨境回源链路,提升跨区域访问体验
大数据·前端·数据库
恋猫de小郭36 分钟前
Android CLI ,谷歌为 Android 开发者专研的 AI Agent,提速三倍
android·前端·flutter
freewlt37 分钟前
从 0 搭建现代前端组件库:2026年完整实战指南
前端
凌冰_42 分钟前
Thymeleaf 核心语法详解
java·前端·javascript
AIBox36544 分钟前
claude 镜像 api 使用指南(2026 年4 月更新)
java·服务器·前端·人工智能·gpt·前端框架
SuperEugene1 小时前
Vue3 配置文件管理:按模块拆分配置,提升配置可维护性|配置驱动开发实战篇
前端·javascript·vue.js·驱动开发
阿凤211 小时前
后端返回文件二进制流
开发语言·前端·javascript·uniapp
落魄江湖行1 小时前
进阶篇四 Nuxt4 Server Routes:写后端 API
前端·vue.js·typescript·nuxt4
萧行之1 小时前
解决Microsoft Edge/Hotmail登录报错(15/25/2603、0x80190001)
前端·microsoft·edge
Eiceblue1 小时前
C# 删除 PDF 页面:单页 / 多页批量删除技巧
前端·pdf·c#