ec-canvas.js 7.1 KB

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