1 | // $Id: Graph.cc 881 2009-11-24 11:19:52Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2009 Jari Häkkinen |
---|
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 <cmath> |
---|
27 | |
---|
28 | namespace theplu { |
---|
29 | namespace svndigest { |
---|
30 | |
---|
31 | std::vector<std::string> Graph::xticks_; |
---|
32 | |
---|
33 | Graph::Graph(const std::string& filename) |
---|
34 | : plots_(0), pls_(1,1,"svg",filename.c_str()), title_(filename) |
---|
35 | { |
---|
36 | // we use colour map 0 position 0 for background colour |
---|
37 | pls_.scolbga(230,230,230,1); |
---|
38 | pls_.init(); |
---|
39 | pls_.adv(0); |
---|
40 | pls_.vsta(); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | Graph::~Graph(void) |
---|
45 | { |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | bool Graph::date_xticks(void) |
---|
50 | { |
---|
51 | return xticks_.size() != 0; |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | void Graph::current_colour(unsigned char r, unsigned char g, unsigned char b) |
---|
56 | { |
---|
57 | // we use colour map 0 position 1 for current colour |
---|
58 | pls_.scol0a(1,r,g,b,1.0); |
---|
59 | pls_.col0(1); |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | void Graph::plot(const std::vector<unsigned int>& y, const std::string& legend, |
---|
64 | const std::string& timeformat) |
---|
65 | { |
---|
66 | unsigned int xmin= date_xticks() ? Date(xticks_.front()).seconds() : 0; |
---|
67 | unsigned int xmax= date_xticks() ? Date(xticks_.back()).seconds() : y.size(); |
---|
68 | pls_.wind(xmin, xmax, 0, yrange_); |
---|
69 | |
---|
70 | if (!plots_) { |
---|
71 | // draw plot frame, x and y ticks only for the first plot |
---|
72 | pls_.scol0a(2,0,0,0,1.0); |
---|
73 | pls_.col0(2); |
---|
74 | |
---|
75 | if (date_xticks()) |
---|
76 | pls_.timefmt(timeformat.c_str()); |
---|
77 | |
---|
78 | unsigned int ytickspacing=tick_spacing(yrange_); |
---|
79 | unsigned int xtickspacing=tick_spacing(xmax-xmin); |
---|
80 | pls_.box("bcnstd", xtickspacing, 1, "bcnstv", ytickspacing, 2); |
---|
81 | pls_.lab("Date", "Number of lines", title_.c_str()); |
---|
82 | } |
---|
83 | ++plots_; |
---|
84 | |
---|
85 | print_legend(xmin,0,xmax,yrange_,legend); |
---|
86 | pls_.col0(1); |
---|
87 | for (unsigned int i=1; i<y.size(); ++i) { |
---|
88 | PLFLT x0=i-1; |
---|
89 | PLFLT x1=i; |
---|
90 | if (date_xticks()) { |
---|
91 | x0=Date(xticks_[i-1]).seconds(); |
---|
92 | x1=Date(xticks_[i]).seconds(); |
---|
93 | } |
---|
94 | pls_.join(x0, y[i-1], x0, y[i]); |
---|
95 | pls_.join(x0, y[i] , x1, y[i]); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | void Graph::print_legend(PLFLT xmin, PLFLT ymin, PLFLT xmax, PLFLT ymax, |
---|
101 | const std::string& legend) |
---|
102 | { |
---|
103 | PLFLT xrange=(xmax-xmin); |
---|
104 | PLFLT length=0.05*xrange; |
---|
105 | PLFLT dx=0.01*xrange; |
---|
106 | PLFLT x=4*dx+xmin+length; |
---|
107 | PLFLT yrange=(ymax-ymin); |
---|
108 | PLFLT dy=0.001*yrange; |
---|
109 | PLFLT y=(1.-0.05*plots_)*yrange; |
---|
110 | pls_.col0(1); |
---|
111 | pls_.join(x-dx, y-3*dy, x-dx-length, y-3*dy); |
---|
112 | pls_.col0(2); |
---|
113 | pls_.ptex(x, y, 0, 0, 0, legend.c_str()); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | void Graph::set_dates(const std::vector<std::string>& date) |
---|
118 | { |
---|
119 | xticks_=date; |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | unsigned int Graph::tick_spacing(const double range) const |
---|
124 | { |
---|
125 | double frac=range/5; |
---|
126 | unsigned char characteristic=log10(frac); |
---|
127 | unsigned int power=pow(10,characteristic); |
---|
128 | unsigned char msn=frac/power; |
---|
129 | return power*msn; |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | const std::vector<std::string>& Graph::xticks(void) |
---|
134 | { |
---|
135 | return xticks_; |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | double Graph::yrange(double ymax) |
---|
140 | { |
---|
141 | return yrange_=ymax; |
---|
142 | } |
---|
143 | |
---|
144 | }} // end of namespace svndigest and namespace theplu |
---|