source: trunk/lib/Graph.cc @ 872

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

Addresses #403. Better looking y and x axis values.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1// $Id: Graph.cc 872 2009-11-22 17:06:04Z 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 <cmath>
25
26namespace theplu {
27namespace svndigest {
28
29  std::vector<std::string> Graph::xticks_;
30
31  Graph::Graph(const std::string& filename)
32    : plots_(0), pls_(1,1,"svg",filename.c_str())
33  {
34    // we use colour map 0 position 0 for background colour
35    pls_.scolbga(230,230,230,1);
36    pls_.init();
37    pls_.adv(0);
38    pls_.vsta();
39  }
40
41
42  Graph::~Graph(void)
43  {
44  }
45
46
47  bool Graph::date_xticks(void)
48  {
49    return xticks_.size() != 0;
50  }
51
52
53  void Graph::current_colour(unsigned char r, unsigned char g, unsigned char b)
54  {
55    // we use colour map 0 position 1 for current colour
56    pls_.scol0a(1,r,g,b,1.0);
57    pls_.col0(1);
58  }
59
60
61  void Graph::plot(const std::vector<unsigned int>& x)
62  {
63    pls_.wind(0, x.size(), 0, yrange_);
64    if (!plots_) {
65      // draw plot frame, x and y ticks only for the first plot
66      pls_.scol0a(2,0,0,0,1.0);
67      pls_.col0(2);
68      unsigned int ytickspacing=tick_spacing(yrange_);
69      unsigned int xtickspacing=tick_spacing(x.size());
70      pls_.box("bcnstv", xtickspacing, 1, "bcnstv", ytickspacing, 1);
71      pls_.col0(1);
72    }
73    ++plots_;
74    for (unsigned int i=1; i<x.size(); ++i) {
75      pls_.join(i-1,x[i-1],i-1,x[i]);
76      pls_.join(i-1,x[i],i,x[i]);
77    }
78  }
79
80
81  unsigned int Graph::tick_spacing(const double range) const
82  {
83    double frac=range/5;
84    unsigned char characteristic=log10(frac);
85    unsigned int power=pow(10,characteristic);
86    unsigned char msn=frac/power;
87    return power*msn;
88  }
89
90
91  const std::vector<std::string>& Graph::xticks(void)
92  {
93    return xticks_;
94  }
95
96
97  double Graph::yrange(double ymax)
98  {
99    return yrange_=ymax;
100  }
101
102}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.