wjc
17 小时以前 2f5411b86c59706d31f6ec7de629d8f860725e46
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
<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
            content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title></title>
        <style type="text/css">
            html,
            body,
            .canvas {
                padding: 0;
                margin: 0;
                overflow-y: hidden;
                background-color: transparent;
                width: 100%;
                height: 100%;
            }
        </style>
    </head>
    <body>
        <div class="canvas" id="limeChart"></div>
        <script type="text/javascript" src="./uni.webview.1.5.3.js"></script>
        <script type="text/javascript" src="./echarts.min.js"></script>
        <script type="text/javascript" src="./ecStat.min.js"></script>
        <!-- <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts-liquidfill@latest/dist/echarts-liquidfill.min.js"></script> -->
        <script>
            let chart = null;
            let cache = [];
            console.log = function(...agrs) {
                postMessage(agrs)
            }
 
            function emit(event, data) {
                let dataStr = JSON.stringify(data, stringify)
                postMessage({
                    event,
                    data: dataStr
                })
                cache = []
            }
 
            function postMessage(data) {
                uni.postMessage({
                    data
                });
            }
 
            function stringify(key, value) {
                if (typeof value === 'object' && value !== null) {
                    if (cache.indexOf(value) !== -1) {
                        return;
                    }
                    cache.push(value);
                }
                return value;
            }
 
            function parse(name, callback, options) {
                const optionNameReg = /[\w]+\.setOption\(([\w]+\.)?([\w]+)\)/
                if (optionNameReg.test(callback)) {
                    const optionNames = callback.match(optionNameReg)
                    if (optionNames[1]) {
                        const _this = optionNames[1].split('.')[0]
                        window[_this] = {}
                        window[_this][optionNames[2]] = options
                        return optionNames[2]
                    } else {
                        return null
                    }
                }
                return null
            }
 
            function init(callback, options, opts = {}, theme = null) {
                if (!chart) {
                    chart = echarts.init(document.getElementById('limeChart'), theme, opts)
                    if (options) {
                        chart.setOption(options)
                    }
                    // const name = parse('a', callback, options)
                    // console.log('options::', callback)
                    // if(name) this[name] = options
                    // eval(`a = ${callback};`)
                    // if(a) {a(chart)}
                }
            }
 
            function setChart(callback, options) {
                if (!callback) return
                if (chart && callback && options) {
                    var r = null
                    const name = parse('r', callback, options)
                    if (name) this[name] = options
                    eval(`r = ${callback};`)
                    if (r) {
                        r(chart)
                    }
                }
            }
 
            function setOption(data) {
                if (chart) chart.setOption(data[0], data[1])
            }
 
            function showLoading(data) {
                if (chart) chart.showLoading(data[0], data[1])
            }
 
            function hideLoading() {
                if (chart) chart.hideLoading()
            }
 
            function clear() {
                if (chart) chart.clear()
 
            }
 
            function dispose() {
                if (chart) chart.dispose()
            }
 
            function resize(size) {
                if (chart) chart.resize(size)
            }
 
            function canvasToTempFilePath(opt = {}) {
                if (chart) {
                    const src = chart.getDataURL(opt)
                    postMessage({
                        file: true,
                        data: src
                    })
                }
            }
 
            function on(data) {
                if (chart && data.length > 0) {
                    const [type, query] = data
                    const useQuery = query && typeof query != 'function'
                    const key = `${type}${useQuery ? JSON.stringify(query): '' }`
                    if (query) {
                        chart.on(type, query, (options) => {
                            const obj = {}
                            Object.keys(options).forEach(key => {
                                if (key != 'event') {
                                    obj[key] = options[key]
                                }
                            })
                            emit(`@${key}`, {
                                event: key,
                                options: obj
                            })
                        })
                    } else {
                        chart.on(type, (options) => {
                            
                            const obj = {}
                            Object.keys(options).forEach(key => {
                                if (key != 'event') {
                                    obj[key] = options[key]
                                }
 
                            })
                            emit(`@${key}`, {
                                event: key,
                                options: obj
                            })
                        })
                    }
                }
 
            }
        </script>
    </body>
</html>