当前位置:   article > 正文

微信小程序中使用echart、动态加载几条折线_微信小程序动态echarts

微信小程序动态echarts

一、示例

 echart小程序示例

gitub地址:GitHub - ecomfe/echarts-for-weixin: Apache ECharts 的微信小程序版本

在小程序中放入

效果

 

二、 小程序代码

json 组件的位置取决于一示例中的的echart的位置

  1. {
  2. "navigationStyle":"custom",
  3. "usingComponents": {
  4. "ec-canvas":"../../ec-canvas/ec-canvas"
  5. }
  6. }

wxml

  1. <coverheader headerOpt="{{headerOpt}}"></coverheader>
  2. <wxs src="./../../filter/urlFilter.wxs" module="filter" />
  3. <view class="tendency">
  4. <view class="chartScreen">
  5. <view class="chart_item" wx:for="{{tendencyList}}" wx:key="index">
  6. <view class="check {{!filter.includesArr(chooseId,item.value)?'checked'+index:''}}" bindtap="clickcheck" data-item="{{item}}"></view>
  7. <view>{{item.label}}</view>
  8. </view>
  9. </view>
  10. <view class="echart_line">
  11. <view class="echart_line1">
  12. <ec-canvas id="mychart" canvas-id="mychart" ec="{{ ec }}"></ec-canvas>
  13. </view>
  14. </view>
  15. </view>

