开发者_高级运维
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this questionI'm trying to plot a graph in real time using GNUplot and C++. Does anyone know of any good libraries that does this? Thanks
gnuplot supports input via pipes (on windows, there's a separate executable for this, pgnuplot
). Then your program can send new commands to gnuplot, such as replot
, just as if you were typing them into the gnuplot interface directly.
How you set up the pipe connection and write to the sending end of the pipe from your C++ program varies by operating system, so you'll have to tell us what you're using if you want more help.
On Windows, there's CreatePipe
and then you set the hStdInput
element of the STARTUPINFO
struct you pass to CreateProcess
. Ditto with hStdOutput
if you need the status messages from pgnuplot
.
On POSIX (Unix, Linux, Mac OSX, etc), you can just use popen
as the quick way to get a unidirectional connection. For bidirectional, it works more like on Windows: pipe
to get handles to the ends, then fork
and in the child process call dup2
to associate stdin and stdout with the pipe, then exec
to have gnuplot
replace the child process, keeping the pipes you set up.
EDIT: From the gnuplot documentation:
The special filename ’-’ specifies that the data are inline; i.e., they follow the command. Only the data follow the command; plot options like filters, titles, and line styles remain on the plot command line. This is similar to << in unix shell script, and $DECK in VMS DCL. The data are entered as though they are being read from a file, one data point per record. The letter "e" at the start of the first column terminates data entry. The using option can be applied to these data — using it to filter them through a function might make sense, but selecting columns probably doesn’t!
Have you tried gnuplot interfaces in ANSI C?, this is an interface for C but in the same links there are some interface for C++. Or you could try PlPlot.
If you're interested in soft-realtime plotting, you're probably best of using a hardware accelerated graphics api (such as OpenGL), and plotting the chart yourself.
Look at http://search.cpan.org/~dkogan/feedGnuplot-1.08/bin/feedGnuplot This is a commandline utility, but also works well if controlled from a program.
In my C++ code, this worked (on mac OsX mavericks, using g++ Apple LLVM version 5.0):
#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.
精彩评论