|
@@ -0,0 +1,265 @@
|
|
1
|
+import WxCanvas from './wx-canvas';
|
|
2
|
+import * as echarts from './echarts';
|
|
3
|
+
|
|
4
|
+let ctx;
|
|
5
|
+
|
|
6
|
+function compareVersion(v1, v2) {
|
|
7
|
+ v1 = v1.split('.')
|
|
8
|
+ v2 = v2.split('.')
|
|
9
|
+ const len = Math.max(v1.length, v2.length)
|
|
10
|
+
|
|
11
|
+ while (v1.length < len) {
|
|
12
|
+ v1.push('0')
|
|
13
|
+ }
|
|
14
|
+ while (v2.length < len) {
|
|
15
|
+ v2.push('0')
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ for (let i = 0; i < len; i++) {
|
|
19
|
+ const num1 = parseInt(v1[i])
|
|
20
|
+ const num2 = parseInt(v2[i])
|
|
21
|
+
|
|
22
|
+ if (num1 > num2) {
|
|
23
|
+ return 1
|
|
24
|
+ } else if (num1 < num2) {
|
|
25
|
+ return -1
|
|
26
|
+ }
|
|
27
|
+ }
|
|
28
|
+ return 0
|
|
29
|
+}
|
|
30
|
+
|
|
31
|
+Component({
|
|
32
|
+ properties: {
|
|
33
|
+ canvasId: {
|
|
34
|
+ type: String,
|
|
35
|
+ value: 'ec-canvas'
|
|
36
|
+ },
|
|
37
|
+
|
|
38
|
+ ec: {
|
|
39
|
+ type: Object
|
|
40
|
+ },
|
|
41
|
+
|
|
42
|
+ forceUseOldCanvas: {
|
|
43
|
+ type: Boolean,
|
|
44
|
+ value: false
|
|
45
|
+ }
|
|
46
|
+ },
|
|
47
|
+
|
|
48
|
+ data: {
|
|
49
|
+ isUseNewCanvas: false
|
|
50
|
+ },
|
|
51
|
+
|
|
52
|
+ ready: function () {
|
|
53
|
+ // Disable prograssive because drawImage doesn't support DOM as parameter
|
|
54
|
+ // See https://developers.weixin.qq.com/miniprogram/dev/api/canvas/CanvasContext.drawImage.html
|
|
55
|
+ echarts.registerPreprocessor(option => {
|
|
56
|
+ if (option && option.series) {
|
|
57
|
+ if (option.series.length > 0) {
|
|
58
|
+ option.series.forEach(series => {
|
|
59
|
+ series.progressive = 0;
|
|
60
|
+ });
|
|
61
|
+ }
|
|
62
|
+ else if (typeof option.series === 'object') {
|
|
63
|
+ option.series.progressive = 0;
|
|
64
|
+ }
|
|
65
|
+ }
|
|
66
|
+ });
|
|
67
|
+
|
|
68
|
+ if (!this.data.ec) {
|
|
69
|
+ console.warn('组件需绑定 ec 变量,例:<ec-canvas id="mychart-dom-bar" '
|
|
70
|
+ + 'canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>');
|
|
71
|
+ return;
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ if (!this.data.ec.lazyLoad) {
|
|
75
|
+ this.init();
|
|
76
|
+ }
|
|
77
|
+ },
|
|
78
|
+
|
|
79
|
+ methods: {
|
|
80
|
+ init: function (callback) {
|
|
81
|
+ const version = wx.getSystemInfoSync().SDKVersion
|
|
82
|
+
|
|
83
|
+ const canUseNewCanvas = compareVersion(version, '2.9.0') >= 0;
|
|
84
|
+ const forceUseOldCanvas = this.data.forceUseOldCanvas;
|
|
85
|
+ const isUseNewCanvas = canUseNewCanvas && !forceUseOldCanvas;
|
|
86
|
+ this.setData({ isUseNewCanvas });
|
|
87
|
+
|
|
88
|
+ if (forceUseOldCanvas && canUseNewCanvas) {
|
|
89
|
+ console.warn('开发者强制使用旧canvas,建议关闭');
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ if (isUseNewCanvas) {
|
|
93
|
+ // console.log('微信基础库版本大于2.9.0,开始使用<canvas type="2d"/>');
|
|
94
|
+ // 2.9.0 可以使用 <canvas type="2d"></canvas>
|
|
95
|
+ this.initByNewWay(callback);
|
|
96
|
+ } else {
|
|
97
|
+ const isValid = compareVersion(version, '1.9.91') >= 0
|
|
98
|
+ if (!isValid) {
|
|
99
|
+ console.error('微信基础库版本过低,需大于等于 1.9.91。'
|
|
100
|
+ + '参见:https://github.com/ecomfe/echarts-for-weixin'
|
|
101
|
+ + '#%E5%BE%AE%E4%BF%A1%E7%89%88%E6%9C%AC%E8%A6%81%E6%B1%82');
|
|
102
|
+ return;
|
|
103
|
+ } else {
|
|
104
|
+ console.warn('建议将微信基础库调整大于等于2.9.0版本。升级后绘图将有更好性能');
|
|
105
|
+ this.initByOldWay(callback);
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+ },
|
|
109
|
+
|
|
110
|
+ initByOldWay(callback) {
|
|
111
|
+ // 1.9.91 <= version < 2.9.0:原来的方式初始化
|
|
112
|
+ ctx = wx.createCanvasContext(this.data.canvasId, this);
|
|
113
|
+ const canvas = new WxCanvas(ctx, this.data.canvasId, false);
|
|
114
|
+
|
|
115
|
+ echarts.setCanvasCreator(() => {
|
|
116
|
+ return canvas;
|
|
117
|
+ });
|
|
118
|
+ // const canvasDpr = wx.getSystemInfoSync().pixelRatio // 微信旧的canvas不能传入dpr
|
|
119
|
+ const canvasDpr = 1
|
|
120
|
+ var query = wx.createSelectorQuery().in(this);
|
|
121
|
+ query.select('.ec-canvas').boundingClientRect(res => {
|
|
122
|
+ if (typeof callback === 'function') {
|
|
123
|
+ this.chart = callback(canvas, res.width, res.height, canvasDpr);
|
|
124
|
+ }
|
|
125
|
+ else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
|
|
126
|
+ this.chart = this.data.ec.onInit(canvas, res.width, res.height, canvasDpr);
|
|
127
|
+ }
|
|
128
|
+ else {
|
|
129
|
+ this.triggerEvent('init', {
|
|
130
|
+ canvas: canvas,
|
|
131
|
+ width: res.width,
|
|
132
|
+ height: res.height,
|
|
133
|
+ canvasDpr: canvasDpr // 增加了dpr,可方便外面echarts.init
|
|
134
|
+ });
|
|
135
|
+ }
|
|
136
|
+ }).exec();
|
|
137
|
+ },
|
|
138
|
+
|
|
139
|
+ initByNewWay(callback) {
|
|
140
|
+ // version >= 2.9.0:使用新的方式初始化
|
|
141
|
+ const query = wx.createSelectorQuery().in(this)
|
|
142
|
+ query
|
|
143
|
+ .select('.ec-canvas')
|
|
144
|
+ .fields({ node: true, size: true })
|
|
145
|
+ .exec(res => {
|
|
146
|
+ const canvasNode = res[0].node
|
|
147
|
+ this.canvasNode = canvasNode
|
|
148
|
+
|
|
149
|
+ const canvasDpr = wx.getSystemInfoSync().pixelRatio
|
|
150
|
+ const canvasWidth = res[0].width
|
|
151
|
+ const canvasHeight = res[0].height
|
|
152
|
+
|
|
153
|
+ const ctx = canvasNode.getContext('2d')
|
|
154
|
+
|
|
155
|
+ const canvas = new WxCanvas(ctx, this.data.canvasId, true, canvasNode)
|
|
156
|
+ echarts.setCanvasCreator(() => {
|
|
157
|
+ return canvas
|
|
158
|
+ })
|
|
159
|
+
|
|
160
|
+ if (typeof callback === 'function') {
|
|
161
|
+ this.chart = callback(canvas, canvasWidth, canvasHeight, canvasDpr)
|
|
162
|
+ } else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
|
|
163
|
+ this.chart = this.data.ec.onInit(canvas, canvasWidth, canvasHeight, canvasDpr)
|
|
164
|
+ } else {
|
|
165
|
+ this.triggerEvent('init', {
|
|
166
|
+ canvas: canvas,
|
|
167
|
+ width: canvasWidth,
|
|
168
|
+ height: canvasHeight,
|
|
169
|
+ dpr: canvasDpr
|
|
170
|
+ })
|
|
171
|
+ }
|
|
172
|
+ })
|
|
173
|
+ },
|
|
174
|
+ canvasToTempFilePath(opt) {
|
|
175
|
+ if (this.data.isUseNewCanvas) {
|
|
176
|
+ // 新版
|
|
177
|
+ const query = wx.createSelectorQuery().in(this)
|
|
178
|
+ query
|
|
179
|
+ .select('.ec-canvas')
|
|
180
|
+ .fields({ node: true, size: true })
|
|
181
|
+ .exec(res => {
|
|
182
|
+ const canvasNode = res[0].node
|
|
183
|
+ opt.canvas = canvasNode
|
|
184
|
+ wx.canvasToTempFilePath(opt)
|
|
185
|
+ })
|
|
186
|
+ } else {
|
|
187
|
+ // 旧的
|
|
188
|
+ if (!opt.canvasId) {
|
|
189
|
+ opt.canvasId = this.data.canvasId;
|
|
190
|
+ }
|
|
191
|
+ ctx.draw(true, () => {
|
|
192
|
+ wx.canvasToTempFilePath(opt, this);
|
|
193
|
+ });
|
|
194
|
+ }
|
|
195
|
+ },
|
|
196
|
+
|
|
197
|
+ touchStart(e) {
|
|
198
|
+ if (this.chart && e.touches.length > 0) {
|
|
199
|
+ var touch = e.touches[0];
|
|
200
|
+ var handler = this.chart.getZr().handler;
|
|
201
|
+ handler.dispatch('mousedown', {
|
|
202
|
+ zrX: touch.x,
|
|
203
|
+ zrY: touch.y,
|
|
204
|
+ preventDefault: () => {},
|
|
205
|
+ stopImmediatePropagation: () => {},
|
|
206
|
+ stopPropagation: () => {}
|
|
207
|
+ });
|
|
208
|
+ handler.dispatch('mousemove', {
|
|
209
|
+ zrX: touch.x,
|
|
210
|
+ zrY: touch.y,
|
|
211
|
+ preventDefault: () => {},
|
|
212
|
+ stopImmediatePropagation: () => {},
|
|
213
|
+ stopPropagation: () => {}
|
|
214
|
+ });
|
|
215
|
+ handler.processGesture(wrapTouch(e), 'start');
|
|
216
|
+ }
|
|
217
|
+ },
|
|
218
|
+
|
|
219
|
+ touchMove(e) {
|
|
220
|
+ if (this.chart && e.touches.length > 0) {
|
|
221
|
+ var touch = e.touches[0];
|
|
222
|
+ var handler = this.chart.getZr().handler;
|
|
223
|
+ handler.dispatch('mousemove', {
|
|
224
|
+ zrX: touch.x,
|
|
225
|
+ zrY: touch.y,
|
|
226
|
+ preventDefault: () => {},
|
|
227
|
+ stopImmediatePropagation: () => {},
|
|
228
|
+ stopPropagation: () => {}
|
|
229
|
+ });
|
|
230
|
+ handler.processGesture(wrapTouch(e), 'change');
|
|
231
|
+ }
|
|
232
|
+ },
|
|
233
|
+
|
|
234
|
+ touchEnd(e) {
|
|
235
|
+ if (this.chart) {
|
|
236
|
+ const touch = e.changedTouches ? e.changedTouches[0] : {};
|
|
237
|
+ var handler = this.chart.getZr().handler;
|
|
238
|
+ handler.dispatch('mouseup', {
|
|
239
|
+ zrX: touch.x,
|
|
240
|
+ zrY: touch.y,
|
|
241
|
+ preventDefault: () => {},
|
|
242
|
+ stopImmediatePropagation: () => {},
|
|
243
|
+ stopPropagation: () => {}
|
|
244
|
+ });
|
|
245
|
+ handler.dispatch('click', {
|
|
246
|
+ zrX: touch.x,
|
|
247
|
+ zrY: touch.y,
|
|
248
|
+ preventDefault: () => {},
|
|
249
|
+ stopImmediatePropagation: () => {},
|
|
250
|
+ stopPropagation: () => {}
|
|
251
|
+ });
|
|
252
|
+ handler.processGesture(wrapTouch(e), 'end');
|
|
253
|
+ }
|
|
254
|
+ }
|
|
255
|
+ }
|
|
256
|
+});
|
|
257
|
+
|
|
258
|
+function wrapTouch(event) {
|
|
259
|
+ for (let i = 0; i < event.touches.length; ++i) {
|
|
260
|
+ const touch = event.touches[i];
|
|
261
|
+ touch.offsetX = touch.x;
|
|
262
|
+ touch.offsetY = touch.y;
|
|
263
|
+ }
|
|
264
|
+ return event;
|
|
265
|
+}
|