This commit is contained in:
张壹 2025-02-24 21:45:40 +08:00
parent a1bf953d4b
commit a43fd84986
4 changed files with 116 additions and 37 deletions

View File

@ -45,7 +45,7 @@
#include "../lib/core.h"
#include "../lib/io.h"
#include "../lib/algorithm.h"
#include "../lib/algorithms.h"
using namespace gctl;

View File

@ -32,9 +32,11 @@ int main(int argc, char *argv[])
gctl::gnuplot gt;
//one line test
//gt.send("plot [-pi/2:pi] cos(x),-(sin(x) > sin(x+1) ? sin(x) : sin(x+1))");
gt.send("set terminal dumb");
gt.send("plot [-pi/2:pi] cos(x),-(sin(x) > sin(x+1) ? sin(x) : sin(x+1))");
//buffer test (bessel animation)
/*
gt.to_buffer();
gt.send("set terminal gif animate delay 10 size 600,400");
gt.send("set output 'bessel.gif'");
@ -67,6 +69,7 @@ int main(int argc, char *argv[])
gt.save_buffer("bessel");
gt.send_buffer();
*/
//data test
/*

View File

@ -119,23 +119,6 @@ void gctl::cliplot::set_hname(const std::string &hname)
return;
}
std::string gctl::cliplot::axis_label(double num, int digs, int &odr)
{
std::stringstream ss;
if (floor(log10(abs(num))) >= digs)
{
ss << std::scientific << std::setprecision(digs) << num;
odr = floor(log10(abs(num)));
if (num < 0) odr *= -1;
}
else
{
ss << std::fixed << std::setprecision(digs) << num;
odr = 0;
}
return ss.str();
}
void gctl::cliplot::plot_line(double x1, double x2, double y1, double y2, char s, const std::string &t)
{
if (x1 > x2)
@ -178,6 +161,44 @@ void gctl::cliplot::plot_data(const std::vector<double> &x, const std::vector<do
}
void gctl::cliplot::display(std::ostream &os)
{
if (new_screen_)
{
os << GCTL_CLEARALL;
GCTL_MOVEUP(os, height_);
}
plot_axis();
for (size_t i = 0; i < height_; i++)
{
for (size_t j = 0; j < width_; j++)
{
os << att_[i*width_ + j] << sym_[i*width_ + j] << GCTL_RESET;
}
os << std::endl;
}
return;
}
std::string gctl::cliplot::axis_label(double num, int digs, int &odr)
{
std::stringstream ss;
if (floor(log10(abs(num))) >= digs)
{
ss << std::scientific << std::setprecision(digs) << num;
odr = floor(log10(abs(num)));
if (num < 0) odr *= -1;
}
else
{
ss << std::fixed << std::setprecision(digs) << num;
odr = 0;
}
return ss.str();
}
void gctl::cliplot::plot_axis()
{
for (size_t i = w0_; i < width_; i++)
set(i, h0_, '-', GCTL_BOLD);
@ -258,20 +279,5 @@ void gctl::cliplot::display(std::ostream &os)
{
set(w0_ + 6 + i, 0, hname_[i], GCTL_BOLD);
}
if (new_screen_)
{
os << GCTL_CLEARALL;
GCTL_MOVEUP(os, height_);
}
for (size_t i = 0; i < height_; i++)
{
for (size_t j = 0; j < width_; j++)
{
os << att_[i*width_ + j] << sym_[i*width_ + j] << GCTL_RESET;
}
os << std::endl;
}
return;
}

View File

@ -55,7 +55,7 @@ namespace gctl
*/
cliplot(size_t width, size_t height, double xmin, double xmax, double ymin, double ymax);
virtual ~cliplot();
virtual ~cliplot(); ///< 析构函数
/**
* @brief
@ -72,27 +72,97 @@ namespace gctl
*/
void set(size_t w, size_t h, char sym, const std::string &att);
/**
* @brief
*
* @param xt_num x轴刻度数量
* @param yt_num y轴刻度数量
*/
void set_axis(int xt_num, int yt_num);
/**
* @brief
*
* @param digs
*/
void set_digs(int digs);
/**
* @brief 使
*
* @param new_screen 使
*/
void set_new_screen(bool new_screen);
/**
* @brief
*
* @param wname x轴名称
*/
void set_wname(const std::string &wname);
/**
* @brief
*
* @param hname y轴名称
*/
void set_hname(const std::string &hname);
std::string axis_label(double num, int digs, int &odr);
/**
* @brief 线
*
* @param x1 线x坐标
* @param x2 线x坐标
* @param y1 线y坐标
* @param y2 线y坐标
* @param s 线
* @param t 线
*/
void plot_line(double x1, double x2, double y1, double y2, char s = '.', const std::string &t = GCTL_BOLDGREEN);
/**
* @brief
*
* @param func
* @param s
* @param t
*/
template <typename FuncOp>
void plot_func(FuncOp func, char s = '.', const std::string &t = GCTL_BOLDGREEN);
/**
* @brief
*
* @param x x坐标
* @param y y坐标
* @param s
* @param t
*/
void plot_data(const std::vector<double> &x, const std::vector<double> &y, char s = '.', const std::string &t = GCTL_BOLDGREEN);
/**
* @brief
*
* @param os
*/
void display(std::ostream &os = std::cout);
private:
/**
* @brief
*
* @param num
* @param digs
* @param odr 10
* @return std::string
*/
std::string axis_label(double num, int digs, int &odr);
/**
* @brief
*/
void plot_axis();
private:
bool new_screen_;
int digs_; // 数字位数(包括小数点)