forked from usubram/nodejs-plotter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotter.js
260 lines (233 loc) · 8.38 KB
/
plotter.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* Richard Meadows 2012, 2013, 2014 */
/* -------- Includes -------- */
var exec = require('child_process').exec;
var _ = require('underscore');
/* -------- Helper Functions -------- */
/**
* Performs a n-point moving average on array.
*/
function moving_average(array, n) {
var nums = [];
for (i in array) {
/* If this item in the array is a number */
if (_.isNumber(array[i])) {
nums.push(array[i]);
if (nums.length > n) {
nums.splice(0,1); /* Remove the first element of the array */
}
/* Take the average of the n items in this array */
var sum = _.reduce(nums, function(memo, num){ return memo + num; }, 0);
array[i] = sum/nums.length;
}
}
return array;
}
/**
* Performs a n-point maximum on array.
*/
function moving_maximum(array, n) {
var nums = [];
for (i in array) {
if (_.isNumber(array[i])) {
nums.push(array[i]);
if (nums.length > n) {
nums.splice(0,1); /* Remove the first element of the array */
}
/* Take the average of the n items in this array */
var maximum = _.max(nums);
array[i] = maximum;
}
}
return array;
}
/**
* Applys an n-point moving filter to a set of series.
*/
function apply_moving_filter(set, filter, n) {
if (!_.isNumber(n)) { n = 3; }
for (series in set) { /* For each series */
/* Apply the filter */
set[series] = filter(set[series], n);
}
return set;
}
/**
* Returns the string to give to gnuplot based on the value of options.time.
*/
function time_format(time) {
if (_.isString(time)) {
/* Translate the string we've been given into a format */
switch(time) {
case 'days':
case 'Days':
return "%d/%m";
case 'hours':
case 'Hours':
return "%H:%M";
default: /* Presume we've been given a gnuplot-readable time format string */
return time;
}
} else { /* Just default to hours */
return "%H:%M";
}
}
/**
* Sets up gnuplot based on the properties we're given in the options object.
*/
function setup_gnuplot(gnuplot, options) {
var bg = (options.color && options.color.bg) || 'black';
var border = (options.color && options.color.border) || 'white';
var ytics = (options.color && options.color.ytics) || 'white';
var xtics = (options.color && options.color.xtics) || 'white';
var xlabel = (options.color && options.color.xlabel) || 'white';
var ylabel = (options.color && options.color.ylabel) || 'white';
var title = (options.color && options.color.title) || 'white';
var grid = (options.color && options.color.grid) || 'white';
var key = (options.color && options.color.key) || 'white';
if (options.format === 'svg') { /* Setup gnuplot for SVG */
gnuplot.stdin.write('set term svg fname \"Verdana\" fsize 11\n');
} else if (options.format == 'pdf') {
/* PDF: setup Gnuplot output to postscript so ps2pdf can interpret it */
gnuplot.stdin.write('set term postscript landscape enhanced color dashed' +
'\"Helvetica\" 14\n');
} else { /* Setup gnuplot for png */
gnuplot.stdin.write('set term png background rgb "' + bg + '" font Verdana 14 size 1280,1024\n');
}
/* Formatting Options */
if (options.time) {
gnuplot.stdin.write('set xdata time \n');
gnuplot.stdin.write('set timefmt "%s"\n');
gnuplot.stdin.write('set border lc rgb "' + border + '"\n');
gnuplot.stdin.write('set ytics textcolor rgb "' + ytics + '"\n');
gnuplot.stdin.write('set xtics textcolor rgb "' + xtics + '"\n');
gnuplot.stdin.write('set key textcolor rgb "' + key + '"\n');
gnuplot.stdin.write('set format x "' + time_format(options.time) + '"\n');
gnuplot.stdin.write('set xlabel "time" textcolor rgb "' + xlabel + '"\n');
}
if (options.title) {
gnuplot.stdin.write('set title "'+options.title+'" textcolor rgb "' + title + '" \n');
}
if (options.logscale) {
gnuplot.stdin.write('set logscale y\n');
}
if (options.xlabel) {
gnuplot.stdin.write('set xlabel "'+options.xlabel+'" textcolor rgb "' + xlabel + '" \n');
}
if (options.ylabel) {
gnuplot.stdin.write('set ylabel "'+options.ylabel+'" textcolor rgb "' + ylabel + '" \n');
}
/* Setup ticks */
gnuplot.stdin.write('set grid xtics ytics mxtics mytics ls 12 lc "' + grid + '", ls 13 lc "' + grid + '" \n');
gnuplot.stdin.write('set mxtics\n');
if (options.nokey) {
gnuplot.stdin.write('set nokey\n');
}
}
/**
* Called after Gnuplot has finished.
*/
function post_gnuplot_processing(error, stdout, stderr) {
/* Print stuff */
if (error !== null) {
console.log('exec error: ' + error);
}
}
/* -------- Public Functions -------- */
/**
* Plots data to a PDF file. If it does not exist, the PDF file will
* be created, otherwise this plot will be appended as a new page.
*/
function plot(options) {
/* Required Options */
if (!options.data) {
throw("The options object must have 'data' property!");
return;
}
/* Translate data into an object if needs be */
if (_.isArray(options.data)) {
/* If it's a one-dimentional array */
if (_.isEqual(_.flatten(options.data), options.data)) {
options.data = { 'Series 1': options.data };
}
}
/* Defaults */
if (!options.style) {
options.style = 'lines'; /* Default to lines */
}
/* Apply moving averages and maximums */
if (options.moving_avg) {
options.data = apply_moving_filter(options.data, moving_average, options.moving_avg);
}
if (options.moving_max) {
options.data = apply_moving_filter(options.data, moving_maximum, options.moving_max);
}
/* Execute Gnuplot specifing a function to be called when it terminates */
if (options.format === 'pdf') { /* Special setup for pdf */
gnuplot = exec('gnuplot | ps2pdf - ' + options.filename,
(options.exec ? options.exec : {}),
function (error, stdout, stderr) {
if (options.finish) {
options.finish.apply(this, [error, stdout, stderr]);
} else {
post_gnuplot_processing(error, stdout, stderr);
}
});
} else if (options.filename) { /* Default for everything else */
gnuplot = exec('gnuplot > ' + options.filename,
(options.exec ? options.exec : {}),
options.finish || post_gnuplot_processing);
} else { /* Default for everything else */
gnuplot = exec('gnuplot ',
(options.exec ? options.exec : {}),
function (error, stdout, stderr) {
if (options.finish) {
options.finish.apply(this, [error, stdout, stderr]);
} else {
post_gnuplot_processing(error, stdout, stderr);
}
});
}
/* Sets up gnuplot based on the properties we've been given in the
* options object */
setup_gnuplot(gnuplot, options);
/* Get an array containing all the series */
var series = _.keys(options.data);
/* Reject series that are functions or come from higher up the protoype chain */
var i;
for (i = 0; i < series.length; i += 1) {
if (!options.data.hasOwnProperty(series[i]) ||
typeof options.data[series[i]] === 'function') {
delete series[i]; /* undefine this element */
}
}
/* Filter out any undefined elements */
series = _.filter(series, function() { return true; });
/* Print the command to actually do the plot */
gnuplot.stdin.write('plot');
for (i = 1; i <= series.length; i += 1) { /* For each series */
/* Instruct gnuplot to plot this series */
var props = options.data[series[i - 1]].props;
var color = (props || {}).color ? 'rgb "' + props.color + '"' : i;
var lineType = (props || {}).lineType ? props.lineType : '1';
var lineWidth = (props || {}).lineWidth ? props.lineWidth : '1';
var lineStyle = (props || {}).lineStyle ? props.lineStyle : options.style;
var smooth = ((props || {}).smooth ? props.smooth : options.smooth) || 'unique';
gnuplot.stdin.write('\'-\' using 1:2 smooth ' + smooth + ' title\'' + series[i - 1] +
'\' with ' + lineStyle + ' lt ' + lineType + ' lw ' + lineWidth + ' lc ' + color);
/* If another series is to follow, add a comma */
if (i < series.length) { gnuplot.stdin.write(','); }
}
gnuplot.stdin.write('\n');
/* Print out the data */
for (i = 0; i < series.length; i += 1) { /* For each series */
var dataSeries = (options.data[series[i]] || {}).series || options.data[series[i]];
for (key in dataSeries) {
gnuplot.stdin.write(key + ' ' + dataSeries[key] + '\n');
}
/* Terminate the data */
gnuplot.stdin.write('e\n');
}
gnuplot.stdin.end();
}
/* -------- Exports -------- */
exports.plot = plot;