This commit is contained in:
2025-01-23 11:29:42 +08:00
parent 737b39fee3
commit 22f8a6aaa2
5 changed files with 241 additions and 0 deletions

83
lib/graphic/gnuplot.cpp Normal file
View File

@@ -0,0 +1,83 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#include "gnuplot.h"
gctl::gnuplot::gnuplot(bool persist)
{
_2buffer = false;
pipe = popen(persist ? "gnuplot -persist" : "gnuplot", "w");
if (!pipe) throw std::runtime_error("gnuplot pipe failed to open. Try install gnuplot first.");
}
gctl::gnuplot::~gnuplot()
{
if (pipe) pclose(pipe);
}
void gctl::gnuplot::to_buffer()
{
_2buffer = true;
return;
}
void gctl::gnuplot::send(const std::string& cmd, bool next_block)
{
if (!pipe) return;
if (!_2buffer) fputs((cmd + "\n").c_str(), pipe);
else
{
if (next_block) buffer.push_back(cmd + "\n\n");
else buffer.push_back(cmd + "\n");
}
return;
}
void gctl::gnuplot::send_buffer(size_t repeat)
{
if (!pipe) return;
for (size_t i = 0; i < repeat; i++)
{
for (auto& line : buffer) fputs(line.c_str(), pipe);
fputs("\n", pipe);
}
fflush(pipe);
buffer.clear();
return;
}
void gctl::gnuplot::save_buffer(const std::string& filename)
{
std::ofstream outfile;
open_outfile(outfile, filename, ".gp");
for (auto& line : buffer) outfile << line;
outfile.close();
return;
}

85
lib/graphic/gnuplot.h Normal file
View File

@@ -0,0 +1,85 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#ifndef _GCTL_GNUPLOT_H
#define _GCTL_GNUPLOT_H
#include "gctl/core.h"
#include "gctl/utility.h"
namespace gctl
{
class gnuplot
{
public:
/**
* @brief Construct a new gnuplot object
*
* @param persist Gnuplot will hold the window after plot if true.
*/
gnuplot(bool persist = true);
// destructor
virtual ~gnuplot();
/**
* @brief Set this to send all lines to buffer.
*/
void to_buffer();
/**
* @brief Send a line to gnuplot and execute it. If send2buffer is called before, then the line will be stored in the buffer.
*
* @param cmd command line.
* @param next_block If true, an empty line will be added to the buffer after the cmd.
*/
void send(const std::string& cmd, bool next_block = false);
/**
* @brief Send the buffer to gnuplot and execute it at once. Then clear the buffer.
*
* @param repeat How many times to repeat the buffer.
*/
void send_buffer(size_t repeat = 1);
/**
* @brief Save the buffer to a file. This will not clear the buffer.
*
* @param filename File name.
*/
void save_buffer(const std::string& filename);
private:
gnuplot(gnuplot const&) = delete;
void operator=(gnuplot const&) = delete;
FILE* pipe;
std::vector<std::string> buffer;
bool _2buffer;
};
}
#endif // _GCTL_GNUPLOT_H