source: trunk/lib/Graph.cc @ 951

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

Minor code improvement.

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