-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrenderPhantom.js
80 lines (68 loc) · 2.8 KB
/
renderPhantom.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
/*
ECharts offline image export server with Node.js
Copyright (C) 2018, 2021 Dirk Stolle
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
var page = require('webpage').create();
var system = require('system');
var fs = require('fs');
if (system.args.length !== 6) {
console.log('Usage: phantom renderPhantom.js template.html config.json output.png width height');
phantom.exit(1);
} else {
// Get all file names from command line parameters.
var template = system.args[1];
var templatePath = fs.absolute(template);
var configData = system.args[2];
var configDataPath = fs.absolute(configData);
var outFile = system.args[3];
var width = parseInt(system.args[4], 10);
var height = parseInt(system.args[5], 10);
// Read the JSON data for the ECharts plot.
var configString = fs.read(configDataPath);
var chartJson = {};
try {
chartJson = JSON.parse(configString);
} catch (e) {
console.log('Could not parse config data into JSON!');
phantom.exit(1);
}
// Add white background colour, if none is set.
if (!chartJson.backgroundColor) {
chartJson.backgroundColor = '#fff';
}
// Disable animation to render immediately.
chartJson.animation = false;
// Handle image size and fallback to 700 x 400, if necessary.
width = width || parseInt(chartJson.imageWidth, 10) || 700;
height = height || parseInt(chartJson.imageHeight, 10) || 400;
page.onLoadFinished = function (status) {
// Create the chart by making a new plot with the given data.
console.log('Creating chart ...');
page.evaluate(function (chartJson, width, height) {
document.getElementById('plot').style.width = width + 'px';
document.getElementById('plot').style.height = height + 'px';
var chart = echarts.init(document.getElementById('plot'));
chart.setOption(chartJson);
}, chartJson, width, height);
// The evaluation and drawing is done, now render it to PNG.
console.log('rendering image...');
page.render(outFile, {
format: 'png'
});
// Success. (Unless someone specified useless JSON data.)
phantom.exit(0);
};
// Setting content of the page directly is effectively the same as a reload.
page.content = fs.read(templatePath);
}