source: trunk/lib/Gnuplot.h @ 796

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

Merged patch release 0.6.7 to trunk. Delta 0.6.7 - 0.6.6

  • 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 768 2009-01-31 21:30:37Z 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 3 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 svndigest. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <cassert>
27#include <cstdio>
28#include <list>
29#include <stdexcept>
30#include <sstream>
31#include <string>
32#include <vector>
33
34
35#include <iostream>
36
37namespace theplu {
38namespace svndigest {
39
40  ///
41  /// If something goes wrong in the use of the Gnuplot class, a
42  /// GnuplotException is thrown.
43  ///
44  struct GnuplotException : public std::runtime_error
45  { inline GnuplotException(const std::string& msg) : runtime_error(msg) {} };
46
47  ///
48  /// The Gnuplot class creates a pipe to 'gnuplot' and facilitates
49  /// communication with the gnuplot binary.
50  ///
51  /// @see GnuplotFE
52  ///
53  class Gnuplot
54  {
55  public:
56    ///
57    /// This constructor sets up the pipe to the first gnuplot
58    /// executable found in the PATH environment variable. The PATH
59    /// variable must exist.
60    ///
61    /// @throw Throws a GnuplotException if a no PATH variable is
62    /// found, if the gnuplot binary cannot be found, or if a pipe
63    /// cannot to the gnuplot binary cannot be established.
64    ///
65    Gnuplot(void);
66
67    ///
68    /// The destructor, closes the pipe to the gnuplot binary.
69    ///
70    virtual ~Gnuplot(void);
71
72    ///
73    /// @brief Send arbitrary commands to Gnuplot.
74    ///
75    /// @return 0 on success (underlying fputs, fputc, and fflush are
76    /// succesful), EOF otherwise.
77    ///
78    int command(const std::string&);
79
80    ///
81    /// Set the \a style of the line in the subsequent plot or
82    /// replot calls. The setting applies until this function is
83    /// called again.
84    ///
85    /// @note The \a style is not checked to be valid.
86    ///
87    inline void linestyle(const std::string& style) { linestyle_=style; }
88
89    ///
90    /// Set the \a title of the line in the subsequent plot or
91    /// replot calls. The setting applies until this function is
92    /// called again.
93    ///
94    inline void linetitle(const std::string& title) { linetitle_=title; }
95
96    ///
97    /// Plot the data \a y as a function of \a x using the Gnuplot
98    /// 'plot' command. The \a x vector can be omitted as in normal
99    /// Gnuplot usage.
100    ///
101    /// @return 0 on success, EOF otherwise.
102    ///
103    template <class T1, class T2>
104    int plot(const std::vector<T1>& y, const std::vector<T2>& x)
105    { return plot(y,x,"plot"); }
106
107    template <class T1>
108    int plot(const std::vector<T1>& y,const std::vector<T1>& x=std::vector<T1>())
109    { return plot(y,x,"plot"); }
110
111    ///
112    /// Plot the data \a y as a function of \a x using the Gnuplot
113    /// 'replot' command. The \a x vector can be omitted as in normal
114    /// Gnuplot usage.
115    ///
116    /// @return 0 on success, EOF otherwise.
117    ///
118    template <class T1, class T2>
119    int replot(const std::vector<T1>& y, const std::vector<T2>& x)
120    { return plot(y,x,"replot"); }
121
122    template <class T1>
123    int replot(const std::vector<T1>& y,
124              const std::vector<T1>& x=std::vector<T1>())
125    { return plot(y,x,"replot"); }
126
127  private:
128    ///
129    /// Copy constructor, not implemented.
130    ///
131    Gnuplot(const Gnuplot&);
132
133    void acquire_program_path(const std::string&);
134
135    ///
136    /// @param \a plotcmd must be "plot" or "replot".
137    ///
138    template <class T1, class T2>
139    int plot(const std::vector<T1>& y, const std::vector<T2>& x,
140             const std::string& plotcmd)
141    {
142      assert(x.size()==y.size() || x.empty());
143      assert(plotcmd=="plot" || plotcmd=="replot");
144
145      std::ostringstream cmdstring;
146      cmdstring << plotcmd << " '-' u 1:2 title '" << linetitle_ << "' with "
147                << linestyle_ << '\n';
148      for (size_t i=0; i<y.size(); ++i) {
149        if (!x.empty())
150          cmdstring << x[i] << '\t';
151        else
152          cmdstring << i << '\t';
153        cmdstring << y[i] << '\n';
154      }
155      cmdstring << "e\n";
156
157      return command(cmdstring.str());
158    }
159
160    void tokenizer(const std::string& in, std::list<std::string>& tokens,
161                   const std::string& delimiters = ":");
162
163    std::string gnuplot_binary_;
164    std::string linestyle_;
165    std::string linetitle_;
166    FILE* pipe_;
167};
168
169}} // end of namespace svndigest and namespace theplu
170
171#endif
Note: See TracBrowser for help on using the repository browser.