-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
118 lines (92 loc) · 3.18 KB
/
index.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
/**
* Created by ji on 2015/7/16.
*/
var http = require('http'),
childProcess = require('child_process'),
path = require("path"),
phantomjs = require('phantomjs');
var binPath = phantomjs.path;
var childArgs = [
path.join( __dirname ,'lib' ,'highcharts-convert.js'),
'-host', '127.0.0.1' ,'-port', '8787'
]
if(!binPath){
throw new Error("can no found phantomjs , see https://github.com/BetterJS/badjs-web/issues/23")
}
var childProcess = childProcess.execFile(binPath, childArgs, {maxBuffer:1024*500},function(err, stdout, stderr) {
if(err){
throw err;
}
console.log(stdout)
});
var defaultExecuted = function(chart) { chart.renderer.arc(0).add();}
process.on('exit' , function() {
childProcess.kill();
})
process.on('disconnect' , function() {
childProcess.kill();
})
/**
* obj
* data - Highcharts configuration object.
* scale - A scaling factor for a higher image resolution. Maximum scaling is set to 4x. Remember that the width parameter has a higher precedence over scaling. default is 1
* width - The exact pixel width of the exported image. Defaults to chart.width or 600px. Maximum width is 2000px.
* type - Image format , the type can be of jpg, png, pfd or svg for , default is png.
* constr - Can be one of Chart or StockChart. This depends on whether you want to generate Highstock or basic Highcharts
* executed - The executed is a function which will be called in the constructor of Highcharts to be executed , the highchart object should as parameter pass into the function and named chart
* options - The options is init for Highcharts , see Highcharts.setOptions
*
* callback
* - when file generated and will be called
* @param obj
* @param callback
*/
module.exports = function (obj , callback){
if(obj.executed){
obj.executed = " function(chart) {" +obj.executed.toString(); + "}";
}else {
obj.executed = defaultExecuted.toString();
}
var globaloptions = {};
if(obj.options){
globaloptions = obj.options;
}
var postData = JSON.stringify(
{
"infile": JSON.stringify(obj.data),
"callback":obj.executed,
"constr": obj.constr || "Chart" ,
"width" : obj.width || 600,
"type" : obj.type || "png",
"scale" : obj.scale || "1",
"globaloptions" : JSON.stringify(globaloptions)
}
);
var post = http.request({
hostname: '127.0.0.1',
port: 8787,
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
'Content-Length': Buffer.byteLength(postData,'utf8')
}
} , function (res){
var data = '';
res.on('data' , function (chunk){
data +=chunk.toString();
})
res.on('end' , function (){
if(data.indexOf("Error") >=0){
callback(new Error(data));
}else {
callback(null , data);
}
})
});
post.on('error' , function (e){
callback(e);
})
post.write(postData);
post.end();
};