source: trunk/lib/Graph.cc @ 949

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

Addresses #416. The legend should not waste memory

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