gnuplot.h
来自「A Library of Efficient Data Types and Al」· C头文件 代码 · 共 427 行
H
427 行
#ifndef GNUPLOT_PIPES_H#define GNUPLOT_PIPES_H#if defined LEDA_INSTALL#include <LEDA/std/stdio.h>#include <LEDA/std/stdlib.h>#include <LEDA/std/string.h>#include <LEDA/std/stdarg.h>#include <LEDA/std/iostream.h>#include <LEDA/std/fstream.h>#else#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#include <iostream.h>#include <fstream.h>#endif#define GNUPLOT_DISABLE_EXCEPTIONStemplate <class T, class GP_ARG>class gp_arg{ T arg; public: class error { const char* what; const char* message; public: error(const char* w, const char* m) : what(w), message(m) { std::cerr << "gnuplot_ctrl:" << "\n"; std::cerr << "\t Bad " << what << " argument!\n";#if defined GNUPLOT_DISABLE_EXCEPTIONS exit(1);#else throw(*this); #endif } const char* get_what() const { return what; } const char* get_message() const { return message; } }; gp_arg(const T& x) : arg(x) { if (!GP_ARG::check_arg(arg)) error(GP_ARG::name, "Bad argument!"); } operator T& () { return (T&) arg; } gp_arg& operator=(const T& x) { arg = x; return *this; } friend bool operator==(const gp_arg<T,GP_ARG>& a, const gp_arg<T,GP_ARG>& b) { return a.arg == b.arg; } };class gp_style{ static const int min = 0; static const int max = 8; static const int size = 9; static const char* const str[size]; protected: static const char* const name; static bool check_arg(int); friend class gp_arg<int, gp_style>; public: typedef gp_arg<int,gp_style> num; /* current admissible arguments for set_style */ static const num lines; static const num points; static const num linespoints; static const num impulses; static const num dots; static const num steps; static const num errorbars; static const num boxes; static const num boxerrorbars; static const char* get(num);};class gp_terminal{ static const int min = 0; static const int max = 3; static const int size = 4; static const char* const str[size]; protected: static const char* const name; static bool check_arg(int); friend class gp_arg<int, gp_terminal>; public: typedef gp_arg<int,gp_terminal> num; static const num x11; static const num postscript; static const num pslatex; static const num png; static const char* get(num);};/*{\Manpage {gnuplot_ctrl} {} {Gnuplot Interface} {GPI}}*/ /*{\Mdefinition An instance |GPI| of type |\Mname| is an object providing an interface to the popular scientific plotting program Gnuplot. The code is based on Nicolas Devillars ANSI-C interface |gnuplot_i|.}*/class gnuplot_ctrl { static const int TMP_FILES = 10; FILE* gnucmd; /* command file handling */ char buffer[4096]; /* dummy buffer */ char cmd[2048]; /* command string */ char pstyle[32]; /* current plotting style */ int nplots; /* number of active plots at the moment */ int ntmp; /* number of active temp files */ void init(); bool check_x_display(); char* get_program_path(const char*); char* get_tmpname(int); bool create_tmpfile(ofstream&, const char*); void plot_user_data(const char*, const char*); public: class error { const char* where; const char* message; public: error(const char* w, const char* m) : where(w), message(m) {#if defined GNUPLOT_DISABLE_EXCEPTIONS std::cerr << "\n gnuplot_ctr::" << where << "\n"; std::cerr << "\t" << message << "\n"; exit(1);#else throw(*this);#endif } const char* get_where() const { return where; } const char* get_message() const { return message; } }; /*{\Mcreation 1 6}*/ gnuplot_ctrl() { init(); }/*{\Mcreate creates an instance |\Mvar| of type |\Mname|.}*/ gnuplot_ctrl(const char* title) { init(); set_title(title); }/*{\Mcreate creates an instance |\Mvar| of type |\Mname| and sets |title| label for this gnuplot session.}*/ ~gnuplot_ctrl() { close(); } /*{\Moperations 1 1}*/ void command(const char* cmd_str, ...); /*{\Mop Sends a command to an active gnuplot session.}*/ void plot_x(double* d, int n, const char* title = "no_name");/*{\Mop Plots out a 2d graph from a list of |n| doubles. The |x|-coordinate is the index of the double in the list, the |y|-coordinate is the double in the list. |title| is the title of the plot.}*/ void plot_xy(double* x, double* y, int n, const char* title = "no_name");/*{\Mop Plots out a 2d graph from a list of points. Provide points through a list of |x| and a list of |y| coordinates. Both provided arrays are assumed to contain the same number |n| of values. }*/ void plot_slope(double a, double b, const char* title = "no_name");/*{\Mop Plot a slope on a gnuplot session. The provided slope has an equation of the form: |y = ax+b|}*/ void plot_equation(char* equation, const char* title = "no_name");/*{\Mop Plots out a curve of given equation. The general form of the equation is |y=f(x)|, you only provide the |f(x)| side of the equation.}*/ void splot_equation(char* equation, const char* title = "no_name");/*{\Mop is the command for drawing 3-d plots.}*/ void reset_plot();/*{\Mop Resets a gnuplot session (next plot will erase previous ones). }*/ void replot();/*{\Mop The |replot| command repeats the last |plot|. }*/ void close(); void set_output(const char* fname);/*{\Mop By default, screens are displayed to the standard output. The |set_output| command redirects the display to the specified file. }*/ void set_title(const char* title, int xoff = 0, int yoff = 0, const char* font = default_font,int size = default_font_size);/*{\Mop The |set_title| command produces a plot title that is centered at the top of the plot. Specifying constants |xoff| or |yoff| as optional offsets for the title will move the title |xoff| or |yoff| character screen coordinates (not graph coordinates). |font| is used to specify the font with which the title is to be written; the units of the font |size| depend upon which terminal is used.}*/ /* void set_key(const char * place = "top", const char * justify = "Right", int v_space, int width_inc, const char * title); *//*{\Mop The |set_key| enables a key (or legend) describing plots on a plot. The contents of the key, i.e., the names given to each plotted data set and function and samples of the lines and/or symbols used to represent them, are determined by the |title| of the plot command. By default the key is placed (|place|) in the upper right corner of the graph. The keywords `left`, `right`, `top`, `bottom`, `outside` and `below` may be used to place the key in the other corners inside the graph or to the right (outside) or below the graph. They may be given alone or combined. Justification of the labels within the key is controlled by `Left` or `Right` (default is `Right`). The text and sample can be reversed (`reverse`) and a box can be drawn around the key (`box {...}`) in a specified `linetype` and `linewidth`, or a user-defined `linestyle`. Note that not all terminal drivers support linewidth selection, though.}*/ void set_samples(int i);/*{\Mop The sampling rate of functions, or for interpolating data, may be changed by the |set_samples| command. }*/ void set_isosamples(int i);/*{\Mop The isoline density (grid) for plotting functions as surfaces may be changed by the |set_isosamples| command. }*//*{\Mtext\bigskip{\bf Axis}}*/ void set_xlabel(const char* label = "X", int xoff = 0, int yoff = 0, const char* font = default_font,int size = default_font_size);/*{\Mop The |set_xlabel| command sets the x axis label. Specifying the constants |xoff| or |yoff| as optional offsets for a label will move it |xoff| or |yoff| character widths or heights. The size of a character depends on both the font and the terminal. |font| is used to specify the font in which the label is written; the units of the font |size| depend upon which terminal is used. }*/ void set_ylabel(const char* label = "Y", int xoff = 0, int yoff = 0, const char* font = default_font,int size = default_font_size);/*{\Mop The |set_ylabel| command sets the y axis label. For details see |set_xlabel|.}*/ void set_zlabel(const char* label = "Z", int xoff = 0, int yoff = 0, const char* font = default_font,int size = default_font_size); /*{\Mop The |set_zlabel| command sets the z axis label. For details see |set_xlabel|.}*/ void set_xrange(const char* xmin = "*", const char* xmax = "*");/*{\Mop The |set_xrange| command sets the horizontal range that will be displayed. Where |xmin| and |xmax| terms are constants, expressions or an asterisk to set autoscaling. If the data are time/date, you must give the range as a quoted string according to the time format.\\ Examples: \begin{itemize} \item sets xrange to the default: |set_xrange("-10","10")| \item sets xrange to increase downwards: |set_xrange("10","-10")| \item changes |xmax| to 10 without affecting |xmin| \\ (which may still be autoscaled): |set_xrange("","10")| \item autoscales |xmin| while leaving |xmax| unchanged: |set_xrange("*","")| \end{itemize}}*/ void set_yrange(const char* ymin = "*", const char* ymax = "*");/*{\Mop The |set_yrange| command sets the vertical range that will be displayed. For details see |set_xrange|.}*/ void set_zrange(const char* zmin = "*", const char* zmax = "*");/*{\Mop The |set_zrange| command sets the range that will be displayed on the z axis. For details see |set_xrange|.}*/ /*{\Mtext\bigskip{\bf Misc Members}}*/ void set_timestamp(const char* format = "%d/%m/%y %H:%M", int xoff = 0,int yoff = 0, const char* font = default_font);/*{\Mop The command |set_timestamp| places the time and date of the plot in the left margin. The |format| string allows you to choose the format used to write the date and time. Its default value is what |asctime()| uses: "%a %b %d %H:%M:%S %Y" (weekday, month name, day of the month, hours, minutes, seconds, four-digit year). The constants |xoff| and |yoff| are offsets from the default position given in character screen coordinates. |font| is used to specify the font with which the time is to be written.\\ Example:\\ |set_timestamp("%d/%m/%y %H:%M",80,-2,"Helvetica")| }*/ void no_timestamp();/*{\Mop disable timestamp.}*//*{\Mtext\bigskip{\bf Styles and Terminals }}*/ void set_style(gp_style::num style);/*{\Mop Changes the plotting style of a gnuplot session. The provided plotting style is a |gp_style::num|. It must be one of the following: \\[-4ex] \begin{tabbing} |gp_style::boxeserrorbars| \quad \= |gp_style::errorbars| \quad \= |gp_style::linespoints| \\ |gp_style::lines| \> |gp_style::points| \> |gp_style::impulses| \\ |gp_style::dots| \> |gp_style::steps| \> |gp_style::boxes| \end{tabbing} }*/ void set_terminal(gp_terminal::num terminal = gp_terminal::x11);/*{\Mop Sets the output terminal for a gnuplot session. The provided terminal is |gp_terminal::num|. It must be one of the following: \\[-4ex] \begin{tabbing} |gp_terminal::x11| \quad \= |gp_terminal::postscript| \quad \= |gp_terminal::pslatex| \end{tabbing} }*/ static const char * const default_font; static const int default_font_size;};/*{\Mtext\bigskip{\bf Non-Member Functions}}*/void gnuplot_plot(double* x, double* y, int n, gp_style::num style, const char* title = "no_title", const char* label_x = "X", const char* label_y = "Y");/*{\Mfunc This function opens a new gnuplot session, plots the provided signal as an |X| or |XY| signal depending on a provided |y|, waits for a carriage return on stdin and closes the session. |title| is the Plot title, |style| is the Plot style |label_x| is the label for the X-axis, |label_y| is the label for the |Y|-axis, |x| and |y| are the arrays of the |X| and |Y| coordinates (|y| can be |NULL|) and |n| is the number of values. }*/#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?