echarts.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <canvas v-if="canvasId" class="ec-canvas" :id="canvasId" :canvasId="canvasId" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd" @error="error"></canvas>
  3. </template>
  4. <script>
  5. import WxCanvas from './wx-canvas';
  6. export default {
  7. props: {
  8. canvasId: {
  9. type: String,
  10. default: 'ec-canvas'
  11. },
  12. lazyLoad: {
  13. type: Boolean,
  14. default: false
  15. },
  16. disableTouch: {
  17. type: Boolean,
  18. default: false
  19. },
  20. throttleTouch: {
  21. type: Boolean,
  22. default: false
  23. }
  24. },
  25. // #ifdef H5
  26. mounted() {
  27. if (!this.lazyLoad) this.init();
  28. },
  29. // #endif
  30. // #ifndef H5
  31. onReady() {
  32. if (!this.lazyLoad) this.init();
  33. },
  34. // #endif
  35. methods: {
  36. setChart(chart){
  37. this.chart = chart
  38. },
  39. init() {
  40. const { canvasId } = this;
  41. this.ctx = wx.createCanvasContext(canvasId, this);
  42. this.canvas = new WxCanvas(this.ctx, canvasId);
  43. const query = wx.createSelectorQuery().in(this);
  44. query
  45. .select(`#${canvasId}`)
  46. .boundingClientRect(res => {
  47. if (!res) {
  48. setTimeout(() => this.init(), 50);
  49. return;
  50. }
  51. this.$emit('onInit', {
  52. width: res.width,
  53. height: res.height
  54. });
  55. })
  56. .exec();
  57. },
  58. canvasToTempFilePath(opt) {
  59. const { canvasId } = this;
  60. this.ctx.draw(true, () => {
  61. wx.canvasToTempFilePath({
  62. canvasId,
  63. ...opt
  64. });
  65. });
  66. },
  67. touchStart(e) {
  68. const { disableTouch, chart } = this;
  69. if (disableTouch || !chart || !e.mp.touches.length) return;
  70. const touch = e.mp.touches[0];
  71. chart._zr.handler.dispatch('mousedown', {
  72. zrX: touch.x,
  73. zrY: touch.y
  74. });
  75. chart._zr.handler.dispatch('mousemove', {
  76. zrX: touch.x,
  77. zrY: touch.y
  78. });
  79. },
  80. touchMove(e) {
  81. const { disableTouch, throttleTouch, chart, lastMoveTime } = this;
  82. if (disableTouch || !chart || !e.mp.touches.length) return;
  83. if (throttleTouch) {
  84. const currMoveTime = Date.now();
  85. if (currMoveTime - lastMoveTime < 240) return;
  86. this.lastMoveTime = currMoveTime;
  87. }
  88. const touch = e.mp.touches[0];
  89. chart._zr.handler.dispatch('mousemove', {
  90. zrX: touch.x,
  91. zrY: touch.y
  92. });
  93. },
  94. touchEnd(e) {
  95. const { disableTouch, chart } = this;
  96. if (disableTouch || !chart) return;
  97. const touch = e.mp.changedTouches ? e.mp.changedTouches[0] : {};
  98. chart._zr.handler.dispatch('mouseup', {
  99. zrX: touch.x,
  100. zrY: touch.y
  101. });
  102. chart._zr.handler.dispatch('click', {
  103. zrX: touch.x,
  104. zrY: touch.y
  105. });
  106. }
  107. }
  108. };
  109. </script>
  110. <style scoped>
  111. .ec-canvas {
  112. width: 100%;
  113. height: 100%;
  114. flex: 1;
  115. }
  116. </style>