source: trunk/lib/Graph.cc @ 875

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

Fixes #403. Dates or revisions are used for x-axis.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: Graph.cc 875 2009-11-23 18:49:49Z 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
28namespace theplu {
29namespace 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& format)
64  {
65    unsigned int xmin= date_xticks() ? Date(xticks_.front()).seconds() : 0;
66    unsigned int xmax= date_xticks() ? Date(xticks_.back()).seconds() : y.size();
67    pls_.wind(xmin, xmax, 0, yrange_);
68
69    if (!plots_) {
70      // draw plot frame, x and y ticks only for the first plot
71      pls_.scol0a(2,0,0,0,1.0);
72      pls_.col0(2);
73
74      if (date_xticks())
75        pls_.timefmt(format.c_str());
76
77      unsigned int ytickspacing=tick_spacing(yrange_);
78      unsigned int xtickspacing=tick_spacing(xmax-xmin);
79      pls_.box("bcnstd", xtickspacing, 1, "bcnstv", ytickspacing, 2);
80      pls_.lab("Date", "Number of lines", title_.c_str());
81
82      pls_.col0(1);
83    }
84    ++plots_;
85    for (unsigned int i=1; i<y.size(); ++i) {
86      PLFLT x0=i-1;
87      PLFLT x1=i;
88      if (date_xticks()) {
89        x0=Date(xticks_[i-1]).seconds();
90        x1=Date(xticks_[i]).seconds();
91      }
92      pls_.join(x0, y[i-1], x0, y[i]);
93      pls_.join(x0, y[i]  , x1, y[i]);
94    }
95  }
96
97
98  void Graph::set_dates(const std::vector<std::string>& date)
99  {
100    xticks_=date;
101  }
102
103
104  unsigned int Graph::tick_spacing(const double range) const
105  {
106    double frac=range/5;
107    unsigned char characteristic=log10(frac);
108    unsigned int power=pow(10,characteristic);
109    unsigned char msn=frac/power;
110    return power*msn;
111  }
112
113
114  const std::vector<std::string>& Graph::xticks(void)
115  {
116    return xticks_;
117  }
118
119
120  double Graph::yrange(double ymax)
121  {
122    return yrange_=ymax;
123  }
124
125}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.