source: branches/0.9-stable/lib/Graph.cc @ 1275

Last change on this file since 1275 was 1275, checked in by Jari Häkkinen, 12 years ago

Addresses #485. Take care of the case when revisions numbers are used for xaxis rather than dates.

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