source: trunk/lib/Graph.cc @ 1017

Last change on this file since 1017 was 1017, checked in by Peter Johansson, 13 years ago

updating copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1// $Id: Graph.cc 1017 2010-01-09 14:41:34Z peter $
2
3/*
4  Copyright (C) 2009 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2010 Peter Johansson
6
7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 3 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Graph.h"
24
25#include "Date.h"
26
27#include <algorithm>
28#include <cmath>
29#include <sstream>
30
31namespace theplu {
32namespace svndigest {
33
34  std::vector<std::string> Graph::xticks_;
35
36  Graph::Graph(const std::string& filename, const std::string& format)
37#ifdef HAVE_PLPLOT
38    : plots_(0), pls_(1,1,format.c_str(),filename.c_str()), timeformat_("%y-%b"),
39      title_(filename), xmin_(0.0), xmax_(0.0), ymin_(0.0), ymax_(0.0)
40  {
41    // should match the maximum number of authors plotted, change this
42    // when the maximum number of authors becomes configurable
43    legend_.reserve(10);
44    // we use color map 0 position 0 for background color
45    if (format=="png")
46      pls_.scolbg(255,255,255);
47    else
48      pls_.scolbga(255,255,255,0);
49    pls_.init();
50    pls_.adv(0);
51    pls_.vsta();
52    pls_.syax(6,0);
53  }
54#else
55{}
56#endif
57
58
59  Graph::~Graph(void)
60  {
61    print_legend();
62  }
63
64
65  bool Graph::date_xticks(void)
66  {
67    return xticks_.size() != 0;
68  }
69
70
71  void Graph::current_color(const legend_data& legend)
72  {
73    // we use color map 0 position 1 for current color
74#ifdef HAVE_PLPLOT
75    pls_.scol0a(1,legend.r,legend.g,legend.b,1.0);
76#endif
77  }
78
79
80  void Graph::current_color(unsigned char r, unsigned char g, unsigned char b)
81  {
82    // we use color map 0 position 1 for current color
83#ifdef HAVE_PLPLOT
84    pls_.scol0a(1,r,g,b,1.0);
85#endif
86  }
87
88
89  void Graph::plot(const std::vector<unsigned int>& y, const std::string& label,
90                   unsigned int lines)
91  {
92#ifdef HAVE_PLPLOT
93    if (!plots_) {
94      // date[0] is not the oldest when repo is imported with cvs2svn
95      xmin_= date_xticks() ? 
96        std::min( Date(xticks_[0]), Date(xticks_[1]) ).seconds() : 0;
97      xmax_= date_xticks() ? Date(xticks_.back()).seconds() : y.size();
98      xrange_=xmax_-xmin_;
99      yrange_=ymax_-ymin_;
100      pls_.wind(xmin_, xmax_, ymin_, ymax_);
101
102      // draw plot frame, x and y ticks only for the first plot
103      pls_.scol0a(2,0,0,0,1.0);
104      pls_.col0(2);
105
106      std::string xopt("bcnstv");
107      if (date_xticks()) {
108        pls_.timefmt(timeformat_.c_str());
109        xopt="bcnstd";
110      }
111
112      unsigned int ytickspacing=tick_spacing(yrange_);
113      unsigned int xtickspacing=tick_spacing(xrange_);
114      pls_.box(xopt.c_str(), xtickspacing, 1, "bcnstv", ytickspacing, 2);
115      pls_.lab("Date", "Number of lines", title_.c_str());
116    }
117    ++plots_;
118
119    pls_.col0(1);
120    for (unsigned int i=1; i<y.size(); ++i) {
121      PLFLT x0=i-1;
122      PLFLT x1=i;
123      if (date_xticks()) {
124        x0=Date(xticks_[i-1]).seconds();
125        x1=Date(xticks_[i]).seconds();
126      }
127      pls_.join(x0, y[i-1], x0, y[i]);
128      pls_.join(x0, y[i]  , x1, y[i]);
129    }
130
131    legend_data legend;
132    legend.label=label;
133    legend.lines=lines;
134    pls_.gcol0(1,legend.r,legend.g,legend.b);
135    legend_.push_back(legend);
136#endif
137  }
138
139
140  void Graph::print_legend(void)
141  {
142#ifdef HAVE_PLPLOT
143    PLFLT line_length=0.05*xrange_;
144    PLFLT x=xmin_+1.7*line_length;
145    unsigned char characteristic=log10(ymax_);
146    PLFLT legend_lines_length=0.016*xrange_*(characteristic+1);
147    PLFLT dx=0.005*xrange_;
148    PLFLT dy=0.003*yrange_;
149    unsigned int row=0;
150    std::reverse(legend_.begin(), legend_.end());
151    for (std::vector<legend_data>::const_iterator i=legend_.begin();
152         i!=legend_.end(); i++, ++row) {
153      PLFLT y=(0.95-0.04*row)*yrange_;
154      current_color(*i);
155      pls_.col0(1);
156      pls_.join(x-line_length, y-dy, x, y-dy);
157      std::stringstream ss;
158      ss << i->lines;
159      pls_.col0(2);
160      pls_.ptex(x+legend_lines_length+dx*2, y, 0, 0, 0, i->label.c_str());
161      pls_.ptex(x+legend_lines_length+dx  , y, 0, 0, 1, ss.str().c_str());
162    }
163#endif
164  }
165
166
167  void Graph::set_dates(const std::vector<std::string>& date)
168  {
169    xticks_=date;
170  }
171
172
173  unsigned int Graph::tick_spacing(const double range) const
174  {
175    double frac=range/5;
176    unsigned char characteristic= static_cast<unsigned char>(std::log10(frac));
177    unsigned int power=static_cast<unsigned int>(std::pow(10.0, characteristic));
178    unsigned char msn=static_cast<unsigned char>(frac/power);
179    return power*msn;
180  }
181
182
183  void Graph::timeformat(const std::string& format)
184  {
185    timeformat_=format;
186  }
187
188
189  const std::vector<std::string>& Graph::xticks(void)
190  {
191    return xticks_;
192  }
193
194
195  double Graph::ymax(double ymax)
196  {
197    return ymax_=ymax;
198  }
199
200}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.