ec-canvas.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. },
  16. ready: function () {
  17. if (!this.data.ec) {
  18. console.warn('组件需绑定 ec 变量,例:<ec-canvas id="mychart-dom-bar" ' +
  19. 'canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>');
  20. return;
  21. }
  22. if (!this.data.ec.lazyLoad) {
  23. this.init();
  24. }
  25. },
  26. methods: {
  27. init: function (callback) {
  28. const version = wx.version.version.split('.').map(n => parseInt(n, 10));
  29. const isValid = version[0] > 1 || (version[0] === 1 && version[1] > 9) ||
  30. (version[0] === 1 && version[1] === 9 && version[2] >= 91);
  31. if (!isValid) {
  32. console.error('微信基础库版本过低,需大于等于 1.9.91。' +
  33. '参见:https://github.com/ecomfe/echarts-for-weixin' +
  34. '#%E5%BE%AE%E4%BF%A1%E7%89%88%E6%9C%AC%E8%A6%81%E6%B1%82');
  35. return;
  36. }
  37. ctx = wx.createCanvasContext(this.data.canvasId, this);
  38. const canvas = new WxCanvas(ctx, this.data.canvasId);
  39. echarts.setCanvasCreator(() => {
  40. return canvas;
  41. });
  42. var query = wx.createSelectorQuery().in(this);
  43. query.select('.ec-canvas').boundingClientRect(res => {
  44. if (typeof callback === 'function') {
  45. this.chart = callback(canvas, res.width, res.height);
  46. } else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  47. this.chart = this.data.ec.onInit(canvas, res.width, res.height);
  48. } else {
  49. this.triggerEvent('init', {
  50. canvas: canvas,
  51. width: res.width,
  52. height: res.height
  53. });
  54. }
  55. }).exec();
  56. },
  57. // canvasToTempFilePath(opt) {
  58. // if (!opt.canvasId) {
  59. // opt.canvasId = this.data.canvasId;
  60. // }
  61. // ctx.draw(true, () => {
  62. // wx.canvasToTempFilePath(opt, this);
  63. // });
  64. // },
  65. canvasToTempFilePath(opt) {
  66. if (!opt.canvasId) {
  67. opt.canvasId = this.data.canvasId;
  68. }
  69. const system = wx.getSystemInfoSync().system
  70. if (/ios/i.test(system)) {
  71. ctx.draw(true, () => {
  72. wx.canvasToTempFilePath(opt, this);
  73. });
  74. } else { //针对安卓机型异步获取已绘制图层
  75. ctx.draw(true, () => {
  76. //断点打印依旧不会执行setTimeout(() => {wx.canvasToTempFilePath(opt, this);}, 1000);}});
  77. ctx.draw(true);
  78. setTimeout(() => { //延迟获取
  79. wx.canvasToTempFilePath(opt, this);
  80. }, 1000);
  81. })
  82. }
  83. },
  84. touchStart(e) {
  85. if (this.chart && e.touches.length > 0) {
  86. var touch = e.touches[0];
  87. var handler = this.chart.getZr().handler;
  88. handler.dispatch('mousedown', {
  89. zrX: touch.x,
  90. zrY: touch.y
  91. });
  92. handler.dispatch('mousemove', {
  93. zrX: touch.x,
  94. zrY: touch.y
  95. });
  96. handler.processGesture(wrapTouch(e), 'start');
  97. }
  98. },
  99. touchMove(e) {
  100. if (this.chart && e.touches.length > 0) {
  101. var touch = e.touches[0];
  102. var handler = this.chart.getZr().handler;
  103. handler.dispatch('mousemove', {
  104. zrX: touch.x,
  105. zrY: touch.y
  106. });
  107. handler.processGesture(wrapTouch(e), 'change');
  108. }
  109. },
  110. touchEnd(e) {
  111. if (this.chart) {
  112. const touch = e.changedTouches ? e.changedTouches[0] : {};
  113. var handler = this.chart.getZr().handler;
  114. handler.dispatch('mouseup', {
  115. zrX: touch.x,
  116. zrY: touch.y
  117. });
  118. handler.dispatch('click', {
  119. zrX: touch.x,
  120. zrY: touch.y
  121. });
  122. handler.processGesture(wrapTouch(e), 'end');
  123. }
  124. }
  125. }
  126. });
  127. function wrapTouch(event) {
  128. for (let i = 0; i < event.touches.length; ++i) {
  129. const touch = event.touches[i];
  130. touch.offsetX = touch.x;
  131. touch.offsetY = touch.y;
  132. }
  133. return event;
  134. }