当前位置:   article > 正文

微信小程序实现echarts并兼容IOS_微信小程序 echarts 苹果ios 滑动

微信小程序 echarts 苹果ios 滑动

前言

本文介绍使用ec-canvas实现小程序图表功能,支持切换更新数据并在IOS顺畅使用。

Tips:本文只介绍柱形图,其它图形类似,具体可查看GitHub上的ecomfe/echarts-for-weixin项目。

实现过程

1. 文件index.wxml和index.wxss代码如下,这一块比较简单,可自行查看,不做过多分析;

  1. <view class="container">
  2. <view class="item-head">
  3. <view class="tit">月份统计</view>
  4. <picker mode="selector" bindchange="chooseChange" value="{{chooseIndex}}" range="{{choossArr}}" range-key="value" name="chooseType" class="picker-box">
  5. <view class="picker">
  6. {{choossArr[chooseIndex].value}}
  7. </view>
  8. <image src="../../images/arrow.png"></image>
  9. </picker>
  10. </view>
  11. <view class="echart-heig">
  12. <image src="{{echartImgSrc}}" class="echart-img" wx:if="{{echartImgSrc1 != ''}}"></image>
  13. <ec-canvas id="mychart" canvas-id="mychart-bar" ec="{{ ec }}" class="{{echartImgSrc != '' ? 'hide' : ''}}"></ec-canvas>
  14. </view>
  15. </view>
  1. .container {
  2. height: 100%;
  3. display: flex;
  4. flex-direction: column;
  5. }
  6. .item-head {
  7. display: flex;
  8. padding: 40rpx 30rpx 20rpx;
  9. }
  10. .item-head .tit {
  11. font-size: 30rpx;
  12. margin: 6rpx 14rpx 0 0;
  13. }
  14. .item-head .picker-box {
  15. display: flex;
  16. width: 196rpx;
  17. height: 50rpx;
  18. font-size: 26rpx;
  19. line-height: 50rpx;
  20. border-radius: 6rpx;
  21. border: 2rpx solid #e6e6e6;
  22. }
  23. .item-head .picker-box .picker {
  24. display: inline-block;
  25. flex: 0.7;
  26. width: 140rpx;
  27. text-align: left;
  28. margin-left: 20rpx;
  29. overflow: hidden;
  30. white-space: nowrap;
  31. text-overflow: ellipsis;
  32. }
  33. .item-head .picker-box image {
  34. flex: 0.3;
  35. width: 12rpx;
  36. height: 8rpx;
  37. margin-top: -34rpx;
  38. vertical-align: middle;
  39. }
  40. .item-head .picker-box.ios image {
  41. margin-top: -10rpx;
  42. }
  43. .hide {
  44. display: none !important;
  45. }
  46. .echart-img {
  47. width: 100%;
  48. height: 100%;
  49. }
  50. .echart-heig {
  51. width: 100%;
  52. height: 1020rpx;
  53. }

2. 文件index.js存放所有功能的逻辑代码,代码实现如下:

1)函数chooseChange用于获取切换数据后的月份;

2)函数getFinishCount用来根据月份判断使用的数据;为了方便使用简单的数据切换,项目情况下,一般都是调用接口获取数据;有个特别要注意的,就是把echartImgSrc的值清空,不然没办法更新数据;

