source: trunk/lib/Gnuplot.h @ 74

Last change on this file since 74 was 73, checked in by Jari Häkkinen, 16 years ago

Divided Gnuplot class into a basic gnuplot communication class
and a front end class specialized for svnstat.
Added option to plot stats against time or revision.
Removed gnuplot usage of temporary files.

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