ec-canvas.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. initByOldWay(callback) {
  94. // 1.9.91 <= version < 2.9.0:原来的方式初始化
  95. ctx = wx.createCanvasContext(this.data.canvasId, this);
  96. const canvas = new WxCanvas(ctx, this.data.canvasId, false);
  97. echarts.setCanvasCreator(() => {
  98. return canvas;
  99. });
  100. // const canvasDpr = wx.getSystemInfoSync().pixelRatio // 微信旧的canvas不能传入dpr
  101. const canvasDpr = 1
  102. var query = wx.createSelectorQuery().in(this);
  103. query.select('.ec-canvas').boundingClientRect(res => {
  104. if (typeof callback === 'function') {
  105. this.chart = callback(canvas, res.width, res.height, canvasDpr);
  106. }
  107. else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  108. this.chart = this.data.ec.onInit(canvas, res.width, res.height, canvasDpr);
  109. }
  110. else {
  111. this.triggerEvent('init', {
  112. canvas: canvas,
  113. width: res.width,
  114. height: res.height,
  115. canvasDpr: canvasDpr // 增加了dpr,可方便外面echarts.init
  116. });
  117. }
  118. }).exec();
  119. },
  120. initByNewWay(callback) {
  121. // version >= 2.9.0:使用新的方式初始化
  122. const query = wx.createSelectorQuery().in(this)
  123. query
  124. .select('.ec-canvas')
  125. .fields({ node: true, size: true })
  126. .exec(res => {
  127. const canvasNode = res[0].node
  128. this.canvasNode = canvasNode
  129. const canvasDpr = wx.getSystemInfoSync().pixelRatio
  130. const canvasWidth = res[0].width
  131. const canvasHeight = res[0].height
  132. const ctx = canvasNode.getContext('2d')
  133. const canvas = new WxCanvas(ctx, this.data.canvasId, true, canvasNode)
  134. echarts.setCanvasCreator(() => {
  135. return canvas
  136. })
  137. if (typeof callback === 'function') {
  138. this.chart = callback(canvas, canvasWidth, canvasHeight, canvasDpr)
  139. } else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  140. this.chart = this.data.ec.onInit(canvas, canvasWidth, canvasHeight, canvasDpr)
  141. } else {
  142. this.triggerEvent('init', {
  143. canvas: canvas,
  144. width: canvasWidth,
  145. height: canvasHeight,
  146. dpr: canvasDpr
  147. })
  148. }
  149. })
  150. },
  151. canvasToTempFilePath(opt) {
  152. if (this.data.isUseNewCanvas) {
  153. // 新版
  154. const query = wx.createSelectorQuery().in(this)
  155. query
  156. .select('.ec-canvas')
  157. .fields({ node: true, size: true })
  158. .exec(res => {
  159. const canvasNode = res[0].node
  160. opt.canvas = canvasNode
  161. wx.canvasToTempFilePath(opt)
  162. })
  163. } else {
  164. // 旧的
  165. if (!opt.canvasId) {
  166. opt.canvasId = this.data.canvasId;
  167. }
  168. ctx.draw(true, () => {
  169. wx.canvasToTempFilePath(opt, this);
  170. });
  171. }
  172. },
  173. touchStart(e) {
  174. if (this.chart && e.touches.length > 0) {
  175. var touch = e.touches[0];
  176. var handler = this.chart.getZr().handler;
  177. handler.dispatch('mousedown', {
  178. zrX: touch.x,
  179. zrY: touch.y
  180. });
  181. handler.dispatch('mousemove', {
  182. zrX: touch.x,
  183. zrY: touch.y
  184. });
  185. handler.processGesture(wrapTouch(e), 'start');
  186. }
  187. },
  188. touchMove(e) {
  189. if (this.chart && e.touches.length > 0) {
  190. var touch = e.touches[0];
  191. var handler = this.chart.getZr().handler;
  192. handler.dispatch('mousemove', {
  193. zrX: touch.x,
  194. zrY: touch.y
  195. });
  196. handler.processGesture(wrapTouch(e), 'change');
  197. }
  198. },
  199. touchEnd(e) {
  200. if (this.chart) {
  201. const touch = e.changedTouches ? e.changedTouches[0] : {};
  202. var handler = this.chart.getZr().handler;
  203. handler.dispatch('mouseup', {
  204. zrX: touch.x,
  205. zrY: touch.y
  206. });
  207. handler.dispatch('click', {
  208. zrX: touch.x,
  209. zrY: touch.y
  210. });
  211. handler.processGesture(wrapTouch(e), 'end');
  212. }
  213. }
  214. }
  215. });
  216. function wrapTouch(event) {
  217. for (let i = 0; i < event.touches.length; ++i) {
  218. const touch = event.touches[i];
  219. touch.offsetX = touch.x;
  220. touch.offsetY = touch.y;
  221. }
  222. return event;
  223. }