微信小店联盟带货小程序

ec-canvas.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import WxCanvas from './wx-canvas';
  2. import * as echarts from './echarts';
  3. let ctx;
  4. Component({
  5. properties: {
  6. canvasId: {
  7. type: String,
  8. value: 'ec-canvas'
  9. },
  10. ec: {
  11. type: Object
  12. }
  13. },
  14. data: {
  15. isUseNewCanvas: false
  16. },
  17. ready: function () {
  18. if (!this.data.ec) {
  19. console.warn('组件需绑定 ec 变量,例:<ec-canvas id="mychart-dom-bar" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>');
  20. return;
  21. }
  22. if (!this.data.ec.onInit) {
  23. console.warn('组件需绑定 ec.onInit 函数,用于初始化图表');
  24. return;
  25. }
  26. this.init();
  27. },
  28. methods: {
  29. init: function (callback) {
  30. const version = wx.getSystemInfoSync().SDKVersion;
  31. const canUseNewCanvas = compareVersion(version, '2.9.0') >= 0;
  32. const forceUseOldCanvas = this.data.ec.forceUseOldCanvas;
  33. const isUseNewCanvas = canUseNewCanvas && !forceUseOldCanvas;
  34. this.setData({ isUseNewCanvas });
  35. if (forceUseOldCanvas && canUseNewCanvas) {
  36. console.warn('开发者强制使用旧canvas,建议关闭');
  37. }
  38. if (isUseNewCanvas) {
  39. // console.log('微信基础库版本大于2.9.0,开始使用<canvas type="2d"/>');
  40. // 2.9.0 可以使用 <canvas type="2d"></canvas>
  41. this.initByNewWay(callback);
  42. } else {
  43. const isValid = compareVersion(version, '1.9.91') >= 0;
  44. if (!isValid) {
  45. console.error('微信基础库版本过低,需大于等于 1.9.91。'
  46. + '参见:https://github.com/ecomfe/echarts-for-weixin'
  47. + '#%E5%BE%AE%E4%BF%A1%E7%89%88%E6%9C%AC%E8%A6%81%E6%B1%82');
  48. return;
  49. } else {
  50. console.warn('建议将微信基础库调整大于等于2.9.0版本。升级后绘图将有更好性能');
  51. this.initByOldWay(callback);
  52. }
  53. }
  54. },
  55. initByOldWay(callback) {
  56. // 1.9.91 <= version < 2.9.0:原来的方式初始化
  57. ctx = wx.createCanvasContext(this.data.canvasId, this);
  58. const canvas = new WxCanvas(ctx, this.data.canvasId, false);
  59. echarts.setCanvasCreator(() => {
  60. return canvas;
  61. });
  62. var query = wx.createSelectorQuery().in(this);
  63. query.select('.ec-canvas').boundingClientRect(res => {
  64. if (typeof callback === 'function') {
  65. this.chart = callback(canvas, res.width, res.height);
  66. }
  67. else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  68. this.chart = this.data.ec.onInit(canvas, res.width, res.height);
  69. }
  70. else {
  71. this.triggerEvent('init', {
  72. canvas: canvas,
  73. width: res.width,
  74. height: res.height
  75. });
  76. }
  77. }).exec();
  78. },
  79. initByNewWay(callback) {
  80. // version >= 2.9.0:使用新的方式初始化
  81. const query = wx.createSelectorQuery().in(this);
  82. query
  83. .select('.ec-canvas')
  84. .fields({ node: true, size: true })
  85. .exec(res => {
  86. const canvasNode = res[0].node;
  87. const canvasWidth = res[0].width;
  88. const canvasHeight = res[0].height;
  89. const ctx = canvasNode.getContext('2d');
  90. const canvas = new WxCanvas(ctx, this.data.canvasId, true, canvasNode);
  91. echarts.setCanvasCreator(() => {
  92. return canvas;
  93. });
  94. if (typeof callback === 'function') {
  95. this.chart = callback(canvas, canvasWidth, canvasHeight);
  96. } else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  97. this.chart = this.data.ec.onInit(canvas, canvasWidth, canvasHeight);
  98. } else {
  99. this.triggerEvent('init', {
  100. canvas: canvas,
  101. width: canvasWidth,
  102. height: canvasHeight
  103. });
  104. }
  105. });
  106. },
  107. canvasToTempFilePath(opt) {
  108. if (this.data.isUseNewCanvas) {
  109. // 新版
  110. const query = wx.createSelectorQuery().in(this);
  111. query
  112. .select('.ec-canvas')
  113. .fields({ node: true, size: true })
  114. .exec(res => {
  115. const canvasNode = res[0].node;
  116. opt.canvas = canvasNode;
  117. wx.canvasToTempFilePath(opt);
  118. });
  119. } else {
  120. // 旧版
  121. if (!opt.canvasId) {
  122. opt.canvasId = this.data.canvasId;
  123. }
  124. ctx.draw(true, () => {
  125. wx.canvasToTempFilePath(opt, this);
  126. });
  127. }
  128. },
  129. touchStart(e) {
  130. if (this.chart && e.touches.length > 0) {
  131. var touch = e.touches[0];
  132. var handler = this.chart.getZr().handler;
  133. handler.dispatch('mousedown', {
  134. zrX: touch.x,
  135. zrY: touch.y
  136. });
  137. handler.dispatch('mousemove', {
  138. zrX: touch.x,
  139. zrY: touch.y
  140. });
  141. handler.processGesture(wrapTouch(e), 'start');
  142. }
  143. },
  144. touchMove(e) {
  145. if (this.chart && e.touches.length > 0) {
  146. var touch = e.touches[0];
  147. var handler = this.chart.getZr().handler;
  148. handler.dispatch('mousemove', {
  149. zrX: touch.x,
  150. zrY: touch.y
  151. });
  152. handler.processGesture(wrapTouch(e), 'change');
  153. }
  154. },
  155. touchEnd(e) {
  156. if (this.chart) {
  157. const touch = e.changedTouches ? e.changedTouches[0] : {};
  158. var handler = this.chart.getZr().handler;
  159. handler.dispatch('mouseup', {
  160. zrX: touch.x,
  161. zrY: touch.y
  162. });
  163. handler.dispatch('click', {
  164. zrX: touch.x,
  165. zrY: touch.y
  166. });
  167. handler.processGesture(wrapTouch(e), 'end');
  168. }
  169. }
  170. }
  171. });
  172. function compareVersion(v1, v2) {
  173. v1 = v1.split('.');
  174. v2 = v2.split('.');
  175. const len = Math.max(v1.length, v2.length);
  176. while (v1.length < len) {
  177. v1.push('0');
  178. }
  179. while (v2.length < len) {
  180. v2.push('0');
  181. }
  182. for (let i = 0; i < len; i++) {
  183. const num1 = parseInt(v1[i]);
  184. const num2 = parseInt(v2[i]);
  185. if (num1 > num2) {
  186. return 1;
  187. } else if (num1 < num2) {
  188. return -1;
  189. }
  190. }
  191. return 0;
  192. }
  193. function wrapTouch(event) {
  194. for (let i = 0; i < event.touches.length; ++i) {
  195. const touch = event.touches[i];
  196. touch.offsetX = touch.x;
  197. touch.offsetY = touch.y;
  198. }
  199. return event;
  200. }