source: trunk/lib/Gnuplot.h @ 84

Last change on this file since 84 was 84, checked in by Jari Häkkinen, 15 years ago

Added copyright statement. Bumped version number to pre0.3. Cleaned up code.

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