拓恒飞手平台小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
5.9KB

  1. import WxCanvas from './wx-canvas';
  2. import * as echarts from './echarts';
  3. let ctx;
  4. function compareVersion(v1, v2) {
  5. v1 = v1.split('.')
  6. v2 = v2.split('.')
  7. const len = Math.max(v1.length, v2.length)
  8. while (v1.length < len) {
  9. v1.push('0')
  10. }
  11. while (v2.length < len) {
  12. v2.push('0')
  13. }
  14. for (let i = 0; i < len; i++) {
  15. const num1 = parseInt(v1[i])
  16. const num2 = parseInt(v2[i])
  17. if (num1 > num2) {
  18. return 1
  19. } else if (num1 < num2) {
  20. return -1
  21. }
  22. }
  23. return 0
  24. }
  25. Component({
  26. properties: {
  27. canvasId: {
  28. type: String,
  29. value: 'ec-canvas'
  30. },
  31. ec: {
  32. type: Object
  33. },
  34. forceUseOldCanvas: {
  35. type: Boolean,
  36. value: false
  37. }
  38. },
  39. data: {
  40. isUseNewCanvas: false
  41. },
  42. ready: function () {
  43. // Disable prograssive because drawImage doesn't support DOM as parameter
  44. // See https://developers.weixin.qq.com/miniprogram/dev/api/canvas/CanvasContext.drawImage.html
  45. echarts.registerPreprocessor(option => {
  46. if (option && option.series) {
  47. if (option.series.length > 0) {
  48. option.series.forEach(series => {
  49. series.progressive = 0;
  50. });
  51. }
  52. else if (typeof option.series === 'object') {
  53. option.series.progressive = 0;
  54. }
  55. }
  56. });
  57. if (!this.data.ec) {
  58. console.warn('组件需绑定 ec 变量,例:<ec-canvas id="mychart-dom-bar" '
  59. + 'canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>');
  60. return;
  61. }
  62. if (!this.data.ec.lazyLoad) {
  63. this.init();
  64. }
  65. },
  66. methods: {
  67. init: function (callback) {
  68. const version = wx.getSystemInfoSync().SDKVersion
  69. const canUseNewCanvas = compareVersion(version, '2.9.0') >= 0;
  70. const forceUseOldCanvas = this.data.forceUseOldCanvas;
  71. const isUseNewCanvas = canUseNewCanvas && !forceUseOldCanvas;
  72. this.setData({ isUseNewCanvas });
  73. if (forceUseOldCanvas && canUseNewCanvas) {
  74. console.warn('开发者强制使用旧canvas,建议关闭');
  75. }
  76. if (isUseNewCanvas) {
  77. // console.log('微信基础库版本大于2.9.0,开始使用<canvas type="2d"/>');
  78. // 2.9.0 可以使用 <canvas type="2d"></canvas>
  79. this.initByNewWay(callback);
  80. } else {
  81. const isValid = compareVersion(version, '1.9.91') >= 0
  82. if (!isValid) {
  83. console.error('微信基础库版本过低,需大于等于 1.9.91。'
  84. + '参见:https://github.com/ecomfe/echarts-for-weixin'
  85. + '#%E5%BE%AE%E4%BF%A1%E7%89%88%E6%9C%AC%E8%A6%81%E6%B1%82');
  86. return;
  87. } else {
  88. console.warn('建议将微信基础库调整大于等于2.9.0版本。升级后绘图将有更好性能');
  89. this.initByOldWay(callback);
  90. }
  91. }
  92. },
  93. initByNewWay(callback) {
  94. // version >= 2.9.0:使用新的方式初始化
  95. const query = wx.createSelectorQuery().in(this)
  96. query
  97. .select('.ec-canvas')
  98. .fields({ node: true, size: true })
  99. .exec(res => {
  100. const canvasNode = res[0].node
  101. this.canvasNode = canvasNode
  102. const canvasDpr = wx.getSystemInfoSync().pixelRatio
  103. const canvasWidth = res[0].width
  104. const canvasHeight = res[0].height
  105. const ctx = canvasNode.getContext('2d')
  106. const canvas = new WxCanvas(ctx, this.data.canvasId, true, canvasNode)
  107. echarts.setCanvasCreator(() => {
  108. return canvas
  109. })
  110. if (typeof callback === 'function') {
  111. this.chart = callback(canvas, canvasWidth, canvasHeight, canvasDpr)
  112. } else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  113. this.chart = this.data.ec.onInit(canvas, canvasWidth, canvasHeight, canvasDpr)
  114. } else {
  115. this.triggerEvent('init', {
  116. canvas: canvas,
  117. width: canvasWidth,
  118. height: canvasHeight,
  119. dpr: canvasDpr
  120. })
  121. }
  122. })
  123. },
  124. canvasToTempFilePath(opt) {
  125. if (this.data.isUseNewCanvas) {
  126. // 新版
  127. const query = wx.createSelectorQuery().in(this)
  128. query
  129. .select('.ec-canvas')
  130. .fields({ node: true, size: true })
  131. .exec(res => {
  132. const canvasNode = res[0].node
  133. opt.canvas = canvasNode
  134. wx.canvasToTempFilePath(opt)
  135. })
  136. } else {
  137. // 旧的
  138. if (!opt.canvasId) {
  139. opt.canvasId = this.data.canvasId;
  140. }
  141. ctx.draw(true, () => {
  142. wx.canvasToTempFilePath(opt, this);
  143. });
  144. }
  145. },
  146. touchStart(e) {
  147. if (this.chart && e.touches.length > 0) {
  148. var touch = e.touches[0];
  149. var handler = this.chart.getZr().handler;
  150. handler.dispatch('mousedown', {
  151. zrX: touch.x,
  152. zrY: touch.y
  153. });
  154. handler.dispatch('mousemove', {
  155. zrX: touch.x,
  156. zrY: touch.y
  157. });
  158. handler.processGesture(wrapTouch(e), 'start');
  159. }
  160. },
  161. touchMove(e) {
  162. if (this.chart && e.touches.length > 0) {
  163. var touch = e.touches[0];
  164. var handler = this.chart.getZr().handler;
  165. handler.dispatch('mousemove', {
  166. zrX: touch.x,
  167. zrY: touch.y
  168. });
  169. handler.processGesture(wrapTouch(e), 'change');
  170. }
  171. },
  172. touchEnd(e) {
  173. if (this.chart) {
  174. const touch = e.changedTouches ? e.changedTouches[0] : {};
  175. var handler = this.chart.getZr().handler;
  176. handler.dispatch('mouseup', {
  177. zrX: touch.x,
  178. zrY: touch.y
  179. });
  180. handler.dispatch('click', {
  181. zrX: touch.x,
  182. zrY: touch.y
  183. });
  184. handler.processGesture(wrapTouch(e), 'end');
  185. }
  186. }
  187. }
  188. });
  189. function wrapTouch(event) {
  190. for (let i = 0; i < event.touches.length; ++i) {
  191. const touch = event.touches[i];
  192. touch.offsetX = touch.x;
  193. touch.offsetY = touch.y;
  194. }
  195. return event;
  196. }