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>

相关推荐
Felven8 小时前
C. Make Them Equal
c语言·开发语言·前端
智能收银研究所11 小时前
收银系统选型对比:美团、客如云、拉卡拉、收钱吧各适合什么门店
java·前端·数据库
滑动变滚动的蜗牛14 小时前
我发布了自己的个人小程序(诉述洞)
前端·ai编程·全栈
ZYH_060114 小时前
Shell 第一阶段笔记:变量、引号与变量截取
前端·chrome·笔记
YIAN15 小时前
流式 TTS 和普通TTS到底有什么区别?一个文档教会你!
前端·websocket·node.js
掘金安东尼16 小时前
智能体包办工作时,批判性思维怎么办(Addy Osmani 全文转译)
前端·程序员·agent
SameX18 小时前
HarmonyOS ArkTS Canvas 画环形进度条 animateTo + @Watch 驱动重绘 120 行实现
前端
欧雷殿19 小时前
我的 AI 外包实战:用数字员工做项目的一人公司
前端·ai编程·团队管理
狗头大军之江苏分军19 小时前
《潮水漫过十七岁》 补一下,运动会
前端·javascript·后端
狗头大军之江苏分军19 小时前
《潮水漫过十七岁》书都带回去
前端·javascript·后端