-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_echarts_action.js
203 lines (188 loc) · 4.42 KB
/
gen_echarts_action.js
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
#!/usr/bin/env node
/**
* @license
* Copyright Google LLC All Rights Reserved.
*/
'use strict';
var request = require('request');
const fs = require('fs');
const ChartUtils = require('./chart-utils.js');
const cwd = process.cwd();
const colors = require('colors');
function readUrl(url, callBack) {
let remoteUrl = url;
if (url.startsWith('./')) {
remoteUrl = cwd + '/src/assets/examples/echarts/' + url;
} else if (url.startsWith('https://') || url.startsWith('http://') ) {
remoteUrl = url;
} else {
remoteUrl = 'https://echarts.apache.org/examples' + url;
}
if (remoteUrl.startsWith('https://') || remoteUrl.startsWith('http://') ) {
request.get(remoteUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(remoteUrl);
callBack(body);
} else {
console.log(remoteUrl, error, response);
}
});
} else {
fs.readFile(remoteUrl, 'utf-8',(err, body) => {
if (err) throw err;
console.log(remoteUrl);
if (callBack !== undefined && callBack !== null) {
callBack(body);
}
});
}
}
const $ = {
get: function (url, callBack) {
readUrl(url, (txt) => {
callBack(JSON.parse(txt));
});
},
getJSON: function (url, callBack) {
readUrl(url, (txt) => {
callBack(JSON.parse(txt));
});
},
when: function (url1, url2, callBack) {
readUrl(url1, (txt) => {
const json1 = JSON.parse(txt);
readUrl(url2, (txt) => {
const json2 = JSON.parse(txt);
callBack(json1, json2);
});
});
},
};
const echarts = {
number: {
parseDate: function (dateStr) {
return new Date(dateStr);
},
},
format : {
formatTime : function(format, time) {
const date = new Date(time);
format = format.replace("yyyy", '' + date.getFullYear());
const month = date.getMonth() + 1;
format = format.replace("MM", month > 9 ? '' + month : '0' + month);
const day = date.getDate();
format = format.replace("dd", day > 9 ? '' + day : '0' + day);
return format;
}
},
color: {
modifyHSL: function (color, hstep) {
return Utils.transparentize(color, hstep / 360);
},
},
};
const Utils = ChartUtils;
function LinearGradient(x, y, x2, y2, colorStops, globalCoord) {
return {
colorStops: colorStops || [],
x: x == null ? 0 : x,
y: y == null ? 0 : y,
x2: x2 == null ? 1 : x2,
y2: y2 == null ? 0 : y2,
type: 'linear',
global: globalCoord || false,
};
}
function RadialGradient(x, y, r, colorStops, globalCoord) {
return {
colorStops: colorStops || [],
x: x == null ? 0 : x,
y: y == null ? 0 : y,
r: r == null ? 1 : r,
type: 'radial',
global: globalCoord || false,
};
}
const ROOT_PATH = '';
let option = {};
const myChart = {
showLoading: function () {},
hideLoading: function () {},
setOption: function (option) {
setTimeout(function () {
const chartConfig = option;
chartConfig.actions = actions;
chartConfig.sharedVar = sharedVar;
writeJson(fileName, chartConfig);
}, 100);
},
getWidth: function () {
return 512;
},
getHeight: function () {
return 512;
},
};
function writeJson(fileName, jsonData, comment, callBack) {
let data = ChartUtils.stringify(jsonData);
if (comment !== undefined && comment !== null && comment !== '') {
data = '/** ' + comment + ' */' + data;
}
const filePath =
cwd +
'/src/assets/examples/echarts/' +
fileName +
(jsonData !== null ? '.json' : '.txt');
fs.writeFile(filePath, data, (err) => {
if (err) throw err;
console.log(filePath);
if (callBack !== undefined && callBack !== null) {
callBack();
}
});
}
let actions = [];
let sharedVar = {};
const fileName = 'geo-seatmap-flight';
try {
$.getJSON('./' + fileName + '.json', function(option) {
sharedVar = option.sharedVar || {};
sharedVar = {};
actions = [
{
name: 'Randomize',
handler: (chart) => {
const options = chart.getOption();
options.geo[0].regions = options.geo[0].regions.map((regions, idx) => {
if (Math.random() > 0.9) {
return {
"name": regions.name,
"silent": true,
"itemStyle": {
"color": "#bf0e08"
},
"emphasis": {
"itemStyle": {
"borderColor": "#aaa",
"borderWidth": 1
}
},
"select": {
"itemStyle": {
"color": "#bf0e08"
}
}
}
} else {
return { name : regions.name }
}
});
chart.setOption(options);
},
},
];
myChart.setOption(option);
});
} catch (ex) {
console.log(ex);
}