欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序中使用ECharts 异步加载数据的方法

程序员文章站 2023-08-21 10:59:39
官网例子都是同步的,怎么引入及同步demo请移步官网

官网例子都是同步的,怎么引入及同步demo请移步官网

<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 请求数据
  return {
   color: ['#37a2da', '#32c5e9', '#67e0e3'],
   tooltip: {
    trigger: 'axis',
    axispointer: {      // 坐标轴指示器,坐标轴触发有效
     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(){
  //请求数据 
  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 异步加载数据的方法,希望对大家有所帮助