zengh
2022-09-14 8ea19a6bb34daab1410f6186992a4c1e90299c92
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<template>
    <view class="lime-echart" :style="customStyle" v-if="canvasId">
        <!-- #ifndef APP-NVUE -->
        <canvas
            class="lime-echart__canvas"
            v-if="use2dCanvas"
            type="2d"
            :id="canvasId"
            :style="'width:' + width + 'px;height:' + height + 'px'"
            :disable-scroll="isDisableScroll"
            @touchstart="touchStart"
            @touchmove="touchMove"
            @touchend="touchEnd"
        />
        <canvas
            class="lime-echart__canvas"
            v-else
            :width="nodeWidth"
            :height="nodeHeight"
            :style="'width:' + width + 'px;height:' + height + 'px'"
            :canvas-id="canvasId"
            :id="canvasId"
            :disable-scroll="isDisableScroll"
            @touchstart="touchStart"
            @touchmove="touchMove"
            @touchend="touchEnd"
        />
        <!-- #endif -->
        <!-- #ifdef APP-NVUE -->
        <web-view
            class="lime-echart__canvas"
            :id="canvasId"
            :webview-styles="webviewStyles"
            ref="webview"
            src="http://liangei.gitee.io/limeui/hybrid/html/lime-ui/lime-echart/index.html?v=0.2.3"
            @pagefinish="finished = true"
            @onPostMessage="onMessage"
        ></web-view>
        <!-- #endif -->
    </view>
