27 #ifndef math_plot_gnuPlot_hpp
28 #define math_plot_gnuPlot_hpp
30 #include <sys/types.h>
48 #include "../constants.hpp"
49 #include "../../mxError.hpp"
50 #include "../../sys/timeUtils.hpp"
52 #ifndef MX_GP_FNAME_SZ
56 #define MX_GP_FNAME_SZ (128)
63 #define MX_GP_TEMP_SZ (128)
72 #define MX_GP_FC_TIME (10000)
75 #ifndef MX_GP_FC_RETRIES
80 #define MX_GP_FC_RETRIES (10)
91 template<
typename dataT>
92 std::string gpBinaryFormat()
94 static_assert(std::is_fundamental<dataT>::value || !std::is_fundamental<dataT>::value,
"No gnuplot format specifier available for this type.");
104 std::string m_modifiers;
106 std::string m_binary;
110 gpCurve(
const std::string & file,
111 const std::string & title,
112 const std::string & modifiers
113 ) : m_file(file), m_title(title), m_modifiers(modifiers)
116 gpCurve(
const std::string & file,
117 const std::string & title,
118 const std::string & modifiers,
119 const std::string & binary
120 ) : m_file(file), m_title(title), m_modifiers(modifiers), m_binary(binary)
123 gpCurve(
const gpCurve & gpc )
126 m_title = gpc.m_title;
127 m_modifiers = gpc.m_modifiers;
128 m_binary = gpc.m_binary;
200 std::string _gpErrorMsg;
227 std::map<std::string, gpCurve> m_curveMap;
431 int plot(
const std::string & fname,
433 const std::string &
title,
434 const std::string & name
449 int plot(
const std::string & fname,
451 const std::string & name
468 int plot(
const std::string & fname,
469 const std::string & name
486 int plot(
const std::string & fname );
499 template<
typename dataT>
500 int plot(
const dataT * y,
503 const std::string &
title,
504 const std::string & name
518 template<
typename dataT>
519 int plot(
const dataT * y,
522 const std::string & name
535 template<
typename dataT>
536 int plot(
const dataT * y,
538 const std::string & name
550 template<
typename dataT>
551 int plot(
const dataT * y,
568 template<
typename dataT>
569 int plot(
const std::vector<dataT> & y,
571 const std::string &
title,
572 const std::string & name
586 template<
typename dataT>
587 int plot(
const std::vector<dataT> & y,
589 const std::string & name
601 template<
typename dataT>
602 int plot(
const std::vector<dataT> & y,
603 const std::string & name
615 template<
typename dataT>
616 int plot(
const std::vector<dataT> & y );
630 template<
typename dataTx,
typename dataTy>
631 int plot(
const dataTx * x,
635 const std::string &
title,
636 const std::string & name
650 template<
typename dataTx,
typename dataTy>
651 int plot(
const dataTx * x,
655 const std::string & name
667 template<
typename dataTx,
typename dataTy>
668 int plot(
const dataTx * x,
671 const std::string & name
683 template<
typename dataTx,
typename dataTy>
684 int plot(
const dataTx * x,
701 template<
typename dataTx,
typename dataTy>
702 int plot(
const std::vector<dataTx> & x,
703 const std::vector<dataTy> & y,
705 const std::string &
title,
706 const std::string & name
720 template<
typename dataTx,
typename dataTy>
721 int plot(
const std::vector<dataTx> & x,
722 const std::vector<dataTy> & y,
724 const std::string & name
736 template<
typename dataTx,
typename dataTy>
737 int plot(
const std::vector<dataTx> & x,
738 const std::vector<dataTy> & y,
739 const std::string & name
751 template<
typename dataTx,
typename dataTy>
752 int plot(
const std::vector<dataTx> & x,
753 const std::vector<dataTy> & y
764 template<
typename dataTx,
typename dataTy>
768 const std::string &
title =
""
778 const std::string &
title =
"",
787 const std::string &
title =
"",
806 void title(
const std::string & name,
807 const std::string &
title
815 const std::string & binary,
817 const std::string &
title,
818 const std::string & name
827 const std::string & binaryx,
828 const std::string & binaryy,
830 const std::string &
title,
831 const std::string & name
851 template<
typename dataT>
854 const std::string & modifiers,
855 const std::string & title,
856 const std::string & name
862 template<
typename dataT>
865 const std::string & modifiers,
866 const std::string & name
869 return plotImpl(y, N*
sizeof(dataT), gpBinaryFormat<dataT>(),
modifiers,
"", name);
872 template<
typename dataT>
875 const std::string & name
878 return plotImpl(y, N*
sizeof(dataT), gpBinaryFormat<dataT>(),
"",
"", name);
881 template<
typename dataT>
886 return plotImpl(y, N*
sizeof(dataT), gpBinaryFormat<dataT>(),
"",
"",
"");
889 template<
typename dataT>
891 const std::string & modifiers,
892 const std::string & title,
893 const std::string & name
899 template<
typename dataT>
901 const std::string & modifiers,
902 const std::string & name
905 return plot<dataT>( y.data(), y.size(),
modifiers,
"", name);
908 template<
typename dataT>
910 const std::string & name
913 return plot<dataT>( y.data(), y.size(),
"",
"", name);
916 template<
typename dataT>
919 return plot<dataT>( y.data(), y.size(),
"",
"",
"");
923 template<
typename dataTx,
typename dataTy>
927 const std::string & modifiers,
928 const std::string & title,
929 const std::string & name
932 return plotImpl(x, y, N,
sizeof(dataTx),
sizeof(dataTy), gpBinaryFormat<dataTx>(), gpBinaryFormat<dataTy>(),
modifiers,
title, name);
935 template<
typename dataTx,
typename dataTy>
939 const std::string & modifiers,
940 const std::string & name
943 return plotImpl(x, y, N,
sizeof(dataTx),
sizeof(dataTy), gpBinaryFormat<dataTx>(), gpBinaryFormat<dataTy>(),
modifiers,
"", name);
946 template<
typename dataTx,
typename dataTy>
950 const std::string & name
953 return plotImpl(x, y, N,
sizeof(dataTx),
sizeof(dataTy), gpBinaryFormat<dataTx>(), gpBinaryFormat<dataTy>(),
"",
"", name);
956 template<
typename dataTx,
typename dataTy>
962 return plotImpl(x, y, N,
sizeof(dataTx),
sizeof(dataTy), gpBinaryFormat<dataTx>(), gpBinaryFormat<dataTy>(),
"",
"",
"");
965 template<
typename dataTx,
typename dataTy>
967 const std::vector<dataTy> & y,
968 const std::string & modifiers,
969 const std::string & title,
970 const std::string & name
976 template<
typename dataTx,
typename dataTy>
978 const std::vector<dataTy> & y,
979 const std::string & modifiers,
980 const std::string & name
983 return plot( x.data(), y.data(), x.size(),
modifiers,
"", name);
986 template<
typename dataTx,
typename dataTy>
988 const std::vector<dataTy> & y,
989 const std::string & name
992 return plot( x.data(), y.data(), x.size(),
"",
"", name);
995 template<
typename dataTx,
typename dataTy>
997 const std::vector<dataTy> & y
1000 return plot( x.data(), y.data(), x.size(),
"",
"",
"");
1003 template<
typename dataTx,
typename dataTy>
1006 const std::string & modifiers,
1007 const std::string & title
1015 std::string gpBinaryFormat<char>();
1018 std::string gpBinaryFormat<unsigned char>();
1021 std::string gpBinaryFormat<short>();
1024 std::string gpBinaryFormat<unsigned short>();
1027 std::string gpBinaryFormat<int>();
1030 std::string gpBinaryFormat<unsigned int>();
1033 std::string gpBinaryFormat<long>();
1036 std::string gpBinaryFormat<unsigned long>();
1039 std::string gpBinaryFormat<float>();
1042 std::string gpBinaryFormat<double>();
An interactive c++ interface to gnuplot.
int circle(double radius, const std::string &modifiers="", const std::string &title="", int npoints=10)
Draw a circle on the plot around the origin.
int doPlotCommand()
Issue the plot command for all the curves.
int plot(const std::string &fname)
Plot from a file without specifying the name or any modifiers.
std::string getResponse(const std::string &com, double timeout=0.5)
Get a response from gnuplot for a given command.
FILE * _pipeH
File stream for the gnuplot interface.
int plot(const std::string &fname, const std::string &modifiers, const std::string &title, const std::string &name)
Plot from a file specifying all curve components.
bool _gpError
Set to true if the response indicates a gnuplot error.
int xrange(double x0, double x1)
Set the x-axis range.
void title(const std::string &name, const std::string &title)
Modify the title for a curve.
int plot(const std::string &fname, const std::string &modifiers, const std::string &name)
Plot from a file without specifying the title.
int plotImpl(const void *x, const void *y, size_t Npts, size_t sizex, size_t sizey, const std::string &binaryx, const std::string &binaryy, const std::string &modifiers, const std::string &title, const std::string &name)
Implementation of 2-d binary plotting.
int plot(const std::string &fname, const std::string &name)
Plot from a file without modifiers.
void reset()
Clear all the curves and reset plot configuration.
int connect()
Connect to gnuplot.
int point(dataTx x, dataTy y, const std::string &modifiers="", const std::string &title="")
Plot a single point.
std::string _errFName
File name of the gnuplot stderr fifo.
std::vector< std::string > _tempFiles
Vector of all temporary file names opened, used for removal on destruction.
int ulogy()
Unset the y axis from log scale.
int plotImpl(const void *y, size_t Nbytes, const std::string &binary, const std::string &modifiers, const std::string &title, const std::string &name)
Implementation of 1-D binary plotting.
bool _deleteTemp
Flag to control whether temporary files are deleted on destruction. Default is true (files deleted).
int logy()
Set the y axis to log scale.
int _errFD
File descriptor for the gnuplot stderr fifo.
std::string _errLocation
Where to create gnuplot stderr fifo.
int ulogxy()
Unset the x and y axes from log scale.
int replot()
Issue the replot command.
int logxy()
Set the x and y axes to log scale.
int yrange()
Unset the y-axis range.
void listCurves()
List the curves and their specifications.
int checkResponse(std::string &response, double timeout=0)
Check for a response from gnuplot.
bool gpError()
Return the value of the gpError flag.
void modifiers(const std::string &name, const std::string &modifiers)
Modify the modifiers for a curve.
int xrange()
Unset the x-axis range.
int ulogx()
Unset the x axis from log scale.
int yrange(double y0, double y1)
Set the y-axis range.
FILE * openTempFile(char *fname)
Open a temporary binary file, and provide the filename.
std::string _tempLocation
Location of temporary files.
int circle(double xcen, double ycen, double radius, const std::string &modifiers="", const std::string &title="", int npoints=10)
Draw a circle on the plot.
int command(const std::string &com, bool flush=true)
Send a command to gnuplot.
int logx()
Set the x axis to log scale.
std::string gpErrorMsg()
Return the gnuplot error message.
void clear()
Clear all the curves.