source: trunk/lib/Graph.h @ 1327

Last change on this file since 1327 was 1327, checked in by Peter Johansson, 11 years ago

refs #489 and #490. Use default tickspacing provided by plplot. With
current implementation, v6.9.6, it means range 6-15 -> spacing=2;
range 15-30 -> spacing=5; range 30-60 -> spacing=10 and similarly for
other order of magnitudes. For dates it is similar, but the unit
changes dynamically depending on the range. Basically they choose the
largest unit of seconds, minutes, hours, days, weeks, and years, such
that range is at least 3 in that unit. So when the range is more than
3 years we're counting in years and when the range is between 3 weeks
and 3 years we're counting in weeks.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1#ifndef _theplu_svndigest_graph_
2#define _theplu_svndigest_graph_
3
4// $Id: Graph.h 1327 2011-01-27 01:23:15Z peter $
5
6/*
7  Copyright (C) 2009 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2010 Peter Johansson
9
10  This file is part of svndigest, http://dev.thep.lu.se/svndigest
11
12  svndigest is free software; you can redistribute it and/or modify it
13  under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 3 of the License, or
15  (at your option) any later version.
16
17  svndigest is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <config.h>
27
28#include "Vector.h"
29
30#include <subversion-1/svn_types.h>
31
32#include <ctime>
33#include <string>
34#include <vector>
35
36#ifdef HAVE_PLPLOT
37#include <plplot/plstream.h>
38#else
39typedef int PLINT;
40typedef double PLFLT;
41typedef double plstream;
42#endif
43
44namespace theplu {
45namespace svndigest {
46
47  /**
48     Graph is used to generate plots in svndigest and is a specialized
49     wrapper to the plplot plot library, http://plplot.sourceforge.net/
50
51     Graph allows some axis label manipulation, setting pen colour,
52     and plotting lines.
53
54     Graph supports three graphics formats - portable network graphics
55     (png), scalable vector graphics (svg), and protable document
56     format (pdf) - if the underlying plplot library was built with
57     the appropriate modules. If not, rebuild and reinstall plplot.
58  */
59  class Graph
60  {
61  public:
62
63    /**
64       \brief Constructor
65
66       \a filename to be defined depending on whether we'll support
67       more output formats than SVG.
68
69       \note The plot legend is created in the destructioe, i.e., when
70       the graph object is destroyed.
71    */
72    Graph(const std::string& filename, const std::string& format);
73
74    /**
75       \brief Destructor
76    */
77    ~Graph(void);
78
79    /**
80       \brief Set the pen colour to use in next drawing call
81    */
82    void current_color(unsigned char r, unsigned char g, unsigned char b);
83
84    static bool date_xticks(void);
85
86    /**
87       \brief Plot \a data and use \a lines and \a label to compose
88       the legend label.
89
90       The legend will be a coloured line followed by \a lines
91       followed by \a label.
92
93       The label order in the legend is reverse to the plot order,
94       i.e., the last plotted line will get the top entry in the
95       legend, the second to last plotted line will be the second
96       legend entry, and so on.
97    */
98    void plot(const SumVector& data, const std::string& label,
99              unsigned int lines);
100
101    /**
102       Sets the right end of xrange to correspond to revision \a rev.
103     */
104    static void rev_max(svn_revnum_t rev);
105
106    /**
107       Gets the right end of xrange to correspond to revision \a rev.
108     */
109    static svn_revnum_t rev_max(void);
110
111    /**
112       Sets the left end of xrange to correspond to revision \a rev.
113     */
114    static void rev_min(svn_revnum_t rev);
115
116    /**
117       Gets the left end of xrange to correspond to revision \a rev.
118     */
119    static svn_revnum_t rev_min(void);
120
121    /**
122       \brief Function setting the dates.
123
124       The date is given in seconds (since 1/1 1970)
125    */
126    static void set_dates(const std::vector<time_t>& date);
127
128    /**
129    */
130    static const std::vector<time_t>& xticks(void);
131
132    /**
133       \brief Set max y value in the plot
134
135       The plot area is (xstart,0) to (xend,\a ymax) where xend is
136       either the length of vector to plot (corresponds to latest
137       revision number) or the date of the last revision commit,
138       xstart is 0 or the date of the first commit.
139    */
140    double ymax(double ymax);
141
142  private:
143
144    // Copy constructor not implemented
145    Graph(const Graph&);
146
147    struct legend_data {
148      std::string label;
149      unsigned int lines;
150      PLINT r,g,b;
151    };
152
153    /**
154       \brief Set the pen colour to use in next drawing call
155    */
156    void current_color(const legend_data&);
157    void print_legend(void);
158    void staircase(svn_revnum_t rev0, PLFLT y0, svn_revnum_t rev1, PLFLT y1);
159    unsigned int tick_spacing(const double range) const;
160
161    std::vector<legend_data> legend_;
162    unsigned int plots_; // keep track of number of plots drawn
163    plstream pls_;
164    static svn_revnum_t rev_min_;
165    static svn_revnum_t rev_max_;
166    std::string title_;
167    static std::vector<time_t> xticks_;
168    PLFLT xmin_, xmax_, xrange_, ymin_, ymax_, yrange_;
169  };
170
171}} // end of namespace svndigest and namespace theplu
172
173#endif
Note: See TracBrowser for help on using the repository browser.