source: trunk/lib/Graph.cc @ 929

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

Changed Colour to Color for consistency.

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