Changeset 60 for trunk/lib/Gnuplot.h


Ignore:
Timestamp:
Jan 16, 2006, 11:03:04 AM (16 years ago)
Author:
Peter Johansson
Message:

plotting vs date rather than revision

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Gnuplot.h

    r39 r60  
    55
    66#include <cstdio>
     7#include <fstream>
    78#include <list>
    89#include <stdexcept>
     
    5556
    5657    ///
    57     /// Set the \a style of the line in the subsequent plot_xy or
    58     /// plot_y calls. The setting applies until this function is
     58    /// @return input format for date
     59    ///
     60    // Peter, obsolete ?
     61    const std::string& date_input_format(void) { return date_input_format_; }
     62
     63    ///
     64    ///
     65    ///
     66    void date_plot(const std::vector<double>& y,
     67                   const std::string& format="%y-%b");
     68    ///
     69    ///
     70    ///
     71    void date_replot(const std::vector<double>& y,
     72                     const std::string& format="%y-%b");
     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
    5977    /// called again.
    6078    ///
     
    6482
    6583    ///
    66     /// Set the \a title of the line in the subsequent plot_xy or
    67     /// plot_y calls. The setting applies until this function is
     84    /// Set the \a title of the line in the subsequent plot or
     85    /// replot calls. The setting applies until this function is
    6886    /// called again.
    6987    ///
     
    7593    /// Gnuplot usage.
    7694    ///
     95    // Peter, do we have to set xdata for replot
    7796    inline void
    7897    plot(const std::vector<double>& y,
    7998         const std::vector<double>& x=std::vector<double>())
    80     { plot(y,x,"plot"); }
     99    { command("set xdata"); plot(y,x,"plot"); }
    81100
    82101    ///
     
    88107    replot(const std::vector<double>& y,
    89108           const std::vector<double>& x=std::vector<double>())
    90     { plot(y,x,"replot"); }
     109    { command("set xdata"); plot(y,x,"replot"); }
     110
     111    ///
     112    /// sets format of date output
     113    ///
     114    inline void set_date_format(const std::string& format)
     115    { date_output_format_ = format;}
     116
     117    ///
     118    /// Function setting the dates. \a format must comply with strings
     119    /// in date
     120    ///
     121    inline void set_dates(const std::vector<std::string>& date,
     122                          const std::string& format="%Y-%m-%d")
     123    { date_=date; date_input_format_=format; }
    91124
    92125  private:
     
    101134    /// @param \a plotcmd must be "plot" or "replot".
    102135    ///
    103     void plot(const std::vector<double>& y, const std::vector<double>& x,
    104               const std::string& plotcmd);
     136    template <class T1, class T2>
     137    void plot(const std::vector<T1>& y, const std::vector<T2>& x,
     138              const std::string& plotcmd)
     139    {
     140      assert(x.size()==y.size() || x.empty());
     141      assert(plotcmd=="plot" || plotcmd=="replot");
     142      char name[]="/tmp/svnstatXXXXXX";
     143      if (mkstemp(name) == -1){
     144        throw GnuplotException(std::string("Failed to get unique filename: ") +
     145                               name);
     146      }
     147      std::ofstream tmp(name);
     148      if (tmp.bad())
     149        throw GnuplotException(std::string("Failed to create file: ") + name);
     150     
     151      for (size_t i = 0; i<y.size() && (i<x.size() || x.empty()); ++i ){
     152        if (x.empty())
     153          tmp << y[i] << '\n';
     154        else
     155          tmp << x[i] << '\t' << y[i] << '\n';
     156      }
     157      tmp.close();
     158      std::string cmdstring(plotcmd + " '" + name + "' u 1:2 title '" +
     159                            linetitle_ + "' with " + linestyle_ + "\n");
     160      command(cmdstring);
     161     
     162      // need to keep track of created files since the gnuplot command
     163      // is not executed until pclose on MacOSX, shouldn't the fflush
     164      // fix this in the command() member function?
     165      tempfiles_.push_back(name);
     166    }
    105167
    106168    void tokenizer(const std::string& in, std::list<std::string>& tokens,
     
    111173    // fix this in the command() member function?
    112174    std::list<std::string> tempfiles_;
     175    std::vector<std::string> date_;
     176    std::string date_input_format_;
     177    std::string date_output_format_;
    113178    std::string gnuplot_binary_;
    114179    std::string linestyle_;
Note: See TracChangeset for help on using the changeset viewer.