微信小程序> 微信小程序中使用ECharts实现报表图表展示-小程序统计图插件-微信小程序统计图

微信小程序中使用ECharts实现报表图表展示-小程序统计图插件-微信小程序统计图

浏览量:3843 时间: 来源:会迟到但不会缺席

Echarts可视化工具很方便的解决了统计图表的问题,但是微信小程序是不支持DOM操作,后来在Echarts官网找到了微信小程序的版本。

开始上代码了,首先要在index.wxml中定义,我要在一个页面中使用两个图表

!--index.wxml--

viewclass="containerForm"

ec-canvasid="mychart-dom-multi-bar"canvas-id="mychart-multi-bar"ec="{{ecBar}}"/ec-canvas

ec-canvasid="mychart-dom-multi-scatter"canvas-id="mychart-multi-scatter"ec="{{ecPie}}"/ec-canvas

/view

index.js中

import*asechartsfrom'../../../ec-canvas/echarts';

1:引入插件,插件可以在官网定制http://echarts.baidu.com/builder.html

2:为了方便赋值,这里建立两个全局变量变量是根据业务情景,关于猪的

varbarGraph=null;

varpieChart=null;

3:在page模块种的data里加入

data:{

ecBar:{

onInit:function(canvas,width,height){

barGraph=echarts.init(canvas,null,{

width:width,

height:height

});

canvas.setChart(barGraph);

returnbarGraph;

}

},

ecPie:{

onInit:function(canvas,width,height){

pieChart=echarts.init(canvas,null,{

width:width,

height:height

});

canvas.setChart(pieChart);

returnpieChart;

}

}

},

4:onLoad中加载请求数据

onLoad:function(){

//加载过快导致echarts未创建完成出现空值错误

setTimeout(this.getData,500);

},

5:getData方法里发送ajax

getData(){

wx.showLoading({

title:'加载中...',

});

letthat=this;

util.request(发送ajax请求).then(function(res){

if(res.errno===0){

//第一个

barGraph.setOption({

color:['#37a2da','#32c5e9','#67e0e3'],

tooltip:{

trigger:'axis',

axisPointer:{

type:'shadow'

}

},

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',

stack:'总量',

label:{

normal:{

show:true

}

},

data:[res.data.value,...],//后台数据

itemStyle:{

}

}

]

});

//第二个

pieChart.setOption({

backgroundColor:"#ffffff",

color:["#37A2DA","#32C5E9","#67E0E3","#91F2DE","#007500"],

series:[{

label:{

normal:{

fontSize:14

}

},

type:'pie',

center:['50%','50%'],

radius:[0,'40%'],

data:[{

value:res.data.sowCount,//数据

name:'饼图模块名称'

},{...}

],

itemStyle:{

emphasis:{

shadowBlur:10,

shadowOffsetX:0,

shadowColor:'rgba(0,2,2,0.3)'

}

}

}]

});

}

wx.hideLoading();

});

}

这样就可以了,其中注意...的省略

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