2017-10-21 11:05:51 +00:00
|
|
|
/*!
|
|
|
|
* \file gnuplot_i.h
|
|
|
|
* \brief A C++ interface to gnuplot.
|
|
|
|
* \author Carles Fernandez-Prades, 2017. cfernandez(at)cttc.es
|
|
|
|
*
|
2017-10-22 07:14:25 +00:00
|
|
|
* Original source code found at https://code.google.com/archive/p/gnuplot-cpp/
|
2017-10-21 11:05:51 +00:00
|
|
|
* by Jeremy Conlin jeremit0(at)gmail.com
|
|
|
|
*
|
|
|
|
* Version history:
|
|
|
|
* 0. C interface
|
|
|
|
* by N. Devillard (27/01/03)
|
|
|
|
* 1. C++ interface: direct translation from the C interface
|
|
|
|
* by Rajarshi Guha (07/03/03)
|
|
|
|
* 2. corrections for Win32 compatibility
|
|
|
|
* by V. Chyzhdzenka (20/05/03)
|
|
|
|
* 3. some member functions added, corrections for Win32 and Linux
|
|
|
|
* compatibility
|
|
|
|
* by M. Burgis (10/03/08)
|
2017-10-22 07:14:25 +00:00
|
|
|
* 4. Some fixes and improvements for Linux and macOS
|
|
|
|
* by C. Fernandez (22/10/17)
|
2017-10-21 11:05:51 +00:00
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
|
2017-10-21 11:05:51 +00:00
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
|
2020-02-08 00:20:02 +00:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2017-10-21 11:05:51 +00:00
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-02-08 09:10:46 +00:00
|
|
|
#ifndef GNSS_SDR_GNUPLOT_I_H
|
|
|
|
#define GNSS_SDR_GNUPLOT_I_H
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2018-06-29 19:53:39 +00:00
|
|
|
#include <gflags/gflags.h>
|
2018-12-09 21:00:09 +00:00
|
|
|
#include <cmath>
|
2018-03-03 01:03:39 +00:00
|
|
|
#include <cstdlib> // for getenv()
|
2018-05-21 19:27:06 +00:00
|
|
|
#include <cstring> // for strncpy
|
2018-12-09 21:00:09 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <list> // for std::list
|
|
|
|
#include <sstream> // for std::ostringstream
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
2018-05-22 18:55:03 +00:00
|
|
|
#include <sys/stat.h>
|
2018-12-09 21:00:09 +00:00
|
|
|
#include <vector>
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2018-06-29 19:53:39 +00:00
|
|
|
DEFINE_bool(show_plots, true, "Show plots on screen. Disable for non-interactive testing.");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2017-10-24 12:23:59 +00:00
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
|
2019-08-18 23:29:04 +00:00
|
|
|
// defined for 32 and 64-bit environments
|
2020-11-06 09:41:20 +00:00
|
|
|
// clang-format off
|
2018-03-03 01:03:39 +00:00
|
|
|
#include <io.h> // for _access(), _mktemp()
|
|
|
|
#define GP_MAX_TMP_FILES 27 // 27 temporary files it's Microsoft restriction
|
2020-11-06 09:41:20 +00:00
|
|
|
// clang-format on
|
2017-10-24 12:23:59 +00:00
|
|
|
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
2020-02-01 10:18:08 +00:00
|
|
|
// all UNIX-like OSs (Linux, *BSD, macOS, Solaris, ...)
|
2018-03-03 01:03:39 +00:00
|
|
|
#include <unistd.h> // for access(), mkstemp()
|
2018-06-20 16:42:06 +00:00
|
|
|
#define GP_MAX_TMP_FILES 1024
|
2017-10-21 11:05:51 +00:00
|
|
|
#else
|
2018-03-03 01:03:39 +00:00
|
|
|
#error unsupported or unknown operating system
|
2017-10-21 11:05:51 +00:00
|
|
|
#endif
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// declare classes in global namespace
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
class GnuplotException : public std::runtime_error
|
|
|
|
{
|
|
|
|
public:
|
2019-08-23 19:25:44 +00:00
|
|
|
explicit GnuplotException(const std::string &msg) : std::runtime_error(msg) {}
|
2017-10-21 11:05:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Gnuplot
|
|
|
|
{
|
|
|
|
private:
|
2019-08-18 23:29:04 +00:00
|
|
|
// -------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
// member data
|
2019-08-18 23:29:04 +00:00
|
|
|
//! pointer to the stream that can be used to write to the pipe
|
2018-03-03 01:03:39 +00:00
|
|
|
FILE *gnucmd;
|
2019-08-18 23:29:04 +00:00
|
|
|
//! validation of gnuplot session
|
2018-03-03 01:03:39 +00:00
|
|
|
bool valid;
|
2019-08-18 23:29:04 +00:00
|
|
|
//! true = 2d, false = 3d
|
2018-03-03 01:03:39 +00:00
|
|
|
bool two_dim;
|
2019-08-18 23:29:04 +00:00
|
|
|
//! number of plots in session
|
2018-03-03 01:03:39 +00:00
|
|
|
int nplots;
|
2019-08-18 23:29:04 +00:00
|
|
|
//! functions and data are displayed in a defined styles
|
2018-03-03 01:03:39 +00:00
|
|
|
std::string pstyle;
|
2019-08-18 23:29:04 +00:00
|
|
|
//! interpolate and approximate data in defined styles (e.g. spline)
|
2018-03-03 01:03:39 +00:00
|
|
|
std::string smooth;
|
2019-08-18 23:29:04 +00:00
|
|
|
//! list of created tmpfiles
|
2017-10-21 11:05:51 +00:00
|
|
|
std::vector<std::string> tmpfile_list;
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// -------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
// static data
|
2019-08-18 23:29:04 +00:00
|
|
|
//! number of all tmpfiles (number of tmpfiles restricted)
|
2018-03-03 01:03:39 +00:00
|
|
|
static int tmpfile_num;
|
2019-08-18 23:29:04 +00:00
|
|
|
//! name of executed GNUPlot file
|
2018-03-03 01:03:39 +00:00
|
|
|
static std::string m_sGNUPlotFileName;
|
2019-08-18 23:29:04 +00:00
|
|
|
//! gnuplot path
|
2018-03-03 01:03:39 +00:00
|
|
|
static std::string m_sGNUPlotPath;
|
2019-08-18 23:29:04 +00:00
|
|
|
//! standard terminal, used by showonscreen
|
2018-03-03 01:03:39 +00:00
|
|
|
static std::string terminal_std;
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// -------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
// member functions (auxiliary functions)
|
|
|
|
// ---------------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
//! get_program_path(); and popen();
|
|
|
|
//
|
|
|
|
// \param --> void
|
|
|
|
//
|
|
|
|
// \return <-- void
|
2017-10-21 11:05:51 +00:00
|
|
|
// ---------------------------------------------------
|
2017-10-22 07:14:25 +00:00
|
|
|
void init();
|
|
|
|
|
2017-10-21 11:05:51 +00:00
|
|
|
// ---------------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
//! creates tmpfile and returns its name
|
|
|
|
//
|
|
|
|
// \param tmp --> points to the tempfile
|
|
|
|
//
|
|
|
|
// \return <-- the name of the tempfile
|
2017-10-21 11:05:51 +00:00
|
|
|
// ---------------------------------------------------
|
2017-10-22 07:14:25 +00:00
|
|
|
std::string create_tmpfile(std::ofstream &tmp);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
//! gnuplot path found?
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- found the gnuplot path (yes == true, no == false)
|
|
|
|
// -------------------------------------------------------------------------
|
2017-10-22 07:14:25 +00:00
|
|
|
static bool get_program_path();
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
//! checks if file is available
|
|
|
|
//
|
|
|
|
// \param filename --> the filename
|
|
|
|
// \param mode --> the mode [optional,default value = 0]
|
|
|
|
//
|
|
|
|
// \return file exists (yes == true, no == false)
|
|
|
|
// -------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
bool file_available(const std::string &filename);
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
//<! \brief checks if file exists
|
|
|
|
//
|
|
|
|
// \param filename --> the filename
|
|
|
|
// \param mode --> the mode [optional,default value = 0]
|
|
|
|
//
|
|
|
|
// \return file exists (yes == true, no == false)
|
|
|
|
// -------------------------------------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
static bool file_exists(const std::string &filename, int mode = 0);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
public:
|
2019-08-18 23:29:04 +00:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// \brief optional function: set Gnuplot path manual
|
|
|
|
// attention: for windows: path with slash '/' not backslash '\'
|
|
|
|
//
|
|
|
|
// \param path --> the gnuplot path
|
|
|
|
//
|
|
|
|
// \return true on success, false otherwise
|
|
|
|
// -------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
static bool set_GNUPlotPath(const std::string &path);
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
// optional: set standard terminal, used by showonscreen
|
|
|
|
// defaults: Windows - win, Linux - x11, Mac - aqua
|
|
|
|
//
|
|
|
|
// \param type --> the terminal type
|
|
|
|
//
|
|
|
|
// \return ---
|
|
|
|
// -------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
static void set_terminal_std(const std::string &type);
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// -------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
// constructors
|
2019-08-18 23:29:04 +00:00
|
|
|
// -------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
//! set a style during construction
|
2019-08-23 19:25:44 +00:00
|
|
|
explicit Gnuplot(const std::string &style = "points");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// plot a single std::vector at one go
|
2017-10-21 11:05:51 +00:00
|
|
|
Gnuplot(const std::vector<double> &x,
|
2018-03-03 01:03:39 +00:00
|
|
|
const std::string &title = "",
|
|
|
|
const std::string &style = "points",
|
|
|
|
const std::string &labelx = "x",
|
|
|
|
const std::string &labely = "y");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// plot pairs std::vector at one go
|
2017-10-21 11:05:51 +00:00
|
|
|
Gnuplot(const std::vector<double> &x,
|
2018-03-03 01:03:39 +00:00
|
|
|
const std::vector<double> &y,
|
|
|
|
const std::string &title = "",
|
|
|
|
const std::string &style = "points",
|
|
|
|
const std::string &labelx = "x",
|
|
|
|
const std::string &labely = "y");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// plot triples std::vector at one go
|
2017-10-21 11:05:51 +00:00
|
|
|
Gnuplot(const std::vector<double> &x,
|
2018-03-03 01:03:39 +00:00
|
|
|
const std::vector<double> &y,
|
|
|
|
const std::vector<double> &z,
|
|
|
|
const std::string &title = "",
|
|
|
|
const std::string &style = "points",
|
|
|
|
const std::string &labelx = "x",
|
|
|
|
const std::string &labely = "y",
|
|
|
|
const std::string &labelz = "z");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2018-07-02 15:43:34 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// destructor: needed to delete temporary files
|
2017-10-21 11:05:51 +00:00
|
|
|
~Gnuplot();
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// send a command to gnuplot
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &cmd(const std::string &cmdstr);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
//! Sends a command to an active gnuplot session, identical to cmd()
|
|
|
|
// send a command to gnuplot using the << operator
|
|
|
|
//
|
|
|
|
// \param cmdstr --> the command string
|
|
|
|
//
|
|
|
|
// \return <-- a reference to the gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// ---------------------------------------------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &operator<<(const std::string &cmdstr)
|
2017-10-22 07:14:25 +00:00
|
|
|
{
|
2017-10-21 11:05:51 +00:00
|
|
|
cmd(cmdstr);
|
2018-03-03 01:03:39 +00:00
|
|
|
return (*this);
|
2017-10-21 11:05:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
// show on screen or write to file
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// sets terminal type to terminal_std
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &showonscreen(); // window output is set by default (win/x11/aqua)
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// sets terminal type to unknown (disable the screen output)
|
2018-07-02 15:43:34 +00:00
|
|
|
Gnuplot &disablescreen();
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// saves a gnuplot session to a postscript file, filename without extension
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &savetops(const std::string &filename = "gnuplot_output");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// saves a gnuplot session to a pdf file, filename without extension
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &savetopdf(const std::string &filename = "gnuplot_output", unsigned int font_size = 12);
|
2017-10-22 20:33:27 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
// set and unset
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// set line style (some of these styles require additional information):
|
|
|
|
// lines, points, linespoints, impulses, dots, steps, fsteps, histeps,
|
|
|
|
// boxes, histograms, filledcurves
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_style(const std::string &stylestr = "points");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// interpolation and approximation of data, arguments:
|
|
|
|
// csplines, bezier, acsplines (for data values > 0), sbezier, unique, frequency
|
|
|
|
// (works only with plot_x, plot_xy, plotfile_x, plotfile_xy
|
|
|
|
// (if smooth is set, set_style has no effect on data plotting)
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_smooth(const std::string &stylestr = "csplines");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// \brief unset smooth
|
|
|
|
// attention: smooth is not set by default
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- a reference to a gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// ----------------------------------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &unset_smooth()
|
|
|
|
{
|
|
|
|
smooth = "";
|
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// scales the size of the points used in plots
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_pointsize(const double pointsize = 1.0);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// turns grid on/off
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &set_grid()
|
|
|
|
{
|
|
|
|
cmd("set grid");
|
|
|
|
return *this;
|
|
|
|
};
|
2019-08-18 23:29:04 +00:00
|
|
|
// grid is not set by default
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &unset_grid()
|
|
|
|
{
|
|
|
|
cmd("unset grid");
|
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// -----------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// set the mulitplot mode
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// -----------------------------------------------
|
2018-06-20 10:04:03 +00:00
|
|
|
inline Gnuplot &set_multiplot(int rows, int cols)
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
2018-06-20 10:04:03 +00:00
|
|
|
cmd("set multiplot layout " + std::to_string(rows) + "," + std::to_string(cols)); //+ " rowfirst");
|
2018-03-03 01:03:39 +00:00
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// -----------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// unsets the mulitplot mode
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// -----------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &unset_multiplot()
|
|
|
|
{
|
|
|
|
cmd("unset multiplot");
|
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// set sampling rate of functions, or for interpolating data
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_samples(const int samples = 100);
|
2019-08-18 23:29:04 +00:00
|
|
|
// set isoline density (grid) for plotting functions as surfaces (for 3d plots)
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_isosamples(const int isolines = 10);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// enables/disables hidden line removal for surface plotting (for 3d plot)
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// --------------------------------------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_hidden3d()
|
|
|
|
{
|
|
|
|
cmd("set hidden3d");
|
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// hidden3d is not set by default
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &unset_hidden3d()
|
|
|
|
{
|
|
|
|
cmd("unset hidden3d");
|
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// enables/disables contour drawing for surfaces (for 3d plot)
|
|
|
|
// base, surface, both
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_contour(const std::string &position = "base");
|
2017-10-21 11:05:51 +00:00
|
|
|
// --------------------------------------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// contour is not set by default, it disables contour drawing for surfaces
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// ------------------------------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &unset_contour()
|
|
|
|
{
|
|
|
|
cmd("unset contour");
|
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// enables/disables the display of surfaces (for 3d plot)
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// ------------------------------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &set_surface()
|
|
|
|
{
|
|
|
|
cmd("set surface");
|
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// surface is set by default,
|
|
|
|
// it disables the display of surfaces (for 3d plot)
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// ------------------------------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &unset_surface()
|
|
|
|
{
|
|
|
|
cmd("unset surface");
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// switches legend on/off
|
|
|
|
// position: inside/outside, left/center/right, top/center/bottom, nobox/box
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_legend(const std::string &position = "default");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// \brief Switches legend off
|
|
|
|
// attention:legend is set by default
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// ------------------------------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &unset_legend()
|
|
|
|
{
|
|
|
|
cmd("unset key");
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2017-10-24 12:23:59 +00:00
|
|
|
// -----------------------------------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// \brief sets and clears the title of a gnuplot session
|
|
|
|
//
|
|
|
|
// \param title --> the title of the plot [optional, default == ""]
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-24 12:23:59 +00:00
|
|
|
// -----------------------------------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &set_title(const std::string &title = "")
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::string cmdstr;
|
|
|
|
cmdstr = "set title \"";
|
2018-03-03 01:03:39 +00:00
|
|
|
cmdstr += title;
|
|
|
|
cmdstr += "\"";
|
|
|
|
*this << cmdstr;
|
2017-10-21 11:05:51 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
//! Clears the title of a gnuplot session
|
|
|
|
// The title is not set by default.
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// ---------------------------------------------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &unset_title()
|
|
|
|
{
|
|
|
|
this->set_title();
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// set x axis label
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_ylabel(const std::string &label = "x");
|
2019-08-18 23:29:04 +00:00
|
|
|
// set y axis label
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_xlabel(const std::string &label = "y");
|
2019-08-18 23:29:04 +00:00
|
|
|
// set z axis label
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_zlabel(const std::string &label = "z");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// set axis - ranges
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_xrange(const double iFrom, const double iTo);
|
2019-08-18 23:29:04 +00:00
|
|
|
// set y-axis - ranges
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_yrange(const double iFrom, const double iTo);
|
2019-08-18 23:29:04 +00:00
|
|
|
// set z-axis - ranges
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_zrange(const double iFrom, const double iTo);
|
2017-10-22 07:14:25 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// autoscale axis (set by default) of xaxis
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// -----------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &set_xautoscale()
|
|
|
|
{
|
|
|
|
cmd("set xrange restore");
|
|
|
|
cmd("set autoscale x");
|
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// -----------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// autoscale axis (set by default) of yaxis
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// -----------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &set_yautoscale()
|
|
|
|
{
|
|
|
|
cmd("set yrange restore");
|
|
|
|
cmd("set autoscale y");
|
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// -----------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// autoscale axis (set by default) of zaxis
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-21 11:05:51 +00:00
|
|
|
// -----------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &set_zautoscale()
|
|
|
|
{
|
|
|
|
cmd("set zrange restore");
|
|
|
|
cmd("set autoscale z");
|
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// turns on/off log scaling for the specified xaxis (logscale is not set by default)
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_xlogscale(const double base = 10);
|
2019-08-18 23:29:04 +00:00
|
|
|
// turns on/off log scaling for the specified yaxis (logscale is not set by default)
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_ylogscale(const double base = 10);
|
2019-08-18 23:29:04 +00:00
|
|
|
// turns on/off log scaling for the specified zaxis (logscale is not set by default)
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_zlogscale(const double base = 10);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2017-10-24 12:23:59 +00:00
|
|
|
// -----------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// turns off log scaling for the x axis
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-24 12:23:59 +00:00
|
|
|
// -----------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &unset_xlogscale()
|
|
|
|
{
|
|
|
|
cmd("unset logscale x");
|
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2017-10-24 12:23:59 +00:00
|
|
|
// -----------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// turns off log scaling for the y axis
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-24 12:23:59 +00:00
|
|
|
// -----------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &unset_ylogscale()
|
|
|
|
{
|
|
|
|
cmd("unset logscale y");
|
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2017-10-24 12:23:59 +00:00
|
|
|
// -----------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// turns off log scaling for the z axis
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return <-- reference to the gnuplot object
|
2017-10-24 12:23:59 +00:00
|
|
|
// -----------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &unset_zlogscale()
|
|
|
|
{
|
|
|
|
cmd("unset logscale z");
|
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// set palette range (autoscale by default)
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &set_cbrange(const double iFrom, const double iTo);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
// plot
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// plot a single std::vector: x
|
|
|
|
// from file
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &plotfile_x(const std::string &filename,
|
|
|
|
const unsigned int column = 1,
|
|
|
|
const std::string &title = "");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// from std::vector
|
2018-03-03 01:03:39 +00:00
|
|
|
template <typename X>
|
|
|
|
Gnuplot &plot_x(const X &x, const std::string &title = "");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// plot x,y pairs: x y
|
|
|
|
// from file
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &plotfile_xy(const std::string &filename,
|
|
|
|
const unsigned int column_x = 1,
|
|
|
|
const unsigned int column_y = 2,
|
|
|
|
const std::string &title = "",
|
|
|
|
const unsigned int decimate = 1);
|
2019-08-18 23:29:04 +00:00
|
|
|
// from data
|
2018-03-03 01:03:39 +00:00
|
|
|
template <typename X, typename Y>
|
|
|
|
Gnuplot &plot_xy(const X &x, const Y &y,
|
|
|
|
const std::string &title = "",
|
|
|
|
const unsigned int decimate = 1);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// plot x,y pairs with dy errorbars: x y dy
|
|
|
|
// from file
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &plotfile_xy_err(const std::string &filename,
|
|
|
|
const unsigned int column_x = 1,
|
|
|
|
const unsigned int column_y = 2,
|
|
|
|
const unsigned int column_dy = 3,
|
|
|
|
const std::string &title = "");
|
2019-08-18 23:29:04 +00:00
|
|
|
// from data
|
2018-03-03 01:03:39 +00:00
|
|
|
template <typename X, typename Y, typename E>
|
|
|
|
Gnuplot &plot_xy_err(const X &x, const Y &y, const E &dy,
|
|
|
|
const std::string &title = "");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
template <typename X, typename Y, typename E>
|
|
|
|
Gnuplot &plot_grid3d(const X &x, const Y &y, const E &mag,
|
|
|
|
const std::string &title = "");
|
2017-10-28 16:15:59 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// plot x,y,z triples: x y z
|
|
|
|
// from file
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &plotfile_xyz(const std::string &filename,
|
|
|
|
const unsigned int column_x = 1,
|
|
|
|
const unsigned int column_y = 2,
|
|
|
|
const unsigned int column_z = 3,
|
|
|
|
const std::string &title = "");
|
2019-08-18 23:29:04 +00:00
|
|
|
// from std::vector
|
2018-03-03 01:03:39 +00:00
|
|
|
template <typename X, typename Y, typename Z>
|
|
|
|
Gnuplot &plot_xyz(const X &x,
|
|
|
|
const Y &y,
|
|
|
|
const Z &z,
|
|
|
|
const std::string &title = "");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// plot an equation of the form: y = ax + b, you supply a and b
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &plot_slope(const double a,
|
|
|
|
const double b,
|
|
|
|
const std::string &title = "");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// plot an equation supplied as a std::string y=f(x), write only the function f(x) not y=
|
|
|
|
// the independent variable has to be x
|
|
|
|
// binary operators: ** exponentiation, * multiply, / divide, + add, - subtract, % modulo
|
|
|
|
// unary operators: - minus, ! factorial
|
|
|
|
// elementary functions: rand(x), abs(x), sgn(x), ceil(x), floor(x), int(x), imag(x), real(x), arg(x),
|
|
|
|
// sqrt(x), exp(x), log(x), log10(x), sin(x), cos(x), tan(x), asin(x), acos(x), atan(x), atan2(y,x),
|
|
|
|
// sinh(x), cosh(x), tanh(x), asinh(x), acosh(x), atanh(x)
|
|
|
|
// special functions: erf(x), erfc(x), inverf(x), gamma(x), igamma(a,x), lgamma(x), ibeta(p,q,x),
|
|
|
|
// besj0(x), besj1(x), besy0(x), besy1(x), lambertw(x)
|
|
|
|
// statistical functions: norm(x), invnorm(x)
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &plot_equation(const std::string &equation, const std::string &title = "");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// plot an equation supplied as a std::string z=f(x,y), write only the function f(x,y) not z=
|
|
|
|
// the independent variables have to be x and y
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &plot_equation3d(const std::string &equation, const std::string &title = "");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// plot image
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &plot_image(const unsigned char *ucPicBuf,
|
|
|
|
const unsigned int iWidth,
|
|
|
|
const unsigned int iHeight,
|
|
|
|
const std::string &title = "");
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// plot circle
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &plot_circle(double east, double north, double radius, const std::string &label = "");
|
2017-10-22 16:05:02 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// --------------------------------------------------------------------------------
|
|
|
|
//! replot repeats the last plot or splot command.
|
|
|
|
// this can be useful for viewing a plot with different set options,
|
|
|
|
// or when generating the same plot for several devices (showonscreen, savetops)
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return ---
|
|
|
|
// --------------------------------------------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline Gnuplot &replot(void)
|
|
|
|
{
|
2019-02-11 20:13:02 +00:00
|
|
|
if (nplots > 0)
|
|
|
|
{
|
|
|
|
cmd("replot");
|
|
|
|
}
|
2018-03-03 01:03:39 +00:00
|
|
|
return *this;
|
|
|
|
};
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// resets a gnuplot session (next plot will erase previous ones)
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &reset_plot();
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// resets a gnuplot session and sets all variables to default
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot &reset_all();
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// deletes temporary files
|
2017-10-21 11:05:51 +00:00
|
|
|
void remove_tmpfiles();
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2019-08-18 23:29:04 +00:00
|
|
|
// \brief Is the gnuplot session valid ??
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// \param ---
|
|
|
|
//
|
|
|
|
// \return true if valid, false if not
|
2017-10-24 12:23:59 +00:00
|
|
|
// -------------------------------------------------------------------
|
2018-03-03 01:03:39 +00:00
|
|
|
inline bool is_valid() { return (valid); };
|
2017-10-21 11:05:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// initialize static data
|
|
|
|
//
|
|
|
|
int Gnuplot::tmpfile_num = 0;
|
|
|
|
|
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
|
|
|
|
std::string Gnuplot::m_sGNUPlotFileName = "pgnuplot.exe";
|
|
|
|
std::string Gnuplot::m_sGNUPlotPath = "C:/program files/gnuplot/bin/";
|
|
|
|
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
|
|
|
std::string Gnuplot::m_sGNUPlotFileName = "gnuplot";
|
|
|
|
std::string Gnuplot::m_sGNUPlotPath = "/usr/local/bin/";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
|
|
|
|
std::string Gnuplot::terminal_std = "windows";
|
2018-03-03 01:03:39 +00:00
|
|
|
#elif (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__APPLE__)
|
2017-10-21 11:05:51 +00:00
|
|
|
std::string Gnuplot::terminal_std = "x11";
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
std::string Gnuplot::terminal_std = "aqua";
|
|
|
|
#endif
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// constructor: set a style during construction
|
|
|
|
//
|
|
|
|
inline Gnuplot::Gnuplot(const std::string &style)
|
2018-12-03 09:05:47 +00:00
|
|
|
: gnucmd(nullptr), valid(false), two_dim(false), nplots(0)
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
set_style(style);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// constructor: open a new session, plot a signal (x)
|
|
|
|
//
|
|
|
|
inline Gnuplot::Gnuplot(const std::vector<double> &x,
|
2018-03-03 01:03:39 +00:00
|
|
|
const std::string &title,
|
|
|
|
const std::string &style,
|
|
|
|
const std::string &labelx,
|
|
|
|
const std::string &labely)
|
2018-12-03 09:05:47 +00:00
|
|
|
: gnucmd(nullptr), valid(false), two_dim(false), nplots(0)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
|
|
|
|
set_style(style);
|
|
|
|
set_xlabel(labelx);
|
|
|
|
set_ylabel(labely);
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
plot_x(x, title);
|
2017-10-21 11:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// constructor: open a new session, plot a signal (x,y)
|
|
|
|
//
|
|
|
|
inline Gnuplot::Gnuplot(const std::vector<double> &x,
|
2018-03-03 01:03:39 +00:00
|
|
|
const std::vector<double> &y,
|
|
|
|
const std::string &title,
|
|
|
|
const std::string &style,
|
|
|
|
const std::string &labelx,
|
|
|
|
const std::string &labely)
|
2018-12-03 09:05:47 +00:00
|
|
|
: gnucmd(nullptr), valid(false), two_dim(false), nplots(0)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
|
|
|
|
set_style(style);
|
|
|
|
set_xlabel(labelx);
|
|
|
|
set_ylabel(labely);
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
plot_xy(x, y, title);
|
2017-10-21 11:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// constructor: open a new session, plot a signal (x,y,z)
|
|
|
|
//
|
|
|
|
inline Gnuplot::Gnuplot(const std::vector<double> &x,
|
2018-03-03 01:03:39 +00:00
|
|
|
const std::vector<double> &y,
|
|
|
|
const std::vector<double> &z,
|
|
|
|
const std::string &title,
|
|
|
|
const std::string &style,
|
|
|
|
const std::string &labelx,
|
|
|
|
const std::string &labely,
|
|
|
|
const std::string &labelz)
|
2018-12-03 09:05:47 +00:00
|
|
|
: gnucmd(nullptr), valid(false), two_dim(false), nplots(0)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
|
|
|
|
set_style(style);
|
|
|
|
set_xlabel(labelx);
|
|
|
|
set_ylabel(labely);
|
|
|
|
set_zlabel(labelz);
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
plot_xyz(x, y, z, title);
|
2017-10-21 11:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
2019-08-18 23:29:04 +00:00
|
|
|
// Plots a 2d graph from a list of doubles: x
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
2018-03-03 01:03:39 +00:00
|
|
|
template <typename X>
|
|
|
|
Gnuplot &Gnuplot::plot_x(const X &x, const std::string &title)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2019-02-11 13:47:18 +00:00
|
|
|
if (x.empty())
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
throw GnuplotException("std::vector too small");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ofstream tmp;
|
|
|
|
std::string name = create_tmpfile(tmp);
|
2019-02-11 13:47:18 +00:00
|
|
|
if (name.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// write the data to file
|
|
|
|
//
|
|
|
|
for (unsigned int i = 0; i < x.size(); i++)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
tmp << x[i] << '\n';
|
2019-02-11 20:13:02 +00:00
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
tmp.flush();
|
|
|
|
tmp.close();
|
|
|
|
|
|
|
|
plotfile_x(name, 1, title);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
2019-08-18 23:29:04 +00:00
|
|
|
// Plots a 2d graph from a list of doubles: x y
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
2018-03-03 01:03:39 +00:00
|
|
|
template <typename X, typename Y>
|
|
|
|
Gnuplot &Gnuplot::plot_xy(const X &x, const Y &y, const std::string &title, const unsigned int decimate)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2019-02-11 13:47:18 +00:00
|
|
|
if (x.empty() || y.empty())
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
throw GnuplotException("std::vectors too small");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x.size() != y.size())
|
|
|
|
{
|
|
|
|
throw GnuplotException("Length of the std::vectors differs");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ofstream tmp;
|
|
|
|
std::string name = create_tmpfile(tmp);
|
2019-02-11 13:47:18 +00:00
|
|
|
if (name.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// write the data to file
|
|
|
|
//
|
|
|
|
for (unsigned int i = 0; i < x.size(); i++)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
tmp << x[i] << " " << y[i] << '\n';
|
2019-02-11 20:13:02 +00:00
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
tmp.flush();
|
|
|
|
tmp.close();
|
|
|
|
|
2017-10-24 12:23:59 +00:00
|
|
|
plotfile_xy(name, 1, 2, title, decimate);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// plot x,y pairs with dy errorbars
|
|
|
|
//
|
2018-03-03 01:03:39 +00:00
|
|
|
template <typename X, typename Y, typename E>
|
|
|
|
Gnuplot &Gnuplot::plot_xy_err(const X &x,
|
|
|
|
const Y &y,
|
|
|
|
const E &dy,
|
|
|
|
const std::string &title)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2019-02-11 17:36:59 +00:00
|
|
|
if (x.empty() || y.empty() || dy.empty())
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
throw GnuplotException("std::vectors too small");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x.size() != y.size() || y.size() != dy.size())
|
|
|
|
{
|
|
|
|
throw GnuplotException("Length of the std::vectors differs");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ofstream tmp;
|
|
|
|
std::string name = create_tmpfile(tmp);
|
2019-02-11 13:47:18 +00:00
|
|
|
if (name.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// write the data to file
|
|
|
|
//
|
|
|
|
for (unsigned int i = 0; i < x.size(); i++)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
tmp << x[i] << " " << y[i] << " " << dy[i] << '\n';
|
2019-02-11 20:13:02 +00:00
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
tmp.flush();
|
|
|
|
tmp.close();
|
|
|
|
|
|
|
|
// Do the actual plot
|
|
|
|
plotfile_xy_err(name, 1, 2, 3, title);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-28 16:15:59 +00:00
|
|
|
//
|
|
|
|
// Plots a 3d grid
|
|
|
|
//
|
2018-03-03 01:03:39 +00:00
|
|
|
template <typename X, typename Y, typename E>
|
|
|
|
Gnuplot &Gnuplot::plot_grid3d(const X &x,
|
|
|
|
const Y &y,
|
|
|
|
const E &mag,
|
|
|
|
const std::string &title)
|
2017-10-28 16:15:59 +00:00
|
|
|
{
|
2019-02-11 13:47:18 +00:00
|
|
|
if (x.empty() || y.empty())
|
2017-10-28 16:15:59 +00:00
|
|
|
{
|
|
|
|
throw GnuplotException("std::vectors too small");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
std::ofstream tmp;
|
|
|
|
std::string name = create_tmpfile(tmp);
|
2019-02-11 13:47:18 +00:00
|
|
|
if (name.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-28 16:15:59 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// write the data to file
|
|
|
|
//
|
|
|
|
for (unsigned int i = 0; i < x.size(); i++)
|
|
|
|
{
|
|
|
|
for (unsigned int k = 0; k < y.size(); k++)
|
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
tmp << static_cast<float>(x.at(i)) << " " << static_cast<float>(y.at(k)) << " " << mag.at(i).at(k) << '\n';
|
2017-10-28 16:15:59 +00:00
|
|
|
}
|
|
|
|
tmp.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp.close();
|
|
|
|
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
cmdstr << "set ticslevel 0\n";
|
|
|
|
cmdstr << "set hidden3d\n";
|
|
|
|
cmdstr << "unset colorbox\n";
|
|
|
|
cmdstr << "set border 5\n";
|
|
|
|
cmdstr << "unset ztics\n";
|
|
|
|
|
|
|
|
cmdstr << " splot \"" << name << "\" u 1:2:3";
|
|
|
|
|
2019-02-11 13:47:18 +00:00
|
|
|
if (title.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << " notitle with " << pstyle << " palette";
|
|
|
|
}
|
2017-10-28 16:15:59 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << " title \"" << title << "\" with " << pstyle << " palette";
|
|
|
|
}
|
2017-10-28 16:15:59 +00:00
|
|
|
cmdstr << "\n";
|
|
|
|
|
|
|
|
//
|
|
|
|
// Do the actual plot
|
|
|
|
//
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Plots a 3d graph from a list of doubles: x y z
|
|
|
|
//
|
2018-03-03 01:03:39 +00:00
|
|
|
template <typename X, typename Y, typename Z>
|
|
|
|
Gnuplot &Gnuplot::plot_xyz(const X &x,
|
|
|
|
const Y &y,
|
|
|
|
const Z &z,
|
|
|
|
const std::string &title)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2019-02-11 13:47:18 +00:00
|
|
|
if (x.empty() || y.empty() || z.empty())
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
throw GnuplotException("std::vectors too small");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x.size() != y.size() || x.size() != z.size())
|
|
|
|
{
|
|
|
|
throw GnuplotException("Length of the std::vectors differs");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ofstream tmp;
|
|
|
|
std::string name = create_tmpfile(tmp);
|
2019-02-11 13:47:18 +00:00
|
|
|
if (name.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// write the data to file
|
|
|
|
//
|
|
|
|
for (unsigned int i = 0; i < x.size(); i++)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
tmp << x[i] << " " << y[i] << " " << z[i] << '\n';
|
2019-02-11 20:13:02 +00:00
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
tmp.flush();
|
|
|
|
tmp.close();
|
|
|
|
|
|
|
|
plotfile_xyz(name, 1, 2, 3, title);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// define static member function: set Gnuplot path manual
|
|
|
|
// for windows: path with slash '/' not backslash '\'
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline bool Gnuplot::set_GNUPlotPath(const std::string &path)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::string tmp = path + "/" + Gnuplot::m_sGNUPlotFileName;
|
|
|
|
|
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
|
2018-03-03 01:03:39 +00:00
|
|
|
if (Gnuplot::file_exists(tmp, 0)) // check existence
|
2017-10-21 11:05:51 +00:00
|
|
|
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
2018-03-03 01:03:39 +00:00
|
|
|
if (Gnuplot::file_exists(tmp, 1)) // check existence and execution permission
|
2017-10-21 11:05:51 +00:00
|
|
|
#endif
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
|
|
|
Gnuplot::m_sGNUPlotPath = path;
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
|
|
|
|
Gnuplot::m_sGNUPlotPath.clear();
|
|
|
|
return false;
|
2017-10-21 11:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
2018-03-25 17:47:28 +00:00
|
|
|
// define static member function: set standard terminal, used by showonscreen
|
2017-10-21 11:05:51 +00:00
|
|
|
// defaults: Windows - win, Linux - x11, Mac - aqua
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline void Gnuplot::set_terminal_std(const std::string &type)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
#if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
2018-12-03 09:05:47 +00:00
|
|
|
if (type.find("x11") != std::string::npos && std::getenv("DISPLAY") == nullptr)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
throw GnuplotException("Can't find DISPLAY variable");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Gnuplot::terminal_std = type;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
2020-02-05 20:24:46 +00:00
|
|
|
// A string tokenizer taken
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
template <typename Container>
|
2018-03-03 01:03:39 +00:00
|
|
|
void stringtok(Container &container,
|
|
|
|
std::string const &in,
|
|
|
|
const char *const delimiters = " \t\n")
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
const std::string::size_type len = in.length();
|
|
|
|
std::string::size_type i = 0;
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
while (i < len)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
// eat leading whitespace
|
2018-03-03 01:03:39 +00:00
|
|
|
i = in.find_first_not_of(delimiters, i);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
if (i == std::string::npos)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
return; // nothing left but white space
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// find the end of the token
|
2018-03-03 01:03:39 +00:00
|
|
|
std::string::size_type j = in.find_first_of(delimiters, i);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// push token
|
|
|
|
if (j == std::string::npos)
|
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
container.push_back(in.substr(i));
|
2017-10-21 11:05:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
|
|
|
|
container.push_back(in.substr(i, j - i));
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// set up for next loop
|
|
|
|
i = j + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Destructor: needed to delete temporary files
|
|
|
|
//
|
|
|
|
Gnuplot::~Gnuplot()
|
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
// remove_tmpfiles();
|
|
|
|
// A stream opened by popen() should be closed by pclose()
|
2017-10-21 11:05:51 +00:00
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
|
|
|
|
if (_pclose(gnucmd) == -1)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
2017-10-21 11:05:51 +00:00
|
|
|
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
2018-03-03 01:03:39 +00:00
|
|
|
if (pclose(gnucmd) == -1)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
2017-10-21 11:05:51 +00:00
|
|
|
#endif
|
2019-02-11 20:13:02 +00:00
|
|
|
// throw GnuplotException("Problem closing communication to gnuplot");
|
2020-07-07 16:53:50 +00:00
|
|
|
std::cout << "Gnuplot window left open.\n";
|
2019-02-11 20:13:02 +00:00
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Resets a gnuplot session (next plot will erase previous ones)
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::reset_plot()
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
// remove_tmpfiles();
|
|
|
|
nplots = 0;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
2018-03-25 17:47:28 +00:00
|
|
|
// resets a gnuplot session and sets all variables to default
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::reset_all()
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
// remove_tmpfiles();
|
|
|
|
nplots = 0;
|
|
|
|
cmd("reset");
|
|
|
|
cmd("clear");
|
|
|
|
pstyle = "points";
|
|
|
|
smooth = "";
|
|
|
|
showonscreen();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Change the plotting style of a gnuplot session
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_style(const std::string &stylestr)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (stylestr.find("lines") == std::string::npos &&
|
|
|
|
stylestr.find("points") == std::string::npos &&
|
|
|
|
stylestr.find("linespoints") == std::string::npos &&
|
|
|
|
stylestr.find("impulses") == std::string::npos &&
|
|
|
|
stylestr.find("dots") == std::string::npos &&
|
|
|
|
stylestr.find("steps") == std::string::npos &&
|
|
|
|
stylestr.find("fsteps") == std::string::npos &&
|
|
|
|
stylestr.find("histeps") == std::string::npos &&
|
|
|
|
stylestr.find("boxes") == std::string::npos && // 1-4 columns of data are required
|
|
|
|
stylestr.find("filledcurves") == std::string::npos &&
|
2019-08-18 23:29:04 +00:00
|
|
|
stylestr.find("histograms") == std::string::npos) // only for one data column
|
2017-10-21 11:05:51 +00:00
|
|
|
// stylestr.find("labels") == std::string::npos && // 3 columns of data are required
|
|
|
|
// stylestr.find("xerrorbars") == std::string::npos && // 3-4 columns of data are required
|
|
|
|
// stylestr.find("xerrorlines") == std::string::npos && // 3-4 columns of data are required
|
|
|
|
// stylestr.find("errorbars") == std::string::npos && // 3-4 columns of data are required
|
|
|
|
// stylestr.find("errorlines") == std::string::npos && // 3-4 columns of data are required
|
|
|
|
// stylestr.find("yerrorbars") == std::string::npos && // 3-4 columns of data are required
|
|
|
|
// stylestr.find("yerrorlines") == std::string::npos && // 3-4 columns of data are required
|
|
|
|
// stylestr.find("boxerrorbars") == std::string::npos && // 3-5 columns of data are required
|
|
|
|
// stylestr.find("xyerrorbars") == std::string::npos && // 4,6,7 columns of data are required
|
|
|
|
// stylestr.find("xyerrorlines") == std::string::npos && // 4,6,7 columns of data are required
|
|
|
|
// stylestr.find("boxxyerrorbars") == std::string::npos && // 4,6,7 columns of data are required
|
|
|
|
// stylestr.find("financebars") == std::string::npos && // 5 columns of data are required
|
|
|
|
// stylestr.find("candlesticks") == std::string::npos && // 5 columns of data are required
|
|
|
|
// stylestr.find("vectors") == std::string::npos &&
|
|
|
|
// stylestr.find("image") == std::string::npos &&
|
|
|
|
// stylestr.find("rgbimage") == std::string::npos &&
|
|
|
|
// stylestr.find("pm3d") == std::string::npos )
|
|
|
|
{
|
|
|
|
pstyle = std::string("points");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pstyle = stylestr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// smooth: interpolation and approximation of data
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_smooth(const std::string &stylestr)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (stylestr.find("unique") == std::string::npos &&
|
|
|
|
stylestr.find("frequency") == std::string::npos &&
|
|
|
|
stylestr.find("csplines") == std::string::npos &&
|
|
|
|
stylestr.find("acsplines") == std::string::npos &&
|
|
|
|
stylestr.find("bezier") == std::string::npos &&
|
|
|
|
stylestr.find("sbezier") == std::string::npos)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
smooth = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
smooth = stylestr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2018-07-02 15:43:34 +00:00
|
|
|
//
|
|
|
|
// Disable screen output
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::disablescreen()
|
2018-07-02 15:43:34 +00:00
|
|
|
{
|
|
|
|
cmd("set output");
|
|
|
|
cmd("set terminal unknown");
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// sets terminal type to windows / x11
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::showonscreen()
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2017-10-22 06:28:59 +00:00
|
|
|
std::string persist(" persist");
|
|
|
|
#ifdef __APPLE__
|
|
|
|
persist = "";
|
|
|
|
#endif
|
2017-10-21 11:05:51 +00:00
|
|
|
cmd("set output");
|
2017-10-22 06:28:59 +00:00
|
|
|
cmd("set terminal " + Gnuplot::terminal_std + persist);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-22 20:33:27 +00:00
|
|
|
//
|
|
|
|
// saves a gnuplot session to a pdf file
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::savetopdf(const std::string &filename, unsigned int font_size)
|
2017-10-22 20:33:27 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
2017-10-22 21:04:39 +00:00
|
|
|
cmdstr << "set term pdfcairo enhanced color font \"Times-New-Roman," + std::to_string(font_size) + "\"\n";
|
2017-10-22 20:33:27 +00:00
|
|
|
cmdstr << "set output \"" << filename << ".pdf\"\n";
|
|
|
|
cmdstr << "replot";
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// saves a gnuplot session to a postscript file
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::savetops(const std::string &filename)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
2017-10-22 06:20:51 +00:00
|
|
|
cmdstr << "set term postscript landscape enhanced color dashed \"Times-Roman\" 18\n";
|
2017-10-21 14:33:26 +00:00
|
|
|
cmdstr << "set output \"" << filename << ".ps\"\n";
|
|
|
|
cmdstr << "replot";
|
2017-10-21 11:05:51 +00:00
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Switches legend on
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_legend(const std::string &position)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
cmdstr << "set key " << position;
|
|
|
|
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// turns on log scaling for the x axis
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_xlogscale(const double base)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
|
|
|
|
cmdstr << "set logscale x " << base;
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// turns on log scaling for the y axis
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_ylogscale(const double base)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
|
|
|
|
cmdstr << "set logscale y " << base;
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// turns on log scaling for the z axis
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_zlogscale(const double base)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
|
|
|
|
cmdstr << "set logscale z " << base;
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// scales the size of the points used in plots
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_pointsize(const double pointsize)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
cmdstr << "set pointsize " << pointsize;
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// set isoline density (grid) for plotting functions as surfaces
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_samples(const int samples)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
cmdstr << "set samples " << samples;
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// set isoline density (grid) for plotting functions as surfaces
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_isosamples(const int isolines)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
cmdstr << "set isosamples " << isolines;
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// enables contour drawing for surfaces set contour {base | surface | both}
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_contour(const std::string &position)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (position.find("base") == std::string::npos &&
|
|
|
|
position.find("surface") == std::string::npos &&
|
|
|
|
position.find("both") == std::string::npos)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
cmd("set contour base");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmd("set contour " + position);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// set labels
|
|
|
|
//
|
|
|
|
// set the xlabel
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_xlabel(const std::string &label)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
|
|
|
|
cmdstr << "set xlabel \"" << label << "\"";
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
// set the ylabel
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_ylabel(const std::string &label)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
|
|
|
|
cmdstr << "set ylabel \"" << label << "\"";
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
// set the zlabel
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_zlabel(const std::string &label)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
|
|
|
|
cmdstr << "set zlabel \"" << label << "\"";
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// set range
|
|
|
|
//
|
|
|
|
// set the xrange
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_xrange(const double iFrom,
|
2018-03-03 01:03:39 +00:00
|
|
|
const double iTo)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
|
|
|
|
cmdstr << "set xrange[" << iFrom << ":" << iTo << "]";
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
// set the yrange
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_yrange(const double iFrom,
|
2018-03-03 01:03:39 +00:00
|
|
|
const double iTo)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
|
|
|
|
cmdstr << "set yrange[" << iFrom << ":" << iTo << "]";
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
// set the zrange
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_zrange(const double iFrom,
|
2018-03-03 01:03:39 +00:00
|
|
|
const double iTo)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
|
|
|
|
cmdstr << "set zrange[" << iFrom << ":" << iTo << "]";
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// set the palette range
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::set_cbrange(const double iFrom,
|
2018-03-03 01:03:39 +00:00
|
|
|
const double iTo)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
|
|
|
|
cmdstr << "set cbrange[" << iFrom << ":" << iTo << "]";
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Plots a linear equation y=ax+b (where you supply the
|
|
|
|
// slope a and intercept b)
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::plot_slope(const double a,
|
2018-03-03 01:03:39 +00:00
|
|
|
const double b,
|
|
|
|
const std::string &title)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
//
|
|
|
|
// command to be sent to gnuplot
|
|
|
|
//
|
2018-03-03 01:03:39 +00:00
|
|
|
if (nplots > 0 && two_dim == true)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "replot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "plot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
cmdstr << a << " * x + " << b << " title \"";
|
|
|
|
|
2019-02-11 13:47:18 +00:00
|
|
|
if (title.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "f(x) = " << a << " * x + " << b;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << title;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
cmdstr << "\" with " << pstyle;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Do the actual plot
|
|
|
|
//
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Plot an equation supplied as a std::string y=f(x) (only f(x) expected)
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::plot_equation(const std::string &equation,
|
2018-03-03 01:03:39 +00:00
|
|
|
const std::string &title)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
//
|
|
|
|
// command to be sent to gnuplot
|
|
|
|
//
|
2018-03-03 01:03:39 +00:00
|
|
|
if (nplots > 0 && two_dim == true)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "replot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "plot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
cmdstr << equation << " title \"";
|
|
|
|
|
2019-02-11 13:47:18 +00:00
|
|
|
if (title.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "f(x) = " << equation;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << title;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
cmdstr << "\" with " << pstyle;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Do the actual plot
|
|
|
|
//
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// plot an equation supplied as a std::string y=(x)
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::plot_equation3d(const std::string &equation,
|
2018-03-03 01:03:39 +00:00
|
|
|
const std::string &title)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
//
|
|
|
|
// command to be sent to gnuplot
|
|
|
|
//
|
2018-03-03 01:03:39 +00:00
|
|
|
if (nplots > 0 && two_dim == false)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "replot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "splot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
cmdstr << equation << " title \"";
|
|
|
|
|
2019-02-11 13:47:18 +00:00
|
|
|
if (title.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "f(x,y) = " << equation;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << title;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
cmdstr << "\" with " << pstyle;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Do the actual plot
|
|
|
|
//
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Plots a 2d graph from a list of doubles (x) saved in a file
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::plotfile_x(const std::string &filename,
|
2018-03-03 01:03:39 +00:00
|
|
|
const unsigned int column,
|
|
|
|
const std::string &title)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// check if file exists
|
|
|
|
//
|
|
|
|
file_available(filename);
|
|
|
|
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
//
|
|
|
|
// command to be sent to gnuplot
|
|
|
|
//
|
2018-03-03 01:03:39 +00:00
|
|
|
if (nplots > 0 && two_dim == true)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "replot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "plot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
cmdstr << "\"" << filename << "\" using " << column;
|
|
|
|
|
2019-02-11 13:47:18 +00:00
|
|
|
if (title.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << " notitle ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << " title \"" << title << "\" ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-02-11 13:47:18 +00:00
|
|
|
if (smooth.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "with " << pstyle;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "smooth " << smooth;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Do the actual plot
|
|
|
|
//
|
2019-08-18 23:29:04 +00:00
|
|
|
cmd(cmdstr.str()); // nplots++; two_dim = true; already in cmd();
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Plots a 2d graph from a list of doubles (x y) saved in a file
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::plotfile_xy(const std::string &filename,
|
2018-03-03 01:03:39 +00:00
|
|
|
const unsigned int column_x,
|
|
|
|
const unsigned int column_y,
|
|
|
|
const std::string &title,
|
|
|
|
const unsigned int decimate)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// check if file exists
|
|
|
|
//
|
|
|
|
file_available(filename);
|
|
|
|
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
//
|
|
|
|
// command to be sent to gnuplot
|
|
|
|
//
|
2018-03-03 01:03:39 +00:00
|
|
|
if (nplots > 0 && two_dim == true)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "replot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "plot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2017-10-24 12:23:59 +00:00
|
|
|
cmdstr << "\"" << filename << "\" using " << column_x << ":" << column_y << " every " << std::to_string(decimate);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-02-11 13:47:18 +00:00
|
|
|
if (title.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << " notitle ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << " title \"" << title << "\" ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-02-11 13:47:18 +00:00
|
|
|
if (smooth.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "with " << pstyle;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "smooth " << smooth;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Do the actual plot
|
|
|
|
//
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Plots a 2d graph with errorbars from a list of doubles (x y dy) in a file
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::plotfile_xy_err(const std::string &filename,
|
2018-03-03 01:03:39 +00:00
|
|
|
const unsigned int column_x,
|
|
|
|
const unsigned int column_y,
|
|
|
|
const unsigned int column_dy,
|
|
|
|
const std::string &title)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// check if file exists
|
|
|
|
//
|
|
|
|
file_available(filename);
|
|
|
|
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
//
|
|
|
|
// command to be sent to gnuplot
|
|
|
|
//
|
2018-03-03 01:03:39 +00:00
|
|
|
if (nplots > 0 && two_dim == true)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "replot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "plot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2017-10-24 12:23:59 +00:00
|
|
|
cmdstr << "\"" << filename << "\" using "
|
2018-03-03 01:03:39 +00:00
|
|
|
<< column_x << ":" << column_y << ":" << column_dy
|
|
|
|
<< " with errorbars ";
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-02-11 13:47:18 +00:00
|
|
|
if (title.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << " notitle ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << " title \"" << title << "\" ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Do the actual plot
|
|
|
|
//
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Plots a 3d graph from a list of doubles (x y z) saved in a file
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::plotfile_xyz(const std::string &filename,
|
2018-03-03 01:03:39 +00:00
|
|
|
const unsigned int column_x,
|
|
|
|
const unsigned int column_y,
|
|
|
|
const unsigned int column_z,
|
|
|
|
const std::string &title)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// check if file exists
|
|
|
|
//
|
|
|
|
file_available(filename);
|
|
|
|
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
//
|
|
|
|
// command to be sent to gnuplot
|
|
|
|
//
|
2018-03-03 01:03:39 +00:00
|
|
|
if (nplots > 0 && two_dim == false)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "replot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "splot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2017-10-24 12:23:59 +00:00
|
|
|
cmdstr << "\"" << filename << "\" using " << column_x << ":" << column_y
|
2018-03-03 01:03:39 +00:00
|
|
|
<< ":" << column_z;
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-02-11 13:47:18 +00:00
|
|
|
if (title.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << " notitle with " << pstyle;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << " title \"" << title << "\" with " << pstyle;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Do the actual plot
|
|
|
|
//
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
2019-08-18 23:29:04 +00:00
|
|
|
// * note that this function is not valid for versions of GNUPlot below 4.2
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::plot_image(const unsigned char *ucPicBuf,
|
2018-03-03 01:03:39 +00:00
|
|
|
const unsigned int iWidth,
|
|
|
|
const unsigned int iHeight,
|
|
|
|
const std::string &title)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ofstream tmp;
|
|
|
|
std::string name = create_tmpfile(tmp);
|
2019-02-11 13:47:18 +00:00
|
|
|
if (name.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// write the data to file
|
|
|
|
//
|
|
|
|
int iIndex = 0;
|
2018-03-03 01:03:39 +00:00
|
|
|
for (unsigned int iRow = 0; iRow < iHeight; iRow++)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
for (unsigned int iColumn = 0; iColumn < iWidth; iColumn++)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
tmp << iColumn << " " << iRow << " "
|
2020-07-07 16:53:50 +00:00
|
|
|
<< static_cast<float>(ucPicBuf[iIndex++]) << '\n';
|
2017-10-21 11:05:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp.flush();
|
|
|
|
tmp.close();
|
|
|
|
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
//
|
|
|
|
// command to be sent to gnuplot
|
|
|
|
//
|
2018-03-03 01:03:39 +00:00
|
|
|
if (nplots > 0 && two_dim == true)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "replot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "plot ";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2019-02-11 13:47:18 +00:00
|
|
|
if (title.empty())
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "\"" << name << "\" with image";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "\"" << name << "\" title \"" << title << "\" with image";
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Do the actual plot
|
|
|
|
//
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::plot_circle(double east, double north, double radius, const std::string &label)
|
2017-10-22 16:05:02 +00:00
|
|
|
{
|
|
|
|
std::ostringstream cmdstr;
|
|
|
|
//
|
|
|
|
// command to be sent to gnuplot
|
|
|
|
//
|
|
|
|
cmdstr << "set object circle at " + std::to_string(east) + "," + std::to_string(north) + " size " +
|
2018-03-03 01:03:39 +00:00
|
|
|
std::to_string(radius) + " back\n";
|
2017-10-22 16:05:02 +00:00
|
|
|
|
2019-02-11 13:47:18 +00:00
|
|
|
if (!label.empty())
|
2017-10-22 16:05:02 +00:00
|
|
|
{
|
|
|
|
double east_label = (std::cos(M_PI / 3.0) * radius) * 1.1 + east;
|
|
|
|
double north_label = (std::sin(M_PI / 3.0) * radius) * 1.1 + north;
|
|
|
|
cmdstr << "set label \"" + label + "\" at first " + std::to_string(east_label) +
|
2018-03-03 01:03:39 +00:00
|
|
|
", " + std::to_string(north_label) + " norotate back nopoint offset 0,0\n";
|
2017-10-22 16:05:02 +00:00
|
|
|
}
|
|
|
|
if (nplots > 0)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "replot ";
|
|
|
|
}
|
2018-03-03 01:03:39 +00:00
|
|
|
else
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
cmdstr << "plot ";
|
|
|
|
}
|
2017-10-22 16:05:02 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Do the actual plot
|
|
|
|
//
|
|
|
|
cmd(cmdstr.str());
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Sends a command to an active gnuplot session
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline Gnuplot &Gnuplot::cmd(const std::string &cmdstr)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (!(valid))
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// int fputs ( const char * str, FILE * stream );
|
|
|
|
// writes the string str to the stream.
|
2017-10-24 12:23:59 +00:00
|
|
|
// The function begins copying from the address specified (str) until it
|
|
|
|
// reaches the terminating null character ('\0'). This final
|
2017-10-21 11:05:51 +00:00
|
|
|
// null-character is not copied to the stream.
|
2018-03-03 01:03:39 +00:00
|
|
|
fputs((cmdstr + "\n").c_str(), gnucmd);
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
// int fflush ( FILE * stream );
|
2017-10-24 12:23:59 +00:00
|
|
|
// If the given stream was open for writing and the last i/o operation was
|
|
|
|
// an output operation, any unwritten data in the output buffer is written
|
|
|
|
// to the file. If the argument is a null pointer, all open files are
|
2017-10-21 11:05:51 +00:00
|
|
|
// flushed. The stream remains open after this call.
|
|
|
|
fflush(gnucmd);
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
if (cmdstr.find("replot") != std::string::npos)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
if (cmdstr.find("splot") != std::string::npos)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
two_dim = false;
|
|
|
|
nplots++;
|
|
|
|
}
|
2018-03-03 01:03:39 +00:00
|
|
|
else if (cmdstr.find("plot") != std::string::npos)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
two_dim = true;
|
|
|
|
nplots++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Opens up a gnuplot session, ready to receive commands
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline void Gnuplot::init()
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
// char * getenv ( const char * name ); get value of environment variable
|
|
|
|
// Retrieves a C string containing the value of the environment variable
|
|
|
|
// whose name is specified as argument. If the requested variable is not
|
|
|
|
// part of the environment list, the function returns a NULL pointer.
|
|
|
|
#if (defined(unix) || defined(__unix) || defined(__unix__)) && !defined(__APPLE__)
|
2019-02-21 10:42:56 +00:00
|
|
|
if (std::getenv("DISPLAY") == nullptr)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
valid = false;
|
|
|
|
throw GnuplotException("Can't find DISPLAY variable");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// if gnuplot not available
|
|
|
|
if (!Gnuplot::get_program_path())
|
|
|
|
{
|
|
|
|
valid = false;
|
|
|
|
throw GnuplotException("Can't find gnuplot");
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// open pipe
|
|
|
|
//
|
2017-10-24 12:23:59 +00:00
|
|
|
std::string tmp = Gnuplot::m_sGNUPlotPath + "/" +
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot::m_sGNUPlotFileName;
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2018-06-20 10:04:03 +00:00
|
|
|
// FILE *popen(const char *command, const char *mode);
|
|
|
|
// The popen() function shall execute the command specified by the string
|
|
|
|
// command, create a pipe between the calling program and the executed
|
|
|
|
// command, and return a pointer to a stream that can be used to either read
|
|
|
|
// from or write to the pipe.
|
2017-10-21 11:05:51 +00:00
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
|
2018-03-03 01:03:39 +00:00
|
|
|
gnucmd = _popen(tmp.c_str(), "w");
|
2017-10-21 11:05:51 +00:00
|
|
|
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
2018-03-03 01:03:39 +00:00
|
|
|
gnucmd = popen(tmp.c_str(), "w");
|
2017-10-21 11:05:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// popen() shall return a pointer to an open stream that can be used to read
|
2017-10-24 12:23:59 +00:00
|
|
|
// or write to the pipe. Otherwise, it shall return a null pointer and may
|
2017-10-21 11:05:51 +00:00
|
|
|
// set errno to indicate the error.
|
|
|
|
if (!gnucmd)
|
|
|
|
{
|
|
|
|
valid = false;
|
|
|
|
throw GnuplotException("Couldn't open connection to gnuplot");
|
|
|
|
}
|
|
|
|
|
|
|
|
nplots = 0;
|
|
|
|
valid = true;
|
|
|
|
smooth = "";
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// set terminal type
|
2017-10-21 11:05:51 +00:00
|
|
|
showonscreen();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Find out if a command lives in m_sGNUPlotPath or in PATH
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline bool Gnuplot::get_program_path()
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// first look in m_sGNUPlotPath for Gnuplot
|
|
|
|
//
|
2017-10-24 12:23:59 +00:00
|
|
|
std::string tmp = Gnuplot::m_sGNUPlotPath + "/" +
|
2018-03-03 01:03:39 +00:00
|
|
|
Gnuplot::m_sGNUPlotFileName;
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
|
2018-03-03 01:03:39 +00:00
|
|
|
if (Gnuplot::file_exists(tmp, 0)) // check existence
|
2017-10-21 11:05:51 +00:00
|
|
|
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
2018-03-03 01:03:39 +00:00
|
|
|
if (Gnuplot::file_exists(tmp, 1)) // check existence and execution permission
|
2017-10-21 11:05:51 +00:00
|
|
|
#endif
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// second look in PATH for Gnuplot
|
|
|
|
//
|
2018-05-22 18:55:03 +00:00
|
|
|
const char *path;
|
2017-10-21 11:05:51 +00:00
|
|
|
// Retrieves a C string containing the value of environment variable PATH
|
2018-01-29 18:22:49 +00:00
|
|
|
path = std::getenv("PATH");
|
2018-05-22 18:55:03 +00:00
|
|
|
std::stringstream s;
|
2019-03-03 12:39:55 +00:00
|
|
|
if (path != nullptr)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2019-03-03 09:57:05 +00:00
|
|
|
s << path;
|
|
|
|
}
|
2019-03-03 10:56:57 +00:00
|
|
|
if (s.fail())
|
2019-03-03 09:57:05 +00:00
|
|
|
{
|
2019-03-03 10:56:57 +00:00
|
|
|
throw GnuplotException("PATH is not well defined");
|
|
|
|
}
|
|
|
|
std::string path_str;
|
2019-03-03 12:39:55 +00:00
|
|
|
path_str = s.str();
|
|
|
|
|
2018-05-21 19:27:06 +00:00
|
|
|
std::list<std::string> ls;
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// split path (one long string) into list ls of strings
|
2017-10-21 11:05:51 +00:00
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
|
2018-05-21 19:27:06 +00:00
|
|
|
stringtok(ls, path_str, ";");
|
2017-10-21 11:05:51 +00:00
|
|
|
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
2018-05-21 19:27:06 +00:00
|
|
|
stringtok(ls, path_str, ":");
|
2017-10-21 11:05:51 +00:00
|
|
|
#endif
|
|
|
|
|
2018-05-21 19:27:06 +00:00
|
|
|
// scan list for Gnuplot program files
|
|
|
|
for (std::list<std::string>::const_iterator i = ls.begin();
|
|
|
|
i != ls.end(); ++i)
|
|
|
|
{
|
|
|
|
tmp = (*i) + "/" + Gnuplot::m_sGNUPlotFileName;
|
2017-10-21 11:05:51 +00:00
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
|
2018-05-21 19:27:06 +00:00
|
|
|
if (Gnuplot::file_exists(tmp, 0)) // check existence
|
2017-10-21 11:05:51 +00:00
|
|
|
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
2018-05-21 19:27:06 +00:00
|
|
|
if (Gnuplot::file_exists(tmp, 1)) // check existence and execution permission
|
2017-10-21 11:05:51 +00:00
|
|
|
#endif
|
2018-05-21 19:27:06 +00:00
|
|
|
{
|
|
|
|
Gnuplot::m_sGNUPlotPath = *i; // set m_sGNUPlotPath
|
|
|
|
return true;
|
2017-10-21 11:05:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-21 19:27:06 +00:00
|
|
|
|
|
|
|
tmp = "Can't find gnuplot neither in PATH nor in \"" +
|
|
|
|
Gnuplot::m_sGNUPlotPath + "\"";
|
|
|
|
Gnuplot::m_sGNUPlotPath = "";
|
|
|
|
throw GnuplotException(tmp);
|
2017-10-21 11:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// check if file exists
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline bool Gnuplot::file_exists(const std::string &filename, int mode)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (mode < 0 || mode > 7)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"In function \"Gnuplot::file_exists\": mode\
|
2017-10-21 11:05:51 +00:00
|
|
|
has to be an integer between 0 and 7");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-20 10:04:03 +00:00
|
|
|
// int _access(const char *path, int mode);
|
|
|
|
// returns 0 if the file has the given mode,
|
|
|
|
// it returns -1 if the named file does not exist or is not accessible in
|
|
|
|
// the given mode
|
|
|
|
// mode = 0 (F_OK) (default): checks file for existence only
|
|
|
|
// mode = 1 (X_OK): execution permission
|
|
|
|
// mode = 2 (W_OK): write permission
|
|
|
|
// mode = 4 (R_OK): read permission
|
|
|
|
// mode = 6 : read and write permission
|
|
|
|
// mode = 7 : read, write and execution permission
|
2017-10-21 11:05:51 +00:00
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
|
|
|
|
if (_access(filename.c_str(), mode) == 0)
|
|
|
|
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
2018-03-03 01:03:39 +00:00
|
|
|
if (access(filename.c_str(), mode) == 0)
|
2017-10-21 11:05:51 +00:00
|
|
|
#endif
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
return false;
|
2017-10-21 11:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-29 20:48:00 +00:00
|
|
|
inline bool Gnuplot::file_available(const std::string &filename)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
std::ostringstream except;
|
2018-03-03 01:03:39 +00:00
|
|
|
if (Gnuplot::file_exists(filename, 0)) // check existence
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2018-03-03 01:03:39 +00:00
|
|
|
if (!(Gnuplot::file_exists(filename, 4)))
|
|
|
|
{ // check read permission
|
2017-10-21 11:05:51 +00:00
|
|
|
except << "No read permission for File \"" << filename << "\"";
|
2018-03-03 01:03:39 +00:00
|
|
|
throw GnuplotException(except.str());
|
2017-10-21 11:05:51 +00:00
|
|
|
return false;
|
2018-03-03 01:03:39 +00:00
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
except << "File \"" << filename << "\" does not exist";
|
2018-03-03 01:03:39 +00:00
|
|
|
throw GnuplotException(except.str());
|
2017-10-21 11:05:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-18 23:29:04 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-10-21 11:05:51 +00:00
|
|
|
//
|
|
|
|
// Opens a temporary file
|
|
|
|
//
|
2019-06-29 20:48:00 +00:00
|
|
|
inline std::string Gnuplot::create_tmpfile(std::ofstream &tmp)
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
|
2019-08-18 23:29:04 +00:00
|
|
|
char name[] = "gnuplotiXXXXXX"; // tmp file in working directory
|
2017-10-21 11:05:51 +00:00
|
|
|
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
2018-03-03 01:03:39 +00:00
|
|
|
char name[] = "/tmp/gnuplotiXXXXXX"; // tmp file in /tmp
|
2017-10-21 11:05:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// check if maximum number of temporary files reached
|
|
|
|
//
|
|
|
|
if (Gnuplot::tmpfile_num == GP_MAX_TMP_FILES - 1)
|
|
|
|
{
|
|
|
|
std::ostringstream except;
|
|
|
|
except << "Maximum number of temporary files reached ("
|
2020-07-07 16:53:50 +00:00
|
|
|
<< GP_MAX_TMP_FILES << "): cannot open more files\n";
|
2017-10-21 11:05:51 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
throw GnuplotException(except.str());
|
2017-10-21 11:05:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 21:00:09 +00:00
|
|
|
// int mkstemp(char *name);
|
|
|
|
// shall replace the contents of the string pointed to by "name" by a unique
|
|
|
|
// filename, and return a file descriptor for the file open for reading and
|
|
|
|
// writing. Otherwise, -1 shall be returned if no suitable file could be
|
|
|
|
// created. The string in template should look like a filename with six
|
|
|
|
// trailing 'X' s; mkstemp() replaces each 'X' with a character from the
|
|
|
|
// portable filename character set. The characters are chosen such that the
|
|
|
|
// resulting name does not duplicate the name of an existing file at the
|
|
|
|
// time of a call to mkstemp()
|
2018-07-02 15:43:34 +00:00
|
|
|
|
2018-12-09 21:00:09 +00:00
|
|
|
//
|
|
|
|
// open temporary files for output
|
|
|
|
//
|
2018-05-22 18:55:03 +00:00
|
|
|
|
2017-10-21 11:05:51 +00:00
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
|
|
|
|
if (_mktemp(name) == NULL)
|
|
|
|
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
2018-05-22 18:55:03 +00:00
|
|
|
mode_t mask = umask(S_IXUSR | S_IRWXG | S_IRWXO);
|
2018-03-03 01:03:39 +00:00
|
|
|
if (mkstemp(name) == -1)
|
2017-10-21 11:05:51 +00:00
|
|
|
#endif
|
2018-03-03 01:03:39 +00:00
|
|
|
{
|
|
|
|
std::ostringstream except;
|
|
|
|
except << "Cannot create temporary file \"" << name << "\"";
|
2018-05-22 18:55:03 +00:00
|
|
|
#if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
|
|
|
umask(mask);
|
|
|
|
#endif
|
2018-03-03 01:03:39 +00:00
|
|
|
throw GnuplotException(except.str());
|
|
|
|
}
|
2018-05-22 18:55:03 +00:00
|
|
|
#if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
|
|
|
umask(mask);
|
|
|
|
#endif
|
2017-10-21 11:05:51 +00:00
|
|
|
tmp.open(name);
|
|
|
|
if (tmp.bad())
|
|
|
|
{
|
|
|
|
std::ostringstream except;
|
|
|
|
except << "Cannot create temporary file \"" << name << "\"";
|
|
|
|
throw GnuplotException(except.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Save the temporary filename
|
|
|
|
//
|
2018-12-03 21:20:40 +00:00
|
|
|
tmpfile_list.emplace_back(name);
|
2017-10-21 11:05:51 +00:00
|
|
|
Gnuplot::tmpfile_num++;
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-29 20:48:00 +00:00
|
|
|
inline void Gnuplot::remove_tmpfiles()
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2019-02-11 13:47:18 +00:00
|
|
|
if (!(tmpfile_list).empty())
|
2017-10-21 11:05:51 +00:00
|
|
|
{
|
2018-12-09 21:00:09 +00:00
|
|
|
for (auto &i : tmpfile_list)
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
if (remove(i.c_str()) != 0)
|
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
std::cout << "Problem closing files\n";
|
2019-02-11 20:13:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-21 11:05:51 +00:00
|
|
|
|
|
|
|
Gnuplot::tmpfile_num -= tmpfile_list.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|