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
RevLine 
[165]1#ifndef _theplu_svndigest_gnuplot_
2#define _theplu_svndigest_gnuplot_
3
[31]4// $Id: Gnuplot.h 687 2008-08-04 19:37:10Z peter $
5
[84]6/*
7  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
8
[687]9  This file is part of svndigest, http://dev.thep.lu.se/svndigest
[84]10
[149]11  svndigest is free software; you can redistribute it and/or modify it
[84]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
[149]16  svndigest is distributed in the hope that it will be useful, but
[84]17  WITHOUT ANY WARRANTY; without even the implied warranty of
[149]18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
[84]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
[65]27#include <cassert>
[31]28#include <cstdio>
29#include <list>
30#include <stdexcept>
[73]31#include <sstream>
[31]32#include <string>
33#include <vector>
34
[63]35
[107]36#include <iostream>
37
[31]38namespace theplu {
[149]39namespace svndigest {
[31]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
[39]46  { inline GnuplotException(const std::string& msg) : runtime_error(msg) {} };
[31]47
48  ///
49  /// The Gnuplot class creates a pipe to 'gnuplot' and facilitates
[73]50  /// communication with the gnuplot binary.
[31]51  ///
[123]52  /// @see GnuplotFE
53  ///
[31]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    ///
[73]69    /// The destructor, closes the pipe to the gnuplot binary.
[31]70    ///
[73]71    virtual ~Gnuplot(void);
[31]72
73    ///
[145]74    /// @brief Send arbitrary commands to Gnuplot.
[31]75    ///
[145]76    /// @return 0 on success (underlying fputs, fputc, and fflush are
77    /// succesful), EOF otherwise.
[107]78    ///
[145]79    int command(const std::string&);
[31]80
81    ///
[60]82    /// Set the \a style of the line in the subsequent plot or
83    /// replot calls. The setting applies until this function is
[31]84    /// called again.
85    ///
86    /// @note The \a style is not checked to be valid.
87    ///
[39]88    inline void linestyle(const std::string& style) { linestyle_=style; }
[31]89
90    ///
[60]91    /// Set the \a title of the line in the subsequent plot or
92    /// replot calls. The setting applies until this function is
[31]93    /// called again.
94    ///
[39]95    inline void linetitle(const std::string& title) { linetitle_=title; }
[31]96
97    ///
98    /// Plot the data \a y as a function of \a x using the Gnuplot
[36]99    /// 'plot' command. The \a x vector can be omitted as in normal
100    /// Gnuplot usage.
[31]101    ///
[145]102    /// @return 0 on success, EOF otherwise.
103    ///
[73]104    template <class T1, class T2>
[145]105    int plot(const std::vector<T1>& y, const std::vector<T2>& x)
106    { return plot(y,x,"plot"); }
[31]107
[73]108    template <class T1>
[145]109    int plot(const std::vector<T1>& y,const std::vector<T1>& x=std::vector<T1>())
110    { return plot(y,x,"plot"); }
[73]111
[31]112    ///
[36]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.
[31]116    ///
[145]117    /// @return 0 on success, EOF otherwise.
118    ///
[73]119    template <class T1, class T2>
[145]120    int replot(const std::vector<T1>& y, const std::vector<T2>& x)
121    { return plot(y,x,"replot"); }
[31]122
[73]123    template <class T1>
[145]124    int replot(const std::vector<T1>& y,
[73]125              const std::vector<T1>& x=std::vector<T1>())
[145]126    { return plot(y,x,"replot"); }
[60]127
[31]128  private:
129    ///
130    /// Copy constructor, not implemented.
131    ///
132    Gnuplot(const Gnuplot&);
133
134    void acquire_program_path(const std::string&);
[36]135
136    ///
137    /// @param \a plotcmd must be "plot" or "replot".
138    ///
[60]139    template <class T1, class T2>
[145]140    int plot(const std::vector<T1>& y, const std::vector<T2>& x,
141             const std::string& plotcmd)
[60]142    {
143      assert(x.size()==y.size() || x.empty());
144      assert(plotcmd=="plot" || plotcmd=="replot");
[73]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';
[60]152        else
[73]153          cmdstring << i << '\t';
154        cmdstring << y[i] << '\n';
155      }
156      cmdstring << "e\n";
[70]157
[145]158      return command(cmdstring.str());
[60]159    }
[36]160
[31]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
[149]170}} // end of namespace svndigest and namespace theplu
[31]171
172#endif
Note: See TracBrowser for help on using the repository browser.