我在节点js中的bash子进程中使用gnuplot绘制一些数据并保存图形。到目前为止,这是我的代码:
var term = require('child_process').spawn('bash');
term.stdin.write('gnuplot -p\n');
term.stdin.write('set key outside\n');
term.stdin.write('set term png\n');
term.stdin.write('set output "plot1.png"\n')
term.stdin.write('plot for [col=2:4] "results" using 1:col with points title columnheader\n');
term.stdin.write('set output "plot2.png"\n')
term.stdin.write('plot for [col=5:7] "results" using 1:col with points title columnheader\n');
这是更正确,最快速的方式吗?无论如何,我可以使它更紧凑和简单?此外,当我在我的node.js脚本中运行它时,当我运行脚本时,孩子不会退出,因此脚本不会退出。怎么解决这个问题?
答案 0 :(得分:1)
解决方案:
直接生成gnuplot var term = require('child_process').spawn('gnuplot');
而不是bash。
删除term.stdin.write('gnuplot -p\n');
(-p标志使gnuplot窗口持久化,但不需要这样做,因为在这种情况下,将图打印到png图像)
在最后添加term.stdin.write('exit\n')
。