3)函数initEcharts是示例的核心,数据结构可自行查看官网,更新数据使用了chart.setOption(option);但这里有个比较特别的代码,就是先使用canvasToTempFilePath把指定区域生成图片,然后赋值echartImgSrc来重新渲染页面,因为直接用ec-canvas渲染,在IOS滑动会很卡。

  1. import * as echarts from '../../ec-canvas/echarts';
  2. const app = getApp();
  3. Page({
  4. data: {
  5. villageArr: [],
  6. villageArr1: [{
  7. value1: 90,
  8. value2: 78,
  9. name: 'aaa'
  10. },
  11. {
  12. value1: 145,
  13. value2: 120,
  14. name: 'bbb'
  15. },
  16. {
  17. value1: 98,
  18. value2: 87,
  19. name: 'ccc'
  20. },
  21. {
  22. value1: 126,
  23. value2: 102,
  24. name: 'ddd'
  25. },
  26. {
  27. value1: 90,
  28. value2: 90,
  29. name: 'eee'
  30. },
  31. {
  32. value1: 108,
  33. value2: 100,
  34. name: 'fff'
  35. },
  36. {
  37. value1: 134,
  38. value2: 120,
  39. name: 'ggg'
  40. }
  41. ],
  42. villageArr2: [{
  43. value1: 50,
  44. value2: 45,
  45. name: 'aaa'
  46. },
  47. {
  48. value1: 40,
  49. value2: 36,
  50. name: 'bbb'
  51. },
  52. {
  53. value1: 70,
  54. value2: 67,
  55. name: 'ccc'
  56. },
  57. {
  58. value1: 80,
  59. value2: 54,
  60. name: 'ddd'
  61. },
  62. {
  63. value1: 77,
  64. value2: 55,
  65. name: 'eee'
  66. },
  67. {
  68. value1: 66,
  69. value2: 57,
  70. name: 'fff'
  71. },
  72. {
  73. value1: 80,
  74. value2: 50,
  75. name: 'ggg'
  76. }
  77. ],
  78. ec: {},
  79. echartImgSrc: '', // canvas在ios下滑动问题,目前只能将echarts图表渲染完成后再生成为图片展示。
  80. chooseIndex: 0, // 选中的下标
  81. choossArr: [{
  82. value: '1月',
  83. id: 1
  84. },
  85. {
  86. value: '2月',
  87. id: 2
  88. }
  89. ]
  90. },
  91. // 切换不同数据
  92. getFinishCount: function (month) {
  93. let self = this;
  94. if (month === 1) {
  95. this.setData({
  96. villageArr: this.data.villageArr1,
  97. echartImgSrc: ''
  98. });
  99. } else {
  100. this.setData({
  101. villageArr: this.data.villageArr2,
  102. echartImgSrc: ''
  103. });
  104. }
  105. self.initEcharts();
  106. },
  107. // 数据渲染
  108. initEcharts: function (canvas, width, height) {
  109. let that = this;
  110. this.selectComponent('#mychart').init((canvas, width, height) => {
  111. // 初始化图表
  112. const chart = echarts.init(canvas, null, {
  113. width: width,
  114. height: height
  115. });
  116. let villageArr = that.data.villageArr;
  117. let villageArrName = [];
  118. let villageArrValue1 = [];
  119. let villageArrValue2 = [];
  120. villageArr.map(function (item, index) {
  121. villageArrName.push({
  122. value: item.name,
  123. id: index
  124. });
  125. villageArrValue1.push({
  126. value: item.value1
  127. });
  128. villageArrValue2.push({
  129. value: item.value2
  130. });
  131. })
  132. this.setData({
  133. villageArr: villageArr
  134. })
  135. let option = {
  136. color: ["#58a7f8", "#63e669"],
  137. legend: {
  138. data: ['上报量', '完成量'],
  139. top: 0,
  140. left: 10,
  141. icon: 'roundRect',
  142. itemWidth: 13,
  143. itemHeight: 13,
  144. itemGap: 20,
  145. },
  146. grid: {
  147. left: 15,
  148. right: 25,
  149. bottom: 0,
  150. top: 35,
  151. containLabel: true
  152. },
  153. xAxis: {
  154. type: 'value',
  155. axisTick: {
  156. show: false
  157. },
  158. axisLine: {
  159. show: false,
  160. },
  161. splitLine: {
  162. lineStyle: {
  163. color: '#e6e6e6'
  164. }
  165. },
  166. axisLabel: {
  167. textStyle: {
  168. color: '#6a737d'
  169. }
  170. }
  171. },
  172. yAxis: {
  173. type: 'category',
  174. axisTick: {
  175. show: false
  176. },
  177. axisLine: {
  178. show: false,
  179. },
  180. axisLabel: {
  181. textStyle: {
  182. color: '#6a737d',
  183. fontSize: 11,
  184. align: 'right',
  185. },
  186. formatter: function (value, index) {
  187. if (value.length > 6) return value.slice(0, 6) + '...';
  188. else return value;
  189. }
  190. },
  191. data: villageArrName
  192. },
  193. series: [{
  194. name: '上报量',
  195. type: 'bar',
  196. barWidth: 12,
  197. barGap: '-100%',
  198. data: villageArrValue1
  199. },
  200. {
  201. name: '完成量',
  202. type: 'bar',
  203. barWidth: 12,
  204. data: villageArrValue2
  205. }
  206. ]
  207. };
  208. chart.on('finished', () => {
  209. that.selectComponent('#mychart').canvasToTempFilePath({
  210. success: res => {
  211. that.setData({
  212. echartImgSrc: res.tempFilePath
  213. })
  214. wx.hideLoading();
  215. },
  216. fail: res => console.log('转换图片失败', res)
  217. });
  218. })
  219. chart.setOption(option);
  220. return chart;
  221. });
  222. },
  223. // 选项改变触发
  224. chooseChange: function (e) {
  225. wx.showLoading({
  226. title: '加载中',
  227. mask: true
  228. });
  229. this.setData({
  230. chooseIndex: e.detail.value
  231. })
  232. let month = this.data.choossArr[e.detail.value].id;
  233. this.getFinishCount(month);
  234. },
  235. // 加载页面
  236. onLoad: function () {
  237. wx.showLoading({
  238. title: '加载中',
  239. mask: true
  240. });
  241. // 默认第一月数据
  242. this.getFinishCount(1);
  243. }
  244. });

 

 

                                                                             扫描公众号,了解更多实例分享

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