ec-canvas.js 7.1 KB

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