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

<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>

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基于 VueEcharts动态柱状图示例: ```html <template> <div id="myChart" style="width: 600px;height:400px;"></div> </template> <script> import * as echarts from 'echarts'; export default { data() { return { chartData: [], // 存储柱状图的数据 chartTimer: null // 定时器 }; }, mounted() { // 初始化 echarts 实例 this.myChart = echarts.init(document.getElementById('myChart')); // 显示初始的柱状图 this.showChart(); // 每隔 1 秒更新一次柱状图 this.chartTimer = setInterval(() => { this.showChart(); }, 1000); }, methods: { // 显示柱状图 showChart() { // 随机生成一些数据 const data = []; for (let i = 0; i < 5; i++) { data.push(Math.round(Math.random() * 100)); } // 更新柱状图的数据 this.chartData = data; // 配置柱状图的参数 const option = { title: { text: '动态柱状图示例' }, tooltip: {}, xAxis: { data: ['数据1', '数据2', '数据3', '数据4', '数据5'] }, yAxis: {}, series: [{ name: '数据量', type: 'bar', data: this.chartData }] }; // 使用刚指定的配置项和数据显示图表 this.myChart.setOption(option); } }, beforeDestroy() { // 在组件销毁前清除定时器 clearInterval(this.chartTimer); } }; </script> ``` 此示例使用了一个计时器来每隔 1 秒更新一次柱状图的数据,并使用 Echarts 的 API 更新图表。您可以将其复制到一个 Vue 单文件组件中并运行以查看动态柱状图。如果需要更多帮助,请告诉我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值