ec-canvas.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import WxCanvas from './wx-canvas';
  2. import * as echarts from './echarts.min';
  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. if (echarts.setPlatformAPI) {
  98. echarts.setPlatformAPI({
  99. createCanvas: () => canvas,
  100. });
  101. } else {
  102. echarts.setCanvasCreator(() => canvas);
  103. };
  104. // const canvasDpr = wx.getSystemInfoSync().pixelRatio // 微信旧的canvas不能传入dpr
  105. const canvasDpr = 1
  106. var query = wx.createSelectorQuery().in(this);
  107. query.select('.ec-canvas').boundingClientRect(res => {
  108. if (typeof callback === 'function') {
  109. this.chart = callback(canvas, res.width, res.height, canvasDpr);
  110. }
  111. else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  112. this.chart = this.data.ec.onInit(canvas, res.width, res.height, canvasDpr);
  113. }
  114. else {
  115. this.triggerEvent('init', {
  116. canvas: canvas,
  117. width: res.width,
  118. height: res.height,
  119. canvasDpr: canvasDpr // 增加了dpr,可方便外面echarts.init
  120. });
  121. }
  122. }).exec();
  123. },
  124. initByNewWay(callback) {
  125. // version >= 2.9.0:使用新的方式初始化
  126. const query = wx.createSelectorQuery().in(this)
  127. query
  128. .select('.ec-canvas')
  129. .fields({ node: true, size: true })
  130. .exec(res => {
  131. const canvasNode = res[0].node
  132. this.canvasNode = canvasNode
  133. const canvasDpr = wx.getSystemInfoSync().pixelRatio
  134. const canvasWidth = res[0].width
  135. const canvasHeight = res[0].height
  136. const ctx = canvasNode.getContext('2d')
  137. const canvas = new WxCanvas(ctx, this.data.canvasId, true, canvasNode)
  138. if (echarts.setPlatformAPI) {
  139. echarts.setPlatformAPI({
  140. createCanvas: () => canvas,
  141. loadImage: (src, onload, onerror) => {
  142. if (canvasNode.createImage) {
  143. const image = canvasNode.createImage();
  144. image.onload = onload;
  145. image.onerror = onerror;
  146. image.src = src;
  147. return image;
  148. }
  149. console.error('加载图片依赖 `Canvas.createImage()` API,要求小程序基础库版本在 2.7.0 及以上。');
  150. // PENDING fallback?
  151. }
  152. })
  153. } else {
  154. echarts.setCanvasCreator(() => canvas)
  155. }
  156. if (typeof callback === 'function') {
  157. this.chart = callback(canvas, canvasWidth, canvasHeight, canvasDpr)
  158. } else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  159. this.chart = this.data.ec.onInit(canvas, canvasWidth, canvasHeight, canvasDpr)
  160. } else {
  161. this.triggerEvent('init', {
  162. canvas: canvas,
  163. width: canvasWidth,
  164. height: canvasHeight,
  165. dpr: canvasDpr
  166. })
  167. }
  168. })
  169. },
  170. canvasToTempFilePath(opt) {
  171. if (this.data.isUseNewCanvas) {
  172. // 新版
  173. const query = wx.createSelectorQuery().in(this)
  174. query
  175. .select('.ec-canvas')
  176. .fields({ node: true, size: true })
  177. .exec(res => {
  178. const canvasNode = res[0].node
  179. opt.canvas = canvasNode
  180. wx.canvasToTempFilePath(opt)
  181. })
  182. } else {
  183. // 旧的
  184. if (!opt.canvasId) {
  185. opt.canvasId = this.data.canvasId;
  186. }
  187. ctx.draw(true, () => {
  188. wx.canvasToTempFilePath(opt, this);
  189. });
  190. }
  191. },
  192. touchStart(e) {
  193. if (this.chart && e.touches.length > 0) {
  194. var touch = e.touches[0];
  195. var handler = this.chart.getZr().handler;
  196. handler.dispatch('mousedown', {
  197. zrX: touch.x,
  198. zrY: touch.y,
  199. preventDefault: () => {},
  200. stopImmediatePropagation: () => {},
  201. stopPropagation: () => {}
  202. });
  203. handler.dispatch('mousemove', {
  204. zrX: touch.x,
  205. zrY: touch.y,
  206. preventDefault: () => {},
  207. stopImmediatePropagation: () => {},
  208. stopPropagation: () => {}
  209. });
  210. handler.processGesture(wrapTouch(e), 'start');
  211. }
  212. },
  213. touchMove(e) {
  214. if (this.chart && e.touches.length > 0) {
  215. var touch = e.touches[0];
  216. var handler = this.chart.getZr().handler;
  217. handler.dispatch('mousemove', {
  218. zrX: touch.x,
  219. zrY: touch.y,
  220. preventDefault: () => {},
  221. stopImmediatePropagation: () => {},
  222. stopPropagation: () => {}
  223. });
  224. handler.processGesture(wrapTouch(e), 'change');
  225. }
  226. },
  227. touchEnd(e) {
  228. if (this.chart) {
  229. const touch = e.changedTouches ? e.changedTouches[0] : {};
  230. var handler = this.chart.getZr().handler;
  231. handler.dispatch('mouseup', {
  232. zrX: touch.x,
  233. zrY: touch.y,
  234. preventDefault: () => {},
  235. stopImmediatePropagation: () => {},
  236. stopPropagation: () => {}
  237. });
  238. handler.dispatch('click', {
  239. zrX: touch.x,
  240. zrY: touch.y,
  241. preventDefault: () => {},
  242. stopImmediatePropagation: () => {},
  243. stopPropagation: () => {}
  244. });
  245. handler.processGesture(wrapTouch(e), 'end');
  246. }
  247. }
  248. }
  249. });
  250. function wrapTouch(event) {
  251. for (let i = 0; i < event.touches.length; ++i) {
  252. const touch = event.touches[i];
  253. touch.offsetX = touch.x;
  254. touch.offsetY = touch.y;
  255. }
  256. return event;
  257. }