source: trunk/lib/Gnuplot.h @ 687

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

Merged patch release 0.6.6 to trunk. Delta 0.6.6 - 0.6.5

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