-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (109 loc) · 3.76 KB
/
Copy pathindex.js
File metadata and controls
146 lines (109 loc) · 3.76 KB
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
/**
* Created by ji on 2015/7/16.
*/
var http = require('http'),
child_process = require('child_process'),
path = require("path"),
phantomjs = require('phantomjs-prebuilt');
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 post = http.request({
hostname: '127.0.0.1',
port: 8787,
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': 0
}
} , function (res){
})
var defaultExecuted = function(chart) { chart.renderer.arc(0).add();}
post.on('error' , function (e){
var childProcess = child_process.execFile(binPath, childArgs, function(err, stdout, stderr) {
if(err){
throw err;
}
console.log(stdout)
});
process.on('exit' , function() {
childProcess.kill();
})
process.on('disconnect' , function() {
childProcess.kill();
})
})
post.write("");
post.end();
/**
* 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)
}
);
// console.log(JSON.stringify(postData))
var post = http.request({
hostname: '127.0.0.1',
port: 8787,
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.replace(/[^\u0000-\u00ff]/g,"aaa").length
}
} , function (res){
var data = '';
res.on('data' , function (chunk){
data +=chunk.toString();
})
res.on('end' , function (){
if(data.indexOf("Error") >=0){
console.log('render error ....')
callback(new Error(data));
}else {
callback(null , data);
}
})
});
post.on('error' , function (e){
callback(e);
})
post.write(postData);
post.end();
};