js

  1. var request = require('../../utils/request.js');
  2. let util=require('../../utils/util')
  3. import {getTopicAnalysis } from "../../utils/api/test/test";
  4. import * as echarts from '../../ec-canvas/echarts';
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. headerOpt:{
  11. showGoHome:false,
  12. title:"详情页",
  13. // styles:'background-image:url(https://assets-dajieimg.oss-cn-beijing.aliyuncs.com/meishu/login/bg_top.png);background-size:100% 100%'
  14. },
  15. navigationBarAndStatusBarHeight:wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 'px',
  16. tendencyList:[{label:'1',value:1},{label:'2',value:2},{label:'3',value:3},{label:'4',value:4},{label:'5',value:5},{label:'6',value:6,}],
  17. chooseList:[],//选中的list
  18. chooseId:[],//选中的id
  19. ec: {
  20. lazyLoad: true // 延迟加载
  21. },
  22. Chart:null,
  23. },
  24. clickcheck(e){
  25. let that=this
  26. let items=e.currentTarget.dataset.item
  27. let id=e.currentTarget.dataset.item.value
  28. let chooseId=that.data.chooseId
  29. let chooseList=that.data.chooseList
  30. console.log(id);
  31. // 如果id不存在加入
  32. if(!chooseId.includes(id)){
  33. chooseId.push(id)
  34. chooseList.push(items)
  35. }else{
  36. //删除id
  37. var index = chooseId.findIndex(item => {return item === id})
  38. chooseId.splice(index,1)
  39. chooseList.splice(index,1)
  40. }
  41. console.log(chooseId);
  42. that.setData({
  43. chooseId,
  44. chooseList,
  45. })
  46. that.getseriesInfo()
  47. },
  48. getseriesInfo(){
  49. let colorArr= ['#556FFD', '#57BE6A', '#DF699B', '#98A1D0', '#EED46C','#D64052','#E3C24B']
  50. //传递的参数--
  51. getTopicAnalysis({chooseId:this.data.chooseId}).then((res)=>{
  52. if(res.data){
  53. let xAxisinfo = [];
  54. let legendInfo = [];
  55. let seriesInfo = [];
  56. res.data.forEach((ele,index) => {
  57. let params = {
  58. data: [],
  59. type: 'line',
  60. showSymbol: false,
  61. name: '',
  62. lineStyle:{
  63. color:colorArr[index]
  64. },
  65. };
  66. legendInfo.push(ele.topicName)//topicId 话题id
  67. params.name = ele.topicName;
  68. ele.dailyData.forEach((eleData) => {
  69. if(index == 0){
  70. xAxisinfo.push(eleData.date)
  71. }
  72. params.data.push(eleData.cnt)
  73. });
  74. seriesInfo.push(params);
  75. })
  76. this.setData({
  77. xAxisinfo,
  78. legendInfo,
  79. seriesInfo
  80. })
  81. console.log(seriesInfo);
  82. if (!this.data.Chart){
  83. this.init_echarts(); //初始化图表
  84. }else{
  85. this.setOption(); //更新数据
  86. }
  87. console.log(seriesInfo);
  88. }
  89. })
  90. },
  91. //综合数据图标初始化
  92. init_echarts: function () {
  93. this.echartsComponnet.init((canvas, width, height,dpr) => {
  94. // 初始化图表
  95. const Chart = echarts.init(canvas, null, {
  96. // renderer: 'svg',
  97. width: width,
  98. height: height,
  99. devicePixelRatio: dpr // new
  100. });
  101. Chart.setOption(this.setOption());
  102. // 注意这里一定要返回 chart 实例,否则会影响事件处理等
  103. return Chart;
  104. });
  105. },
  106. //综合数据直线设置数据
  107. setOption(){
  108. return {
  109. // color: ['#DC2FDC', '#1B89FF', '#87EEFB', '#EEAD6C', '#EED46C'],
  110. tooltip: {
  111. trigger: 'axis',
  112. confine: true,
  113. // showContent: false,
  114. backgroundColor: 'rgba(255,255,255,0.5)',
  115. borderWidth: 1,
  116. borderColor: '#e2e6f5',
  117. borderRadius: 4,
  118. padding: [10, 20, 10, 20],
  119. textStyle: {
  120. color: "#949AB7",
  121. },
  122. axisPointer: {
  123. type: "cross",
  124. label: {
  125. show:false,
  126. // backgroundColor: 'rgba(39, 100, 131, 0.71)',
  127. // backgroundColor: 'null',
  128. },
  129. lineStyle:{
  130. backgroundColor: 'rgba(39, 100, 131, 0.71)',
  131. }
  132. },
  133. formatter: function (params) {
  134. let str = params[0].name+'\n'
  135. let newParams = [];
  136. let tooltipString = [];
  137. newParams = [...params];
  138. newParams.sort((a,b) => {return Number(b.value) - Number(a.value)});
  139. newParams.forEach((ele,index) => {
  140. str +=ele.seriesName+ele.value+'\n'
  141. })
  142. return str
  143. }
  144. },
  145. grid: {
  146. left: 20,
  147. right: 20,
  148. bottom: 15,
  149. top: 40,
  150. containLabel: true
  151. },
  152. legend: {
  153. icon:'circle',
  154. show:false,
  155. itemHeight: 12,
  156. itemWidth: 12,
  157. top:'bottom',
  158. textStyle: {
  159. //图例文字的样式
  160. color: '#3F96D5',
  161. fontSize: 12
  162. },
  163. data: this.data.legendInfo,
  164. },
  165. xAxis: {
  166. axisLabel:{
  167. color: "#9095A7", //刻度线标签颜色
  168. lineStyle:{
  169. color:'#9095A7',//更改坐标轴颜色
  170. },
  171. },
  172. axisTick:{show:false},
  173. axisPointer: {
  174. lineStyle:{
  175. color:'rgba(39, 100, 131, 1)'
  176. }
  177. },
  178. //设置轴线的属性
  179. axisLine:{
  180. lineStyle: {
  181. color: '#DFE1EB',//刻度线的颜色
  182. }
  183. },
  184. type: 'category',
  185. boundaryGap: false,
  186. data: this.data.xAxisinfo,
  187. },
  188. yAxis: {
  189. splitLine: {
  190. lineStyle: {
  191. type: 'dashed',
  192. color:'#DFE1EB'
  193. }
  194. },
  195. boundaryGap: [0, '50%'],
  196. type: 'value',
  197. // name: 'GMV',
  198. position: 'left',
  199. axisLabel: {
  200. color: "#9095A7", //刻度线标签颜色
  201. formatter(v) {
  202. v = v.toString()
  203. if (v >= 100000000000) {
  204. return (v.substring(0, 5) / 10) + '亿'
  205. } else if (v >= 10000000000) {
  206. return (v.substring(0, 4) / 10) + '亿'
  207. } else if (v >= 1000000000) {
  208. return (v.substring(0, 3) / 10) + '亿'
  209. } else if (v >= 100000000) {
  210. return (v.substring(0, 2) / 10) + '亿'
  211. } else if (v >= 10000000) {
  212. return v.substring(0, 4) + 'w'
  213. } else if (v >= 1000000) {
  214. return v.substring(0, 3) + 'w'
  215. } else if (v >= 100000) {
  216. return v.substring(0, 2) + 'w'
  217. } else if (v >= 10000) {
  218. return (v.substring(0, 2) / 10) + 'w'
  219. } else if (v >= 1000) {
  220. return v
  221. } else {
  222. return v
  223. }
  224. },
  225. },
  226. axisPointer: {
  227. lineStyle:{
  228. color:'rgba(39, 100, 131, 1)'
  229. }
  230. },
  231. },
  232. series: this.data.seriesInfo,
  233. grid: {top: '5%',right:'8%',bottom:'10%',left:'14%'},
  234. }
  235. },
  236. /**
  237. * 生命周期函数--监听页面加载
  238. */
  239. onLoad(options) {
  240. this.echartsComponnet = this.selectComponent('#mychart');
  241. },
  242. /**
  243. * 生命周期函数--监听页面初次渲染完成
  244. */
  245. onReady() {
  246. },
  247. /**
  248. * 生命周期函数--监听页面显示
  249. */
  250. onShow() {
  251. },
  252. /**
  253. * 生命周期函数--监听页面隐藏
  254. */
  255. onHide() {
  256. },
  257. /**
  258. * 生命周期函数--监听页面卸载
  259. */
  260. onUnload() {
  261. },
  262. /**
  263. * 页面相关事件处理函数--监听用户下拉动作
  264. */
  265. onPullDownRefresh() {
  266. },
  267. /**
  268. * 页面上拉触底事件的处理函数
  269. */
  270. onReachBottom() {
  271. },
  272. /**
  273. * 用户点击右上角分享
  274. */
  275. onShareAppMessage() {
  276. }
  277. })

