Changeset 36


Ignore:
Timestamp:
Jan 13, 2006, 2:05:53 AM (16 years ago)
Author:
Jari Häkkinen
Message:

Changed Gnuplot interface. Fixed sever bug in Stats::accumulated(void).

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Gnuplot.cc

    r31 r36  
    8181
    8282
    83   void Gnuplot::plot_xy(const std::vector<double>& x,
    84                         const std::vector<double>& y, bool replot)
     83  void Gnuplot::plot(const std::vector<double>& y, const std::vector<double>& x,
     84                     const std::string& plotcmd)
    8585  {
    8686    char name[]="/tmp/svnstatXXXXXX";
     
    9292      throw GnuplotException(std::string("Failed to create file: ") + name);
    9393    for (std::vector<double>::size_type i=0; i<y.size(); ++i)
    94       tmp << x[i] << '\t' << y[i] << '\n';
     94      if (x.empty())
     95        tmp << y[i] << '\n';
     96      else
     97        tmp << x[i] << '\t' << y[i] << '\n';
    9598    tmp.close();
    96     std::string cmdstring((replot ? "replot" : "plot"));
    97     cmdstring+=(std::string(" '") + name + "' title '" + linetitle_ + "' with " +
    98                 linestyle_ + "\n");
    99     command(cmdstring);
    100 
    101     // need to keep track of created files since the gnuplot command
    102     // is not executed until pclose on MacOSX, shouldn't the fflush
    103     // fix this in the command() member function?
    104     tempfiles_.push_back(name);
    105   }
    106 
    107 
    108   void Gnuplot::plot_y(const std::vector<double>& y, bool replot)
    109   {
    110     char name[]="/tmp/svnstatXXXXXX";
    111     if (mkstemp(name) == -1)
    112       throw GnuplotException(std::string("Failed to get unique filename: ") +
    113                              name);
    114     std::ofstream tmp(name);
    115     if (tmp.bad())
    116       throw GnuplotException(std::string("Failed to create file: ") + name);
    117     for (std::vector<double>::size_type i=0; i<y.size(); ++i)
    118       tmp << y[i] << '\n';
    119     tmp.close();
    120     std::string cmdstring((replot ? "replot" : "plot"));
    121     cmdstring+=(std::string(" '") + name + "' title '" + linetitle_ + "' with " +
    122                 linestyle_ + "\n");
     99    std::string cmdstring(plotcmd + " '" + name + "' title '" + linetitle_ +
     100                          "' with " + linestyle_ + "\n");
    123101    command(cmdstring);
    124102
  • trunk/lib/Gnuplot.h

    r31 r36  
    7272    ///
    7373    /// Plot the data \a y as a function of \a x using the Gnuplot
    74     /// 'plot' command. If \a replot is set to true, then the Gnuplot
    75     /// 'replot' command is used to create the plot.
     74    /// 'plot' command. The \a x vector can be omitted as in normal
     75    /// Gnuplot usage.
    7676    ///
    77     void plot_xy(const std::vector<double>& x, const std::vector<double>& y,
    78                  bool replot=false);
     77    inline void
     78    plot(const std::vector<double>& y,
     79         const std::vector<double>& x=std::vector<double>())
     80    { plot(y,x,"plot"); }
    7981
    8082    ///
    81     /// Plot the data in \a y using the Gnuplot 'plot' command. If \a
    82     /// replot is set to true, then the Gnuplot 'replot' command is
    83     /// used to create the plot.
     83    /// Plot the data \a y as a function of \a x using the Gnuplot
     84    /// 'replot' command. The \a x vector can be omitted as in normal
     85    /// Gnuplot usage.
    8486    ///
    85     void plot_y(const std::vector<double>& y, bool replot=false);
     87    inline void
     88    replot(const std::vector<double>& y,
     89           const std::vector<double>& x=std::vector<double>())
     90    { plot(y,x,"replot"); }
    8691
    8792  private:
     
    9297
    9398    void acquire_program_path(const std::string&);
     99
     100    ///
     101    /// @param \a plotcmd must be "plot" or "replot".
     102    ///
     103    void plot(const std::vector<double>& y, const std::vector<double>& x,
     104              const std::string& plotcmd);
     105
    94106    void tokenizer(const std::string& in, std::list<std::string>& tokens,
    95107                   const std::string& delimiters = ":");
  • trunk/lib/Stats.cc

    r35 r36  
    22
    33#include "Stats.h"
     4#include "Gnuplot.h"
    45#include "utility.h"
    56
     
    2223  std::vector<double> Stats::accumulated(void) const
    2324  {
    24     std::vector<u_int> sum;
    2525    if (map_.empty())
    2626      return std::vector<double>();
    2727
    28     // sum of all users
    29     std::accumulate(map_.begin(), map_.end(), sum,
    30         PairValuePlus<std::string,u_int>());
     28    // sum of all users
     29    std::vector<u_int> sum;
     30    sum=std::accumulate(map_.begin(), map_.end(), sum,
     31                        PairValuePlus<std::string,u_int>());
    3132
    3233    // calculate accumulated sum
     
    6061      vec[rev]++;
    6162    map_[user]=vec;
    62 
    6363  }
    6464
     
    7070      exit(-1);
    7171    }
    72     // Jari, temporary empty files are created that needs to be removed.
    7372    std::string cmd=std::string("set term png; set output '")+name+".png'";
    7473    gnuplot_pipe_.command(cmd);
    75     std::vector<double> x=accumulated();
    76     gnuplot_pipe_.plot_y(x);
    77     for (MapConstIter_ i= map_.begin(); i != map_.end(); i++) {
    78       x=accumulated(i->first);
    79       gnuplot_pipe_.plot_y(x);
    80     }
     74    gnuplot_pipe_.plot(accumulated());
     75    for (MapConstIter_ i= map_.begin(); i != map_.end(); i++)
     76      gnuplot_pipe_.replot(accumulated(i->first));
     77
     78    // Jari, must rewrite output once since the replots above are not
     79    // added to the plot. Rather, the plot only contains the result
     80    // from the first 'plot' call.
     81    gnuplot_pipe_.command(cmd);
     82    gnuplot_pipe_.command("replot");
    8183    return std::string(name)+".png";
    8284  }
  • trunk/lib/Stats.h

    r35 r36  
    33#ifndef _theplu_svnstat_stats_
    44#define _theplu_svnstat_stats_
    5 
    6 #include "Gnuplot.h"
    75
    86#include <map>
     
    1210namespace theplu{
    1311namespace svnstat{
     12
     13  class Gnuplot;
    1414
    1515  ///
  • trunk/test/gnuplot_pipe.cc

    r32 r36  
    2323  svnstat::Gnuplot gnuplot2;
    2424  gnuplot2.command("set output 'test2.png'; set term png");
    25   gnuplot2.plot_y(x);
     25  gnuplot2.plot(x);
    2626
    2727  svnstat::Gnuplot gnuplot3;
    2828  gnuplot3.command("set output 'test3.png'; set term png");
    2929  gnuplot3.linetitle("parabola");
    30   gnuplot3.plot_xy(x,y);
     30  gnuplot3.plot(x,y);
    3131
    3232  gnuplot3.command("set output 'test4.png'; set title 'composite'");
    3333  gnuplot3.linestyle("linespoints");
    3434  gnuplot3.linetitle("");
    35   gnuplot3.plot_y(x,true);
     35  gnuplot3.replot(x);
    3636
    3737  /*
  • trunk/test/tree.cc

    r35 r36  
    22
    33#include "Directory.h"
     4#include "Gnuplot.h"
    45
    56#include <string>
Note: See TracChangeset for help on using the changeset viewer.