echarts拖动滚动条,实现数据加载效果



<template>
<!-- 拖动dataZoom加载数据-->
<div class="demo2" v-loading="loading">
<div id="echart1"></div>
</div>
</template>
<script>
import echarts from 'echarts'
export default {
name:'demo2',
data(){
return{
total:30,
loading:false,
echartsData:[
{time:'2019-12-25',bmp:120},
{time:'2019-12-26',bmp:125},
{time:'2019-12-27',bmp:130},
{time:'2019-12-28',bmp:136},
{time:'2019-12-29',bmp:141},
{time:'2019-12-30',bmp:142},
{time:'2019-12-31',bmp:120},
{time:'2020-01-01',bmp:125},
{time:'2020-01-02',bmp:120},
{time:'2020-01-03',bmp:110},
{time:'2020-01-04',bmp:160},
{time:'2020-01-05',bmp:100},
{time:'2020-01-06',bmp:150},
{time:'2020-01-07',bmp:126},
{time:'2020-01-08',bmp:136},
]
}
},
mounted() {
this.getEcharts()
},
methods:{
getEcharts(){
let that = this
let mychart=echarts.init(document.getElementById('echart1'))
let dataX=[]
let dataY=[]
that.echartsData.map(item=>{
dataX.push(item.time)
dataY.push(item.bmp)
return item
})
let num=Math.floor(that.echartsData.length/6)
let startVal=0
if(that.echartsData.length>15){
startVal=(num-2)*6
}
let option={
xAxis: {
type: 'category',
data:dataX
},
yAxis: {
type: 'value',
// scale: true,
name: 'BMP',
},
dataZoom : [ //内置型数据区域缩放组件, (平移:在坐标系中滑动拖拽进行数据区域平移)
{
show:true,
xAxisIndex:[0],
type:'slider',
bottom:'0',
left:'center',
startValue:startVal,
endValue:startVal+6,
orient:'horizontal'
},
],
series: [{
data:dataY,
type: 'line',
// smooth: true,
showAllSymbol: true, //显示全部的折线点
symbol:'circle',
symbolSize:8,
hoverAnimation: false,
itemStyle:{
normal:{
color:'#2ECD70', //改变折线点的颜色
lineStyle:{
color:'#2ECD70', //连线的颜色
}
}
},

}],
}
mychart.setOption(option)
let startArr=[]
mychart.on( 'datazoom', function (params) {
startArr.push(params)
if(startArr.length>1){
       let lastVal=startArr[startArr.length-1].end //拖动动作完成后的最后一条
       let lastVal2=startArr[startArr.length-2].end   //拖动动作完成后的倒数第二条
      //判断是否在向右拖动  判断数据是否全部请求完成  判断在什么位置开始加载
       if(lastVal>lastVal2 && that.echartsData.length<that.total && startArr[startArr.length-1].end>85){
        that.getAsynaData()
       }
          }
});
},
getAsynaData(){
this.loading=true
let newData=[
{time:'2020-01-09',bmp:'109'},
{time:'2020-01-10',bmp:'124'},
{time:'2020-01-11',bmp:'135'},
{time:'2020-01-12',bmp:'146'},
{time:'2020-01-13',bmp:'132'},
{time:'2020-01-14',bmp:'128'},
{time:'2020-01-15',bmp:'119'},
]
setTimeout(()=>{
this.loading=false
},500)
newData.forEach(item=>{
this.echartsData.push(item)
})
this.getEcharts()
}
}
}
</script>
<style scoped lang="scss">
.demo2{
padding:40px;
#echart1{
width:100%;
height: 600px;
}
}
</style>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM