我正在尝试使用GNUplot和C ++实时绘制图表。 有人知道任何能做到这一点的好库吗? 谢谢
pgnuplot
)。然后,您的程序可以向gnuplot发送新命令,例如replot
,就像您直接在gnuplot界面中键入它们一样。CreatePipe
,然后您设置传递给CreateProcess
的STARTUPINFO
结构体的hStdInput
元素。如果您需要来自pgnuplot
的状态消息,则同样适用于hStdOutput
。popen
作为快速获取单向连接的方法。对于双向连接,它的工作方式更像是在 Windows 上:使用 pipe
获取端点的句柄,然后在子进程中调用 dup2
将 stdin 和 stdout 关联到管道,然后使用 exec
让 gnuplot
替换子进程,并保留您设置的管道。
编辑:来自 gnuplot 文档:
特殊的文件名'-'指定数据为内联,即它们跟随命令。只有数据跟随命令;plot选项,如过滤器、标题和线条样式,保留在plot命令行上。这类似于unix shell脚本中的<<和VMS DCL中的$DECK。数据被输入,就好像它们是从文件中读取的,每个记录一个数据点。第一列开头的字母"e"终止数据输入。using选项可以应用于这些数据-使用它通过函数过滤它们可能是有意义的,但选择列可能没有意义!#include <sys/types.h>
#include <unistd.h>
...
// ready to make a plot
pid_t childpid=fork();
if(childpid==0) {
// child process makes plot
std::FILE* pipehandle=popen("gnuplot -persist","w");
// make some plot. You can send multiple commands to the pipe each ending in \n
std::fprintf(pipehandle,"plot \"results.txt\" using 1:2 with lines\n");
std::fprintf(pipehandle,"quit\n");
std::fflush(pipehandle);
std::fclose(pipehandle);
// child process exits
exit(0);
}
// parent process waits for child process to exit
waitpid(childpid,NULL,0);
// you can now repeat to make other gnuplots; all will appear simultaneously in the
// terminal and are persistent after the parent process has finished.