SoFunction
Updated on 2025-03-01

Methods to load data asynchronously using ECharts in WeChat applets

The examples of the official website are all synchronized. Please move to the official website for how to introduce and synchronize demos.

<view class="container">
 <ec-canvas  canvas- ec="{{ ecBar }}"></ec-canvas>
 <ec-canvas  canvas- ec="{{ ecScatter }}"></ec-canvas>
</view>
import * as echarts from '../../ec-canvas/echarts';
Page({
 data: {
  ecBar: {
   lazyLoad: true // Delay loading  },
  ecScatter: {
   lazyLoad: true 
  }
 },
 onLoad(){
   = ('#mychart-dom-multi-bar');
   = ('#mychart-dom-multi-scatter');
  this.init_bar();
  this.init_sca();
 },
 init_bar: function (){
  ((canvas, width, height) =&gt; {
   // Initialize the chart   const barChart = (canvas, null, {
    width: width,
    height: height
   });
   (());
   // Note that you must return the chart instance here, otherwise it will affect event processing, etc.   return barChart;
  });
 },
 init_sca: function () {
  ((canvas, width, height) =&gt; {
   // Initialize the chart   const scaChart = (canvas, null, {
    width: width,
    height: height
   });
   (());
   // Note that you must return the chart instance here, otherwise it will affect event processing, etc.   return scaChart;
  });
 },
 getBarOption:function(){
  //return request data  return {
   color: ['#37a2da', '#32c5e9', '#67e0e3'],
   tooltip: {
    trigger: 'axis',
    axisPointer: {      // Axis indicator, axis trigger is valid     type: 'shadow'    // Default is a straight line, optional: 'line' | 'shadow'    }
   },
   legend: {
    data: ['heat', 'front', 'Negative']
   },
   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: ['Auto House', 'Today', 'Baidu Tieba', 'A little information', 'WeChat', 'Weibo', 'Zhihu'],
     axisLine: {
      lineStyle: {
       color: '#999'
      }
     },
     axisLabel: {
      color: '#666'
     }
    }
   ],
   series: [
    {
     name: 'heat',
     type: 'bar',
     label: {
      normal: {
       show: true,
       position: 'inside'
      }
     },
     data: [300, 270, 340, 344, 300, 320, 310]
    },
    {
     name: 'front',
     type: 'bar',
     stack: 'Total quantity',
     label: {
      normal: {
       show: true
      }
     },
     data: [120, 102, 141, 174, 190, 250, 220]
    },
    {
     name: 'Negative',
     type: 'bar',
     stack: 'Total quantity',
     label: {
      normal: {
       show: true,
       position: 'left'
      }
     },
     data: [-20, -32, -21, -34, -90, -130, -110]
    }
   ]
  };
 },
 getScaOption:function(){
  //Request data  var data = [];
  var data2 = [];
  for (var i = 0; i &lt; 10; i++) {
   (
    [
     (() * 100),
     (() * 100),
     (() * 40)
    ]
   );
   (
    [
     (() * 100),
     (() * 100),
     (() * 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'
  };
 },
});

Note: When loading asynchronously, the ec-canvas tag display must be preceded by the loading, otherwise an error will be reported.

Summarize

The above is the method of using ECharts asynchronously loading data in WeChat applets introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!