Today I planned to draw some graph and i found a very good tool gnuplot. I used this tool without any kind of linking of library with my code, that's the best part of this tool. gnuplot have good stream operation functionality. you just need to open a pipe with gnuplot binary and run commands and draw required drawing.
you can get more details about this tool from
gnuplot.
Header file.
class CGnuPlotter
{
FILE* m_fp;
public:
CGnuPlotter(void);
virtual ~CGnuPlotter(void);
bool runGnuPlot(const char* _gnuplot_path);
void closeGnuPlot();
void sendCmd2GnuPlot(const char* _cmd);
};
Implementation file
bool CGnuPlotter::runGnuPlot(const char* _gnuplot_path)
{
std::string gp(_gnuplot_path);
gp += " -persist";
m_fp=_popen(gp.c_str(),"w");
if(m_fp==NULL)
return false;
return true;
}
void CGnuPlotter::closeGnuPlot()
{
if(m_fp){
while(feof(m_fp)) Sleep(100);
_pclose(m_fp);
m_fp=NULL;
}
}
void CGnuPlotter::sendCmd2GnuPlot(const char* _cmd)
{
fprintf(m_fp,"%s\n",_cmd);
fflush(m_fp);
// you need to flush for force gnuplot to run the command.
}
Test Program
void testCGnuPlotter ()
{
CGnuPlotter cgp;
cgp.runGnuPlot("C:\\gp444win32\\gnuplot\\binary\\gnuplot.exe");
cgp.sendCmd2GnuPlot("set multiplot");
cgp.sendCmd2GnuPlot("plot sin(x) , 0.5 w l, -0.5 w l");
int i=0;
// This is intentional stopped the program for have look of drawing.
std::cout<<"waiting for awake"<<std::endl;
while(i<100){std::cin>>i;}
cgp.closeGnuPlot();
}