</template>
<script>
// #ifndef APP-NVUE
import Canvas from './canvas';
import * as echarts from './echarts';
import { compareVersion, wrapTouch, devicePixelRatio } from './utils';
// #endif
// #ifdef APP-NVUE
import { base64ToPath } from './utils';
// #endif
export default {
    // version: '0.2.9' 
    name: 'lime-echart',
    props: {
        // #ifdef MP-WEIXIN || MP-TOUTIAO
        type: {
            type: String,
            default: '2d'
        },
        // #endif
        ec: {
            type: Object,
            default: () => {}
        },
        onInit: Function,
        // #ifdef APP-NVUE
        webviewStyles: Object,
        params: {
            type: Object,
            default: () => {}
        },
        // #endif
        customStyle: String,
        isAutoPlay: Boolean,
        isDisableScroll: Boolean,
        isEnable: Boolean,
        isClickable: {
            type: Boolean,
            default: true
        }
    },
    data() {
        return {
            // #ifdef MP-WEIXIN || MP-TOUTIAO
            use2dCanvas: true,
            // #endif
            // #ifndef MP-WEIXIN || MP-TOUTIAO
            use2dCanvas: false,
            // #endif
            width: null,
            height: null,
            nodeWidth: null,
            nodeHeight: null,
            canvasNode: null,
            config: {},
            inited: false,
            finished: false,
            file: '',
            platform: ''
        };
    },
    computed: {
        canvasId() {
            return `lime-echart${this._uid}`
        }
    },
    watch: {
        'ec.option': {
            deep: true,
            handler(val, oldVal) {
                if(this.isAutoPlay) {
                    if(this.ec.clear){this.clear()}
                    this.setOption(val);
                }
            }
        }
    },
    beforeDestroy() {
        this.clear()
        this.dispose()
    },
    created() {
        // #ifdef APP-NVUE
        // this.parent = this.getParent()
        // #endif
        // #ifndef APP-NVUE
        echarts.registerPreprocessor(option => {
            if (option && option.series) {
                if (option.series.length > 0) {
                    option.series.forEach(series => {
                        series.progressive = 0;
                    });
                } else if (typeof option.series === 'object') {
                    option.series.progressive = 0;
                }
            }
        });
        // #ifdef MP-WEIXIN || MP-TOUTIAO
        const { SDKVersion, version, platform, environment } = uni.getSystemInfoSync();
        // #endif
        // #ifdef MP-WEIXIN
        this.isPC = /windows/i.test(platform)
        this.use2dCanvas = this.type === '2d' && compareVersion(SDKVersion, '2.9.2') >= 0 && !((/ios/i.test(platform) && /7.0.20/.test(version)) || /wxwork/i.test(environment)) && !this.isPC;
        // #endif
        // #ifdef MP-TOUTIAO
        this.isPC = /devtools/i.test(platform)
        this.use2dCanvas = this.type === '2d' && compareVersion(SDKVersion, '1.78.0') >= 0;
        // #endif
        // #endif
    },
    
    mounted() {
        if(this.ec && !this.ec?.lazyLoad || this.isEnable) {
            if(!this.onInit) {
                this.init();
                return
            }
        }
        if(this.onInit) {
            this.init(this.onInit)
        }
    },
    methods: {
        // #ifdef APP-NVUE
        getParent() {
            let parent = this.$parent;
            let parentRefs = Object.values(parent.$refs)
            while(!parentRefs.length || parentRefs.find((v) => v._uid && v.uid == this._uid)) {
                parent = parent.$parent
                parentRefs = Object.values(parent.$refs)
                if (!parent) return false
            }
            return parent;
        },
        getWebview() {
            if(this.finished) {
                return Promise.resolve(this.finished)
            }
            return new Promise(resolve => {
                this.$watch('finished', (val) => {
                    if(val) {
                        resolve(this.finished)
                    }
                })
            })
        },
        onMessage(e) {
            const res = e?.detail?.data[0] || null;
            if (res?.event) {
                if(res.event === 'inited') {
                    this.inited = true
                }
                this.$emit(res.event, JSON.parse(res.data));
            } else if(res?.file){
                this.file = res.data
            } else if(!res[0] && JSON.stringify(res[0]) != '{}'){
                console.error(res);
            }
        },
        // #endif
        async setChart(callback, params) {
            await this.enabled()
            // #ifndef APP-NVUE
            if(typeof callback === 'function' && this.chart) {
                await callback(this.chart);
            }
            // #endif
            // #ifdef APP-NVUE
            if(typeof callback === 'function') {
                this.$refs.webview.evalJs(`setChart(${JSON.stringify(callback.toString())}, ${JSON.stringify(params || this.params)})`);
            }
            // #endif
        },
        async setOption() {
            await this.enabled()
            // #ifndef APP-NVUE
            if (!this.chart || !this.chart.setOption) {
                console.warn(`组件还未初始化`)
            } else {
                this.chart.setOption(...arguments);
            }
            // #endif
            // #ifdef APP-NVUE
            if(arguments) {
                this.$refs.webview.evalJs(`setOption(${JSON.stringify(arguments)})`);
            }
            // #endif
        },
        async showLoading() {
            // #ifndef APP-NVUE
            await this.enabled()
            if(this.chart) {
                this.chart.showLoading()
            }
            // #endif
            // #ifdef APP-NVUE
            this.$refs.webview.evalJs(`showLoading()`);
            // #endif
        },
        hideLoading() {
            // #ifndef APP-NVUE
            if(this.chart) {
                this.chart.hideLoading()
            }
            // #endif
            // #ifdef APP-NVUE
            this.$refs.webview.evalJs(`hideLoading()`);
            // #endif
        },
        clear() {
            // #ifndef APP-NVUE
            if(this.chart) {
                this.chart.clear()
            }
            // #endif
            // #ifdef APP-NVUE
            this.$refs.webview.evalJs(`clear()`);
            // #endif
        },
        dispose() {
            // #ifndef APP-NVUE
            if(this.chart) {
                this.chart.dispose()
            }
            // #endif
            // #ifdef APP-NVUE
            this.$refs.webview.evalJs(`dispose()`);
            // #endif
        },
        canvasToTempFilePath(args = {}) {
            // #ifndef APP-NVUE
            const { use2dCanvas, canvasId, canvasNode } = this;
            return new Promise((resolve, reject) => {
                const copyArgs = Object.assign({
                    canvasId,
                    success: resolve,
                    fail: reject
                }, args);
                if (use2dCanvas) {
                    delete copyArgs.canvasId;
                    copyArgs.canvas = canvasNode;
                }
                uni.canvasToTempFilePath(copyArgs, this);
            });
            // #endif
            // #ifdef APP-NVUE
            this.file = ''
            this.$refs.webview.evalJs(`canvasToTempFilePath()`);
            return new Promise((resolve, reject) => {
                this.$watch('file', async (file) => {
                    if(file) {
                        const tempFilePath = await base64ToPath(file)
                        resolve(args.success({tempFilePath}))
                    } else {
                        reject(args.fail({error: ``}))
                    }
                })
            })
            // #endif
        },
        async init(callback, params) {
            
            
            
            // #ifndef APP-NVUE
            let config = await this.getContext();
            if(typeof callback === 'function') {
                this.chart = await callback(config);
            } else {
                const {canvas} = config
                this.chart = echarts.init(canvas, null, config);
                canvas.setChart(this.chart);
                if(this.ec && this.ec.option) {
                    this.setOption(this.ec.option)
                }
                this.inited = true
                this.$emit('inited', true)
            }
            // #endif
            // #ifdef APP-NVUE
            await this.getWebview()
            if(typeof callback === 'function') {
                this.$refs.webview.evalJs(`init(${JSON.stringify(callback.toString())}, ${JSON.stringify(params || this.params)})`);
            } else {
                this.$refs.webview.evalJs(`init()`);
                if(this.ec && this.ec.option) {
                    this.setOption(this.ec.option)
                }
            }
            // #endif
        },
        async enabled() {
            if(this.isEnable) {
                // #ifndef APP-NVUE
                await this.getContext();
                // #endif
                // #ifdef APP-NVUE
                if(this.inited) {
                    return Promise.resolve(this.inited)
                }
                return new Promise(resolve => {
                    this.$watch('inited', (val) => {
                        if(val) {
                            resolve(this.inited)
                        }
                    })
                })
                // #endif
            }
        },
        // #ifndef APP-NVUE
        getContext() {
            const { use2dCanvas, type = '2d', config } = this;
            let dpr = devicePixelRatio
            if (config.canvas) {
                return Promise.resolve(config);
            }
            if (use2dCanvas) {
                return new Promise(resolve => {
                    uni.createSelectorQuery()
                        .in(this)
                        .select(`#${this.canvasId}`)
                        .fields({
                            node: true,
                            size: true
                        })
                        .exec(res => {
                            let { node, width, height } = res[0];
                            width = width || 300;
                            height = height || 300;
                            const ctx = node.getContext(type);
                            // #ifdef MP-WEIXIN
                            node.width = width * dpr;
                            node.height = height * dpr;
                            // #endif
                            // #ifdef MP-TOUTIAO
                            // dpr = 1
                            // #endif
                            const canvas = new Canvas(ctx, this.canvasId, true, node);
                            echarts.setCanvasCreator(() => canvas);
                            this.canvasNode = node
                            this.config = { canvas, width, height, devicePixelRatio: dpr };
                            resolve(this.config);
                        });
                });
            }
            return new Promise(resolve => {
                uni.createSelectorQuery()
                    .in(this)
                    .select(`#${this.canvasId}`)
                    .boundingClientRect()
                    .exec(res => {
                        if (res) {
                            let { width, height } = res[0];
                            width = width || 300;
                            height = height || 300;
                            // #ifdef MP-TOUTIAO
                            dpr = !this.isPC ? devicePixelRatio : 1// 1.25
                            // #endif
                            // #ifdef MP-ALIPAY
                            dpr = devicePixelRatio
                            // #endif
                            // #ifndef MP-ALIPAY || MP-TOUTIAO
                            dpr = this.isPC ? devicePixelRatio : 1
                            // #endif
                            this.nodeWidth = width * dpr ;
                            this.nodeHeight = height * dpr ;
                            const ctx = uni.createCanvasContext(this.canvasId, this);
                            const canvas = new Canvas(ctx, this.canvasId, false);
                            echarts.setCanvasCreator(() => canvas);
                            this.config = { canvas, width, height, devicePixelRatio: dpr };
                            resolve(this.config);
                        }
                    });
            });
        },
        touchStart(e) {
            if (this.chart && e.touches.length > 0) {
                var touch = e.touches[0];
                var handler = this.chart.getZr().handler;
                handler.dispatch('mousedown', {
                    zrX: touch.x,
                    zrY: touch.y
                });
                handler.dispatch('mousemove', {
                    zrX: touch.x,
                    zrY: touch.y
                });
                handler.processGesture(wrapTouch(e), 'start');
            }
        },
        touchMove(e) {
            if (this.chart && e.touches.length > 0) {
                var touch = e.touches[0];
                var handler = this.chart.getZr().handler;
                handler.dispatch('mousemove', {
                    zrX: touch.x,
                    zrY: touch.y
                });
                handler.processGesture(wrapTouch(e), 'change');
            }
        },
        touchEnd(e) {
            if (this.chart) {
                const touch = e.changedTouches ? e.changedTouches[0] : {};
                var handler = this.chart.getZr().handler;
                handler.dispatch('mouseup', {
                    zrX: touch.x,
                    zrY: touch.y
                });
                if(this.isClickable) {
                    handler.dispatch('click', {
                        zrX: touch.x,
                        zrY: touch.y
                    });
                }
                handler.processGesture(wrapTouch(e), 'end');
            }
        }
        // #endif
    }
};
</script>
<style scoped lang="stylus">
full()
    // #ifndef APP-NVUE
    width 100%
    height 100%
    // #endif
    // #ifdef APP-NVUE
    flex 1
    // #endif
.lime-echart
    full()
    position relative
    &__canvas
        full()
</style>