西藏巴青项目

echarts.vue 2.8KB

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