wxss

  1. page{
  2. background-color: #F5F5F5;
  3. color:#212848;
  4. }
  5. .tendency{background-color: #fff;padding: 30rpx;margin-top:20rpx ;}
  6. .sec_title{font-size: 32rpx;font-weight: 500;margin-bottom: 36rpx;}
  7. .chartScreen{display: flex;align-items: center;flex-wrap: wrap;}
  8. .chart_item{display: flex;align-items: center;flex-shrink: 0;margin-right: 30rpx;font-size: 24rpx;margin-bottom: 18rpx;}
  9. .check{width: 28rpx;height: 28rpx; border: 2rpx solid #dbdbdb;border-radius: 4rpx;margin-right: 8rpx;}
  10. .checked0{background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAF8SURBVHgB7Za/S8NAFMe/76pInUpBF5f+Ce3iqqPgokNBXETcRLAOnXXvYAd1k9ZFBIfWQXHU2UFB3By6OAmSRYst6fNeQrGIlzYxvcV+lnBw3OfejySPoFkpcMZlrgCc1csUYoZA9RZ97tTLyQb5ss79MEQ/cNrUyimX3X0LMiE1zhMVpQNegjU4q2CXlG0hRsLQTKeBzVWCFaHIdreCjxxDTGRmgOKGQuOFcXTKxn2xRCiyPR3ZexM4DJDFIuyVlY47+GgG7zemNL/gF/782nzj+VnyGuT1TUsPOt6zH0bhVNo/0CTtyiSiQWWCMaUnNfYaQCJdW6ZfZUIYWaDQrwl7hy3OfQt6ZVXvUggF5bfdwLaS1JaKCpNJeHJZC5LmoPqa6NulIpHu68qFq9tosoGEwtOzX1NBUlitRZMJA39pLnVUorl7jC4LJRQklX/lf/wPHVhEprYH2IL5QiWI1mEnSqet2gV1VqZGglRO6+sYDo5+oW5k6pZR/wuVDZYh1UtPwwAAAABJRU5ErkJggg==') no-repeat;background-size: 100% 100%;}
  11. .checked1{background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAGiSURBVHgB7Za9S0JRFMDPfUlqNEgiBAW5NDQEOgRt4ZhTg1BLQVMfk0FLS1FbYNRmtUgfg4NDDelW/QGBkVCBQwo29cGDzCzk3c559cLA9/T5cZf6LfddOO/97jnvcu9hgEwkZt3c2hYB4B6cOqDZcDhiH8pCdHQ7w75kUrIlot/I7F3xSkq7tClARji4lUUkxmAMhME8EojFIVoI/0LTuGxdMDcwCUKEJFv2Bg1jLNAk+jp7YHFwBrIvOQjfHOjGNSVDkq1gZq+lIoRvDw1jGxaWyzZSOzgWDON1Sxpw+/FgYBC7O9F9eaR7WN0gD8VnWEtu4fgE1dAVuuxO9YNEJakmK5TeapYRuiXdS8cgm8+pmU71ByrKiFUTMkMhrTyU2lXL5e/1/QjKZdqizMDGT+e5UYDL5oT1oSXosNjVTGhOxDJxw/+rR9VdSpIQ7j5NTsRzZ3XJahIS13JaLR+Ryd/D/vdzPdR80iQwK+Li8QoawdTRpkkb4W/chzIIBIX8EoShHGObyKdBTJYy3gVBKerD9pspXmrHoTVgMuycHOT6BBgHmtv8l4eBAAAAAElFTkSuQmCC') no-repeat;background-size: 100% 100%;}
  12. .checked2{background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAGKSURBVHgB7Za/S8NAFMe/CbWDBCm1iIiDLp0cqlNdFBxcdBBxVXDSbnX0r9BNxUHQUQcFXRT8NXVSkW4O7SAo0oZQpEpoe967WlD00qRJb9EPhEvgkU/eu8vd08DJLW8MQAvt8NsEvyIIHHYIVl0Z3EzltU/ZbXtE37DAKsM6tI41BTIIB6+iztOdgToSOtQSUS3Ev9AzoW4DsYUxKBGSrDc97RyDgAj3R9GzNAn7sYjC7rU0LpAMSUaZ1d5sFPauHGN9C7/KXrZOUSvbjvHSkkamRsRondzIQmAk42KBVIqveF4/FmMzpMJQ1IAxGpdKGzLKyK2MkJbUPMiIBUCZRueSv8oILzJHYX1OzsTLuiaGEJsf/yGrf5QJL2i51DZzCqB/q291FnpnWMjpmaAyO82vjKarlCSUaUNOlC6yLclcCYn3hydRPoJKaO5n0Cqud5rSeZaf1UD5Pg8/eNraqJR++RvnoQWFkPAOimCMHem8OV2EmiwtDdW0Tu03dcSiHW+TiF+X5CDXBwmPnVkfFK3qAAAAAElFTkSuQmCC') no-repeat;background-size: 100% 100%;}
  13. .checked3{background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAGgSURBVHgB7ZZNSwJRFIbfew0jUsgiiqAQjIQWleCm2tjKTUHQL3DZrn5Jy9pJ/8BolZtsoS1U1Fy4CoYkSbIyEiRBb/dMKQbO6Pgxm3o2d2Y4M8+ccz8ZJKfBlJNZWFAIbDBgCsOGISTq4vgw4FEYycBZaiSiX06UGw3h4dzCTkYtI4R0UBW5LOM+TILJLuMwEcrSVCHxLzSMfdIK39YiTBGSbM/vksORacaMYUjMOCbg33Gi9FZFJPqgGTeUDElGmdVqddxE87qxAwvbZVcRBZ+y1UOzpN71OZqoSGaKmi+7XQ74tpfwUanhMnyvtt3QFNrkAHAvT6vXnaRNGWXUq4zQLGksUcDLa1VmOo9N70JHGWFEpits9gl9bG11tjW32mWx+PdPGYGdnaeFXoDdZsXB7grGrRZVTvdEQpY5mXmCUbqOUpKEr5WWnMjmSn3JehIShWIFt7J8BJUwFn9Ev/S80tzlntVpouTfMQiGlraslA7KH9gPZb+UYSJcbl1pmIRoiAsuT8QBM7L8cRxxOn5DnojlkxBGgCoSiJCDXF/Qr6qL/ZClCgAAAABJRU5ErkJggg==') no-repeat;background-size: 100% 100%;}
  14. .checked4{background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAGdSURBVHgB7Za/S0JRFMe/72UpEWhgIS5ltLTpf6BTDQ0SYaM155BTLQZtCoWLTkLmaBQY+Ac4NLXoUlOQYYhmmJBJIvq650Hi4I/3tHeX+kzvvnfhw3nnnHuPAEbW6V42QIhLEOxsacLvk2qi43dkUnmBZHqIWY1EvdSY1CHqMRXmICNMevYXRUBygxuCXQRfTLyF+BeqZtqyCOuhD1yEJFsKn7BihPZCw6pNln095lEMRqCp8EfWrjdQDEWG7p1Y2Ct7CYTQqX9iLKHZ68GCdwfDMK67YIudyrJn/zFapVeMQjfowwwrAOOGS36uJJJ9ZdYjH4tIuYwYGGE5GkeTFYB51wPL/l5fGaFGNlTYZrkosJy0ShXMb292e6tXVo6es6p8ghqEB+eWNGwD9dZK7Azi3KwcCa2Jt8QlKhdJqGVklZKkEAh25UT1Oj2WTJGQaOTuUWI5Jaixy5E4xkWndOP7VVo+sT5u7zAJioVElUkn5W/chzVwhKa2HDghQboRBUh0bvGIsiZCOhDXaPxmEzF7kYI2sGCkDDnI9Q1gapfGPx2fagAAAABJRU5ErkJggg==') no-repeat;background-size: 100% 100%;}
  15. .checked5{background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAGbSURBVHgB7ZbLSwJBHMe/rksFhpiIQoee4CUQ7T+IOgQd6uytQ3c9Bh2Luhl19NDNs966FZ26FQkdhLYXBWnZKito7aP5DdkDdHV97KU+MMwsDPvZ78zOwwHG9dHihOh0HrBmmBUPeo6RVjU9Pjl3eOP4lJ31R/QLWdW0iCA6xYQNMsJDoyiwuCuwj7AAe/HYLcS/0DLikB/eYAy2CEnmC+3AMOuDHjHgmoJ3ZgOqIuE1t9u0X08SkswX2oahKijmEqZ9uxZ+yyp4udyCzuqOhO7xKC9muALz8M/ucVkhuw61+oRWNJ1D56CfvXCBt8u3qYaykWDckoxomrAkJfFekeAei8IzvdZQRhQu2peZCnU+J5vQankMjy5/ra2fMvkqiTf2UVZw3J8smS0bvrYCkX04RBdLkufPBA1z+S4Fq7T8S0nyzJLW5YTykOlI1paQqJWyfPgImldZSqJT2t5plMcMr6vFU3SDpa2tLu2Gv3EeyrAREp7DJgzDyAjscroKO1IakDVdjwl0/aYbMdt00ugHBg9zrOpahFwfXxGjuLpZ9F0AAAAASUVORK5CYII=') no-repeat;background-size: 100% 100%;}
  16. .echart_line{display: flex;align-items: center;}
  17. .echart_line .echart_line1{height: 420rpx;width:100%;}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/203710
推荐阅读