Blame view

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