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>

相关推荐
Nicander1 小时前
我给 Excalidraw 做了一个本地工作区:DrawSpace
前端·开源
linux_cfan3 小时前
17 · 引擎适配器全景:HLS/DASH/Vimeo/Mux/Cast
前端·javascript·音视频
计算机魔术师3 小时前
烧掉2780亿美元还不够?OpenAI的资本豪赌让人头皮发麻
前端
IT_陈寒4 小时前
Java线程池用错参数,我的服务居然悄悄崩溃了
前端·人工智能·后端
卡布鲁4 小时前
React useMemo 与 useCallback 完全指南:原理、场景与避坑
前端·react.js
碳基修炼5 小时前
排查记:本地 devServer 是 http,浏览器却把 302 重定向升级成了 https
前端·http
步行cgn5 小时前
Spring p 命名空间注入详解
java·前端·spring
何何____5 小时前
Vue 生命周期详解
前端·javascript
江米小枣tonylua5 小时前
TypeScript 全面の拥抱 !Prisma 8 + Electron 升级实战
前端
闪耀之光M785 小时前
npm命令解析
前端