ec-canvas.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import WxCanvas from './wx-canvas';
  2. import * as echarts from './echarts';
  3. const app = getApp();
  4. const scale = app.scale;
  5. let ctx;
  6. Component({
  7. properties: {
  8. canvasId: {
  9. type: String,
  10. value: 'ec-canvas'
  11. },
  12. ec: {
  13. type: Object
  14. }
  15. },
  16. data: {
  17. },
  18. ready: function () {
  19. if (!this.data.ec) {
  20. console.warn('组件需绑定 ec 变量,例:<ec-canvas id="mychart-dom-bar" '
  21. + 'canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>');
  22. return;
  23. }
  24. if (!this.data.ec.lazyLoad) {
  25. this.init();
  26. }
  27. },
  28. methods: {
  29. init: function (callback) {
  30. const version = wx.version.version.split('.').map(n => parseInt(n, 10));
  31. const isValid = version[0] > 1 || (version[0] === 1 && version[1] > 9)
  32. || (version[0] === 1 && version[1] === 9 && version[2] >= 91);
  33. if (!isValid) {
  34. console.error('微信基础库版本过低,需大于等于 1.9.91。'
  35. + '参见:https://github.com/ecomfe/echarts-for-weixin'
  36. + '#%E5%BE%AE%E4%BF%A1%E7%89%88%E6%9C%AC%E8%A6%81%E6%B1%82');
  37. return;
  38. }
  39. ctx = wx.createCanvasContext(this.data.canvasId, this);
  40. const canvas = new WxCanvas(ctx, this.data.canvasId);
  41. echarts.setCanvasCreator(() => {
  42. return canvas;
  43. });
  44. var query = wx.createSelectorQuery().in(this);
  45. query.select('.ec-canvas').boundingClientRect(res => {
  46. if (typeof callback === 'function') {
  47. this.chart = callback(canvas, res.width, res.height);
  48. }
  49. else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  50. this.chart = this.data.ec.onInit(canvas, res.width, res.height);
  51. }
  52. else {
  53. this.triggerEvent('init', {
  54. canvas: canvas,
  55. width: res.width,
  56. height: res.height
  57. });
  58. }
  59. }).exec();
  60. },
  61. canvasToTempFilePath(opt) {
  62. if (!opt.canvasId) {
  63. opt.canvasId = this.data.canvasId;
  64. }
  65. ctx.draw(true, () => {
  66. wx.canvasToTempFilePath(opt, this);
  67. });
  68. },
  69. touchStart(e) {
  70. if (this.chart && e.touches.length > 0) {
  71. var touch = e.touches[0];
  72. var handler = this.chart.getZr().handler;
  73. handler.dispatch('mousedown', {
  74. zrX: touch.x,
  75. zrY: touch.y
  76. });
  77. handler.dispatch('mousemove', {
  78. zrX: touch.x,
  79. zrY: touch.y
  80. });
  81. handler.processGesture(wrapTouch(e), 'start');
  82. }
  83. },
  84. touchMove(e) {
  85. if (this.chart && e.touches.length > 0) {
  86. var touch = e.touches[0];
  87. var handler = this.chart.getZr().handler;
  88. var width = this.chart._dom.width;
  89. var height = this.chart._dom.height;
  90. if (touch.x < 50 * scale || touch.x > width || touch.y < 40 * scale || touch.y > (height - 120 * scale)) return;
  91. handler.dispatch('mousemove', {
  92. zrX: touch.x,
  93. zrY: touch.y
  94. });
  95. handler.processGesture(wrapTouch(e), 'change');
  96. }
  97. },
  98. touchEnd(e) {
  99. if (this.chart) {
  100. const touch = e.changedTouches ? e.changedTouches[0] : {};
  101. var handler = this.chart.getZr().handler;
  102. handler.dispatch('mouseup', {
  103. zrX: touch.x,
  104. zrY: touch.y
  105. });
  106. handler.dispatch('click', {
  107. zrX: touch.x,
  108. zrY: touch.y
  109. });
  110. handler.processGesture(wrapTouch(e), 'end');
  111. }
  112. }
  113. }
  114. });
  115. function wrapTouch(event) {
  116. for (let i = 0; i < event.touches.length; ++i) {
  117. const touch = event.touches[i];
  118. touch.offsetX = touch.x;
  119. touch.offsetY = touch.y;
  120. }
  121. return event;
  122. }