ec-canvas.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. }
  47. else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  48. this.chart = this.data.ec.onInit(canvas, res.width, res.height);
  49. }
  50. else {
  51. this.triggerEvent('init', {
  52. canvas: canvas,
  53. width: res.width,
  54. height: res.height
  55. });
  56. }
  57. }).exec();
  58. },
  59. // canvasToTempFilePath(opt) {
  60. // if (!opt.canvasId) {
  61. // opt.canvasId = this.data.canvasId;
  62. // }
  63. // ctx.draw(true, () => {
  64. // wx.canvasToTempFilePath(opt, this);
  65. // });
  66. // },
  67. canvasToTempFilePath(opt) {
  68. if (!opt.canvasId) {
  69. opt.canvasId = this.data.canvasId;
  70. }
  71. const system = wx.getSystemInfoSync().system
  72. if (/ios/i.test(system)) {
  73. ctx.draw(true, () => {
  74. wx.canvasToTempFilePath(opt, this);
  75. });
  76. } else {//针对安卓机型异步获取已绘制图层
  77. ctx.draw(true,()=>{
  78. //断点打印依旧不会执行setTimeout(() => {wx.canvasToTempFilePath(opt, this);}, 1000);}});
  79. ctx.draw(true);
  80. setTimeout(() => {//延迟获取
  81. wx.canvasToTempFilePath(opt, this);
  82. }, 1000);
  83. })
  84. }
  85. },
  86. touchStart(e) {
  87. if (this.chart && e.touches.length > 0) {
  88. var touch = e.touches[0];
  89. var handler = this.chart.getZr().handler;
  90. handler.dispatch('mousedown', {
  91. zrX: touch.x,
  92. zrY: touch.y
  93. });
  94. handler.dispatch('mousemove', {
  95. zrX: touch.x,
  96. zrY: touch.y
  97. });
  98. handler.processGesture(wrapTouch(e), 'start');
  99. }
  100. },
  101. touchMove(e) {
  102. if (this.chart && e.touches.length > 0) {
  103. var touch = e.touches[0];
  104. var handler = this.chart.getZr().handler;
  105. handler.dispatch('mousemove', {
  106. zrX: touch.x,
  107. zrY: touch.y
  108. });
  109. handler.processGesture(wrapTouch(e), 'change');
  110. }
  111. },
  112. touchEnd(e) {
  113. if (this.chart) {
  114. const touch = e.changedTouches ? e.changedTouches[0] : {};
  115. var handler = this.chart.getZr().handler;
  116. handler.dispatch('mouseup', {
  117. zrX: touch.x,
  118. zrY: touch.y
  119. });
  120. handler.dispatch('click', {
  121. zrX: touch.x,
  122. zrY: touch.y
  123. });
  124. handler.processGesture(wrapTouch(e), 'end');
  125. }
  126. }
  127. }
  128. });
  129. function wrapTouch(event) {
  130. for (let i = 0; i < event.touches.length; ++i) {
  131. const touch = event.touches[i];
  132. touch.offsetX = touch.x;
  133. touch.offsetY = touch.y;
  134. }
  135. return event;
  136. }