source: trunk/lib/Graph.cc @ 1202

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

Typo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1// $Id: Graph.cc 1202 2010-10-04 17:32:11Z jari $
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<time_t> 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    pls_.scolbga(255,255,255,0);
46    pls_.setopt("geometry", "600x500");
47    pls_.init();
48    pls_.adv(0);
49    pls_.vsta();
50    pls_.syax(6,0);
51  }
52#else
53{}
54#endif
55
56
57  Graph::~Graph(void)
58  {
59    print_legend();
60  }
61
62
63  bool Graph::date_xticks(void)
64  {
65    return xticks_.size() != 0;
66  }
67
68
69  void Graph::current_color(const legend_data& legend)
70  {
71    // we use color map 0 position 1 for current color
72#ifdef HAVE_PLPLOT
73    pls_.scol0a(1,legend.r,legend.g,legend.b,1.0);
74#endif
75  }
76
77
78  void Graph::current_color(unsigned char r, unsigned char g, unsigned char b)
79  {
80    // we use color map 0 position 1 for current color
81#ifdef HAVE_PLPLOT
82    pls_.scol0a(1,r,g,b,1.0);
83#endif
84  }
85
86
87  void Graph::plot(const SumVector& y, const std::string& label,
88                   unsigned int lines)
89  {
90#ifdef HAVE_PLPLOT
91    if (!plots_) {
92      // date[0] is not the oldest when repo is imported with cvs2svn
93      xmin_= date_xticks() ? std::min(xticks_[0], xticks_[1]) : 0;
94      xmax_= date_xticks() ? xticks_.back() : y.size();
95      xrange_=xmax_-xmin_;
96      yrange_=ymax_-ymin_;
97      pls_.wind(xmin_, xmax_, ymin_, ymax_);
98
99      // draw plot frame, x and y ticks only for the first plot
100      pls_.scol0a(2,0,0,0,1.0);
101      pls_.col0(2);
102
103      std::string xopt("bcnstv");
104      if (date_xticks()) {
105        pls_.timefmt(timeformat_.c_str());
106        xopt="bcnstd";
107      }
108
109      unsigned int ytickspacing=tick_spacing(yrange_);
110      unsigned int xtickspacing=tick_spacing(xrange_);
111      pls_.box(xopt.c_str(), xtickspacing, 1, "bcnstv", ytickspacing, 2);
112      pls_.lab("Date", "Number of lines", title_.c_str());
113    }
114    ++plots_;
115
116    pls_.col0(1);
117
118    SumVector::const_iterator iter = y.begin();
119    svn_revnum_t x0=0;
120    PLFLT y0=0;
121    for (; iter!=y.end(); ++iter) {
122      staircase(x0, y0, iter->first, iter->second);
123      x0 = iter->first;
124      y0 = iter->second;
125    }
126    staircase(x0, y0, y.size()-1, y0);
127
128    legend_data legend;
129    legend.label=label;
130    legend.lines=lines;
131    pls_.gcol0(1,legend.r,legend.g,legend.b);
132    legend_.push_back(legend);
133#endif
134  }
135
136
137  void Graph::print_legend(void)
138  {
139#ifdef HAVE_PLPLOT
140    PLFLT line_length=0.05*xrange_;
141    PLFLT x=xmin_+1.7*line_length;
142    unsigned char characteristic=log10(ymax_);
143    PLFLT legend_lines_length=0.016*xrange_*(characteristic+1);
144    PLFLT dx=0.005*xrange_;
145    PLFLT dy=0.003*yrange_;
146    unsigned int row=0;
147    std::reverse(legend_.begin(), legend_.end());
148    for (std::vector<legend_data>::const_iterator i=legend_.begin();
149         i!=legend_.end(); i++, ++row) {
150      PLFLT y=(0.95-0.04*row)*yrange_;
151      current_color(*i);
152      pls_.col0(1);
153      pls_.join(x-line_length, y-dy, x, y-dy);
154      std::stringstream ss;
155      ss << i->lines;
156      pls_.col0(2);
157      pls_.ptex(x+legend_lines_length+dx*2, y, 0, 0, 0, i->label.c_str());
158      pls_.ptex(x+legend_lines_length+dx  , y, 0, 0, 1, ss.str().c_str());
159    }
160#endif
161  }
162
163
164  void Graph::set_dates(const std::vector<time_t>& date)
165  {
166    xticks_=date;
167  }
168
169
170  void Graph::staircase(svn_revnum_t rev0, PLFLT y0, 
171                        svn_revnum_t rev1, PLFLT y1)
172  {
173    PLFLT x0 = rev0;
174    PLFLT x1 = rev1;
175    if (date_xticks()) {
176      x0 = xticks_[rev0];
177      x1 = xticks_[rev1];
178    }
179#ifdef HAVE_PLPLOT
180    // join {x0,y0} with {x1,y1} via {x1,y0}
181    pls_.join(x0,y0,x1,y0);
182    pls_.join(x1,y0,x1,y1);
183#endif
184  }
185
186
187  unsigned int Graph::tick_spacing(const double range) const
188  {
189    double frac=range/5;
190    unsigned char characteristic= static_cast<unsigned char>(std::log10(frac));
191    unsigned int power=static_cast<unsigned int>(std::pow(10.0, characteristic));
192    unsigned char msn=static_cast<unsigned char>(frac/power);
193    return power*msn;
194  }
195
196
197  void Graph::timeformat(const std::string& format)
198  {
199    timeformat_=format;
200  }
201
202
203  const std::vector<time_t>& Graph::xticks(void)
204  {
205    return xticks_;
206  }
207
208
209  double Graph::ymax(double ymax)
210  {
211    return ymax_=ymax;
212  }
213
214}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.