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


Posted in Javascript onJune 27, 2018

官网例子都是同步的,怎么引入及同步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 异步加载数据的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
使用隐藏的new来创建对象
Mar 29 Javascript
firefox下jQuery UI Autocomplete 1.8.*中文输入修正方法
Sep 19 Javascript
jquery网页回到顶部效果(图标渐隐,自写)
Jun 16 Javascript
jQuery Mobile 和 Kendo UI 的比较
May 05 Javascript
简单的分页代码js实现
May 17 Javascript
jquery获取下拉框中的循环值
Feb 08 Javascript
JavaScript数据结构中栈的应用之表达式求值问题详解
Apr 11 Javascript
js中this对象用法分析
Jan 05 Javascript
vue实现添加与删除图书功能
Oct 07 Javascript
Electron整合React使用搭建开发环境的步骤详解
Jun 07 Javascript
详解Vue之计算属性
Jun 20 Javascript
vue下载二进制流图片操作
Oct 26 Javascript
浅谈Webpack下多环境配置的思路
Jun 27 #Javascript
Vue使用vue-area-linkage实现地址三级联动效果的示例
Jun 27 #Javascript
详解关于vue-area-linkage走过的坑
Jun 27 #Javascript
详解nuxt sass全局变量(公共scss解决方案)
Jun 27 #Javascript
Vue引入sass并配置全局变量的方法
Jun 27 #Javascript
详解解决使用axios发送json后台接收不到的问题
Jun 27 #Javascript
vue中v-model的应用及使用详解
Jun 27 #Javascript
You might like
使用JS进行目录上传(相当于批量上传)
2010/12/05 Javascript
关于jQuery新的事件绑定机制on()的使用技巧
2013/04/26 Javascript
基于JavaScript实现继承机制之构造函数方法对象冒充的使用详解
2013/05/07 Javascript
JS根据变量保存方法名并执行方法示例
2014/04/04 Javascript
javascript实现全局匹配并替换的方法
2015/04/27 Javascript
js控制文本框输入的字符类型方法汇总
2015/06/19 Javascript
javascript入门之数组[新手必看]
2016/11/21 Javascript
基于jQuery实现选项卡效果
2017/01/04 Javascript
vue2 mint-ui loadmore实现下拉刷新,上拉更多功能
2018/03/21 Javascript
浅析JS中什么是自定义react数据验证组件
2018/10/19 Javascript
vue-cli V3.0版本的使用详解
2018/10/24 Javascript
关于引入vue.js 文件的知识点总结
2020/01/28 Javascript
基于Vue实现微前端的示例代码
2020/04/24 Javascript
[02:16]DOTA2超级联赛专访Burning 逆袭需要抓住机会
2013/06/24 DOTA
Python Sql数据库增删改查操作简单封装
2016/04/18 Python
在Python程序和Flask框架中使用SQLAlchemy的教程
2016/06/06 Python
django解决跨域请求的问题
2018/11/11 Python
Python实现的文轩网爬虫完整示例
2019/05/16 Python
给ubuntu18安装python3.7的详细教程
2020/06/08 Python
python不同系统中打开方法
2020/06/23 Python
python获取系统内存占用信息的实例方法
2020/07/17 Python
PyQt5的QWebEngineView使用示例
2020/10/20 Python
使用tkinter实现三子棋游戏
2021/02/25 Python
25个CSS3动画按钮和菜单教程分享
2012/10/03 HTML / CSS
CSS3实现酷炫的3D旋转透视效果
2019/11/21 HTML / CSS
HTML5 实现图片上传预处理功能
2020/02/06 HTML / CSS
英文简历中的自我评价
2013/10/06 职场文书
会计岗位描述
2014/02/22 职场文书
采购意向书范本
2014/03/31 职场文书
爱护花草树木的标语
2014/06/11 职场文书
房屋租赁协议书
2014/10/18 职场文书
幼儿教师继续教育培训心得体会
2016/01/19 职场文书
《思路决定出路》读后感3篇
2019/12/11 职场文书
mysql在项目中怎么选事务隔离级别
2021/05/25 MySQL
linux中nohup和后台运行进程查看及终止
2021/06/24 Python
eval(cmd)与eval($cmd)的区别与联系
2021/07/07 PHP