Echarts 动态刷新的实现

该博客介绍了如何使用Echarts创建一个动态刷新的横向柱状图,数据从小到大排序,每五个元素显示一页。启动和停止刷新的时机分别在获取数据后和鼠标移出图表时,而停止则在组件销毁和鼠标移入图表时。同时,文章详细讲解了如何处理边界值,确保currentPage不超过totalPage。示例代码展示了具体的实现过程,包括数据获取、图表初始化、更新图表和定时器管理等步骤。
摘要由CSDN通过智能技术生成
1: echarts 动态刷新的实现
  1.1: 从小到大的排序
  1.2: 每五个元素显示一页 (currentPage, totalPage)

2: 启动和停止时机:
   启动:  获取数据之后穷启动定时器
          鼠标移出图表时启动定时器

   停止:  组件销毁时停止定时器
         鼠标移入图表图表时启动定时
3: 边界值的判断(处理)  判断currentPage  是否大于totalPage
-------------------------------------------------------
// 商家销售统计的横向柱状图
<template>
  <div class="com-container">
    <div class="com-charts" ref="seller_ref">
     
    </div>
  </div>
</template>

<script>
export default {
   data() {
     return {
       chartsInstance = null,   // 保存chartsInstance 数据
       allData: null,   //  服务器返回的数据
       currentPage: 1,  //  当前显示的页数
       totalPage: 0,    //  一共显示多少页
       timerId: null,   //  定时器的标识
     }
   },
   mounted() {
    this.initCharts(),  
    this.getData(),
   },
   // 组件销毁的时候, 取消定时器
   destroyed() {
     clearInterval(this.timerId)
   },
   methods: {
     // 初始化 chartsInstance 对象
     initCharts () {
       this.chartsInstance = this.$echrts.init(this.$refs.seller_ref);
       // 对图标对象进行鼠标事件的监听
       this.chartInstance.on('mouseover', ()=> {  // 鼠标进入事件清除定时器
         clearInterval(this.timerId)
       })
       this.chartInstance.on('mouseout', ()=> {   // 鼠标离开事件开启定时器
         startInterval(this.timerId)
       })

     },
     // 获取服务器的数据
     async getData() {
        // 访问服务器地址对象     // http:// 172.0.0.1:8089/api/seller
        const {data: ret}  await this.$http({
          url: 'seller',
          method: 'get'
        })
       console.log(ret)
       this.allData = ret;
       //  对数组进行排序的操作
       this.allData.sort((a, b)=> {
          return a.value < b.value   // 从小到大的排序
       })
       // 每五个元素显示一页
       this.totalPage = this.allData.lemgth % 5 === 0 ? this.allData.lemgth % 5 : 
       this.allData.lemgth % 5 + 1
       this.updateCharts()   // 第一次图标的更新
       this.startInterval();  // 开启定时器
     }
     // 更新图表
     updateCharts() {
       const start = (this.currentPage - 1) * 5;   // 0
       const end =  this.currentPage * 5;     // 5
       const showData = this.allData.slice(start, end)

       const sellerNames = showData.map((item)=> {
           return item.name   
       })
       const sellerValues = showData.map((item)=> {
           return item.value
       })

       const option = {
         xAis: {
           type: 'value'
         },
         yAxis: {
           type: 'category'
           data: sellerNames
         },
         series: [
           {
             type: 'bar',
             data: sellerValues 
           }
         ]
       },
       this.chartsInstance.setOption(option);
    },
    startInterval () {
      if(this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(()=> {
        this.currentPage++;
        if(this.currentPage < this.totalPage) {
           this.currentPage = 1
        }
        this.updateChart();
      }, 5000)   // 间隔5 s 刷新一次
    }
  }
}
</script>

<style lang="less" scoped>

</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值