echarts柱状图自动翻页,动态展示数据

该文章描述了一个使用ECharts和Vue.js开发的场景,当柱状图数据超过5条时,通过前端实现自动翻页轮播效果。数据从后台获取并进行分页处理,利用定时器每隔5秒切换页面。在页面销毁时,会关闭定时器以优化性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

业务需求

下图的柱状图,当条目大于5的时候,实现轮播自动翻页。
在这里插入图片描述

开发环境

ECharts + vue

代码实现过程

export default {
	data() {
		return {
			//后台返回的完整数据
			materialsCountByTypeY :[],
			materialsCountByTypeX : [],
			nowPage : 1,
			pageSize : 5,
			totalPage : 0,
			timeKey : null,
			// 渲染到echarts上的数据
			changshaMaterialsCountByTypeY: [],
			changshaMaterialsCountByTypeX: [],
		}
	}created(){
		//请求后台接口
		shenZhenGoodsList({...}).then((response) => {
		  if (response.data) {
		    // 记录原数据
		    response.data.map((e) => {
		      this.materialsCountByTypeY.push(e.materialTypeNm);
		      this.materialsCountByTypeX.push(Number(e.percentage));
		    });
		    // 判断需不需要分页
		    if (response.data.length > this.pageSize) {
		      // 如果大于5条物资信息,则需要分页
		      this.totalPage = Math.ceil(response.data.length / this.pageSize); //计算总页数
		      // 定时函数
		      this.timeKey = window.setInterval(() => {
		        this.getNewPageData();
		      }, 5000);
		    } else {
		      // 如果小于等于五条,则不需要分页
		      this.changshaMaterialsCountByTypeY = this.materialsCountByTypeY;
		      this.changshaMaterialsCountByTypeX = this.materialsCountByTypeX;
		
		      // 物资种类绘制
		      this.getTypeOfMaterial();
		    }
		  }
		});
	},
	destroyed() {
		//页面销毁时,关闭定时器
		clearInterval(this.timeKey);
	},
	methods:{
		getNewPageData(){
	      // slice不改变原数组,但含头不含尾
	      var start = this.pageSize * (this.nowPage - 1);
	      var stop = start + this.pageSize;
	      this.changshaMaterialsCountByTypeY = this.materialsCountByTypeY.slice(start, stop);
	      this.changshaMaterialsCountByTypeX = this.materialsCountByTypeX.slice(start, stop);
	      if (this.nowPage < this.totalPage) {
	        // page数+1
	        this.nowPage += 1;
	      }else if(this.nowPage = this.totalPage){
	        // page数回到1,下次从头开始
	        this.nowPage = 1;
	      }
	      // 物资种类绘制
	      this.getTypeOfMaterial();
	    }
	}
}

此处是前端处理了翻页数据,也可以由后台进行翻页工作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值