source: tags/0.6.8/lib/Gnuplot.h @ 848

Last change on this file since 848 was 731, checked in by Peter Johansson, 13 years ago

update copyright statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1#ifndef _theplu_svndigest_gnuplot_
2#define _theplu_svndigest_gnuplot_
3
4// $Id: Gnuplot.h 731 2008-12-15 19:03:04Z peter $
5
6/*
7  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2007, 2008 Peter Johansson
9
10  This file is part of svndigest, http://dev.thep.lu.se/svndigest
11
12  svndigest is free software; you can redistribute it and/or modify it
13  under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16
17  svndigest is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  02111-1307, USA.
26*/
27
28#include <cassert>
29#include <cstdio>
30#include <list>
31#include <stdexcept>
32#include <sstream>
33#include <string>
34#include <vector>
35
36
37#include <iostream>
38
39namespace theplu {
40namespace svndigest {
41
42  ///
43  /// If something goes wrong in the use of the Gnuplot class, a
44  /// GnuplotException is thrown.
45  ///
46  struct GnuplotException : public std::runtime_error
47  { inline GnuplotException(const std::string& msg) : runtime_error(msg) {} };
48
49  ///
50  /// The Gnuplot class creates a pipe to 'gnuplot' and facilitates
51  /// communication with the gnuplot binary.
52  ///
53  /// @see GnuplotFE
54  ///
55  class Gnuplot
56  {
57  public:
58    ///
59    /// This constructor sets up the pipe to the first gnuplot
60    /// executable found in the PATH environment variable. The PATH
61    /// variable must exist.
62    ///
63    /// @throw Throws a GnuplotException if a no PATH variable is
64    /// found, if the gnuplot binary cannot be found, or if a pipe
65    /// cannot to the gnuplot binary cannot be established.
66    ///
67    Gnuplot(void);
68
69    ///
70    /// The destructor, closes the pipe to the gnuplot binary.
71    ///
72    virtual ~Gnuplot(void);
73
74    ///
75    /// @brief Send arbitrary commands to Gnuplot.
76    ///
77    /// @return 0 on success (underlying fputs, fputc, and fflush are
78    /// succesful), EOF otherwise.
79    ///
80    int command(const std::string&);
81
82    ///
83    /// Set the \a style of the line in the subsequent plot or
84    /// replot calls. The setting applies until this function is
85    /// called again.
86    ///
87    /// @note The \a style is not checked to be valid.
88    ///
89    inline void linestyle(const std::string& style) { linestyle_=style; }
90
91    ///
92    /// Set the \a title of the line in the subsequent plot or
93    /// replot calls. The setting applies until this function is
94    /// called again.
95    ///
96    inline void linetitle(const std::string& title) { linetitle_=title; }
97
98    ///
99    /// Plot the data \a y as a function of \a x using the Gnuplot
100    /// 'plot' command. The \a x vector can be omitted as in normal
101    /// Gnuplot usage.
102    ///
103    /// @return 0 on success, EOF otherwise.
104    ///
105    template <class T1, class T2>
106    int plot(const std::vector<T1>& y, const std::vector<T2>& x)
107    { return plot(y,x,"plot"); }
108
109    template <class T1>
110    int plot(const std::vector<T1>& y,const std::vector<T1>& x=std::vector<T1>())
111    { return plot(y,x,"plot"); }
112
113    ///
114    /// Plot the data \a y as a function of \a x using the Gnuplot
115    /// 'replot' command. The \a x vector can be omitted as in normal
116    /// Gnuplot usage.
117    ///
118    /// @return 0 on success, EOF otherwise.
119    ///
120    template <class T1, class T2>
121    int replot(const std::vector<T1>& y, const std::vector<T2>& x)
122    { return plot(y,x,"replot"); }
123
124    template <class T1>
125    int replot(const std::vector<T1>& y,
126              const std::vector<T1>& x=std::vector<T1>())
127    { return plot(y,x,"replot"); }
128
129  private:
130    ///
131    /// Copy constructor, not implemented.
132    ///
133    Gnuplot(const Gnuplot&);
134
135    void acquire_program_path(const std::string&);
136
137    ///
138    /// @param \a plotcmd must be "plot" or "replot".
139    ///
140    template <class T1, class T2>
141    int plot(const std::vector<T1>& y, const std::vector<T2>& x,
142             const std::string& plotcmd)
143    {
144      assert(x.size()==y.size() || x.empty());
145      assert(plotcmd=="plot" || plotcmd=="replot");
146
147      std::ostringstream cmdstring;
148      cmdstring << plotcmd << " '-' u 1:2 title '" << linetitle_ << "' with "
149                << linestyle_ << '\n';
150      for (size_t i=0; i<y.size(); ++i) {
151        if (!x.empty())
152          cmdstring << x[i] << '\t';
153        else
154          cmdstring << i << '\t';
155        cmdstring << y[i] << '\n';
156      }
157      cmdstring << "e\n";
158
159      return command(cmdstring.str());
160    }
161
162    void tokenizer(const std::string& in, std::list<std::string>& tokens,
163                   const std::string& delimiters = ":");
164
165    std::string gnuplot_binary_;
166    std::string linestyle_;
167    std::string linetitle_;
168    FILE* pipe_;
169};
170
171}} // end of namespace svndigest and namespace theplu
172
173#endif
Note: See TracBrowser for help on using the repository browser.