微信小程序中如何使用ECharts異步加載數(shù)據(jù)-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關微信小程序中如何使用ECharts 異步加載數(shù)據(jù)的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯(lián)長期為上千余家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為臺江企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站建設,臺江網(wǎng)站改版等技術服務。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

官網(wǎng)例子都是同步的,怎么引入及同步demo請移步官網(wǎng)

<view class="container">
 <ec-canvas id="mychart-dom-multi-bar" canvas-id="mychart-multi-bar" ec="{{ ecBar }}"></ec-canvas>
 <ec-canvas id="mychart-dom-multi-scatter" canvas-id="mychart-multi-scatter" ec="{{ ecScatter }}"></ec-canvas>
</view>
import * as echarts from '../../ec-canvas/echarts';
Page({
 data: {
  ecBar: {
   lazyLoad: true // 延遲加載
  },
  ecScatter: {
   lazyLoad: true 
  }
 },
 onLoad(){
  this.barComponent = this.selectComponent('#mychart-dom-multi-bar');
  this.scaComponnet = this.selectComponent('#mychart-dom-multi-scatter');
  this.init_bar();
  this.init_sca();
 },
 init_bar: function (){
  this.barComponent.init((canvas, width, height) => {
   // 初始化圖表
   const barChart = echarts.init(canvas, null, {
    width: width,
    height: height
   });
   barChart.setOption(this.getBarOption());
   // 注意這里一定要返回 chart 實例,否則會影響事件處理等
   return barChart;
  });
 },
 init_sca: function () {
  this.scaComponnet.init((canvas, width, height) => {
   // 初始化圖表
   const scaChart = echarts.init(canvas, null, {
    width: width,
    height: height
   });
   scaChart.setOption(this.getScaOption());
   // 注意這里一定要返回 chart 實例,否則會影響事件處理等
   return scaChart;
  });
 },
 getBarOption:function(){
  //return 請求數(shù)據(jù)
  return {
   color: ['#37a2da', '#32c5e9', '#67e0e3'],
   tooltip: {
    trigger: 'axis',
    axisPointer: {      // 坐標軸指示器,坐標軸觸發(fā)有效
     type: 'shadow'    // 默認為直線,可選為:'line' | 'shadow'
    }
   },
   legend: {
    data: ['熱度', '正面', '負面']
   },
   grid: {
    left: 20,
    right: 20,
    bottom: 15,
    top: 40,
    containLabel: true
   },
   xAxis: [
    {
     type: 'value',
     axisLine: {
      lineStyle: {
       color: '#999'
      }
     },
     axisLabel: {
      color: '#666'
     }
    }
   ],
   yAxis: [
    {
     type: 'category',
     axisTick: { show: false },
     data: ['汽車之家', '今日頭條', '百度貼吧', '一點資訊', '微信', '微博', '知乎'],
     axisLine: {
      lineStyle: {
       color: '#999'
      }
     },
     axisLabel: {
      color: '#666'
     }
    }
   ],
   series: [
    {
     name: '熱度',
     type: 'bar',
     label: {
      normal: {
       show: true,
       position: 'inside'
      }
     },
     data: [300, 270, 340, 344, 300, 320, 310]
    },
    {
     name: '正面',
     type: 'bar',
     stack: '總量',
     label: {
      normal: {
       show: true
      }
     },
     data: [120, 102, 141, 174, 190, 250, 220]
    },
    {
     name: '負面',
     type: 'bar',
     stack: '總量',
     label: {
      normal: {
       show: true,
       position: 'left'
      }
     },
     data: [-20, -32, -21, -34, -90, -130, -110]
    }
   ]
  };
 },
 getScaOption:function(){
  //請求數(shù)據(jù) 
  var data = [];
  var data2 = [];
  for (var i = 0; i < 10; i++) {
   data.push(
    [
     Math.round(Math.random() * 100),
     Math.round(Math.random() * 100),
     Math.round(Math.random() * 40)
    ]
   );
   data2.push(
    [
     Math.round(Math.random() * 100),
     Math.round(Math.random() * 100),
     Math.round(Math.random() * 100)
    ]
   );
  }
  var axisCommon = {
   axisLabel: {
    textStyle: {
     color: '#C8C8C8'
    }
   },
   axisTick: {
    lineStyle: {
     color: '#fff'
    }
   },
   axisLine: {
    lineStyle: {
     color: '#C8C8C8'
    }
   },
   splitLine: {
    lineStyle: {
     color: '#C8C8C8',
     type: 'solid'
    }
   }
  };
  return {
   color: ["#FF7070", "#60B6E3"],
   backgroundColor: '#eee',
   xAxis: axisCommon,
   yAxis: axisCommon,
   legend: {
    data: ['aaaa', 'bbbb']
   },
   visualMap: {
    show: false,
    max: 100,
    inRange: {
     symbolSize: [20, 70]
    }
   },
   series: [{
    type: 'scatter',
    name: 'aaaa',
    data: data
   },
   {
    name: 'bbbb',
    type: 'scatter',
    data: data2
   }
   ],
   animationDelay: function (idx) {
    return idx * 50;
   },
   animationEasing: 'elasticOut'
  };
 },
});

注意:異步加載時,ec-canvas標簽加載顯示要先于this.scaComponnet.init,否則會報錯。

感謝各位的閱讀!關于“微信小程序中如何使用ECharts 異步加載數(shù)據(jù)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

網(wǎng)頁名稱:微信小程序中如何使用ECharts異步加載數(shù)據(jù)-創(chuàng)新互聯(lián)
本文來源:http://www.bm7419.com/article26/dcdsjg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、動態(tài)網(wǎng)站、App開發(fā)、定制網(wǎng)站、小程序開發(fā)、外貿(mào)網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設網(wǎng)站維護公司