Vue实现ECharts柱状图数据轮播(自动分页加载)
vue
<div id="main"></div>```
js
import echarts from './echarts'
export default {
data() {
return {
curPage: 1,
totalPage: 1,
itemNumPerPage: 1,//每页显示条数,自己调整
dataList :{
xData:["测试1","测试2"],
yData:[20,30]
}
}
}
mounted() {
this.myEcharts();
let that=this;
const timer = setInterval(() => {
let curPage=that.curPage==that.totalPage?1:that.curPage+1;
that.curPage=curPage;
that.myEcharts();
}, 5000)//设置时间
//销毁定时器
this.$once('hook:beforeDestroy', () => {
clearInterval(timer)
})
},
methods: {
myEcharts() {
this.totalPage = Math.ceil(this.dataList / this.itemNumPerPage)
this.totalPage = this.totalPage < 1 ? 1 : this.totalPage;
let chartDom = document.getElementById("main");
let myChart = echarts.init(chartDom);
let option;
option = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#6a7985'
}
}
},
//图列
legend: {
data:["测试"]
},
//x轴
xAxis: {
type: 'category',
data: this.getCurPageContent(this.dataList.xData,this.curPage,this.itemNumPerPage),
},
yAxis: {
type: 'value',
},
series: [
{
name: "测试",
type: 'bar',
stack: 'Total',
data: this.dataList.yData,
}
],
};
option && myChart.setOption(option);
},
getCurPageContent: function (numberArr, curPage, itemNumPerPage) {
return numberArr.filter(function (element, index) {
if (index >= (curPage - 1) * itemNumPerPage && index < curPage * itemNumPerPage) {
return true
} else {
return false
}
})
},
}