source: trunk/lib/Gnuplot.h @ 107

Last change on this file since 107 was 107, checked in by Peter Johansson, 15 years ago

Gnuplot functions returning int and utilizing that in the test. However the warning sent from Gnuplot ref #37 is not caught since there is no problem with the pipe there.

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