ec-canvas.js 7.0 KB

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