微信小程序中使用ECharts 异步加载数据 实现图表

<view class="container">
  <ec-canvas id="mychart-dom-bar" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>
</view>

import * as echarts from '../../ec-canvas/echarts';
var barec = null
Page({
  onShareAppMessage: function (res) {
    return {
      title: 'ECharts 可以在微信小程序中使用啦!',
      path: '/pages/index/index',
      success: function () { },
      fail: function () { }
    }
  },
  data: {
    ec: {
      onInit: function (canvas, width, height) {
        barec = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(barec);
        return barec;
      }
    }
  },

  onReady() {
    setTimeout(this.getData, 500);
  },
//getData方法里发送ajax
  getData(){
    wx.showLoading({
      title: '加载中...',
    })
     wx.request({
       url: 'http://192.168.3.81/heart.php',
       herder:{
         "content-type":"json"
       },
       success:function(res){
         console.log(res);
         var data = res.data.info;
         console.log(data);
         barec.setOption({
           grid: {
             left: '3%',
             right: '7%',
             bottom: '3%',
             containLabel: true
           },
           tooltip: {
             // trigger: 'axis',
             showDelay: 0,
             formatter: function (params) {
               if (params.value.length > 1) {
                 return params.seriesName + ' :<br/>'
                   + params.value[0] + 'm '
                   + params.value[1] + 'm ';
               }
               else {
                 return params.seriesName + ' :<br/>'
                   + params.name + ' : '
                   + params.value + 'm ';
               }
             },
             axisPointer: {
               show: true,
               type: 'cross',
               lineStyle: {
                 type: 'dashed',
                 width: 1
               }
             }
           },
           legend: {
             data: ["学生"],
             left: 'center'
           },
           xAxis: [
             {
               type: 'value',
               scale: true,
               axisLabel: {
                 formatter: '{value} m'
               },
               splitLine: {
                 show: false
               }
             }
           ],
           yAxis: [
             {
               type: 'value',
               scale: true,
               axisLabel: {
                 formatter: '{value} m'
               },
               splitLine: {
                 show: false
               }
             }
           ],
           series: [{
             name: '学生',
            //  symbolSize: 20,
             data:data,
             type: 'scatter',
             markArea: {
               silent: true,
               itemStyle: {
                 normal: {
                   color: 'transparent',
                   borderWidth: 1,
                   borderType: 'dashed'
                 }
               },
               data: [[{
                 name: '教室',
                 xAxis: '0',
                 yAxis: '0'
               }, {
                 xAxis: '20',
                 yAxis: '20'
               }]]
             },
             markPoint: {
               // data: [
               //   { type: 'max', name: '最大值' },
               //   { type: 'min', name: '最小值' }
               // ]
             },
             markLine: {
               lineStyle: {
                 normal: {
                   type: 'solid'
                 }
               },
               // data: [
               //   { type: 'average', name: '平均值' },
               //   { xAxis: 160 }
               // ]
             }
           }]
         })
         wx.hideLoading(); 
       },
       fail: function (res) { },
       complete: function (res) { },
     })
  }
});

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值